From 4e0e957ebaf1d0e8e798c56b34d19dfea33f5415 Mon Sep 17 00:00:00 2001 From: Joan Esteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 9 Nov 2023 08:17:08 +0100 Subject: [PATCH] Fix e2e group 10 (forced batches) (#2750) * adapt ARR552 fixes from PR #2694 * add failfast to reduce the time on fail * fix problem with update GER * avoid duplicated code for vector_groupx --- .../forced_batches_vector_shared.go | 1 + .../forced_batches_vector_shared.go | 1 + ci/e2e-group9/forced_batches_vector_shared.go | 1 + test/Makefile | 23 +- test/e2e/forced_batches_test.go | 292 ++++++++++++------ test/e2e/forced_batches_vector_group1_test.go | 125 +------- test/e2e/forced_batches_vector_group2_test.go | 124 +------- test/e2e/forced_batches_vector_group3_test.go | 124 +------- test/e2e/forced_batches_vector_shared.go | 132 ++++++++ test/e2e/shared.go | 1 - test/operations/manager.go | 22 +- 11 files changed, 355 insertions(+), 491 deletions(-) create mode 120000 ci/e2e-group10/forced_batches_vector_shared.go create mode 120000 ci/e2e-group11/forced_batches_vector_shared.go create mode 120000 ci/e2e-group9/forced_batches_vector_shared.go create mode 100644 test/e2e/forced_batches_vector_shared.go diff --git a/ci/e2e-group10/forced_batches_vector_shared.go b/ci/e2e-group10/forced_batches_vector_shared.go new file mode 120000 index 0000000000..d8db51b440 --- /dev/null +++ b/ci/e2e-group10/forced_batches_vector_shared.go @@ -0,0 +1 @@ +../../test/e2e/forced_batches_vector_shared.go \ No newline at end of file diff --git a/ci/e2e-group11/forced_batches_vector_shared.go b/ci/e2e-group11/forced_batches_vector_shared.go new file mode 120000 index 0000000000..d8db51b440 --- /dev/null +++ b/ci/e2e-group11/forced_batches_vector_shared.go @@ -0,0 +1 @@ +../../test/e2e/forced_batches_vector_shared.go \ No newline at end of file diff --git a/ci/e2e-group9/forced_batches_vector_shared.go b/ci/e2e-group9/forced_batches_vector_shared.go new file mode 120000 index 0000000000..d8db51b440 --- /dev/null +++ b/ci/e2e-group9/forced_batches_vector_shared.go @@ -0,0 +1 @@ +../../test/e2e/forced_batches_vector_shared.go \ No newline at end of file diff --git a/test/Makefile b/test/Makefile index c1a6ebbfd9..6646085ac4 100644 --- a/test/Makefile +++ b/test/Makefile @@ -93,6 +93,7 @@ test-full-non-e2e: stop ## Runs non-e2e tests checking race conditions sleep 7 $(RUNL1NETWORK) sleep 15 + docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -short -race -p 1 -covermode=atomic -coverprofile=../coverage.out -timeout 70s ../... @@ -105,7 +106,7 @@ test-e2e-group-1: stop ## Runs group 1 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group1/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group1/... .PHONY: test-e2e-group-2 test-e2e-group-2: stop ## Runs group 2 e2e tests checking race conditions @@ -116,7 +117,7 @@ test-e2e-group-2: stop ## Runs group 2 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group2/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group2/... .PHONY: test-e2e-group-3 test-e2e-group-3: stop ## Runs group 3 e2e tests checking race conditions @@ -127,7 +128,7 @@ test-e2e-group-3: stop ## Runs group 3 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group3/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group3/... .PHONY: test-e2e-group-4 test-e2e-group-4: stop ## Runs group 4 e2e tests checking race conditions @@ -138,7 +139,7 @@ test-e2e-group-4: stop ## Runs group 4 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group4/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group4/... .PHONY: test-e2e-group-5 test-e2e-group-5: stop ## Runs group 5 e2e tests checking race conditions @@ -149,7 +150,7 @@ test-e2e-group-5: stop ## Runs group 5 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 1200s ../ci/e2e-group5/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 1200s ../ci/e2e-group5/... .PHONY: test-e2e-group-6 test-e2e-group-6: stop ## Runs group 6 e2e tests checking race conditions @@ -160,7 +161,7 @@ test-e2e-group-6: stop ## Runs group 6 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group6/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group6/... .PHONY: test-e2e-group-7 test-e2e-group-7: stop ## Runs group 7 e2e tests checking race conditions @@ -171,7 +172,7 @@ test-e2e-group-7: stop ## Runs group 7 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 600s ../ci/e2e-group7/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 600s ../ci/e2e-group7/... .PHONY: test-e2e-group-8 test-e2e-group-8: stop ## Runs group 8 e2e tests checking race conditions @@ -182,7 +183,7 @@ test-e2e-group-8: stop ## Runs group 8 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 1200s ../ci/e2e-group8/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 1200s ../ci/e2e-group8/... .PHONY: test-e2e-group-9 @@ -194,7 +195,7 @@ test-e2e-group-9: stop ## Runs group 9 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 2000s ../ci/e2e-group9/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 2000s ../ci/e2e-group9/... .PHONY: test-e2e-group-10 @@ -206,7 +207,7 @@ test-e2e-group-10: stop ## Runs group 10 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 2000s ../ci/e2e-group10/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 2000s ../ci/e2e-group10/... .PHONY: test-e2e-group-11 @@ -218,7 +219,7 @@ test-e2e-group-11: stop ## Runs group 11 e2e tests checking race conditions $(RUNZKPROVER) docker ps -a docker logs $(DOCKERCOMPOSEZKPROVER) - trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -race -v -p 1 -timeout 2000s ../ci/e2e-group11/... + trap '$(STOP)' EXIT; MallocNanoZone=0 go test -count=1 -failfast -race -v -p 1 -timeout 2000s ../ci/e2e-group11/... .PHONY: benchmark-sequencer-eth-transfers benchmark-sequencer-eth-transfers: stop diff --git a/test/e2e/forced_batches_test.go b/test/e2e/forced_batches_test.go index 149114e28f..8c3483ce3c 100644 --- a/test/e2e/forced_batches_test.go +++ b/test/e2e/forced_batches_test.go @@ -6,10 +6,10 @@ import ( "testing" "time" - "github.com/0xPolygonHermez/zkevm-node/config" - "github.com/ethereum/go-ethereum/core/types" + "github.com/0xPolygonHermez/zkevm-node/config" + "github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/matic" "github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm" "github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevmglobalexitroot" "github.com/0xPolygonHermez/zkevm-node/log" @@ -23,64 +23,112 @@ import ( "github.com/stretchr/testify/require" ) +const ( + // dockersArePreLaunched is a flag that indicates if dockers are pre-launched, used for local development + // avoiding launch time and reset database time at end (so you can check the database after the test) + dockersArePreLaunched = false + gerFinalityBlocks = uint64(9223372036854775807) // The biggeset uint64 +) + +type l1Stuff struct { + ethClient *ethclient.Client + authSequencer *bind.TransactOpts + authForcedBatch *bind.TransactOpts + zkEvmAddr common.Address + zkEvm *polygonzkevm.Polygonzkevm +} + +type l2Stuff struct { + opsman *operations.Manager + authSequencer *bind.TransactOpts + client *ethclient.Client + amount *big.Int + gasLimit uint64 + gasPrice *big.Int + nonce uint64 +} + func TestForcedBatches(t *testing.T) { if testing.Short() { t.Skip() } - - defer func() { - require.NoError(t, operations.Teardown()) - }() + log.Infof("Running TestForcedBatches ==========================") + if !dockersArePreLaunched { + defer func() { + require.NoError(t, operations.Teardown()) + }() + } var err error nTxs := 10 ctx := context.Background() - opsman, auth, client, amount, gasLimit, gasPrice, nonce := setupEnvironment(ctx, t) + l2 := setupEnvironment(ctx, t) + l1 := setupEnvironmentL1(ctx, t) + l2BlockNumbersTxsBeforeForcedBatch := generateTxsBeforeSendingForcedBatch(ctx, t, nTxs, l2) + time.Sleep(2 * time.Second) + l2.amount = big.NewInt(0).Add(l2.amount, big.NewInt(10)) + encodedTxs := generateSignedAndEncodedTxForForcedBatch(ctx, t, l2) + forcedBatch, err := sendForcedBatch(ctx, t, encodedTxs, l2.opsman, l1) + require.NoError(t, err) + checkThatPreviousTxsWereProcessedWithinPreviousClosedBatch(ctx, t, l2.opsman.State(), l2BlockNumbersTxsBeforeForcedBatch, forcedBatch.BatchNumber) +} +func generateTxsBeforeSendingForcedBatch(ctx context.Context, t *testing.T, nTxs int, l2 *l2Stuff) []*big.Int { txs := make([]*types.Transaction, 0, nTxs) for i := 0; i < nTxs; i++ { - tx := types.NewTransaction(nonce, toAddress, amount, gasLimit, gasPrice, nil) - nonce = nonce + 1 + tx := types.NewTransaction(l2.nonce, toAddress, l2.amount, l2.gasLimit, l2.gasPrice, nil) + l2.nonce = l2.nonce + 1 txs = append(txs, tx) } var l2BlockNumbers []*big.Int - l2BlockNumbers, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel) - require.NoError(t, err) - - time.Sleep(2 * time.Second) - amount = big.NewInt(0).Add(amount, big.NewInt(10)) - unsignedTx := types.NewTransaction(nonce, toAddress, amount, gasLimit, gasPrice, nil) - signedTx, err := auth.Signer(auth.From, unsignedTx) - require.NoError(t, err) - encodedTxs, err := state.EncodeTransactions([]types.Transaction{*signedTx}, constants.EffectivePercentage, forkID6) - require.NoError(t, err) - forcedBatch, err := sendForcedBatch(t, encodedTxs, opsman) + l2BlockNumbers, err := operations.ApplyL2Txs(ctx, txs, l2.authSequencer, l2.client, operations.VerifiedConfirmationLevel) require.NoError(t, err) + return l2BlockNumbers +} +func checkThatPreviousTxsWereProcessedWithinPreviousClosedBatch(ctx context.Context, t *testing.T, state *state.State, l2BlockNumbers []*big.Int, forcedBatchNumber uint64) { // Checking if all txs sent before the forced batch were processed within previous closed batch for _, l2blockNum := range l2BlockNumbers { - batch, err := opsman.State().GetBatchByL2BlockNumber(ctx, l2blockNum.Uint64(), nil) + batch, err := state.GetBatchByL2BlockNumber(ctx, l2blockNum.Uint64(), nil) require.NoError(t, err) - require.Less(t, batch.BatchNumber, forcedBatch.BatchNumber) + require.Less(t, batch.BatchNumber, forcedBatchNumber) } } -func setupEnvironment(ctx context.Context, t *testing.T) (*operations.Manager, *bind.TransactOpts, *ethclient.Client, *big.Int, uint64, *big.Int, uint64) { - err := operations.Teardown() +func generateSignedAndEncodedTxForForcedBatch(ctx context.Context, t *testing.T, l2 *l2Stuff) []byte { + unsignedTx := types.NewTransaction(l2.nonce, toAddress, l2.amount, l2.gasLimit, l2.gasPrice, nil) + signedTx, err := l2.authSequencer.Signer(l2.authSequencer.From, unsignedTx) require.NoError(t, err) + log.Info("Forced Batch: 1 tx -> ", signedTx.Hash()) + encodedTxs, err := state.EncodeTransactions([]types.Transaction{*signedTx}, constants.EffectivePercentage, forkID6) + require.NoError(t, err) + return encodedTxs +} + +func setupEnvironment(ctx context.Context, t *testing.T) *l2Stuff { + if !dockersArePreLaunched { + err := operations.Teardown() + require.NoError(t, err) + } opsCfg := operations.GetDefaultOperationsConfig() opsCfg.State.MaxCumulativeGasUsed = 80000000000 - genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") - require.NoError(t, err) - genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) - require.NoError(t, err) - opsman, err := operations.NewManager(ctx, opsCfg) - require.NoError(t, err) - require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) - err = opsman.Setup() - require.NoError(t, err) - time.Sleep(5 * time.Second) + + var opsman *operations.Manager + var err error + + if !dockersArePreLaunched { + log.Info("Launching dockers and resetting Database") + opsman, err = operations.NewManager(ctx, opsCfg) + require.NoError(t, err) + log.Info("Setting Genesis") + setInitialState(t, opsman) + } else { + log.Info("Using pre-launched dockers: no reset Database") + opsman, err = operations.NewManagerNoInitDB(ctx, opsCfg) + require.NoError(t, err) + } + // Load account with balance on local genesis auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL2ChainID) require.NoError(t, err) @@ -104,128 +152,170 @@ func setupEnvironment(ctx context.Context, t *testing.T) (*operations.Manager, * gasPrice, err := client.SuggestGasPrice(ctx) require.NoError(t, err) - - nonce, err := client.PendingNonceAt(ctx, auth.From) - require.NoError(t, err) - return opsman, auth, client, amount, gasLimit, gasPrice, nonce + return &l2Stuff{opsman, auth, client, amount, gasLimit, gasPrice, senderNonce} } -func sendForcedBatch(t *testing.T, txs []byte, opsman *operations.Manager) (*state.Batch, error) { - ctx := context.Background() - st := opsman.State() +func setupEnvironmentL1(ctx context.Context, t *testing.T) *l1Stuff { // Connect to ethereum node ethClient, err := ethclient.Dial(operations.DefaultL1NetworkURL) require.NoError(t, err) + authSequencer, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID) + require.NoError(t, err) + authForcedBatch, err := operations.GetAuth(operations.DefaultForcedBatchesPrivateKey, operations.DefaultL1ChainID) + require.NoError(t, err) + maticSmc, err := matic.NewMatic(common.HexToAddress(operations.DefaultL1MaticSmartContract), ethClient) + require.NoError(t, err) + maticAmount, _ := big.NewInt(0).SetString("9999999999999999999999", 0) + log.Debugf("Charging MATIC from sequencer -> forcedBatchesAddress") + txValue, err := maticSmc.Transfer(authSequencer, common.HexToAddress(operations.DefaultForcedBatchesAddress), maticAmount) + require.NoError(t, err) + log.Debugf("Waiting for tx %s to be mined (transfer of matic from sequencer -> forcedBatches)", txValue.Hash().String()) + err = operations.WaitTxToBeMined(ctx, ethClient, txValue, operations.DefaultTimeoutTxToBeMined) + require.NoError(t, err) + balance, err := maticSmc.BalanceOf(&bind.CallOpts{Pending: false}, common.HexToAddress(operations.DefaultSequencerAddress)) + require.NoError(t, err) + log.Debugf("Account (sequencer) %s MATIC balance %s", operations.DefaultSequencerAddress, balance.String()) - initialGer, _, err := st.GetLatestGer(ctx, gerFinalityBlocks) + balance, err = maticSmc.BalanceOf(&bind.CallOpts{Pending: false}, common.HexToAddress(operations.DefaultForcedBatchesAddress)) + require.NoError(t, err) + log.Debugf("Account (force_batches) %s MATIC balance %s", operations.DefaultForcedBatchesAddress, balance.String()) + log.Debugf("Approve to zkEVM SMC to spend %s MATIC", maticAmount.String()) + _, err = maticSmc.Approve(authForcedBatch, common.HexToAddress(operations.DefaultL1ZkEVMSmartContract), maticAmount) require.NoError(t, err) - // Create smc client zkEvmAddr := common.HexToAddress(operations.DefaultL1ZkEVMSmartContract) zkEvm, err := polygonzkevm.NewPolygonzkevm(zkEvmAddr, ethClient) require.NoError(t, err) + return &l1Stuff{ethClient: ethClient, authSequencer: authSequencer, authForcedBatch: authForcedBatch, zkEvmAddr: zkEvmAddr, zkEvm: zkEvm} +} - auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID) +func setInitialState(t *testing.T, opsman *operations.Manager) { + genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") + require.NoError(t, err) + genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) require.NoError(t, err) + require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) + err = opsman.Setup() + require.NoError(t, err) + time.Sleep(5 * time.Second) +} - log.Info("Using address: ", auth.From) +func sendForcedBatch(ctx context.Context, t *testing.T, txs []byte, opsman *operations.Manager, l1 *l1Stuff) (*state.Batch, error) { + st := opsman.State() - num, err := zkEvm.LastForceBatch(&bind.CallOpts{Pending: false}) + initialGer, _, err := st.GetLatestGer(ctx, gerFinalityBlocks) + require.NoError(t, err) + + log.Info("Using address: ", l1.authForcedBatch.From) + + num, err := l1.zkEvm.LastForceBatch(&bind.CallOpts{Pending: false}) require.NoError(t, err) log.Info("Number of forceBatches in the smc: ", num) // Get tip - tip, err := zkEvm.GetForcedBatchFee(&bind.CallOpts{Pending: false}) + tip, err := l1.zkEvm.GetForcedBatchFee(&bind.CallOpts{Pending: false}) require.NoError(t, err) - - managerAddress, err := zkEvm.GlobalExitRootManager(&bind.CallOpts{Pending: false}) + log.Infof("Foced Batch Fee:%s", tip.String()) + managerAddress, err := l1.zkEvm.GlobalExitRootManager(&bind.CallOpts{Pending: false}) require.NoError(t, err) - manager, err := polygonzkevmglobalexitroot.NewPolygonzkevmglobalexitroot(managerAddress, ethClient) + manager, err := polygonzkevmglobalexitroot.NewPolygonzkevmglobalexitroot(managerAddress, l1.ethClient) require.NoError(t, err) rootInContract, err := manager.GetLastGlobalExitRoot(&bind.CallOpts{Pending: false}) require.NoError(t, err) rootInContractHash := common.BytesToHash(rootInContract[:]) - disallowed, err := zkEvm.IsForcedBatchDisallowed(&bind.CallOpts{Pending: false}) + disallowed, err := l1.zkEvm.IsForcedBatchDisallowed(&bind.CallOpts{Pending: false}) require.NoError(t, err) if disallowed { - tx, err := zkEvm.ActivateForceBatches(auth) + log.Infof("Forced batch is disallowed. Activating...") + tx, err := l1.zkEvm.ActivateForceBatches(l1.authSequencer) require.NoError(t, err) - err = operations.WaitTxToBeMined(ctx, ethClient, tx, operations.DefaultTimeoutTxToBeMined) + log.Infof("Forced batch is disallowed. Activated. Waiting for tx %s to be mined", tx.Hash()) + err = operations.WaitTxToBeMined(ctx, l1.ethClient, tx, operations.DefaultTimeoutTxToBeMined) require.NoError(t, err) } - currentBlock, err := ethClient.BlockByNumber(ctx, nil) + currentBlock, err := l1.ethClient.BlockByNumber(ctx, nil) require.NoError(t, err) - log.Debug("currentBlock.Time(): ", currentBlock.Time()) + log.Debugf("L1: currentBlock: number:%s Time():%s ", currentBlock.Number().String(), currentBlock.Time()) // Send forceBatch - tx, err := zkEvm.ForceBatch(auth, txs, tip) + tx, err := l1.zkEvm.ForceBatch(l1.authForcedBatch, txs, tip) require.NoError(t, err) log.Info("TxHash: ", tx.Hash()) time.Sleep(1 * time.Second) - err = operations.WaitTxToBeMined(ctx, ethClient, tx, operations.DefaultTimeoutTxToBeMined) + err = operations.WaitTxToBeMined(ctx, l1.ethClient, tx, operations.DefaultTimeoutTxToBeMined) require.NoError(t, err) - query := ethereum.FilterQuery{ - FromBlock: currentBlock.Number(), - Addresses: []common.Address{zkEvmAddr}, + fb, vLog, err := findForcedBatchInL1Logs(ctx, t, currentBlock.Number(), l1) + if err != nil { + log.Errorf("failed to parse force batch log event, err: ", err) + } + ger := fb.LastGlobalExitRoot + + log.Debugf("log decoded: %+v", fb) + log.Info("GlobalExitRoot: ", ger) + log.Info("Transactions: ", common.Bytes2Hex(fb.Transactions)) + log.Info("ForcedBatchNum: ", fb.ForceBatchNum) + fullBlock, err := l1.ethClient.BlockByHash(ctx, vLog.BlockHash) + if err != nil { + log.Errorf("error getting hashParent. BlockNumber: %d. Error: %v", vLog.BlockNumber, err) + return nil, err + } + log.Info("MinForcedTimestamp: ", fullBlock.Time()) + forcedBatch, err := st.GetBatchByForcedBatchNum(ctx, fb.ForceBatchNum, nil) + for err == state.ErrStateNotSynchronized { + time.Sleep(1 * time.Second) + forcedBatch, err = st.GetBatchByForcedBatchNum(ctx, fb.ForceBatchNum, nil) } - logs, err := ethClient.FilterLogs(ctx, query) + require.NoError(t, err) + require.NotNil(t, forcedBatch) - var forcedBatch *state.Batch - for _, vLog := range logs { - if vLog.Topics[0] != constants.ForcedBatchSignatureHash { - logs, err = ethClient.FilterLogs(ctx, query) - require.NoError(t, err) - continue - } - fb, err := zkEvm.ParseForceBatch(vLog) - if err != nil { - log.Errorf("failed to parse force batch log event, err: ", err) - } - log.Debugf("log decoded: %+v", fb) - ger := fb.LastGlobalExitRoot - log.Info("GlobalExitRoot: ", ger) - log.Info("Transactions: ", common.Bytes2Hex(fb.Transactions)) - fullBlock, err := ethClient.BlockByHash(ctx, vLog.BlockHash) - if err != nil { - log.Errorf("error getting hashParent. BlockNumber: %d. Error: %v", vLog.BlockNumber, err) - return nil, err - } - log.Info("MinForcedTimestamp: ", fullBlock.Time()) - forcedBatch, err = st.GetBatchByForcedBatchNum(ctx, fb.ForceBatchNum, nil) - for err == state.ErrStateNotSynchronized { - time.Sleep(1 * time.Second) - forcedBatch, err = st.GetBatchByForcedBatchNum(ctx, fb.ForceBatchNum, nil) - } - log.Info("ForcedBatchNum: ", forcedBatch.BatchNumber) - require.NoError(t, err) - require.NotNil(t, forcedBatch) + log.Info("Waiting for batch to be virtualized...") + err = operations.WaitBatchToBeVirtualized(forcedBatch.BatchNumber, 4*time.Minute, st) + require.NoError(t, err) - log.Info("Waiting for batch to be virtualized...") - err = operations.WaitBatchToBeVirtualized(forcedBatch.BatchNumber, 4*time.Minute, st) - require.NoError(t, err) + log.Info("Waiting for batch to be consolidated...") + err = operations.WaitBatchToBeConsolidated(forcedBatch.BatchNumber, 4*time.Minute, st) + require.NoError(t, err) - log.Info("Waiting for batch to be consolidated...") - err = operations.WaitBatchToBeConsolidated(forcedBatch.BatchNumber, 4*time.Minute, st) + if rootInContractHash != initialGer.GlobalExitRoot { + log.Info("Checking if global exit root is updated...") + finalGer, _, err := st.GetLatestGer(ctx, gerFinalityBlocks) require.NoError(t, err) + require.Equal(t, rootInContractHash, finalGer.GlobalExitRoot, "global exit root is not updated") + } + + return forcedBatch, nil +} - if rootInContractHash != initialGer.GlobalExitRoot { - finalGer, _, err := st.GetLatestGer(ctx, gerFinalityBlocks) - require.NoError(t, err) - if finalGer.GlobalExitRoot != rootInContractHash { - log.Fatal("global exit root is not updated") +func findForcedBatchInL1Logs(ctx context.Context, t *testing.T, fromBlock *big.Int, l1 *l1Stuff) (*polygonzkevm.PolygonzkevmForceBatch, *types.Log, error) { + query := ethereum.FilterQuery{ + FromBlock: fromBlock, + Addresses: []common.Address{l1.zkEvmAddr}, + } + + found := false + for found != true { + log.Debugf("Looking for forced batch in logs from block %s", fromBlock.String()) + logs, err := l1.ethClient.FilterLogs(ctx, query) + require.NoError(t, err) + for _, vLog := range logs { + if vLog.Topics[0] == constants.ForcedBatchSignatureHash { + fb, err := l1.zkEvm.ParseForceBatch(vLog) + return fb, &vLog, err } } + log.Info("Forced batch not found in logs. Waiting 1 second...") + time.Sleep(1 * time.Second) } + return nil, nil, nil - return forcedBatch, nil } diff --git a/test/e2e/forced_batches_vector_group1_test.go b/test/e2e/forced_batches_vector_group1_test.go index 7bf6c70cf3..d5e6f8b5a5 100644 --- a/test/e2e/forced_batches_vector_group1_test.go +++ b/test/e2e/forced_batches_vector_group1_test.go @@ -1,135 +1,12 @@ package e2e import ( - "context" - "math/big" - "os" - "path/filepath" - "strings" "testing" - "time" - - "github.com/0xPolygonHermez/zkevm-node/config" - "github.com/0xPolygonHermez/zkevm-node/hex" - "github.com/0xPolygonHermez/zkevm-node/log" - "github.com/0xPolygonHermez/zkevm-node/state" - "github.com/0xPolygonHermez/zkevm-node/test/operations" - "github.com/0xPolygonHermez/zkevm-node/test/vectors" - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" ) func TestForcedBatchesVectorFilesGroup1(t *testing.T) { - if testing.Short() { t.Skip() } - vectorFilesDir := "./../vectors/src/state-transition/forced-tx/group1" - ctx := context.Background() - genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") - require.NoError(t, err) - genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) - require.NoError(t, err) - err = filepath.Walk(vectorFilesDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() && !strings.HasSuffix(info.Name(), "list.json") { - - t.Run(info.Name(), func(t *testing.T) { - - defer func() { - require.NoError(t, operations.Teardown()) - }() - - // Load test vectors - log.Info("=====================================================================") - log.Info(path) - log.Info("=====================================================================") - testCase, err := vectors.LoadStateTransitionTestCaseV2(path) - require.NoError(t, err) - - opsCfg := operations.GetDefaultOperationsConfig() - opsCfg.State.MaxCumulativeGasUsed = 80000000000 - opsman, err := operations.NewManager(ctx, opsCfg) - require.NoError(t, err) - - // Setting Genesis - log.Info("###################") - log.Info("# Setting Genesis #") - log.Info("###################") - genesisActions := vectors.GenerateGenesisActions(testCase.Genesis) - require.NoError(t, opsman.SetGenesis(genesisConfig.Genesis.GenesisBlockNum, genesisActions)) - require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) - require.NoError(t, opsman.Setup()) - - // Check initial root - log.Info("################################") - log.Info("# Verifying initial state root #") - log.Info("################################") - actualOldStateRoot, err := opsman.State().GetLastStateRoot(ctx, nil) - require.NoError(t, err) - require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex()) - decodedData, err := hex.DecodeHex(testCase.BatchL2Data) - require.NoError(t, err) - _, txBytes, _, err := state.DecodeTxs(decodedData, forkID6) - require.NoError(t, err) - forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman) - require.NoError(t, err) - actualNewStateRoot := forcedBatch.StateRoot - isClosed, err := opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - - // wait until is closed - for !isClosed { - time.Sleep(1 * time.Second) - isClosed, err = opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - } - - log.Info("#######################") - log.Info("# Verifying new leafs #") - log.Info("#######################") - merkleTree := opsman.State().GetTree() - for _, expectedNewLeaf := range testCase.ExpectedNewLeafs { - if expectedNewLeaf.IsSmartContract { - log.Info("Smart Contract Address: ", expectedNewLeaf.Address) - } else { - log.Info("Account Address: ", expectedNewLeaf.Address) - } - log.Info("Verifying Balance...") - actualBalance, err := merkleTree.GetBalance(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Balance.String(), actualBalance.String()) - - log.Info("Verifying Nonce...") - actualNonce, err := merkleTree.GetNonce(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Nonce, actualNonce.String()) - if expectedNewLeaf.IsSmartContract { - log.Info("Verifying Storage...") - for positionHex, expectedNewStorageHex := range expectedNewLeaf.Storage { - position, ok := big.NewInt(0).SetString(positionHex[2:], 16) - require.True(t, ok) - expectedNewStorage, ok := big.NewInt(0).SetString(expectedNewStorageHex[2:], 16) - require.True(t, ok) - actualStorage, err := merkleTree.GetStorageAt(ctx, common.HexToAddress(expectedNewLeaf.Address), position, actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewStorage, actualStorage) - } - - log.Info("Verifying HashBytecode...") - actualHashByteCode, err := merkleTree.GetCodeHash(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.HashBytecode, common.BytesToHash(actualHashByteCode).String()) - } - } - return - }) - - return nil - } - return nil - }) - require.NoError(t, err) + LaunchTestForcedBatchesVectorFilesGroup(t, "./../vectors/src/state-transition/forced-tx/group1") } diff --git a/test/e2e/forced_batches_vector_group2_test.go b/test/e2e/forced_batches_vector_group2_test.go index 21070db52f..aeefa22d42 100644 --- a/test/e2e/forced_batches_vector_group2_test.go +++ b/test/e2e/forced_batches_vector_group2_test.go @@ -1,134 +1,12 @@ package e2e import ( - "context" - "math/big" - "os" - "path/filepath" - "strings" "testing" - "time" - - "github.com/0xPolygonHermez/zkevm-node/config" - "github.com/0xPolygonHermez/zkevm-node/hex" - "github.com/0xPolygonHermez/zkevm-node/log" - "github.com/0xPolygonHermez/zkevm-node/state" - "github.com/0xPolygonHermez/zkevm-node/test/operations" - "github.com/0xPolygonHermez/zkevm-node/test/vectors" - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" ) func TestForcedBatchesVectorFilesGroup2(t *testing.T) { - if testing.Short() { t.Skip() } - vectorFilesDir := "./../vectors/src/state-transition/forced-tx/group2" - ctx := context.Background() - genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") - require.NoError(t, err) - genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) - require.NoError(t, err) - err = filepath.Walk(vectorFilesDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() && !strings.HasSuffix(info.Name(), "list.json") { - - t.Run(info.Name(), func(t *testing.T) { - - defer func() { - require.NoError(t, operations.Teardown()) - }() - - // Load test vectors - log.Info("=====================================================================") - log.Info(path) - log.Info("=====================================================================") - testCase, err := vectors.LoadStateTransitionTestCaseV2(path) - require.NoError(t, err) - - opsCfg := operations.GetDefaultOperationsConfig() - opsCfg.State.MaxCumulativeGasUsed = 80000000000 - opsman, err := operations.NewManager(ctx, opsCfg) - require.NoError(t, err) - - // Setting Genesis - log.Info("###################") - log.Info("# Setting Genesis #") - log.Info("###################") - genesisActions := vectors.GenerateGenesisActions(testCase.Genesis) - require.NoError(t, opsman.SetGenesis(genesisConfig.Genesis.GenesisBlockNum, genesisActions)) - require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) - require.NoError(t, opsman.Setup()) - - // Check initial root - log.Info("################################") - log.Info("# Verifying initial state root #") - log.Info("################################") - actualOldStateRoot, err := opsman.State().GetLastStateRoot(ctx, nil) - require.NoError(t, err) - require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex()) - decodedData, err := hex.DecodeHex(testCase.BatchL2Data) - require.NoError(t, err) - _, txBytes, _, err := state.DecodeTxs(decodedData, forkID6) - forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman) - require.NoError(t, err) - actualNewStateRoot := forcedBatch.StateRoot - isClosed, err := opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - - // wait until is closed - for !isClosed { - time.Sleep(1 * time.Second) - isClosed, err = opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - } - - log.Info("#######################") - log.Info("# Verifying new leafs #") - log.Info("#######################") - merkleTree := opsman.State().GetTree() - for _, expectedNewLeaf := range testCase.ExpectedNewLeafs { - if expectedNewLeaf.IsSmartContract { - log.Info("Smart Contract Address: ", expectedNewLeaf.Address) - } else { - log.Info("Account Address: ", expectedNewLeaf.Address) - } - log.Info("Verifying Balance...") - actualBalance, err := merkleTree.GetBalance(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Balance.String(), actualBalance.String()) - - log.Info("Verifying Nonce...") - actualNonce, err := merkleTree.GetNonce(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Nonce, actualNonce.String()) - if expectedNewLeaf.IsSmartContract { - log.Info("Verifying Storage...") - for positionHex, expectedNewStorageHex := range expectedNewLeaf.Storage { - position, ok := big.NewInt(0).SetString(positionHex[2:], 16) - require.True(t, ok) - expectedNewStorage, ok := big.NewInt(0).SetString(expectedNewStorageHex[2:], 16) - require.True(t, ok) - actualStorage, err := merkleTree.GetStorageAt(ctx, common.HexToAddress(expectedNewLeaf.Address), position, actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewStorage, actualStorage) - } - - log.Info("Verifying HashBytecode...") - actualHashByteCode, err := merkleTree.GetCodeHash(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.HashBytecode, common.BytesToHash(actualHashByteCode).String()) - } - } - return - }) - - return nil - } - return nil - }) - require.NoError(t, err) + LaunchTestForcedBatchesVectorFilesGroup(t, "./../vectors/src/state-transition/forced-tx/group2") } diff --git a/test/e2e/forced_batches_vector_group3_test.go b/test/e2e/forced_batches_vector_group3_test.go index ec01446114..f5d2815bfd 100644 --- a/test/e2e/forced_batches_vector_group3_test.go +++ b/test/e2e/forced_batches_vector_group3_test.go @@ -1,134 +1,12 @@ package e2e import ( - "context" - "math/big" - "os" - "path/filepath" - "strings" "testing" - "time" - - "github.com/0xPolygonHermez/zkevm-node/config" - "github.com/0xPolygonHermez/zkevm-node/hex" - "github.com/0xPolygonHermez/zkevm-node/log" - "github.com/0xPolygonHermez/zkevm-node/state" - "github.com/0xPolygonHermez/zkevm-node/test/operations" - "github.com/0xPolygonHermez/zkevm-node/test/vectors" - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" ) func TestForcedBatchesVectorFilesGroup3(t *testing.T) { - if testing.Short() { t.Skip() } - vectorFilesDir := "./../vectors/src/state-transition/forced-tx/group3" - ctx := context.Background() - genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") - require.NoError(t, err) - genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) - require.NoError(t, err) - err = filepath.Walk(vectorFilesDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() && !strings.HasSuffix(info.Name(), "list.json") { - - t.Run(info.Name(), func(t *testing.T) { - - defer func() { - require.NoError(t, operations.Teardown()) - }() - - // Load test vectors - log.Info("=====================================================================") - log.Info(path) - log.Info("=====================================================================") - testCase, err := vectors.LoadStateTransitionTestCaseV2(path) - require.NoError(t, err) - - opsCfg := operations.GetDefaultOperationsConfig() - opsCfg.State.MaxCumulativeGasUsed = 80000000000 - opsman, err := operations.NewManager(ctx, opsCfg) - require.NoError(t, err) - - // Setting Genesis - log.Info("###################") - log.Info("# Setting Genesis #") - log.Info("###################") - genesisActions := vectors.GenerateGenesisActions(testCase.Genesis) - require.NoError(t, opsman.SetGenesis(genesisConfig.Genesis.GenesisBlockNum, genesisActions)) - require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) - require.NoError(t, opsman.Setup()) - - // Check initial root - log.Info("################################") - log.Info("# Verifying initial state root #") - log.Info("################################") - actualOldStateRoot, err := opsman.State().GetLastStateRoot(ctx, nil) - require.NoError(t, err) - require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex()) - decodedData, err := hex.DecodeHex(testCase.BatchL2Data) - require.NoError(t, err) - _, txBytes, _, err := state.DecodeTxs(decodedData, forkID6) - forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman) - require.NoError(t, err) - actualNewStateRoot := forcedBatch.StateRoot - isClosed, err := opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - - // wait until is closed - for !isClosed { - time.Sleep(1 * time.Second) - isClosed, err = opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) - require.NoError(t, err) - } - - log.Info("#######################") - log.Info("# Verifying new leafs #") - log.Info("#######################") - merkleTree := opsman.State().GetTree() - for _, expectedNewLeaf := range testCase.ExpectedNewLeafs { - if expectedNewLeaf.IsSmartContract { - log.Info("Smart Contract Address: ", expectedNewLeaf.Address) - } else { - log.Info("Account Address: ", expectedNewLeaf.Address) - } - log.Info("Verifying Balance...") - actualBalance, err := merkleTree.GetBalance(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Balance.String(), actualBalance.String()) - - log.Info("Verifying Nonce...") - actualNonce, err := merkleTree.GetNonce(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.Nonce, actualNonce.String()) - if expectedNewLeaf.IsSmartContract { - log.Info("Verifying Storage...") - for positionHex, expectedNewStorageHex := range expectedNewLeaf.Storage { - position, ok := big.NewInt(0).SetString(positionHex[2:], 16) - require.True(t, ok) - expectedNewStorage, ok := big.NewInt(0).SetString(expectedNewStorageHex[2:], 16) - require.True(t, ok) - actualStorage, err := merkleTree.GetStorageAt(ctx, common.HexToAddress(expectedNewLeaf.Address), position, actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewStorage, actualStorage) - } - - log.Info("Verifying HashBytecode...") - actualHashByteCode, err := merkleTree.GetCodeHash(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) - require.NoError(t, err) - require.Equal(t, expectedNewLeaf.HashBytecode, common.BytesToHash(actualHashByteCode).String()) - } - } - return - }) - - return nil - } - return nil - }) - require.NoError(t, err) + LaunchTestForcedBatchesVectorFilesGroup(t, "./../vectors/src/state-transition/forced-tx/group3") } diff --git a/test/e2e/forced_batches_vector_shared.go b/test/e2e/forced_batches_vector_shared.go new file mode 100644 index 0000000000..1390d2f6c3 --- /dev/null +++ b/test/e2e/forced_batches_vector_shared.go @@ -0,0 +1,132 @@ +package e2e + +import ( + "context" + "math/big" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/0xPolygonHermez/zkevm-node/config" + "github.com/0xPolygonHermez/zkevm-node/hex" + "github.com/0xPolygonHermez/zkevm-node/log" + "github.com/0xPolygonHermez/zkevm-node/state" + "github.com/0xPolygonHermez/zkevm-node/test/operations" + "github.com/0xPolygonHermez/zkevm-node/test/vectors" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" +) + +func LaunchTestForcedBatchesVectorFilesGroup(t *testing.T, vectorFilesDir string) { + + //vectorFilesDir := "./../vectors/src/state-transition/forced-tx/group1" + ctx := context.Background() + genesisFileAsStr, err := config.LoadGenesisFileAsString("../../test/config/test.genesis.config.json") + require.NoError(t, err) + genesisConfig, err := config.LoadGenesisFromJSONString(genesisFileAsStr) + require.NoError(t, err) + err = filepath.Walk(vectorFilesDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && !strings.HasSuffix(info.Name(), "list.json") { + + t.Run(info.Name(), func(t *testing.T) { + + defer func() { + require.NoError(t, operations.Teardown()) + }() + + // Load test vectors + log.Info("=====================================================================") + log.Info(path) + log.Info("=====================================================================") + testCase, err := vectors.LoadStateTransitionTestCaseV2(path) + require.NoError(t, err) + + opsCfg := operations.GetDefaultOperationsConfig() + opsCfg.State.MaxCumulativeGasUsed = 80000000000 + opsman, err := operations.NewManager(ctx, opsCfg) + require.NoError(t, err) + + // Setting Genesis + log.Info("###################") + log.Info("# Setting Genesis #") + log.Info("###################") + genesisActions := vectors.GenerateGenesisActions(testCase.Genesis) + require.NoError(t, opsman.SetGenesis(genesisConfig.Genesis.GenesisBlockNum, genesisActions)) + require.NoError(t, opsman.SetForkID(genesisConfig.Genesis.GenesisBlockNum, forkID6)) + require.NoError(t, opsman.Setup()) + + // Check initial root + log.Info("################################") + log.Info("# Verifying initial state root #") + log.Info("################################") + actualOldStateRoot, err := opsman.State().GetLastStateRoot(ctx, nil) + require.NoError(t, err) + require.Equal(t, testCase.ExpectedOldStateRoot, actualOldStateRoot.Hex()) + decodedData, err := hex.DecodeHex(testCase.BatchL2Data) + require.NoError(t, err) + _, txBytes, _, err := state.DecodeTxs(decodedData, forkID6) + require.NoError(t, err) + forcedBatch, err := sendForcedBatchForVector(t, txBytes, opsman) + require.NoError(t, err) + actualNewStateRoot := forcedBatch.StateRoot + isClosed, err := opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) + require.NoError(t, err) + + // wait until is closed + for !isClosed { + time.Sleep(1 * time.Second) + isClosed, err = opsman.State().IsBatchClosed(ctx, forcedBatch.BatchNumber, nil) + require.NoError(t, err) + } + + log.Info("#######################") + log.Info("# Verifying new leafs #") + log.Info("#######################") + merkleTree := opsman.State().GetTree() + for _, expectedNewLeaf := range testCase.ExpectedNewLeafs { + if expectedNewLeaf.IsSmartContract { + log.Info("Smart Contract Address: ", expectedNewLeaf.Address) + } else { + log.Info("Account Address: ", expectedNewLeaf.Address) + } + log.Info("Verifying Balance...") + actualBalance, err := merkleTree.GetBalance(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedNewLeaf.Balance.String(), actualBalance.String()) + + log.Info("Verifying Nonce...") + actualNonce, err := merkleTree.GetNonce(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedNewLeaf.Nonce, actualNonce.String()) + if expectedNewLeaf.IsSmartContract { + log.Info("Verifying Storage...") + for positionHex, expectedNewStorageHex := range expectedNewLeaf.Storage { + position, ok := big.NewInt(0).SetString(positionHex[2:], 16) + require.True(t, ok) + expectedNewStorage, ok := big.NewInt(0).SetString(expectedNewStorageHex[2:], 16) + require.True(t, ok) + actualStorage, err := merkleTree.GetStorageAt(ctx, common.HexToAddress(expectedNewLeaf.Address), position, actualNewStateRoot.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedNewStorage, actualStorage) + } + + log.Info("Verifying HashBytecode...") + actualHashByteCode, err := merkleTree.GetCodeHash(ctx, common.HexToAddress(expectedNewLeaf.Address), actualNewStateRoot.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedNewLeaf.HashBytecode, common.BytesToHash(actualHashByteCode).String()) + } + } + return + }) + + return nil + } + return nil + }) + require.NoError(t, err) +} diff --git a/test/e2e/shared.go b/test/e2e/shared.go index 31c04cdcb3..5ebbe14e4c 100644 --- a/test/e2e/shared.go +++ b/test/e2e/shared.go @@ -25,7 +25,6 @@ import ( const ( invalidParamsErrorCode = -32602 toAddressHex = "0x4d5Cf5032B2a844602278b01199ED191A86c93ff" - gerFinalityBlocks = uint64(250) forkID6 = 6 ) diff --git a/test/operations/manager.go b/test/operations/manager.go index 6719eaa0ef..aa95ffcf05 100644 --- a/test/operations/manager.go +++ b/test/operations/manager.go @@ -33,14 +33,17 @@ const ( // Public shared const ( - DefaultSequencerAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" - DefaultSequencerPrivateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" - DefaultSequencerBalance = 400000 - DefaultMaxCumulativeGasUsed = 800000 - DefaultL1ZkEVMSmartContract = "0x610178dA211FEF7D417bC0e6FeD39F05609AD788" - DefaultL1NetworkURL = "http://localhost:8545" - DefaultL1NetworkWebSocketURL = "ws://localhost:8546" - DefaultL1ChainID uint64 = 1337 + DefaultSequencerAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + DefaultSequencerPrivateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + DefaultForcedBatchesAddress = "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC" + DefaultForcedBatchesPrivateKey = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a" + DefaultSequencerBalance = 400000 + DefaultMaxCumulativeGasUsed = 800000 + DefaultL1ZkEVMSmartContract = "0x610178dA211FEF7D417bC0e6FeD39F05609AD788" + DefaultL1MaticSmartContract = "0x5FbDB2315678afecb367f032d93F642f64180aa3" + DefaultL1NetworkURL = "http://localhost:8545" + DefaultL1NetworkWebSocketURL = "ws://localhost:8546" + DefaultL1ChainID uint64 = 1337 DefaultL2NetworkURL = "http://localhost:8123" PermissionlessL2NetworkURL = "http://localhost:8125" @@ -94,7 +97,10 @@ type Manager struct { func NewManager(ctx context.Context, cfg *Config) (*Manager, error) { // Init database instance initOrResetDB() + return NewManagerNoInitDB(ctx, cfg) +} +func NewManagerNoInitDB(ctx context.Context, cfg *Config) (*Manager, error) { opsman := &Manager{ cfg: cfg, ctx: ctx,