From 9ee665551a997826b8a22188fb3c764d3dab84eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 29 Nov 2024 11:34:43 +0100 Subject: [PATCH 1/5] feat: calculate network id --- bridgesync/config.go | 2 -- cmd/run.go | 15 +++++++++++++-- config/default.go | 2 -- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bridgesync/config.go b/bridgesync/config.go index d2373b53..66eb00ed 100644 --- a/bridgesync/config.go +++ b/bridgesync/config.go @@ -24,6 +24,4 @@ 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/cmd/run.go b/cmd/run.go index cff1188b..be01b8eb 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -690,6 +690,12 @@ func runBridgeSyncL1IfNeeded( if !isNeeded([]string{cdkcommon.RPC}, components) { return nil } + + originNetwork, err := l1Client.NetworkID(ctx) + if err != nil { + log.Fatalf("error getting network ID: %s", err) + } + bridgeSyncL1, err := bridgesync.NewL1( ctx, cfg.DBPath, @@ -702,7 +708,7 @@ func runBridgeSyncL1IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - cfg.OriginNetwork, + uint32(originNetwork.Uint64()), ) if err != nil { log.Fatalf("error creating bridgeSyncL1: %s", err) @@ -723,6 +729,11 @@ func runBridgeSyncL2IfNeeded( return nil } + originNetwork, err := l2Client.NetworkID(ctx) + if err != nil { + log.Fatalf("error getting network ID: %s", err) + } + bridgeSyncL2, err := bridgesync.NewL2( ctx, cfg.DBPath, @@ -735,7 +746,7 @@ func runBridgeSyncL2IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - cfg.OriginNetwork, + uint32(originNetwork.Uint64()), ) if err != nil { log.Fatalf("error creating bridgeSyncL2: %s", err) diff --git a/config/default.go b/config/default.go index b1cfe16e..7b90a81f 100644 --- a/config/default.go +++ b/config/default.go @@ -296,7 +296,6 @@ SyncBlockChunkSize = 100 RetryAfterErrorPeriod = "1s" MaxRetryAttemptsAfterError = -1 WaitForNewBlocksPeriod = "3s" -OriginNetwork=0 [BridgeL2Sync] DBPath = "{{PathRWData}}/bridgel2sync.sqlite" @@ -307,7 +306,6 @@ SyncBlockChunkSize = 100 RetryAfterErrorPeriod = "1s" MaxRetryAttemptsAfterError = -1 WaitForNewBlocksPeriod = "3s" -OriginNetwork=1 [LastGERSync] DBPath = "{{PathRWData}}/lastgersync.sqlite" From 94289305a05aad1e137717d9f24d94fd5998b927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 29 Nov 2024 12:08:45 +0100 Subject: [PATCH 2/5] feat: refactor --- cmd/run.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index be01b8eb..cba38712 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -77,8 +77,8 @@ func start(cliCtx *cli.Context) error { l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *c, l1Client, reorgDetectorL1) claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, c.ClaimSponsor) - l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, c.BridgeL1Sync, reorgDetectorL1, l1Client) - l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, c.BridgeL2Sync, reorgDetectorL2, l2Client) + l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, *c, reorgDetectorL1, l1Client) + l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, *c, reorgDetectorL2, l2Client) lastGERSync := runLastGERSyncIfNeeded( cliCtx.Context, components, c.LastGERSync, reorgDetectorL2, l2Client, l1InfoTreeSync, ) @@ -683,19 +683,19 @@ func runLastGERSyncIfNeeded( func runBridgeSyncL1IfNeeded( ctx context.Context, components []string, - cfg bridgesync.Config, + c config.Config, reorgDetectorL1 *reorgdetector.ReorgDetector, l1Client *ethclient.Client, ) *bridgesync.BridgeSync { if !isNeeded([]string{cdkcommon.RPC}, components) { return nil } - - originNetwork, err := l1Client.NetworkID(ctx) + ethermanClient, err := newEtherman(c) if err != nil { - log.Fatalf("error getting network ID: %s", err) + log.Fatal(err) } + cfg := c.BridgeL1Sync bridgeSyncL1, err := bridgesync.NewL1( ctx, cfg.DBPath, @@ -708,7 +708,7 @@ func runBridgeSyncL1IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - uint32(originNetwork.Uint64()), + ethermanClient.RollupID, ) if err != nil { log.Fatalf("error creating bridgeSyncL1: %s", err) @@ -721,7 +721,7 @@ func runBridgeSyncL1IfNeeded( func runBridgeSyncL2IfNeeded( ctx context.Context, components []string, - cfg bridgesync.Config, + c config.Config, reorgDetectorL2 *reorgdetector.ReorgDetector, l2Client *ethclient.Client, ) *bridgesync.BridgeSync { @@ -729,11 +729,12 @@ func runBridgeSyncL2IfNeeded( return nil } - originNetwork, err := l2Client.NetworkID(ctx) + ethermanClient, err := newEtherman(c) if err != nil { - log.Fatalf("error getting network ID: %s", err) + log.Fatal(err) } + cfg := c.BridgeL2Sync bridgeSyncL2, err := bridgesync.NewL2( ctx, cfg.DBPath, @@ -746,7 +747,7 @@ func runBridgeSyncL2IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - uint32(originNetwork.Uint64()), + ethermanClient.RollupID, ) if err != nil { log.Fatalf("error creating bridgeSyncL2: %s", err) From 64ee12e11898d5eab2ae685996a75115b5f3e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Mon, 2 Dec 2024 10:13:10 +0100 Subject: [PATCH 3/5] feat: refactor --- cmd/run.go | 65 +++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index cba38712..7d9491d2 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -44,55 +44,59 @@ import ( ) func start(cliCtx *cli.Context) error { - c, err := config.Load(cliCtx) + cfg, err := config.Load(cliCtx) if err != nil { return err } - log.Init(c.Log) + log.Init(cfg.Log) - if c.Log.Environment == log.EnvironmentDevelopment { + if cfg.Log.Environment == log.EnvironmentDevelopment { zkevm.PrintVersion(os.Stdout) log.Info("Starting application") - } else if c.Log.Environment == log.EnvironmentProduction { + } else if cfg.Log.Environment == log.EnvironmentProduction { logVersion() } components := cliCtx.StringSlice(config.FlagComponents) - l1Client := runL1ClientIfNeeded(components, c.Etherman.URL) - l2Client := runL2ClientIfNeeded(components, getL2RPCUrl(c)) - reorgDetectorL1, errChanL1 := runReorgDetectorL1IfNeeded(cliCtx.Context, components, l1Client, &c.ReorgDetectorL1) + l1Client := runL1ClientIfNeeded(components, cfg.Etherman.URL) + l2Client := runL2ClientIfNeeded(components, getL2RPCUrl(cfg)) + reorgDetectorL1, errChanL1 := runReorgDetectorL1IfNeeded(cliCtx.Context, components, l1Client, &cfg.ReorgDetectorL1) go func() { if err := <-errChanL1; err != nil { log.Fatal("Error from ReorgDetectorL1: ", err) } }() - reorgDetectorL2, errChanL2 := runReorgDetectorL2IfNeeded(cliCtx.Context, components, l2Client, &c.ReorgDetectorL2) + reorgDetectorL2, errChanL2 := runReorgDetectorL2IfNeeded(cliCtx.Context, components, l2Client, &cfg.ReorgDetectorL2) go func() { if err := <-errChanL2; err != nil { log.Fatal("Error from ReorgDetectorL2: ", err) } }() - l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *c, l1Client, reorgDetectorL1) - claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, c.ClaimSponsor) - l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, *c, reorgDetectorL1, l1Client) - l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, *c, reorgDetectorL2, l2Client) + rollupID := uint32(0) + + l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *cfg, l1Client, reorgDetectorL1) + claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, cfg.ClaimSponsor) + l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, cfg.BridgeL1Sync, reorgDetectorL1, + l1Client, rollupID) + l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, cfg.BridgeL2Sync, reorgDetectorL2, + l2Client, rollupID) lastGERSync := runLastGERSyncIfNeeded( - cliCtx.Context, components, c.LastGERSync, reorgDetectorL2, l2Client, l1InfoTreeSync, + cliCtx.Context, components, cfg.LastGERSync, reorgDetectorL2, l2Client, l1InfoTreeSync, ) for _, component := range components { switch component { case cdkcommon.SEQUENCE_SENDER: - c.SequenceSender.Log = c.Log - seqSender := createSequenceSender(*c, l1Client, l1InfoTreeSync) + cfg.SequenceSender.Log = cfg.Log + seqSender := createSequenceSender(*cfg, l1Client, l1InfoTreeSync) // start sequence sender in a goroutine, checking for errors go seqSender.Start(cliCtx.Context) case cdkcommon.AGGREGATOR: - aggregator := createAggregator(cliCtx.Context, *c, !cliCtx.Bool(config.FlagMigrations)) + aggregator := createAggregator(cliCtx.Context, *cfg, !cliCtx.Bool(config.FlagMigrations)) // start aggregator in a goroutine, checking for errors go func() { if err := aggregator.Start(); err != nil { @@ -101,12 +105,12 @@ func start(cliCtx *cli.Context) error { } }() case cdkcommon.AGGORACLE: - aggOracle := createAggoracle(*c, l1Client, l2Client, l1InfoTreeSync) + aggOracle := createAggoracle(*cfg, l1Client, l2Client, l1InfoTreeSync) go aggOracle.Start(cliCtx.Context) case cdkcommon.RPC: server := createRPC( - c.RPC, - c.Common.NetworkID, + cfg.RPC, + cfg.Common.NetworkID, claimSponsor, l1InfoTreeSync, lastGERSync, @@ -121,7 +125,7 @@ func start(cliCtx *cli.Context) error { case cdkcommon.AGGSENDER: aggsender, err := createAggSender( cliCtx.Context, - c.AggSender, + cfg.AggSender, l1Client, l1InfoTreeSync, l2BridgeSync, @@ -683,19 +687,15 @@ func runLastGERSyncIfNeeded( func runBridgeSyncL1IfNeeded( ctx context.Context, components []string, - c config.Config, + cfg bridgesync.Config, reorgDetectorL1 *reorgdetector.ReorgDetector, l1Client *ethclient.Client, + rollupID uint32, ) *bridgesync.BridgeSync { if !isNeeded([]string{cdkcommon.RPC}, components) { return nil } - ethermanClient, err := newEtherman(c) - if err != nil { - log.Fatal(err) - } - cfg := c.BridgeL1Sync bridgeSyncL1, err := bridgesync.NewL1( ctx, cfg.DBPath, @@ -708,7 +708,7 @@ func runBridgeSyncL1IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - ethermanClient.RollupID, + rollupID, ) if err != nil { log.Fatalf("error creating bridgeSyncL1: %s", err) @@ -721,20 +721,15 @@ func runBridgeSyncL1IfNeeded( func runBridgeSyncL2IfNeeded( ctx context.Context, components []string, - c config.Config, + cfg bridgesync.Config, reorgDetectorL2 *reorgdetector.ReorgDetector, l2Client *ethclient.Client, + rollupID uint32, ) *bridgesync.BridgeSync { if !isNeeded([]string{cdkcommon.RPC, cdkcommon.AGGSENDER}, components) { return nil } - ethermanClient, err := newEtherman(c) - if err != nil { - log.Fatal(err) - } - - cfg := c.BridgeL2Sync bridgeSyncL2, err := bridgesync.NewL2( ctx, cfg.DBPath, @@ -747,7 +742,7 @@ func runBridgeSyncL2IfNeeded( cfg.WaitForNewBlocksPeriod.Duration, cfg.RetryAfterErrorPeriod.Duration, cfg.MaxRetryAttemptsAfterError, - ethermanClient.RollupID, + rollupID, ) if err != nil { log.Fatalf("error creating bridgeSyncL2: %s", err) From 0fd63f8086e0fd22f748b36f53f1c394aadfcd71 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:18:58 +0100 Subject: [PATCH 4/5] feat: separeted function to obtain the rollupID --- cmd/run.go | 5 ++++- etherman/etherman.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/run.go b/cmd/run.go index 7d9491d2..79cf39f2 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -75,7 +75,10 @@ func start(cliCtx *cli.Context) error { } }() - rollupID := uint32(0) + rollupID, err := etherman.GetRollupID(cfg.NetworkConfig.L1Config, l1Client) + if err != nil { + log.Fatal(err) + } l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *cfg, l1Client, reorgDetectorL1) claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, cfg.ClaimSponsor) diff --git a/etherman/etherman.go b/etherman/etherman.go index 4f9e1c81..1a766f87 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -87,6 +87,22 @@ type Client struct { auth map[common.Address]bind.TransactOpts // empty in case of read-only client } +func GetRollupID(l1Config config.L1Config, ethClient bind.ContractBackend) (uint32, error) { + contracts, err := contracts.NewContracts(l1Config, ethClient) + if err != nil { + return 0, fmt.Errorf("error creating contracts. Err: %w", err) + } + rollupID, err := contracts.Banana.RollupManager.RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr) + if err != nil { + log.Errorf("error getting rollupID from %s : %+v", contracts.Banana.RollupManager.String(), err) + + return 0, fmt.Errorf("error calling SMC RollupManager.RollupAddressToID(%s). Err: %w", l1Config.ZkEVMAddr.String(), err) + } + log.Infof("rollupID: %d (obtenied from SMC: %s )", rollupID, contracts.Banana.RollupManager.String()) + + return rollupID, nil +} + // NewClient creates a new etherman. func NewClient(cfg config.Config, l1Config config.L1Config, commonConfig cdkcommon.Config) (*Client, error) { // Connect to ethereum node From 8c9ef2b734931628630bfdfdfef0f4b112be2b63 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:46:57 +0100 Subject: [PATCH 5/5] feat: obtain RollupID without etherman instance --- cmd/run.go | 22 ++++++++++++++++------ etherman/etherman.go | 6 +++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 79cf39f2..0900c2fa 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -75,15 +75,11 @@ func start(cliCtx *cli.Context) error { } }() - rollupID, err := etherman.GetRollupID(cfg.NetworkConfig.L1Config, l1Client) - if err != nil { - log.Fatal(err) - } - + rollupID := getRollUpIDIfNeeded(components, cfg.NetworkConfig.L1Config, l1Client) l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *cfg, l1Client, reorgDetectorL1) claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, cfg.ClaimSponsor) l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, cfg.BridgeL1Sync, reorgDetectorL1, - l1Client, rollupID) + l1Client, 0) l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, cfg.BridgeL2Sync, reorgDetectorL2, l2Client, rollupID) lastGERSync := runLastGERSyncIfNeeded( @@ -553,6 +549,20 @@ func runL1ClientIfNeeded(components []string, urlRPCL1 string) *ethclient.Client return l1CLient } +func getRollUpIDIfNeeded(components []string, networkConfig ethermanconfig.L1Config, + l1Client *ethclient.Client) uint32 { + if !isNeeded([]string{ + cdkcommon.AGGSENDER, + }, components) { + return 0 + } + rollupID, err := etherman.GetRollupID(networkConfig, networkConfig.ZkEVMAddr, l1Client) + if err != nil { + log.Fatal(err) + } + return rollupID +} + func runL2ClientIfNeeded(components []string, urlRPCL2 string) *ethclient.Client { if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.RPC, cdkcommon.AGGSENDER}, components) { return nil diff --git a/etherman/etherman.go b/etherman/etherman.go index 1a766f87..1f801699 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -87,16 +87,16 @@ type Client struct { auth map[common.Address]bind.TransactOpts // empty in case of read-only client } -func GetRollupID(l1Config config.L1Config, ethClient bind.ContractBackend) (uint32, error) { +func GetRollupID(l1Config config.L1Config, rollupAddr common.Address, ethClient bind.ContractBackend) (uint32, error) { contracts, err := contracts.NewContracts(l1Config, ethClient) if err != nil { return 0, fmt.Errorf("error creating contracts. Err: %w", err) } - rollupID, err := contracts.Banana.RollupManager.RollupAddressToID(&bind.CallOpts{Pending: false}, l1Config.ZkEVMAddr) + rollupID, err := contracts.Banana.RollupManager.RollupAddressToID(&bind.CallOpts{Pending: false}, rollupAddr) if err != nil { log.Errorf("error getting rollupID from %s : %+v", contracts.Banana.RollupManager.String(), err) - return 0, fmt.Errorf("error calling SMC RollupManager.RollupAddressToID(%s). Err: %w", l1Config.ZkEVMAddr.String(), err) + return 0, fmt.Errorf("error calling SMC RollupManager.RollupAddressToID(%s). Err: %w", rollupAddr.String(), err) } log.Infof("rollupID: %d (obtenied from SMC: %s )", rollupID, contracts.Banana.RollupManager.String())