From 826280253abb9eb4acbaf73526b694bdc16b3077 Mon Sep 17 00:00:00 2001 From: maskpp Date: Fri, 31 Mar 2023 13:58:46 +0800 Subject: [PATCH] fix(test): fix bug in `testBatchProposerProposeBatch` (#399) Co-authored-by: colinlyguo <651734127@qq.com> --- bridge/relayer/l2_relayer_test.go | 12 ++++++++++++ bridge/watcher/batch_proposer_test.go | 26 +++++++++++++++++++++++++- bridge/watcher/watcher_test.go | 20 -------------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/bridge/relayer/l2_relayer_test.go b/bridge/relayer/l2_relayer_test.go index 1d8df0cb1..1ff220b40 100644 --- a/bridge/relayer/l2_relayer_test.go +++ b/bridge/relayer/l2_relayer_test.go @@ -78,6 +78,12 @@ func testL2RelayerProcessSaveEvents(t *testing.T) { } assert.NoError(t, db.InsertWrappedBlocks(traces)) + parentBatch1 := &types.BlockBatch{ + Index: 0, + Hash: common.Hash{}.String(), + StateRoot: common.Hash{}.String(), + } + batchData1 := types.NewBatchData(parentBatch1, []*types.WrappedBlock{wrappedBlock1}, nil) dbTx, err := db.Beginx() assert.NoError(t, err) assert.NoError(t, db.NewBatchInDBTx(dbTx, batchData1)) @@ -106,6 +112,12 @@ func testL2RelayerProcessCommittedBatches(t *testing.T) { relayer, err := relayer.NewLayer2Relayer(context.Background(), l2Cli, db, l2Cfg.RelayerConfig) assert.NoError(t, err) + parentBatch1 := &types.BlockBatch{ + Index: 0, + Hash: common.Hash{}.String(), + StateRoot: common.Hash{}.String(), + } + batchData1 := types.NewBatchData(parentBatch1, []*types.WrappedBlock{wrappedBlock1}, nil) dbTx, err := db.Beginx() assert.NoError(t, err) assert.NoError(t, db.NewBatchInDBTx(dbTx, batchData1)) diff --git a/bridge/watcher/batch_proposer_test.go b/bridge/watcher/batch_proposer_test.go index 9f9007505..3b7471dae 100644 --- a/bridge/watcher/batch_proposer_test.go +++ b/bridge/watcher/batch_proposer_test.go @@ -6,6 +6,7 @@ import ( "math" "testing" + "github.com/scroll-tech/go-ethereum/common" "github.com/stretchr/testify/assert" "scroll-tech/database" @@ -38,6 +39,16 @@ func testBatchProposerProposeBatch(t *testing.T) { wc := watcher.NewL2WatcherClient(context.Background(), l2Cli, l2cfg.Confirmations, l2cfg.L2MessengerAddress, l2cfg.L2MessageQueueAddress, l2cfg.WithdrawTrieRootSlot, db) loopToFetchEvent(subCtx, wc) + batch, err := db.GetLatestBatch() + assert.NoError(t, err) + + // Create a new batch. + batchData := types.NewBatchData(&types.BlockBatch{ + Index: 0, + Hash: batch.Hash, + StateRoot: batch.StateRoot, + }, []*types.WrappedBlock{wrappedBlock1}, nil) + relayer, err := relayer.NewLayer2Relayer(context.Background(), l2Cli, db, cfg.L2Config.RelayerConfig) assert.NoError(t, err) @@ -55,7 +66,7 @@ func testBatchProposerProposeBatch(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 0, len(infos)) - exist, err := db.BatchRecordExist(batchData1.Hash().Hex()) + exist, err := db.BatchRecordExist(batchData.Hash().Hex()) assert.NoError(t, err) assert.Equal(t, true, exist) } @@ -74,6 +85,19 @@ func testBatchProposerGracefulRestart(t *testing.T) { assert.NoError(t, db.InsertWrappedBlocks([]*types.WrappedBlock{wrappedBlock2})) // Insert block batch into db. + batchData1 := types.NewBatchData(&types.BlockBatch{ + Index: 0, + Hash: common.Hash{}.String(), + StateRoot: common.Hash{}.String(), + }, []*types.WrappedBlock{wrappedBlock1}, nil) + + parentBatch2 := &types.BlockBatch{ + Index: batchData1.Batch.BatchIndex, + Hash: batchData1.Hash().Hex(), + StateRoot: batchData1.Batch.NewStateRoot.String(), + } + batchData2 := types.NewBatchData(parentBatch2, []*types.WrappedBlock{wrappedBlock2}, nil) + dbTx, err := db.Beginx() assert.NoError(t, err) assert.NoError(t, db.NewBatchInDBTx(dbTx, batchData1)) diff --git a/bridge/watcher/watcher_test.go b/bridge/watcher/watcher_test.go index 8f19ba5ab..88460ea8a 100644 --- a/bridge/watcher/watcher_test.go +++ b/bridge/watcher/watcher_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/scroll-tech/go-ethereum/ethclient" - "github.com/scroll-tech/go-ethereum/log" "github.com/stretchr/testify/assert" "scroll-tech/common/docker" @@ -27,10 +26,6 @@ var ( // block trace wrappedBlock1 *types.WrappedBlock wrappedBlock2 *types.WrappedBlock - - // batch data - batchData1 *types.BatchData - batchData2 *types.BatchData ) func setupEnv(t *testing.T) (err error) { @@ -57,12 +52,6 @@ func setupEnv(t *testing.T) (err error) { if err = json.Unmarshal(templateBlockTrace1, wrappedBlock1); err != nil { return err } - parentBatch1 := &types.BlockBatch{ - Index: 0, - Hash: "0x0cc6b102c2924402c14b2e3a19baccc316252bfdc44d9ec62e942d34e39ec729", - StateRoot: "0x2579122e8f9ec1e862e7d415cef2fb495d7698a8e5f0dddc5651ba4236336e7d", - } - batchData1 = types.NewBatchData(parentBatch1, []*types.WrappedBlock{wrappedBlock1}, nil) templateBlockTrace2, err := os.ReadFile("../../common/testdata/blockTrace_03.json") if err != nil { @@ -73,15 +62,6 @@ func setupEnv(t *testing.T) (err error) { if err = json.Unmarshal(templateBlockTrace2, wrappedBlock2); err != nil { return err } - parentBatch2 := &types.BlockBatch{ - Index: batchData1.Batch.BatchIndex, - Hash: batchData1.Hash().Hex(), - StateRoot: batchData1.Batch.NewStateRoot.String(), - } - batchData2 = types.NewBatchData(parentBatch2, []*types.WrappedBlock{wrappedBlock2}, nil) - - log.Info("batchHash", "batchhash1", batchData1.Hash().Hex(), "batchhash2", batchData2.Hash().Hex()) - return err }