From 48f368906a69f041c278291b432a218312afbe1a Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Wed, 22 Mar 2023 15:25:21 +0100 Subject: [PATCH 01/21] Flush state db after setting genesis --- state/state.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/state/state.go b/state/state.go index 6f26586333..5139608dc7 100644 --- a/state/state.go +++ b/state/state.go @@ -1405,6 +1405,13 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db root.SetBytes(newRoot) + // flush state db + err = s.tree.Flush(ctx) + if err != nil { + log.Errorf("error flushing state tree after genesis: %v", err) + return newRoot, err + } + // store L1 block related to genesis batch err = s.AddBlock(ctx, &block, dbTx) if err != nil { From 0806f3fc36a5caaadad73f2858777bfd1058f1f8 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 11:45:05 +0100 Subject: [PATCH 02/21] fix test --- sequencer/finalizer_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sequencer/finalizer_test.go b/sequencer/finalizer_test.go index b7864b015a..9c41f59aa1 100644 --- a/sequencer/finalizer_test.go +++ b/sequencer/finalizer_test.go @@ -61,6 +61,15 @@ var ( SleepDurationInMs: cfgTypes.Duration{ Duration: 60, }, + ClosingSignalsManagerWaitForCheckingL1Timeout: cfgTypes.Duration{ + Duration: 10 * time.Second, + }, + ClosingSignalsManagerWaitForCheckingGER: cfgTypes.Duration{ + Duration: 10 * time.Second, + }, + ClosingSignalsManagerWaitForCheckingForcedBatches: cfgTypes.Duration{ + Duration: 10 * time.Second, + }, ResourcePercentageToCloseBatch: 10, GERFinalityNumberOfBlocks: 64, } From 0c83520baaef2f61963973cc0dca35fc18f9fd0b Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 12:06:40 +0100 Subject: [PATCH 03/21] fix test --- sequencer/closingsignalsmanager_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index dd85d6967a..917b441b67 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -71,8 +71,8 @@ func setupTest(t *testing.T) { // Set genesis batch dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) - _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) - require.NoError(t, err) + // _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) + // require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } From f0aa0e2f83df594f1b7dde6840ab949d27e2a289 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 12:24:48 +0100 Subject: [PATCH 04/21] fix test --- sequencer/closingsignalsmanager_test.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 917b441b67..a7696222b5 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -13,7 +13,6 @@ import ( "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -39,7 +38,7 @@ func setupTest(t *testing.T) { panic(err) } - zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") + zkProverURI := testutils.GetEnv("ZKPROVER_URI", "34.245.104.156") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} var mtDBCancel context.CancelFunc mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) @@ -71,8 +70,8 @@ func setupTest(t *testing.T) { // Set genesis batch dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) - // _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) - // require.NoError(t, err) + _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) + require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } @@ -92,13 +91,6 @@ func TestClosingSignalsManager(t *testing.T) { Etherman: NewEthermanMock(t), } - ctxMatchBy := mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil }) - lastL1BlockNumber := uint64(1) - - m.Etherman. - On("GetLatestBlockNumber", ctxMatchBy). - Return(lastL1BlockNumber, nil) - setupTest(t) channels := ClosingSignalCh{ ForcedBatchCh: make(chan state.ForcedBatch), From c5e253bb799012b9d461e2f82891c9722334bd6f Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 13:21:14 +0100 Subject: [PATCH 05/21] fix test --- sequencer/closingsignalsmanager_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index a7696222b5..3c0e0fe859 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -68,6 +68,7 @@ func setupTest(t *testing.T) { testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch + time.Sleep(5 * time.Second) dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) From 47d25090ccc8dc60270b5566952fc634b9afa203 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 13:53:41 +0100 Subject: [PATCH 06/21] fix test --- sequencer/closingsignalsmanager_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 3c0e0fe859..305528b0d0 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -38,7 +38,7 @@ func setupTest(t *testing.T) { panic(err) } - zkProverURI := testutils.GetEnv("ZKPROVER_URI", "34.245.104.156") + zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} var mtDBCancel context.CancelFunc mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) From 50a353b1529e6a9c3a099799a8844346a12129d8 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 14:14:00 +0100 Subject: [PATCH 07/21] fix test --- state/state.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/state/state.go b/state/state.go index 5139608dc7..a263f2c98b 100644 --- a/state/state.go +++ b/state/state.go @@ -1351,6 +1351,13 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db return newRoot, ErrDBTxNil } + // flush state db + err = s.tree.Flush(ctx) + if err != nil { + log.Errorf("TO BE DELETED error flushing state tree after genesis: %v", err) + return newRoot, err + } + for _, action := range genesis.Actions { address := common.HexToAddress(action.Address) switch action.Type { From 8ec5c8d8e4599e08235335b65644a9b315a495a0 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 14:22:33 +0100 Subject: [PATCH 08/21] fix test --- sequencer/closingsignalsmanager_test.go | 13 +++++++++++-- state/state.go | 7 ------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 305528b0d0..2982ab96db 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -68,10 +68,19 @@ func setupTest(t *testing.T) { testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - time.Sleep(5 * time.Second) + genesis := state.Genesis{ + Actions: []*state.GenesisAction{ + { + Address: "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D", + Type: int(merkletree.LeafTypeBalance), + Value: "10000000", + }, + }, + } + dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) - _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) + _, err = testState.SetGenesis(ctx, state.Block{}, genesis, dbTx) require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } diff --git a/state/state.go b/state/state.go index a263f2c98b..5139608dc7 100644 --- a/state/state.go +++ b/state/state.go @@ -1351,13 +1351,6 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db return newRoot, ErrDBTxNil } - // flush state db - err = s.tree.Flush(ctx) - if err != nil { - log.Errorf("TO BE DELETED error flushing state tree after genesis: %v", err) - return newRoot, err - } - for _, action := range genesis.Actions { address := common.HexToAddress(action.Address) switch action.Type { From ae5dc2d084cd0873626fe6ec94f4846225f7fd62 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 15:16:18 +0100 Subject: [PATCH 09/21] fix test --- sequencer/closingsignalsmanager_test.go | 26 ++++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 2982ab96db..12eb6add6a 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -19,6 +19,7 @@ import ( const numberOfForcesBatches = 10 var ( + mtDBCancel context.CancelFunc testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") @@ -40,14 +41,10 @@ func setupTest(t *testing.T) { zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} - var mtDBCancel context.CancelFunc + mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) s := mtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) - defer func() { - mtDBCancel() - mtDBClientConn.Close() - }() stateTree = merkletree.NewStateTree(mtDBServiceClient) testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree) @@ -68,23 +65,18 @@ func setupTest(t *testing.T) { testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - genesis := state.Genesis{ - Actions: []*state.GenesisAction{ - { - Address: "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D", - Type: int(merkletree.LeafTypeBalance), - Value: "10000000", - }, - }, - } - dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) - _, err = testState.SetGenesis(ctx, state.Block{}, genesis, dbTx) + _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } +func cleanup(t *testing.T) { + mtDBCancel() + mtDBClientConn.Close() +} + func prepareForcedBatches(t *testing.T) { // Create block const sql = `INSERT INTO state.forced_batch (forced_batch_num, global_exit_root, timestamp, raw_txs_data, coinbase, block_num) VALUES ($1, $2, $3, $4, $5, $6)` @@ -136,4 +128,6 @@ func TestClosingSignalsManager(t *testing.T) { require.Equal(t, testGER, fb.GlobalExitRoot) require.Equal(t, testAddr, fb.Sequencer) require.Equal(t, testRawData, fb.RawTxsData) + + cleanup(t) } From 67e3f4dd6028a4ebdcb08030197a36cc2ac55679 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 15:31:12 +0100 Subject: [PATCH 10/21] fix test --- sequencer/closingsignalsmanager_test.go | 31 ++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 12eb6add6a..80b523eccf 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -9,20 +9,25 @@ import ( "github.com/0xPolygonHermez/zkevm-node/db" "github.com/0xPolygonHermez/zkevm-node/log" "github.com/0xPolygonHermez/zkevm-node/merkletree" + mtDBclientpb "github.com/0xPolygonHermez/zkevm-node/merkletree/pb" "github.com/0xPolygonHermez/zkevm-node/state" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "google.golang.org/grpc" ) const numberOfForcesBatches = 10 var ( - mtDBCancel context.CancelFunc - testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") + localMtDBCancel context.CancelFunc + localMtDBServiceClient mtDBclientpb.StateDBServiceClient + localMtDBClientConn *grpc.ClientConn + localState *state.State + testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") ) type mocks struct { @@ -42,12 +47,12 @@ func setupTest(t *testing.T) { zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} - mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) + localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) s := mtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) - stateTree = merkletree.NewStateTree(mtDBServiceClient) - testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree) + localStateTree := merkletree.NewStateTree(localMtDBServiceClient) + localState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, localStateTree) batchConstraints := batchConstraints{ MaxTxsPerBatch: 150, @@ -62,19 +67,19 @@ func setupTest(t *testing.T) { MaxSteps: 8388608, } - testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) + testDbManager = newDBManager(ctx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - dbTx, err := testState.BeginStateTransaction(ctx) + dbTx, err := localState.BeginStateTransaction(ctx) require.NoError(t, err) - _, err = testState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) + _, err = localState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } func cleanup(t *testing.T) { - mtDBCancel() - mtDBClientConn.Close() + localMtDBCancel() + localMtDBClientConn.Close() } func prepareForcedBatches(t *testing.T) { @@ -83,7 +88,7 @@ func prepareForcedBatches(t *testing.T) { for x := 0; x < numberOfForcesBatches; x++ { forcedBatchNum := int64(x) - _, err := testState.PostgresStorage.Exec(ctx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) + _, err := localState.PostgresStorage.Exec(ctx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) assert.NoError(t, err) } } From b43cacfb40265b876eef6a4ea494ce6b7f471b50 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 15:33:41 +0100 Subject: [PATCH 11/21] fix test --- sequencer/closingsignalsmanager_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 80b523eccf..3d24879bf4 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -44,11 +44,11 @@ func setupTest(t *testing.T) { panic(err) } - zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") + zkProverURI := testutils.GetEnv("ZKPROVER_URI", "34.245.104.156") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) - s := mtDBClientConn.GetState() + s := localMtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) localStateTree := merkletree.NewStateTree(localMtDBServiceClient) From 08bbacb1f3f4b46ec29daf56bdba9c9073dc2593 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 15:50:09 +0100 Subject: [PATCH 12/21] fix test --- sequencer/closingsignalsmanager_test.go | 34 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 3d24879bf4..b0ec21a41f 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -11,6 +11,8 @@ import ( "github.com/0xPolygonHermez/zkevm-node/merkletree" mtDBclientpb "github.com/0xPolygonHermez/zkevm-node/merkletree/pb" "github.com/0xPolygonHermez/zkevm-node/state" + "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor" + executorclientpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/assert" @@ -21,13 +23,14 @@ import ( const numberOfForcesBatches = 10 var ( - localMtDBCancel context.CancelFunc - localMtDBServiceClient mtDBclientpb.StateDBServiceClient - localMtDBClientConn *grpc.ClientConn - localState *state.State - testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") + localMtDBCancel, localExecutorCancel context.CancelFunc + localMtDBServiceClient mtDBclientpb.StateDBServiceClient + localMtDBClientConn, localExecutorClientConn *grpc.ClientConn + localState *state.State + localExecutorClient executorclientpb.ExecutorServiceClient + testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") ) type mocks struct { @@ -44,15 +47,20 @@ func setupTest(t *testing.T) { panic(err) } - zkProverURI := testutils.GetEnv("ZKPROVER_URI", "34.245.104.156") - mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} + zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") + locatlMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} + localExecutorServerConfig := executor.Config{URI: fmt.Sprintf("%s:50071", zkProverURI)} - localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) - s := localMtDBClientConn.GetState() + localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(ctx, localExecutorServerConfig) + s := localExecutorClientConn.GetState() + log.Infof("executorClientConn state: %s", s.String()) + + localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(ctx, locatlMtDBServerConfig) + s = localMtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) localStateTree := merkletree.NewStateTree(localMtDBServiceClient) - localState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, localStateTree) + localState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), localExecutorClient, localStateTree) batchConstraints := batchConstraints{ MaxTxsPerBatch: 150, @@ -80,6 +88,8 @@ func setupTest(t *testing.T) { func cleanup(t *testing.T) { localMtDBCancel() localMtDBClientConn.Close() + localExecutorCancel() + localExecutorClientConn.Close() } func prepareForcedBatches(t *testing.T) { From d21e07f71baf0fabc356209011af972142123607 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 16:04:17 +0100 Subject: [PATCH 13/21] fix test --- sequencer/closingsignalsmanager_test.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index b0ec21a41f..1e0a91cb27 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -23,6 +23,7 @@ import ( const numberOfForcesBatches = 10 var ( + localCtx context.Context localMtDBCancel, localExecutorCancel context.CancelFunc localMtDBServiceClient mtDBclientpb.StateDBServiceClient localMtDBClientConn, localExecutorClientConn *grpc.ClientConn @@ -40,7 +41,7 @@ type mocks struct { func setupTest(t *testing.T) { initOrResetDB() - ctx = context.Background() + localCtx = context.Background() stateDb, err = db.NewSQLDB(stateDBCfg) if err != nil { @@ -51,11 +52,11 @@ func setupTest(t *testing.T) { locatlMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} localExecutorServerConfig := executor.Config{URI: fmt.Sprintf("%s:50071", zkProverURI)} - localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(ctx, localExecutorServerConfig) + localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(localCtx, localExecutorServerConfig) s := localExecutorClientConn.GetState() log.Infof("executorClientConn state: %s", s.String()) - localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(ctx, locatlMtDBServerConfig) + localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(localCtx, locatlMtDBServerConfig) s = localMtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) @@ -75,14 +76,14 @@ func setupTest(t *testing.T) { MaxSteps: 8388608, } - testDbManager = newDBManager(ctx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) + testDbManager = newDBManager(localCtx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - dbTx, err := localState.BeginStateTransaction(ctx) + dbTx, err := localState.BeginStateTransaction(localCtx) require.NoError(t, err) - _, err = localState.SetGenesis(ctx, state.Block{}, state.Genesis{}, dbTx) + _, err = localState.SetGenesis(localCtx, state.Block{}, state.Genesis{}, dbTx) require.NoError(t, err) - require.NoError(t, dbTx.Commit(ctx)) + require.NoError(t, dbTx.Commit(localCtx)) } func cleanup(t *testing.T) { @@ -98,7 +99,7 @@ func prepareForcedBatches(t *testing.T) { for x := 0; x < numberOfForcesBatches; x++ { forcedBatchNum := int64(x) - _, err := localState.PostgresStorage.Exec(ctx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) + _, err := localState.PostgresStorage.Exec(localCtx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) assert.NoError(t, err) } } @@ -114,10 +115,10 @@ func TestClosingSignalsManager(t *testing.T) { } prepareForcedBatches(t) - closingSignalsManager := newClosingSignalsManager(ctx, testDbManager, channels, cfg, m.Etherman) + closingSignalsManager := newClosingSignalsManager(localCtx, testDbManager, channels, cfg, m.Etherman) closingSignalsManager.Start() - newCtx, cancelFunc := context.WithTimeout(ctx, time.Second*3) + newCtx, cancelFunc := context.WithTimeout(localCtx, time.Second*3) defer cancelFunc() var fb *state.ForcedBatch @@ -125,7 +126,7 @@ func TestClosingSignalsManager(t *testing.T) { for { select { case <-newCtx.Done(): - log.Infof("received context done, Err: %s", ctx.Err()) + log.Infof("received context done, Err: %s", localCtx.Err()) return // Forced batch ch case fb := <-channels.ForcedBatchCh: From 271a49bb14a65d2e84e9288aa7102e3279755626 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 16:51:45 +0100 Subject: [PATCH 14/21] fix test --- sequencer/closingsignalsmanager_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 1e0a91cb27..26279d42e6 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -126,7 +126,7 @@ func TestClosingSignalsManager(t *testing.T) { for { select { case <-newCtx.Done(): - log.Infof("received context done, Err: %s", localCtx.Err()) + log.Infof("received context done, Err: %s", newCtx.Err()) return // Forced batch ch case fb := <-channels.ForcedBatchCh: From dd0e364118deafa0dd29a718061c20eff197e166 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 17:10:46 +0100 Subject: [PATCH 15/21] fix test --- sequencer/closingsignalsmanager_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 26279d42e6..417f5c3716 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -23,6 +23,7 @@ import ( const numberOfForcesBatches = 10 var ( + localTestDbManager *dbManager localCtx context.Context localMtDBCancel, localExecutorCancel context.CancelFunc localMtDBServiceClient mtDBclientpb.StateDBServiceClient @@ -76,7 +77,7 @@ func setupTest(t *testing.T) { MaxSteps: 8388608, } - testDbManager = newDBManager(localCtx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) + localTestDbManager = newDBManager(localCtx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch dbTx, err := localState.BeginStateTransaction(localCtx) @@ -115,7 +116,7 @@ func TestClosingSignalsManager(t *testing.T) { } prepareForcedBatches(t) - closingSignalsManager := newClosingSignalsManager(localCtx, testDbManager, channels, cfg, m.Etherman) + closingSignalsManager := newClosingSignalsManager(localCtx, localTestDbManager, channels, cfg, m.Etherman) closingSignalsManager.Start() newCtx, cancelFunc := context.WithTimeout(localCtx, time.Second*3) From 6cf933c3646bcefe7bf4c649c8ff6ad153fc70fd Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 17:32:18 +0100 Subject: [PATCH 16/21] fix test --- sequencer/closingsignalsmanager_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 417f5c3716..6d7f30d1f5 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -23,6 +23,7 @@ import ( const numberOfForcesBatches = 10 var ( + localStateDb *db.SQLDB localTestDbManager *dbManager localCtx context.Context localMtDBCancel, localExecutorCancel context.CancelFunc @@ -44,25 +45,25 @@ func setupTest(t *testing.T) { localCtx = context.Background() - stateDb, err = db.NewSQLDB(stateDBCfg) + localStateDb, err = db.NewSQLDB(stateDBCfg) if err != nil { panic(err) } zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") - locatlMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} + localMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} localExecutorServerConfig := executor.Config{URI: fmt.Sprintf("%s:50071", zkProverURI)} localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(localCtx, localExecutorServerConfig) s := localExecutorClientConn.GetState() log.Infof("executorClientConn state: %s", s.String()) - localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(localCtx, locatlMtDBServerConfig) + localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(localCtx, localMtDBServerConfig) s = localMtDBClientConn.GetState() - log.Infof("stateDbClientConn state: %s", s.String()) + log.Infof("localStateDbClientConn state: %s", s.String()) localStateTree := merkletree.NewStateTree(localMtDBServiceClient) - localState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), localExecutorClient, localStateTree) + localState = state.NewState(stateCfg, state.NewPostgresStorage(localStateDb), localExecutorClient, localStateTree) batchConstraints := batchConstraints{ MaxTxsPerBatch: 150, From 13d0d0ae7b76d54370db9b1a8c192f016f78f38c Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 17:32:54 +0100 Subject: [PATCH 17/21] fix test --- sequencer/closingsignalsmanager_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 6d7f30d1f5..c5affe21ac 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -15,6 +15,7 @@ import ( executorclientpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" + "github.com/jackc/pgx/v4/pgxpool" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc" @@ -23,7 +24,7 @@ import ( const numberOfForcesBatches = 10 var ( - localStateDb *db.SQLDB + localStateDb *pgxpool.Pool localTestDbManager *dbManager localCtx context.Context localMtDBCancel, localExecutorCancel context.CancelFunc From 894c05b629f54def9198f6437110017b390a4823 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 17:45:14 +0100 Subject: [PATCH 18/21] fix test --- sequencer/closingsignalsmanager_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index c5affe21ac..99e2e282e1 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -13,6 +13,7 @@ import ( "github.com/0xPolygonHermez/zkevm-node/state" "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor" executorclientpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb" + "github.com/0xPolygonHermez/zkevm-node/test/dbutils" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" "github.com/jackc/pgx/v4/pgxpool" @@ -46,7 +47,7 @@ func setupTest(t *testing.T) { localCtx = context.Background() - localStateDb, err = db.NewSQLDB(stateDBCfg) + localStateDb, err = db.NewSQLDB(dbutils.NewStateConfigFromEnv()) if err != nil { panic(err) } From e7feada06627b1f6fa60e5c032777b4ba32c5120 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 18:08:48 +0100 Subject: [PATCH 19/21] fix test --- sequencer/closingsignalsmanager_test.go | 84 +++++++++++-------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index 99e2e282e1..e62ff38b69 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -9,33 +9,20 @@ import ( "github.com/0xPolygonHermez/zkevm-node/db" "github.com/0xPolygonHermez/zkevm-node/log" "github.com/0xPolygonHermez/zkevm-node/merkletree" - mtDBclientpb "github.com/0xPolygonHermez/zkevm-node/merkletree/pb" "github.com/0xPolygonHermez/zkevm-node/state" - "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor" - executorclientpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb" - "github.com/0xPolygonHermez/zkevm-node/test/dbutils" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" - "github.com/jackc/pgx/v4/pgxpool" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "google.golang.org/grpc" ) const numberOfForcesBatches = 10 var ( - localStateDb *pgxpool.Pool - localTestDbManager *dbManager - localCtx context.Context - localMtDBCancel, localExecutorCancel context.CancelFunc - localMtDBServiceClient mtDBclientpb.StateDBServiceClient - localMtDBClientConn, localExecutorClientConn *grpc.ClientConn - localState *state.State - localExecutorClient executorclientpb.ExecutorServiceClient - testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") + testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") ) type mocks struct { @@ -45,27 +32,26 @@ type mocks struct { func setupTest(t *testing.T) { initOrResetDB() - localCtx = context.Background() + ctx = context.Background() - localStateDb, err = db.NewSQLDB(dbutils.NewStateConfigFromEnv()) + stateDb, err = db.NewSQLDB(stateDBCfg) if err != nil { panic(err) } zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") - localMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} - localExecutorServerConfig := executor.Config{URI: fmt.Sprintf("%s:50071", zkProverURI)} - - localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(localCtx, localExecutorServerConfig) - s := localExecutorClientConn.GetState() - log.Infof("executorClientConn state: %s", s.String()) - - localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(localCtx, localMtDBServerConfig) - s = localMtDBClientConn.GetState() - log.Infof("localStateDbClientConn state: %s", s.String()) - - localStateTree := merkletree.NewStateTree(localMtDBServiceClient) - localState = state.NewState(stateCfg, state.NewPostgresStorage(localStateDb), localExecutorClient, localStateTree) + mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} + var mtDBCancel context.CancelFunc + mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) + s := mtDBClientConn.GetState() + log.Infof("stateDbClientConn state: %s", s.String()) + defer func() { + mtDBCancel() + mtDBClientConn.Close() + }() + + stateTree = merkletree.NewStateTree(mtDBServiceClient) + testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree) batchConstraints := batchConstraints{ MaxTxsPerBatch: 150, @@ -80,21 +66,16 @@ func setupTest(t *testing.T) { MaxSteps: 8388608, } - localTestDbManager = newDBManager(localCtx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) + testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - dbTx, err := localState.BeginStateTransaction(localCtx) + dbTx, err := testState.BeginStateTransaction(ctx) require.NoError(t, err) - _, err = localState.SetGenesis(localCtx, state.Block{}, state.Genesis{}, dbTx) + // Insert into Batch table + const sql = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)" + _, err = testState.PostgresStorage.Exec(ctx, sql, 0, common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), time.Now(), common.Address{}, []byte{}, 0) require.NoError(t, err) - require.NoError(t, dbTx.Commit(localCtx)) -} - -func cleanup(t *testing.T) { - localMtDBCancel() - localMtDBClientConn.Close() - localExecutorCancel() - localExecutorClientConn.Close() + require.NoError(t, dbTx.Commit(ctx)) } func prepareForcedBatches(t *testing.T) { @@ -103,7 +84,7 @@ func prepareForcedBatches(t *testing.T) { for x := 0; x < numberOfForcesBatches; x++ { forcedBatchNum := int64(x) - _, err := localState.PostgresStorage.Exec(localCtx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) + _, err := testState.PostgresStorage.Exec(ctx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) assert.NoError(t, err) } } @@ -113,16 +94,23 @@ func TestClosingSignalsManager(t *testing.T) { Etherman: NewEthermanMock(t), } + ctxMatchBy := mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil }) + lastL1BlockNumber := uint64(1) + + m.Etherman. + On("GetLatestBlockNumber", ctxMatchBy). + Return(lastL1BlockNumber, nil) + setupTest(t) channels := ClosingSignalCh{ ForcedBatchCh: make(chan state.ForcedBatch), } prepareForcedBatches(t) - closingSignalsManager := newClosingSignalsManager(localCtx, localTestDbManager, channels, cfg, m.Etherman) + closingSignalsManager := newClosingSignalsManager(ctx, testDbManager, channels, cfg, m.Etherman) closingSignalsManager.Start() - newCtx, cancelFunc := context.WithTimeout(localCtx, time.Second*3) + newCtx, cancelFunc := context.WithTimeout(ctx, time.Second*3) defer cancelFunc() var fb *state.ForcedBatch @@ -130,7 +118,7 @@ func TestClosingSignalsManager(t *testing.T) { for { select { case <-newCtx.Done(): - log.Infof("received context done, Err: %s", newCtx.Err()) + log.Infof("received context done, Err: %s", ctx.Err()) return // Forced batch ch case fb := <-channels.ForcedBatchCh: @@ -148,6 +136,4 @@ func TestClosingSignalsManager(t *testing.T) { require.Equal(t, testGER, fb.GlobalExitRoot) require.Equal(t, testAddr, fb.Sequencer) require.Equal(t, testRawData, fb.RawTxsData) - - cleanup(t) } From 45195c89f0245bda6d9ad8a8a49542756e9ac30e Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 18:22:45 +0100 Subject: [PATCH 20/21] fix test --- sequencer/closingsignalsmanager_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index e62ff38b69..abebedfe8d 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -73,7 +73,7 @@ func setupTest(t *testing.T) { require.NoError(t, err) // Insert into Batch table const sql = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)" - _, err = testState.PostgresStorage.Exec(ctx, sql, 0, common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), time.Now(), common.Address{}, []byte{}, 0) + _, err = testState.PostgresStorage.Exec(ctx, sql, 0, common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), time.Now(), common.Address{}.String(), []byte{}, 0) require.NoError(t, err) require.NoError(t, dbTx.Commit(ctx)) } From 8fa11c2651e1e5451f825dfdb0e378e7ab8217d7 Mon Sep 17 00:00:00 2001 From: ToniRamirezM Date: Thu, 23 Mar 2023 18:33:16 +0100 Subject: [PATCH 21/21] fix tests --- sequencer/closingsignalsmanager_test.go | 84 ++++++++++++++----------- sequencer/dbmanager_test.go | 15 +++-- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/sequencer/closingsignalsmanager_test.go b/sequencer/closingsignalsmanager_test.go index abebedfe8d..99e2e282e1 100644 --- a/sequencer/closingsignalsmanager_test.go +++ b/sequencer/closingsignalsmanager_test.go @@ -9,20 +9,33 @@ import ( "github.com/0xPolygonHermez/zkevm-node/db" "github.com/0xPolygonHermez/zkevm-node/log" "github.com/0xPolygonHermez/zkevm-node/merkletree" + mtDBclientpb "github.com/0xPolygonHermez/zkevm-node/merkletree/pb" "github.com/0xPolygonHermez/zkevm-node/state" + "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor" + executorclientpb "github.com/0xPolygonHermez/zkevm-node/state/runtime/executor/pb" + "github.com/0xPolygonHermez/zkevm-node/test/dbutils" "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/common" + "github.com/jackc/pgx/v4/pgxpool" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + "google.golang.org/grpc" ) const numberOfForcesBatches = 10 var ( - testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") - testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") + localStateDb *pgxpool.Pool + localTestDbManager *dbManager + localCtx context.Context + localMtDBCancel, localExecutorCancel context.CancelFunc + localMtDBServiceClient mtDBclientpb.StateDBServiceClient + localMtDBClientConn, localExecutorClientConn *grpc.ClientConn + localState *state.State + localExecutorClient executorclientpb.ExecutorServiceClient + testGER = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testAddr = common.HexToAddress("0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D") + testRawData = common.Hex2Bytes("0xee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e880801cee7e01dc62f69a12c3510c6d64de04ee6346d84b6a017f3e786c7d87f963e75d8cc91fa983cd6d9cf55fff80d73bd26cd333b0f098acc1e58edb1fd484ad731b") ) type mocks struct { @@ -32,26 +45,27 @@ type mocks struct { func setupTest(t *testing.T) { initOrResetDB() - ctx = context.Background() + localCtx = context.Background() - stateDb, err = db.NewSQLDB(stateDBCfg) + localStateDb, err = db.NewSQLDB(dbutils.NewStateConfigFromEnv()) if err != nil { panic(err) } zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") - mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} - var mtDBCancel context.CancelFunc - mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) - s := mtDBClientConn.GetState() - log.Infof("stateDbClientConn state: %s", s.String()) - defer func() { - mtDBCancel() - mtDBClientConn.Close() - }() - - stateTree = merkletree.NewStateTree(mtDBServiceClient) - testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree) + localMtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} + localExecutorServerConfig := executor.Config{URI: fmt.Sprintf("%s:50071", zkProverURI)} + + localExecutorClient, localExecutorClientConn, localExecutorCancel = executor.NewExecutorClient(localCtx, localExecutorServerConfig) + s := localExecutorClientConn.GetState() + log.Infof("executorClientConn state: %s", s.String()) + + localMtDBServiceClient, localMtDBClientConn, localMtDBCancel = merkletree.NewMTDBServiceClient(localCtx, localMtDBServerConfig) + s = localMtDBClientConn.GetState() + log.Infof("localStateDbClientConn state: %s", s.String()) + + localStateTree := merkletree.NewStateTree(localMtDBServiceClient) + localState = state.NewState(stateCfg, state.NewPostgresStorage(localStateDb), localExecutorClient, localStateTree) batchConstraints := batchConstraints{ MaxTxsPerBatch: 150, @@ -66,16 +80,21 @@ func setupTest(t *testing.T) { MaxSteps: 8388608, } - testDbManager = newDBManager(ctx, dbManagerCfg, nil, testState, nil, closingSignalCh, txsStore, batchConstraints) + localTestDbManager = newDBManager(localCtx, dbManagerCfg, nil, localState, nil, closingSignalCh, txsStore, batchConstraints) // Set genesis batch - dbTx, err := testState.BeginStateTransaction(ctx) + dbTx, err := localState.BeginStateTransaction(localCtx) require.NoError(t, err) - // Insert into Batch table - const sql = "INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)" - _, err = testState.PostgresStorage.Exec(ctx, sql, 0, common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), common.Hash{}.String(), time.Now(), common.Address{}.String(), []byte{}, 0) + _, err = localState.SetGenesis(localCtx, state.Block{}, state.Genesis{}, dbTx) require.NoError(t, err) - require.NoError(t, dbTx.Commit(ctx)) + require.NoError(t, dbTx.Commit(localCtx)) +} + +func cleanup(t *testing.T) { + localMtDBCancel() + localMtDBClientConn.Close() + localExecutorCancel() + localExecutorClientConn.Close() } func prepareForcedBatches(t *testing.T) { @@ -84,7 +103,7 @@ func prepareForcedBatches(t *testing.T) { for x := 0; x < numberOfForcesBatches; x++ { forcedBatchNum := int64(x) - _, err := testState.PostgresStorage.Exec(ctx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) + _, err := localState.PostgresStorage.Exec(localCtx, sql, forcedBatchNum, testGER.String(), time.Now(), testRawData, testAddr.String(), 0) assert.NoError(t, err) } } @@ -94,23 +113,16 @@ func TestClosingSignalsManager(t *testing.T) { Etherman: NewEthermanMock(t), } - ctxMatchBy := mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil }) - lastL1BlockNumber := uint64(1) - - m.Etherman. - On("GetLatestBlockNumber", ctxMatchBy). - Return(lastL1BlockNumber, nil) - setupTest(t) channels := ClosingSignalCh{ ForcedBatchCh: make(chan state.ForcedBatch), } prepareForcedBatches(t) - closingSignalsManager := newClosingSignalsManager(ctx, testDbManager, channels, cfg, m.Etherman) + closingSignalsManager := newClosingSignalsManager(localCtx, localTestDbManager, channels, cfg, m.Etherman) closingSignalsManager.Start() - newCtx, cancelFunc := context.WithTimeout(ctx, time.Second*3) + newCtx, cancelFunc := context.WithTimeout(localCtx, time.Second*3) defer cancelFunc() var fb *state.ForcedBatch @@ -118,7 +130,7 @@ func TestClosingSignalsManager(t *testing.T) { for { select { case <-newCtx.Done(): - log.Infof("received context done, Err: %s", ctx.Err()) + log.Infof("received context done, Err: %s", newCtx.Err()) return // Forced batch ch case fb := <-channels.ForcedBatchCh: @@ -136,4 +148,6 @@ func TestClosingSignalsManager(t *testing.T) { require.Equal(t, testGER, fb.GlobalExitRoot) require.Equal(t, testAddr, fb.Sequencer) require.Equal(t, testRawData, fb.RawTxsData) + + cleanup(t) } diff --git a/sequencer/dbmanager_test.go b/sequencer/dbmanager_test.go index 16016fafaf..d4c4b16031 100644 --- a/sequencer/dbmanager_test.go +++ b/sequencer/dbmanager_test.go @@ -23,6 +23,7 @@ import ( ) var ( + mtDBCancel context.CancelFunc ctx context.Context testState *state.State stateTree *merkletree.StateTree @@ -51,14 +52,10 @@ func setupDBManager() { zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost") mtDBServerConfig := merkletree.Config{URI: fmt.Sprintf("%s:50061", zkProverURI)} - var mtDBCancel context.CancelFunc + mtDBServiceClient, mtDBClientConn, mtDBCancel = merkletree.NewMTDBServiceClient(ctx, mtDBServerConfig) s := mtDBClientConn.GetState() log.Infof("stateDbClientConn state: %s", s.String()) - defer func() { - mtDBCancel() - mtDBClientConn.Close() - }() stateTree = merkletree.NewStateTree(mtDBServiceClient) testState = state.NewState(stateCfg, state.NewPostgresStorage(stateDb), executorClient, stateTree) @@ -98,6 +95,11 @@ func initOrResetDB() { } } +func cleanupDBManager() { + mtDBCancel() + mtDBClientConn.Close() +} + func TestOpenBatch(t *testing.T) { setupDBManager() defer stateDb.Close() @@ -119,6 +121,7 @@ func TestOpenBatch(t *testing.T) { require.NoError(t, err) err = dbTx.Commit(ctx) require.NoError(t, err) + cleanupDBManager() } func TestGetLastBatchNumber(t *testing.T) { @@ -146,6 +149,7 @@ func TestGetLastBatchNumber(t *testing.T) { lastBatchNum, err := testDbManager.GetLastBatchNumber(ctx) require.NoError(t, err) require.Equal(t, uint64(1), lastBatchNum) + cleanupDBManager() } func TestCreateFirstBatch(t *testing.T) { @@ -161,4 +165,5 @@ func TestCreateFirstBatch(t *testing.T) { processingContext := testDbManager.CreateFirstBatch(ctx, common.Address{}) require.Equal(t, uint64(1), processingContext.BatchNumber) + cleanupDBManager() }