diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index 259271484..d58ff3fed 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -12,8 +12,8 @@ import ( "github.com/0xPolygon/cdk/bridgesync" cdkcommon "github.com/0xPolygon/cdk/common" "github.com/0xPolygon/cdk/config/types" + "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" - "github.com/0xPolygon/cdk/sync" "github.com/ethereum/go-ethereum/common" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv/mdbx" @@ -41,30 +41,39 @@ func (n network) getTable() string { } } -func (n network) getCfg(a *AggSender) bridgesync.Config { +func (n network) getClient(a *AggSender) bridgesync.EthClienter { switch n { case L1: - return a.l1Cfg + return a.l1Client default: - return a.l2Cfg + return a.l2Client } } -func (n network) getClient(a *AggSender) bridgesync.EthClienter { +func (n network) getSyncer(a *AggSender) *bridgesync.BridgeSync { switch n { case L1: - return a.l1Client + return a.l1Syncer default: - return a.l2Client + return a.l2Syncer } } -func (n network) getSyncer(a *AggSender) *bridgesync.LocalBridgeSync { +func (n network) getOriginNetwork(a *AggSender) uint32 { switch n { case L1: - return a.l1Syncer + return a.l1Syncer.OriginNetwork() default: - return a.l2Syncer + return a.l2Syncer.OriginNetwork() + } +} + +func (n network) getBlockFinality(a *AggSender) etherman.BlockNumberFinality { + switch n { + case L1: + return a.l1Syncer.BlockFinality() + default: + return a.l2Syncer.BlockFinality() } } @@ -86,13 +95,11 @@ func tableCfgFunc(defaultBuckets kv.TableCfg) kv.TableCfg { // AggSender is a component that will send certificates to the aggLayer type AggSender struct { - l2Syncer *bridgesync.LocalBridgeSync + l2Syncer *bridgesync.BridgeSync l2Client bridgesync.EthClienter - l2Cfg bridgesync.Config - l1Syncer *bridgesync.LocalBridgeSync + l1Syncer *bridgesync.BridgeSync l1Client bridgesync.EthClienter - l1Cfg bridgesync.Config db kv.RwDB aggLayerClient agglayer.AggLayerClient @@ -106,21 +113,11 @@ type AggSender struct { func New( ctx context.Context, cfg Config, - l1ReorgDetector sync.ReorgDetector, - l2ReorgDetector sync.ReorgDetector, aggLayerClient agglayer.AggLayerClient, + l1Syncer *bridgesync.BridgeSync, + l2Syncer *bridgesync.BridgeSync, l1Client bridgesync.EthClienter, l2Client bridgesync.EthClienter) (*AggSender, error) { - l1Syncer, err := bridgesync.NewL1(ctx, cfg.L1BridgeSyncer, l1ReorgDetector, l1Client) - if err != nil { - return nil, err - } - - l2Syncer, err := bridgesync.NewL2(ctx, cfg.L2BridgeSyncer, l2ReorgDetector, l2Client) - if err != nil { - return nil, err - } - db, err := mdbx.NewMDBX(nil). Path(filepath.Join(cfg.DBPath, aggSenderDBFolder)). WithTableCfg(tableCfgFunc). @@ -175,7 +172,7 @@ func (a *AggSender) sendCertificates(ctx context.Context) { } // buildCertificate builds a certificate from the bridge events -func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.LocalBridgeSync, +func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.BridgeSync, bridgeEvents []bridgesync.Event, originNetwork uint32, previousExitRoot common.Hash) (*agglayer.Certificate, error) { bridgeExits := make([]*bridgesync.Bridge, 0, len(bridgeEvents)) importedBridgeExits := make([]*bridgesync.Claim, 0, len(bridgeEvents)) @@ -203,7 +200,8 @@ func (a *AggSender) buildCertificate(ctx context.Context, syncer *bridgesync.Loc // sendCertificatesForNetwork sends certificates for a network func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) error { - cfg := n.getCfg(a) + blockFinalityType := n.getBlockFinality(a) + originNetwork := n.getOriginNetwork(a) client := n.getClient(a) syncer := n.getSyncer(a) @@ -212,7 +210,7 @@ func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) e return fmt.Errorf("error getting last sent certificate: %w. Network: %s", err, n) } - blockFinality, err := cfg.BlockFinalityType.ToBlockNum() + blockFinality, err := blockFinalityType.ToBlockNum() if err != nil { return fmt.Errorf("error getting block finality: %w. Network: %s", err, n) } @@ -237,7 +235,7 @@ func (a *AggSender) sendCertificatesForNetwork(ctx context.Context, n network) e previousExitRoot = lastSentCertificate.NewLocalExitRoot } - certificate, err := a.buildCertificate(ctx, syncer, bridgeEvents, cfg.OriginNetwork, previousExitRoot) + certificate, err := a.buildCertificate(ctx, syncer, bridgeEvents, originNetwork, previousExitRoot) if err != nil { return fmt.Errorf("error building certificate: %w. Network: %s", err, n) } diff --git a/aggsender/config.go b/aggsender/config.go index bb83f76eb..ace7943ac 100644 --- a/aggsender/config.go +++ b/aggsender/config.go @@ -1,9 +1,7 @@ package aggsender import ( - "github.com/0xPolygon/cdk/bridgesync" "github.com/0xPolygon/cdk/config/types" - "github.com/0xPolygon/cdk/reorgdetector" ) // Config is the configuration for the AggSender @@ -11,9 +9,5 @@ type Config struct { DBPath string `mapstructure:"DBPath"` AggLayerUrl string `mapstructure:"AggLayerUrl"` CertificateSendInterval types.Duration `mapstructure:"CertificateSendInterval"` - L1BridgeSyncer bridgesync.Config `mapstructure:"L1BridgeSyncer"` - L2BridgeSyncer bridgesync.Config `mapstructure:"L2BridgeSyncer"` SequencerPrivateKey types.KeystoreFileConfig `mapstructure:"SequencerPrivateKey"` - ReorgDetectorL1 reorgdetector.Config `mapstructure:"ReorgDetectorL1"` - ReorgDetectorL2 reorgdetector.Config `mapstructure:"ReorgDetectorL2"` } diff --git a/bridgesync/bridgesync.go b/bridgesync/bridgesync.go index c2beb6d12..986a71a38 100644 --- a/bridgesync/bridgesync.go +++ b/bridgesync/bridgesync.go @@ -18,6 +18,9 @@ const ( type BridgeSync struct { processor *processor driver *sync.EVMDriver + + originNetwork uint32 + blockFinality etherman.BlockNumberFinality } // NewL1 creates a bridge syncer that synchronizes the mainnet exit tree @@ -33,53 +36,73 @@ func NewL1( waitForNewBlocksPeriod time.Duration, retryAfterErrorPeriod time.Duration, maxRetryAttemptsAfterError int, + originNetwork uint32, ) (*BridgeSync, error) { return newBridgeSync( ctx, - cfg, + dbPath, + bridge, + syncBlockChunkSize, + blockFinalityType, rd, ethClient, + initialBlock, bridgeSyncL1, waitForNewBlocksPeriod, retryAfterErrorPeriod, maxRetryAttemptsAfterError, + originNetwork, ) } // NewL2 creates a bridge syncer that synchronizes the local exit tree func NewL2( ctx context.Context, - cfg Config, + dbPath string, + bridge common.Address, + syncBlockChunkSize uint64, + blockFinalityType etherman.BlockNumberFinality, rd sync.ReorgDetector, ethClient EthClienter, initialBlock uint64, waitForNewBlocksPeriod time.Duration, retryAfterErrorPeriod time.Duration, maxRetryAttemptsAfterError int, + originNetwork uint32, ) (*BridgeSync, error) { return newBridgeSync( ctx, - cfg, + dbPath, + bridge, + syncBlockChunkSize, + blockFinalityType, rd, ethClient, + initialBlock, bridgeSyncL2, waitForNewBlocksPeriod, retryAfterErrorPeriod, maxRetryAttemptsAfterError, + originNetwork, ) } func newBridgeSync( ctx context.Context, - cfg Config, + dbPath string, + bridge common.Address, + syncBlockChunkSize uint64, + blockFinalityType etherman.BlockNumberFinality, rd sync.ReorgDetector, ethClient EthClienter, + initialBlock uint64, l1OrL2ID string, waitForNewBlocksPeriod time.Duration, retryAfterErrorPeriod time.Duration, maxRetryAttemptsAfterError int, + originNetwork uint32, ) (*BridgeSync, error) { - processor, err := newProcessor(ctx, cfg.DBPath, l1OrL2ID) + processor, err := newProcessor(ctx, dbPath, l1OrL2ID) if err != nil { return nil, err } @@ -87,9 +110,9 @@ func newBridgeSync( if err != nil { return nil, err } - if lastProcessedBlock < cfg.InitialBlock { + if lastProcessedBlock < initialBlock { err = processor.ProcessBlock(ctx, sync.Block{ - Num: cfg.InitialBlock, + Num: initialBlock, }) if err != nil { return nil, err @@ -100,18 +123,18 @@ func newBridgeSync( RetryAfterErrorPeriod: retryAfterErrorPeriod, } - appender, err := buildAppender(ethClient, cfg.BridgeAddr) + appender, err := buildAppender(ethClient, bridge) if err != nil { return nil, err } downloader, err := sync.NewEVMDownloader( l1OrL2ID, ethClient, - cfg.SyncBlockChunkSize, - cfg.BlockFinalityType, + syncBlockChunkSize, + blockFinalityType, waitForNewBlocksPeriod, appender, - []common.Address{cfg.BridgeAddr}, + []common.Address{bridge}, rh, ) if err != nil { @@ -123,8 +146,10 @@ func newBridgeSync( return nil, err } return &BridgeSync{ - processor: processor, - driver: driver, + processor: processor, + driver: driver, + originNetwork: originNetwork, + blockFinality: blockFinalityType, }, nil } @@ -159,3 +184,13 @@ func (s *BridgeSync) GetExitRootByIndex(ctx context.Context, index uint32) (comm return s.processor.exitTree.GetRootByIndex(tx, index) } + +// OriginNetwork returns the network ID of the origin chain +func (s *BridgeSync) OriginNetwork() uint32 { + return s.originNetwork +} + +// BlockFinality returns the block finality type +func (s *BridgeSync) BlockFinality() etherman.BlockNumberFinality { + return s.blockFinality +} diff --git a/bridgesync/config.go b/bridgesync/config.go index 9aa849e2c..1be261c60 100644 --- a/bridgesync/config.go +++ b/bridgesync/config.go @@ -24,4 +24,6 @@ type Config struct { MaxRetryAttemptsAfterError int `mapstructure:"MaxRetryAttemptsAfterError"` // WaitForNewBlocksPeriod time that will be waited when the synchronizer has reached the latest block WaitForNewBlocksPeriod types.Duration `mapstructure:"WaitForNewBlocksPeriod"` + // OriginNetwork is the id of the network where the bridge is deployed + OriginNetwork uint32 `mapstructure:"OriginNetwork"` } diff --git a/bridgesync/downloader.go b/bridgesync/downloader.go index 09fd8b4fd..9ed031b59 100644 --- a/bridgesync/downloader.go +++ b/bridgesync/downloader.go @@ -15,8 +15,9 @@ import ( ) var ( - bridgeEventSignature = crypto.Keccak256Hash([]byte("BridgeEvent(uint8,uint32,address,uint32,address,uint256,bytes,uint32)")) - claimEventSignature = crypto.Keccak256Hash([]byte("ClaimEvent(uint256,uint32,address,address,uint256)")) + bridgeEventSignature = crypto.Keccak256Hash([]byte("BridgeEvent(uint8,uint32,address,uint32,address,uint256,bytes,uint32)")) + claimEventSignature = crypto.Keccak256Hash([]byte("ClaimEvent(uint256,uint32,address,address,uint256)")) + claimEventSignaturePreEtrog = crypto.Keccak256Hash([]byte("ClaimEvent(uint32,uint32,address,address,uint256)")) ) type EthClienter interface { diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index d733a53eb..db846bcc1 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -55,7 +55,7 @@ func TestBridgeEventE2E(t *testing.T) { require.NoError(t, err) go rd.Start(ctx) - syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0) + syncer, err := bridgesync.NewL1(ctx, dbPathSyncer, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0, 1) require.NoError(t, err) go syncer.Start(ctx) diff --git a/claimsponsor/e2e_test.go b/claimsponsor/e2e_test.go index 533de1137..bcf18ae6e 100644 --- a/claimsponsor/e2e_test.go +++ b/claimsponsor/e2e_test.go @@ -22,7 +22,7 @@ func TestE2EL1toEVML2(t *testing.T) { ctx := context.Background() env := helpers.SetupAggoracleWithEVMChain(t) dbPathBridgeSyncL1 := t.TempDir() - bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetector, env.L1Client.Client(), 0, time.Millisecond*10, 0, 0) + bridgeSyncL1, err := bridgesync.NewL1(ctx, dbPathBridgeSyncL1, env.BridgeL1Addr, 10, etherman.LatestBlock, env.ReorgDetector, env.L1Client.Client(), 0, time.Millisecond*10, 0, 0, 1) require.NoError(t, err) go bridgeSyncL1.Start(ctx) diff --git a/cmd/run.go b/cmd/run.go index b451df9fa..affefa23b 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -619,6 +619,7 @@ func runBridgeSyncL1IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, + cfg.OriginNetwork, ) if err != nil { log.Fatalf("error creating bridgeSyncL1: %s", err) @@ -650,6 +651,7 @@ func runBridgeSyncL2IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, + cfg.OriginNetwork, ) if err != nil { log.Fatalf("error creating bridgeSyncL2: %s", err) diff --git a/config/default.go b/config/default.go index f248ca071..ec2fda1dd 100644 --- a/config/default.go +++ b/config/default.go @@ -254,22 +254,4 @@ DBPath = "/tmp/aggsender" AggLayerURL = "http://zkevm-agglayer" SequencerPrivateKey = {Path = "/pk/sequencer.keystore", Password = "testonly"} CertificateSendInterval = "1m" - [ReorgDetectorL1] - DBPath = "/tmp/aggsender/reorgdetectorL1" - [ReorgDetectorL2] - DBPath = "/tmp/aggsender/reorgdetectorL2" - [AggSender.L1BridgeSyncer] - OriginNetwork = 0 - DBPath = "/tmp/aggsender/l1" - BridgeAddr = "" - SyncBlockChunkSize = "10" - InitialBlock = "0" - BlockFinalityType = "latest" - [AggSender.L2BridgeSyncer] - OriginNetwork = 1 - DBPath = "/tmp/aggsender/l2" - BridgeAddr = "" - SyncBlockChunkSize = "10" - InitialBlock = "0" - BlockFinalityType = "latest" ` diff --git a/l1bridge2infoindexsync/e2e_test.go b/l1bridge2infoindexsync/e2e_test.go index deb613f32..6002247f8 100644 --- a/l1bridge2infoindexsync/e2e_test.go +++ b/l1bridge2infoindexsync/e2e_test.go @@ -135,7 +135,7 @@ func TestE2E(t *testing.T) { rd, err := reorgdetector.New(ctx, client.Client(), dbPathReorg) go rd.Start(ctx) - bridgeSync, err := bridgesync.NewL1(ctx, dbPathBridgeSync, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0) + bridgeSync, err := bridgesync.NewL1(ctx, dbPathBridgeSync, bridgeAddr, 10, etherman.LatestBlock, rd, client.Client(), 0, time.Millisecond*10, 0, 0, 1) require.NoError(t, err) go bridgeSync.Start(ctx)