diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index 6f1e10c4..0b350006 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -2,7 +2,6 @@ package bridgesync_test import ( "context" - "fmt" "math/big" "path" "testing" @@ -63,25 +62,9 @@ func TestBridgeEventE2E(t *testing.T) { } // Wait for syncer to catch up - syncerUpToDate := false - - var errMsg string lb, err := client.Client().BlockNumber(ctx) require.NoError(t, err) - - for i := 0; i < 10; i++ { - lpb, err := syncer.GetLastProcessedBlock(ctx) - require.NoError(t, err) - if lpb == lb { - syncerUpToDate = true - - break - } - - time.Sleep(time.Millisecond * 100) - errMsg = fmt.Sprintf("last block from client: %d, last block from syncer: %d", lb, lpb) - } - require.True(t, syncerUpToDate, errMsg) + helpers.RequireProcessorUpdated(t, syncer, lb) // Get bridges lastBlock, err := client.Client().BlockNumber(ctx) diff --git a/crates/cdk/versions.json b/crates/cdk/versions.json index bafbd00b..39bfb4dc 100644 --- a/crates/cdk/versions.json +++ b/crates/cdk/versions.json @@ -1,15 +1,15 @@ { "agglayer_image": "ghcr.io/agglayer/agglayer:0.2.0-rc.5", "cdk_erigon_node_image": "hermeznetwork/cdk-erigon:v2.1.2", - "cdk_node_image": "ghcr.io/0xpolygon/cdk:0.4.0-beta5", + "cdk_node_image": "ghcr.io/0xpolygon/cdk:0.4.0-beta8", "cdk_validium_node_image": "0xpolygon/cdk-validium-node:0.7.0-cdk", "zkevm_bridge_proxy_image": "haproxy:3.0-bookworm", "zkevm_bridge_service_image": "hermeznetwork/zkevm-bridge-service:v0.6.0-RC1", - "zkevm_bridge_ui_image": "leovct/zkevm-bridge-ui:multi-network-2", + "zkevm_bridge_ui_image": "leovct/zkevm-bridge-ui:multi-network", "zkevm_contracts_image": "leovct/zkevm-contracts:v8.0.0-rc.4-fork.12", "zkevm_da_image": "0xpolygon/cdk-data-availability:0.0.10", "zkevm_node_image": "hermeznetwork/zkevm-node:v0.7.3", - "zkevm_pool_manager_image": "hermeznetwork/zkevm-pool-manager:v0.1.1", + "zkevm_pool_manager_image": "hermeznetwork/zkevm-pool-manager:v0.1.2", "zkevm_prover_image": "hermeznetwork/zkevm-prover:v8.0.0-RC14-fork.12", "zkevm_sequence_sender_image": "hermeznetwork/zkevm-sequence-sender:v0.2.4" } diff --git a/go.mod b/go.mod index 430e8326..c42bb806 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,6 @@ require ( ) require ( - github.com/0xPolygonHermez/zkevm-data-streamer v0.2.7 // indirect github.com/DataDog/zstd v1.5.6 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/StackExchange/wmi v1.2.1 // indirect diff --git a/go.sum b/go.sum index 3ad80938..d6cbdb2b 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,6 @@ github.com/0xPolygon/cdk-rpc v0.0.0-20241004114257-6c3cb6eebfb6 h1:FXL/rcO7/GtZ3 github.com/0xPolygon/cdk-rpc v0.0.0-20241004114257-6c3cb6eebfb6/go.mod h1:2scWqMMufrQXu7TikDgQ3BsyaKoX8qP26D6E262vSOg= github.com/0xPolygon/zkevm-ethtx-manager v0.2.1 h1:2Yb+KdJFMpVrS9LIkd658XiWuN+MCTs7SgeWaopXScg= github.com/0xPolygon/zkevm-ethtx-manager v0.2.1/go.mod h1:lqQmzSo2OXEZItD0R4Cd+lqKFxphXEWgqHefVcGDZZc= -github.com/0xPolygonHermez/zkevm-data-streamer v0.2.7/go.mod h1:7nM7Ihk+fTG1TQPwdZoGOYd3wprqqyIyjtS514uHzWE= github.com/0xPolygonHermez/zkevm-synchronizer-l1 v1.0.5 h1:YmnhuCl349MoNASN0fMeGKU1o9HqJhiZkfMsA/1cTRA= github.com/0xPolygonHermez/zkevm-synchronizer-l1 v1.0.5/go.mod h1:X4Su/M/+hSISqdl9yomKlRsbTyuZHsRohporyHsP8gg= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -481,8 +480,6 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= -golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= -golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo= golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -490,9 +487,8 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= -golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -512,8 +508,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -575,9 +569,8 @@ golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= -golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o= +golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/l1infotreesync/e2e_test.go b/l1infotreesync/e2e_test.go index 132f563f..ffb00e35 100644 --- a/l1infotreesync/e2e_test.go +++ b/l1infotreesync/e2e_test.go @@ -36,7 +36,6 @@ func newSimulatedClient(t *testing.T) ( *verifybatchesmock.Verifybatchesmock, ) { t.Helper() - ctx := context.Background() client, setup := helpers.SimulatedBackend(t, nil, 0) @@ -79,11 +78,11 @@ func TestE2E(t *testing.T) { client.Commit() g, err := gerSc.L1InfoRootMap(nil, uint32(i+1)) require.NoError(t, err) - // Let the processor catch up - time.Sleep(time.Millisecond * 100) receipt, err := client.Client().TransactionReceipt(ctx, tx.Hash()) require.NoError(t, err) require.Equal(t, receipt.Status, types.ReceiptStatusSuccessful) + // Let the processor catch up + helpers.RequireProcessorUpdated(t, syncer, receipt.BlockNumber.Uint64()) expectedGER, err := gerSc.GetLastGlobalExitRoot(&bind.CallOpts{Pending: false}) require.NoError(t, err) @@ -118,17 +117,7 @@ func TestE2E(t *testing.T) { require.True(t, len(receipt.Logs) == 1+i%2+i%2) // Let the processor catch - processorUpdated := false - for i := 0; i < 30; i++ { - lpb, err := syncer.GetLastProcessedBlock(ctx) - require.NoError(t, err) - if receipt.BlockNumber.Uint64() == lpb { - processorUpdated = true - break - } - time.Sleep(time.Millisecond * 10) - } - require.True(t, processorUpdated) + helpers.RequireProcessorUpdated(t, syncer, receipt.BlockNumber.Uint64()) // Assert rollup exit root expectedRollupExitRoot, err := verifySC.GetRollupExitRoot(&bind.CallOpts{Pending: false}) @@ -351,24 +340,17 @@ func TestStressAndReorgs(t *testing.T) { func waitForSyncerToCatchUp(ctx context.Context, t *testing.T, syncer *l1infotreesync.L1InfoTreeSync, client *simulated.Backend) { t.Helper() - - syncerUpToDate := false - var errMsg string - - for i := 0; i < 200; i++ { - lpb, err := syncer.GetLastProcessedBlock(ctx) + for { + lastBlockNum, err := client.Client().BlockNumber(ctx) require.NoError(t, err) - lb, err := client.Client().BlockNumber(ctx) + helpers.RequireProcessorUpdated(t, syncer, lastBlockNum) + time.Sleep(time.Second / 2) + lastBlockNum2, err := client.Client().BlockNumber(ctx) require.NoError(t, err) - if lpb == lb { - syncerUpToDate = true - break + if lastBlockNum == lastBlockNum2 { + return } - time.Sleep(time.Second / 2) - errMsg = fmt.Sprintf("last block from client: %d, last block from syncer: %d", lb, lpb) } - - require.True(t, syncerUpToDate, errMsg) } // commitBlocks commits the specified number of blocks with the given client and waits for the specified duration after each block diff --git a/l1infotreesync/l1infotreesync.go b/l1infotreesync/l1infotreesync.go index 9719fcd7..e6262ffb 100644 --- a/l1infotreesync/l1infotreesync.go +++ b/l1infotreesync/l1infotreesync.go @@ -106,7 +106,7 @@ func (s *L1InfoTreeSync) Start(ctx context.Context) { // GetL1InfoTreeMerkleProof creates a merkle proof for the L1 Info tree func (s *L1InfoTreeSync) GetL1InfoTreeMerkleProof(ctx context.Context, index uint32) (types.Proof, types.Root, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Proof{}, types.Root{}, sync.ErrInconsistentState } return s.processor.GetL1InfoTreeMerkleProof(ctx, index) @@ -118,7 +118,7 @@ func (s *L1InfoTreeSync) GetRollupExitTreeMerkleProof( networkID uint32, root common.Hash, ) (types.Proof, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Proof{}, sync.ErrInconsistentState } if networkID == 0 { @@ -141,7 +141,7 @@ func translateError(err error) error { // - ErrBlockNotProcessed, // - ErrNotFound func (s *L1InfoTreeSync) GetLatestInfoUntilBlock(ctx context.Context, blockNum uint64) (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } leaf, err := s.processor.GetLatestInfoUntilBlock(ctx, blockNum) @@ -150,7 +150,7 @@ func (s *L1InfoTreeSync) GetLatestInfoUntilBlock(ctx context.Context, blockNum u // GetInfoByIndex returns the value of a leaf (not the hash) of the L1 info tree func (s *L1InfoTreeSync) GetInfoByIndex(ctx context.Context, index uint32) (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetInfoByIndex(ctx, index) @@ -158,7 +158,7 @@ func (s *L1InfoTreeSync) GetInfoByIndex(ctx context.Context, index uint32) (*L1I // GetL1InfoTreeRootByIndex returns the root of the L1 info tree at the moment the leaf with the given index was added func (s *L1InfoTreeSync) GetL1InfoTreeRootByIndex(ctx context.Context, index uint32) (types.Root, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Root{}, sync.ErrInconsistentState } return s.processor.l1InfoTree.GetRootByIndex(ctx, index) @@ -166,7 +166,7 @@ func (s *L1InfoTreeSync) GetL1InfoTreeRootByIndex(ctx context.Context, index uin // GetLastRollupExitRoot return the last rollup exit root processed func (s *L1InfoTreeSync) GetLastRollupExitRoot(ctx context.Context) (types.Root, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Root{}, sync.ErrInconsistentState } return s.processor.rollupExitTree.GetLastRoot(nil) @@ -174,7 +174,7 @@ func (s *L1InfoTreeSync) GetLastRollupExitRoot(ctx context.Context) (types.Root, // GetLastL1InfoTreeRoot return the last root and index processed from the L1 Info tree func (s *L1InfoTreeSync) GetLastL1InfoTreeRoot(ctx context.Context) (types.Root, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Root{}, sync.ErrInconsistentState } return s.processor.l1InfoTree.GetLastRoot(nil) @@ -182,7 +182,7 @@ func (s *L1InfoTreeSync) GetLastL1InfoTreeRoot(ctx context.Context) (types.Root, // GetLastProcessedBlock return the last processed block func (s *L1InfoTreeSync) GetLastProcessedBlock(ctx context.Context) (uint64, error) { - if s.processor.halted { + if s.processor.isHalted() { return 0, sync.ErrInconsistentState } return s.processor.GetLastProcessedBlock(ctx) @@ -191,7 +191,7 @@ func (s *L1InfoTreeSync) GetLastProcessedBlock(ctx context.Context) (uint64, err func (s *L1InfoTreeSync) GetLocalExitRoot( ctx context.Context, networkID uint32, rollupExitRoot common.Hash, ) (common.Hash, error) { - if s.processor.halted { + if s.processor.isHalted() { return common.Hash{}, sync.ErrInconsistentState } if networkID == 0 { @@ -202,56 +202,56 @@ func (s *L1InfoTreeSync) GetLocalExitRoot( } func (s *L1InfoTreeSync) GetLastVerifiedBatches(rollupID uint32) (*VerifyBatches, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetLastVerifiedBatches(rollupID) } func (s *L1InfoTreeSync) GetFirstVerifiedBatches(rollupID uint32) (*VerifyBatches, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetFirstVerifiedBatches(rollupID) } func (s *L1InfoTreeSync) GetFirstVerifiedBatchesAfterBlock(rollupID uint32, blockNum uint64) (*VerifyBatches, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetFirstVerifiedBatchesAfterBlock(rollupID, blockNum) } func (s *L1InfoTreeSync) GetFirstL1InfoWithRollupExitRoot(rollupExitRoot common.Hash) (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetFirstL1InfoWithRollupExitRoot(rollupExitRoot) } func (s *L1InfoTreeSync) GetLastInfo() (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetLastInfo() } func (s *L1InfoTreeSync) GetFirstInfo() (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetFirstInfo() } func (s *L1InfoTreeSync) GetFirstInfoAfterBlock(blockNum uint64) (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetFirstInfoAfterBlock(blockNum) } func (s *L1InfoTreeSync) GetInfoByGlobalExitRoot(ger common.Hash) (*L1InfoTreeLeaf, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetInfoByGlobalExitRoot(ger) @@ -261,7 +261,7 @@ func (s *L1InfoTreeSync) GetInfoByGlobalExitRoot(ger common.Hash) (*L1InfoTreeLe func (s *L1InfoTreeSync) GetL1InfoTreeMerkleProofFromIndexToRoot( ctx context.Context, index uint32, root common.Hash, ) (types.Proof, error) { - if s.processor.halted { + if s.processor.isHalted() { return types.Proof{}, sync.ErrInconsistentState } return s.processor.l1InfoTree.GetProof(ctx, index, root) @@ -269,7 +269,7 @@ func (s *L1InfoTreeSync) GetL1InfoTreeMerkleProofFromIndexToRoot( // GetInitL1InfoRootMap returns the initial L1 info root map, nil if no root map has been set func (s *L1InfoTreeSync) GetInitL1InfoRootMap(ctx context.Context) (*L1InfoTreeInitial, error) { - if s.processor.halted { + if s.processor.isHalted() { return nil, sync.ErrInconsistentState } return s.processor.GetInitL1InfoRootMap(nil) diff --git a/l1infotreesync/processor.go b/l1infotreesync/processor.go index ee94e829..ac58fb5c 100644 --- a/l1infotreesync/processor.go +++ b/l1infotreesync/processor.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "errors" "fmt" + mutex "sync" "github.com/0xPolygon/cdk/db" "github.com/0xPolygon/cdk/l1infotreesync/migrations" @@ -28,6 +29,7 @@ type processor struct { db *sql.DB l1InfoTree *tree.AppendOnlyTree rollupExitTree *tree.UpdatableTree + mu mutex.RWMutex halted bool haltedReason string } @@ -267,6 +269,8 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error { return err } if rowsAffected > 0 { + p.mu.Lock() + defer p.mu.Unlock() p.halted = false p.haltedReason = "" } @@ -277,7 +281,7 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error { // ProcessBlock process the events of the block to build the rollup exit tree and the l1 info tree // and updates the last processed block (can be called without events for that purpose) func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error { - if p.halted { + if p.isHalted() { log.Errorf("processor is halted due to: %s", p.haltedReason) return sync.ErrInconsistentState } @@ -361,8 +365,10 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error { block.Num, ) log.Error(errStr) + p.mu.Lock() p.haltedReason = errStr p.halted = true + p.mu.Unlock() return sync.ErrInconsistentState } } @@ -464,3 +470,9 @@ func (p *processor) getDBQuerier(tx db.Txer) db.Querier { } return p.db } + +func (p *processor) isHalted() bool { + p.mu.RLock() + defer p.mu.RUnlock() + return p.halted +} diff --git a/lastgersync/e2e_test.go b/lastgersync/e2e_test.go index 9b9a6f36..6f62f12d 100644 --- a/lastgersync/e2e_test.go +++ b/lastgersync/e2e_test.go @@ -11,6 +11,7 @@ import ( "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/lastgersync" "github.com/0xPolygon/cdk/test/aggoraclehelpers" + "github.com/0xPolygon/cdk/test/helpers" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" @@ -49,22 +50,9 @@ func TestE2E(t *testing.T) { require.True(t, isInjected, fmt.Sprintf("iteration %d, GER: %s", i, common.Bytes2Hex(expectedGER[:]))) // Wait for syncer to catch up - syncerUpToDate := false - var errMsg string - for i := 0; i < 10; i++ { - lpb, err := syncer.GetLastProcessedBlock(ctx) - require.NoError(t, err) - lb, err := env.L2Client.Client().BlockNumber(ctx) - require.NoError(t, err) - if lpb == lb { - syncerUpToDate = true - - break - } - time.Sleep(time.Millisecond * 100) - errMsg = fmt.Sprintf("last block from client: %d, last block from syncer: %d", lb, lpb) - } - require.True(t, syncerUpToDate, errMsg) + lb, err := env.L2Client.Client().BlockNumber(ctx) + require.NoError(t, err) + helpers.RequireProcessorUpdated(t, syncer, lb) e, err := syncer.GetFirstGERAfterL1InfoTreeIndex(ctx, uint32(i)) require.NoError(t, err, fmt.Sprint("iteration: ", i)) diff --git a/reorgdetector/types.go b/reorgdetector/types.go index 20d4562c..2c860277 100644 --- a/reorgdetector/types.go +++ b/reorgdetector/types.go @@ -93,9 +93,9 @@ func (hl *headersList) get(num uint64) *header { // getSorted returns headers in sorted order func (hl *headersList) getSorted() []header { + hl.RLock() sortedBlocks := make([]header, 0, len(hl.headers)) - hl.RLock() for _, b := range hl.headers { sortedBlocks = append(sortedBlocks, b) } diff --git a/sync/evmdownloader.go b/sync/evmdownloader.go index 13539f2f..74b8ec7c 100644 --- a/sync/evmdownloader.go +++ b/sync/evmdownloader.go @@ -163,9 +163,13 @@ func (d *EVMDownloaderImplementation) WaitForNewBlocks( case <-ticker.C: header, err := d.ethClient.HeaderByNumber(ctx, d.blockFinality) if err != nil { - attempts++ - d.log.Error("error getting last block num from eth client: ", err) - d.rh.Handle("waitForNewBlocks", attempts) + if ctx.Err() == nil { + attempts++ + d.log.Error("error getting last block num from eth client: ", err) + d.rh.Handle("waitForNewBlocks", attempts) + } else { + d.log.Warn("context has been canceled while trying to get header by number") + } continue } if header.Number.Uint64() > lastBlockSeen { diff --git a/test/helpers/wait.go b/test/helpers/wait.go new file mode 100644 index 00000000..86a6f9fb --- /dev/null +++ b/test/helpers/wait.go @@ -0,0 +1,32 @@ +package helpers + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +type Processorer interface { + GetLastProcessedBlock(ctx context.Context) (uint64, error) +} + +func RequireProcessorUpdated(t *testing.T, processor Processorer, targetBlock uint64) { + t.Helper() + const ( + maxIterations = 100 + sleepTimePerIteration = time.Millisecond * 10 + ) + ctx := context.Background() + for i := 0; i < maxIterations; i++ { + lpb, err := processor.GetLastProcessedBlock(ctx) + require.NoError(t, err) + if targetBlock <= lpb { + return + } + time.Sleep(sleepTimePerIteration) + } + require.NoError(t, errors.New("processor not updated")) +}