From 6c9e20bd5998029e168efab79ed327b98540788c Mon Sep 17 00:00:00 2001 From: curryxbo Date: Mon, 30 Dec 2024 15:26:44 +0800 Subject: [PATCH 1/5] refactor parse batch --- node/db/store.go | 22 ---------------------- node/derivation/batch_info.go | 27 +++++++++++++++++++-------- node/derivation/database.go | 2 -- node/derivation/derivation.go | 13 ++++--------- 4 files changed, 23 insertions(+), 41 deletions(-) diff --git a/node/db/store.go b/node/db/store.go index 9699f0f0d..1a87a227c 100644 --- a/node/db/store.go +++ b/node/db/store.go @@ -123,34 +123,12 @@ func (s *Store) ReadL1MessageByIndex(index uint64) *types.L1Message { return &l1Msg } -func (s *Store) ReadBlockNumberByIndex(batchIndex uint64) *uint64 { - data, err := s.db.Get(BatchBlockNumberKey(batchIndex)) - if err != nil && !isNotFoundErr(err) { - panic(fmt.Sprintf("failed to read batch block number from database, err: %v", err)) - } - if len(data) == 0 { - return nil - } - number := new(big.Int).SetBytes(data) - if !number.IsUint64() { - panic(fmt.Sprintf("unexpected block number in database, number: %d", number)) - } - value := number.Uint64() - return &value -} - func (s *Store) WriteLatestDerivationL1Height(latest uint64) { if err := s.db.Put(derivationL1HeightKey, new(big.Int).SetUint64(latest).Bytes()); err != nil { panic(fmt.Sprintf("failed to update derivation synced L1 height, err: %v", err)) } } -func (s *Store) WriteBatchBlockNumber(batchIndex, blockNumber uint64) { - if err := s.db.Put(BatchBlockNumberKey(batchIndex), new(big.Int).SetUint64(blockNumber).Bytes()); err != nil { - panic(fmt.Sprintf("failed to store last block number of batch, err: %v", err)) - } -} - func (s *Store) WriteLatestSyncedL1Height(latest uint64) { if err := s.db.Put(syncedL1HeightKey, new(big.Int).SetUint64(latest).Bytes()); err != nil { panic(fmt.Sprintf("failed to update synced L1 height, err: %v", err)) diff --git a/node/derivation/batch_info.go b/node/derivation/batch_info.go index 81972c74e..190959785 100644 --- a/node/derivation/batch_info.go +++ b/node/derivation/batch_info.go @@ -57,9 +57,10 @@ type BatchInfo struct { lastBlockNumber uint64 firstBlockNumber uint64 - root common.Hash - withdrawalRoot common.Hash - skippedL1MessageBitmap *big.Int + root common.Hash + withdrawalRoot common.Hash + skippedL1MessageBitmap *big.Int + parentTotalL1MessagePopped uint64 } func (bi *BatchInfo) FirstBlockNumber() uint64 { @@ -79,20 +80,30 @@ func (bi *BatchInfo) TxNum() uint64 { } // ParseBatch This method is externally referenced for parsing Batch -func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch, parentBatchBlock *uint64) error { +func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error { + parentBatchHeader, err := types.DecodeBatchHeader(batch.ParentBatchHeader) + if err != nil { + return fmt.Errorf("decode parent batch header error:%v", err) + } + bi.parentTotalL1MessagePopped = parentBatchHeader.TotalL1MessagePopped bi.root = batch.PostStateRoot + bi.batchIndex = parentBatchHeader.BatchIndex + 1 bi.withdrawalRoot = batch.WithdrawRoot bi.version = uint64(batch.Version) tq := newTxQueue() var rawBlockContexts hexutil.Bytes var txsData []byte var blockCount uint64 + // TODO parentBatchBlock = parentBatchHeader.LastBlock + var parentBatchBlock uint64 if batch.BlockContexts == nil { - if parentBatchBlock == nil { - return fmt.Errorf("block number of parent batch not found") - } - blockCount = batch.LastBlockNumber - *parentBatchBlock + //if parentBatchBlock == nil { + // return fmt.Errorf("block number of parent batch not found") + //} + blockCount = batch.LastBlockNumber - parentBatchBlock } + // If BlockContexts is not nil, the block context should not be included in the blob. + // Therefore, the required length must be zero. length := blockCount * 60 for _, blob := range batch.Sidecar.Blobs { blobCopy := blob diff --git a/node/derivation/database.go b/node/derivation/database.go index 078fe1c3b..a63f4eba1 100644 --- a/node/derivation/database.go +++ b/node/derivation/database.go @@ -12,10 +12,8 @@ type Database interface { type Reader interface { ReadLatestDerivationL1Height() *uint64 - ReadBlockNumberByIndex(batchIndex uint64) *uint64 } type Writer interface { WriteLatestDerivationL1Height(latest uint64) - WriteBatchBlockNumber(batchIndex, blockNumber uint64) } diff --git a/node/derivation/derivation.go b/node/derivation/derivation.go index 6c93edf9f..2e6762bc9 100644 --- a/node/derivation/derivation.go +++ b/node/derivation/derivation.go @@ -408,20 +408,15 @@ func (d *Derivation) UnPackData(data []byte) (geth.RPCRollupBatch, error) { } func (d *Derivation) parseBatch(batch geth.RPCRollupBatch, l2Height uint64) (*BatchInfo, error) { - parentBatchHeader, err := types.DecodeBatchHeader(batch.ParentBatchHeader) - if err != nil { - return nil, fmt.Errorf("decode batch header error:%v", err) - } + batchInfo := new(BatchInfo) - parentBatchBlock := d.db.ReadBlockNumberByIndex(parentBatchHeader.BatchIndex) - if err := batchInfo.ParseBatch(batch, parentBatchBlock); err != nil { + if err := batchInfo.ParseBatch(batch); err != nil { return nil, fmt.Errorf("parse batch error:%v", err) } - d.db.WriteBatchBlockNumber(batchInfo.batchIndex, batchInfo.lastBlockNumber) - if err := d.handleL1Message(batchInfo, parentBatchHeader.TotalL1MessagePopped, l2Height); err != nil { + if err := d.handleL1Message(batchInfo, batchInfo.parentTotalL1MessagePopped, l2Height); err != nil { return nil, fmt.Errorf("handle l1 message error:%v", err) } - batchInfo.batchIndex = parentBatchHeader.BatchIndex + 1 + return batchInfo, nil } From c50ae7615114b558396e1d1c02adb844bb2ab405 Mon Sep 17 00:00:00 2001 From: curryxbo Date: Mon, 30 Dec 2024 15:27:00 +0800 Subject: [PATCH 2/5] refactor parse batch --- ops/tools/batchparse/main.go | 3 ++- oracle/oracle/batch.go | 3 +-- oracle/oracle/reward.go | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ops/tools/batchparse/main.go b/ops/tools/batchparse/main.go index 3b1b9a441..429756a1d 100644 --- a/ops/tools/batchparse/main.go +++ b/ops/tools/batchparse/main.go @@ -3,9 +3,10 @@ package main import ( "context" "fmt" - "morph-l2/node/derivation" "github.com/morph-l2/go-ethereum/ethclient" + + "morph-l2/node/derivation" ) var ( diff --git a/oracle/oracle/batch.go b/oracle/oracle/batch.go index be4d22023..7c24fae35 100644 --- a/oracle/oracle/batch.go +++ b/oracle/oracle/batch.go @@ -180,8 +180,7 @@ func (o *Oracle) getBatchSubmissionByLogs(rLogs []types.Log, recordBatchSubmissi // set batchIndex to new batch index + 1 batchIndex = rollupCommitBatch.BatchIndex.Uint64() + 1 var batchData derivation.BatchInfo - // TODO - if err = batchData.ParseBatch(batch, new(uint64)); err != nil { + if err = batchData.ParseBatch(batch); err != nil { return fmt.Errorf("parse batch error:%v", err) } log.Info("received new batch", "batch_index", rollupCommitBatch.BatchIndex.Uint64()) diff --git a/oracle/oracle/reward.go b/oracle/oracle/reward.go index cd0d47dd0..f67ad451e 100644 --- a/oracle/oracle/reward.go +++ b/oracle/oracle/reward.go @@ -37,8 +37,7 @@ func (o *Oracle) getBlockTimeAndNumber(isFinalized bool) (uint64, *big.Int, erro return 0, nil, fmt.Errorf("batch not found") } var batchData derivation.BatchInfo - // TODO - if err = batchData.ParseBatch(*batch, new(uint64)); err != nil { + if err = batchData.ParseBatch(*batch); err != nil { return 0, nil, fmt.Errorf("parse batch error:%v", err) } lastBlockNumber = big.NewInt(int64(batchData.LastBlockNumber())) From 43dfbc76789c5e5ce23b9413b37d43e2a3673903 Mon Sep 17 00:00:00 2001 From: curryxbo Date: Mon, 30 Dec 2024 15:37:09 +0800 Subject: [PATCH 3/5] clean skipmap --- node/derivation/batch_info.go | 1 - node/derivation/derivation.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/node/derivation/batch_info.go b/node/derivation/batch_info.go index 190959785..7097c7bfe 100644 --- a/node/derivation/batch_info.go +++ b/node/derivation/batch_info.go @@ -59,7 +59,6 @@ type BatchInfo struct { root common.Hash withdrawalRoot common.Hash - skippedL1MessageBitmap *big.Int parentTotalL1MessagePopped uint64 } diff --git a/node/derivation/derivation.go b/node/derivation/derivation.go index 2e6762bc9..38ba2f83e 100644 --- a/node/derivation/derivation.go +++ b/node/derivation/derivation.go @@ -438,9 +438,6 @@ func (d *Derivation) handleL1Message(rollupData *BatchInfo, parentTotalL1Message totalL1MessagePopped += uint64(block.l1MsgNum) if len(l1Messages) > 0 { for _, l1Message := range l1Messages { - if rollupData.skippedL1MessageBitmap != nil && rollupData.skippedL1MessageBitmap.Bit(int(l1Message.QueueIndex)-int(parentTotalL1MessagePopped)) == 1 { - continue - } transaction := eth.NewTx(&l1Message.L1MessageTx) l1Transactions = append(l1Transactions, transaction) } From f8c4e0559f406a988716abea227b0e43f12375dd Mon Sep 17 00:00:00 2001 From: curryxbo Date: Mon, 6 Jan 2025 10:37:46 +0800 Subject: [PATCH 4/5] fix --- node/derivation/batch_info.go | 26 +++++++++++++++----------- node/derivation/derivation.go | 1 - node/derivation/derivation_test.go | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/node/derivation/batch_info.go b/node/derivation/batch_info.go index 7097c7bfe..9da2ae621 100644 --- a/node/derivation/batch_info.go +++ b/node/derivation/batch_info.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "math/big" - "morph-l2/node/zstd" "github.com/morph-l2/go-ethereum/common" "github.com/morph-l2/go-ethereum/common/hexutil" @@ -15,6 +14,7 @@ import ( "github.com/morph-l2/go-ethereum/eth/catalyst" "morph-l2/node/types" + "morph-l2/node/zstd" ) type BlockContext struct { @@ -80,25 +80,30 @@ func (bi *BatchInfo) TxNum() uint64 { // ParseBatch This method is externally referenced for parsing Batch func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error { - parentBatchHeader, err := types.DecodeBatchHeader(batch.ParentBatchHeader) + parentBatchHeader := types.BatchHeaderBytes(batch.ParentBatchHeader) + parentBatchIndex, err := parentBatchHeader.BatchIndex() if err != nil { - return fmt.Errorf("decode parent batch header error:%v", err) + return fmt.Errorf("decode batch header index error:%v", err) } - bi.parentTotalL1MessagePopped = parentBatchHeader.TotalL1MessagePopped + totalL1MessagePopped, err := parentBatchHeader.TotalL1MessagePopped() + if err != nil { + return fmt.Errorf("decode batch header totalL1MessagePopped error:%v", err) + } + bi.parentTotalL1MessagePopped = totalL1MessagePopped bi.root = batch.PostStateRoot - bi.batchIndex = parentBatchHeader.BatchIndex + 1 + bi.batchIndex = parentBatchIndex + 1 bi.withdrawalRoot = batch.WithdrawRoot bi.version = uint64(batch.Version) tq := newTxQueue() var rawBlockContexts hexutil.Bytes var txsData []byte var blockCount uint64 - // TODO parentBatchBlock = parentBatchHeader.LastBlock - var parentBatchBlock uint64 if batch.BlockContexts == nil { - //if parentBatchBlock == nil { - // return fmt.Errorf("block number of parent batch not found") - //} + parentBatchBlock, err := parentBatchHeader.LastBlockNumber() + if err != nil { + return fmt.Errorf("decode batch header lastBlockNumber error:%v", err) + } + blockCount = batch.LastBlockNumber - parentBatchBlock } // If BlockContexts is not nil, the block context should not be included in the blob. @@ -192,7 +197,6 @@ func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error { } bi.txNum += txsNum bi.blockContexts = blockContexts - return nil } diff --git a/node/derivation/derivation.go b/node/derivation/derivation.go index 62a3354dc..458ee4ddc 100644 --- a/node/derivation/derivation.go +++ b/node/derivation/derivation.go @@ -415,7 +415,6 @@ func (d *Derivation) parseBatch(batch geth.RPCRollupBatch, l2Height uint64) (*Ba if err := d.handleL1Message(batchInfo, batchInfo.parentTotalL1MessagePopped, l2Height); err != nil { return nil, fmt.Errorf("handle l1 message error:%v", err) } - return batchInfo, nil } diff --git a/node/derivation/derivation_test.go b/node/derivation/derivation_test.go index 9f05cafba..653ce96ce 100644 --- a/node/derivation/derivation_test.go +++ b/node/derivation/derivation_test.go @@ -37,13 +37,13 @@ func TestUnPackData(t *testing.T) { legacyBatch, err := d.UnPackData(legacyTxData) require.NoError(t, err) LegacyBatchInfo := new(BatchInfo) - err = LegacyBatchInfo.ParseBatch(legacyBatch, new(uint64)) + err = LegacyBatchInfo.ParseBatch(legacyBatch) require.NoError(t, err) beforeMoveBctxTxData, err := hexutil.Decode(beforeMoveBctxData) require.NoError(t, err) beforeMoveBctxBatch, err := d.UnPackData(beforeMoveBctxTxData) require.NoError(t, err) beforeMoveBctxBatchInfo := new(BatchInfo) - err = beforeMoveBctxBatchInfo.ParseBatch(beforeMoveBctxBatch, new(uint64)) + err = beforeMoveBctxBatchInfo.ParseBatch(beforeMoveBctxBatch) require.NoError(t, err) } From 2e6ce854a42b4d2ee4a766d37383e5449ad9dd7b Mon Sep 17 00:00:00 2001 From: curryxbo Date: Mon, 6 Jan 2025 10:50:29 +0800 Subject: [PATCH 5/5] clean --- node/derivation/batch_info.go | 4 ---- node/derivation/derivation.go | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/node/derivation/batch_info.go b/node/derivation/batch_info.go index 9da2ae621..3393ad079 100644 --- a/node/derivation/batch_info.go +++ b/node/derivation/batch_info.go @@ -111,10 +111,6 @@ func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error { length := blockCount * 60 for _, blob := range batch.Sidecar.Blobs { blobCopy := blob - // TODO - //if isEmptyBlob(blob) { - // return eth.Transactions{}, nil - //} blobData, err := types.RetrieveBlobBytes(&blobCopy) if err != nil { return err diff --git a/node/derivation/derivation.go b/node/derivation/derivation.go index 458ee4ddc..a3fc43fbe 100644 --- a/node/derivation/derivation.go +++ b/node/derivation/derivation.go @@ -421,7 +421,7 @@ func (d *Derivation) parseBatch(batch geth.RPCRollupBatch, l2Height uint64) (*Ba func (d *Derivation) handleL1Message(rollupData *BatchInfo, parentTotalL1MessagePopped, l2Height uint64) error { totalL1MessagePopped := parentTotalL1MessagePopped for bIndex, block := range rollupData.blockContexts { - // This may happen to nodes started from sanpshot, in which case we will no longer handle L1Msg + // This may happen to nodes started from snapshot, in which case we will no longer handle L1Msg if block.Number <= l2Height { continue }