Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parse batch #692

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions node/db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 23 additions & 13 deletions node/derivation/batch_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/morph-l2/go-ethereum/eth/catalyst"

"morph-l2/node/types"
"morph-l2/node/zstd"
)

type BlockContext struct {
Expand Down Expand Up @@ -57,9 +57,9 @@ type BatchInfo struct {
lastBlockNumber uint64
firstBlockNumber uint64

root common.Hash
withdrawalRoot common.Hash
skippedL1MessageBitmap *big.Int
root common.Hash
withdrawalRoot common.Hash
parentTotalL1MessagePopped uint64
}

func (bi *BatchInfo) FirstBlockNumber() uint64 {
Expand All @@ -79,27 +79,38 @@ 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 := types.BatchHeaderBytes(batch.ParentBatchHeader)
parentBatchIndex, err := parentBatchHeader.BatchIndex()
if err != nil {
return fmt.Errorf("decode batch header index error:%v", err)
}
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 = parentBatchIndex + 1
bi.withdrawalRoot = batch.WithdrawRoot
bi.version = uint64(batch.Version)
tq := newTxQueue()
var rawBlockContexts hexutil.Bytes
var txsData []byte
var blockCount 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

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
// TODO
//if isEmptyBlob(blob) {
// return eth.Transactions{}, nil
//}
blobData, err := types.RetrieveBlobBytes(&blobCopy)
if err != nil {
return err
Expand Down Expand Up @@ -182,7 +193,6 @@ func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch, parentBatchBlock *uin
}
bi.txNum += txsNum
bi.blockContexts = blockContexts

return nil
}

Expand Down
2 changes: 0 additions & 2 deletions node/derivation/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 3 additions & 19 deletions node/derivation/derivation.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,33 +408,20 @@ func (d *Derivation) UnPackData(data []byte) (geth.RPCRollupBatch, error) {
}

func (d *Derivation) parseBatch(batch geth.RPCRollupBatch, l2Height uint64) (*BatchInfo, error) {
parentBatchHeader := types.BatchHeaderBytes(batch.ParentBatchHeader)
parentBatchIndex, err := parentBatchHeader.BatchIndex()
if err != nil {
return nil, fmt.Errorf("decode batch header index error:%v", err)
}
totalL1MessagePopped, err := parentBatchHeader.TotalL1MessagePopped()
if err != nil {
return nil, fmt.Errorf("decode batch header totalL1MessagePopped error:%v", err)
}

batchInfo := new(BatchInfo)
parentBatchBlock := d.db.ReadBlockNumberByIndex(parentBatchIndex)
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, 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 = parentBatchIndex + 1
return batchInfo, nil
}

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
}
Expand All @@ -449,9 +436,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)
}
Expand Down
4 changes: 2 additions & 2 deletions node/derivation/derivation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 4 additions & 8 deletions ops/tools/batchparse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"context"
"fmt"
"log"
"morph-l2/node/derivation"

"github.com/morph-l2/go-ethereum/ethclient"

"morph-l2/node/derivation"
)

var (
Expand All @@ -19,16 +19,12 @@ func main() {
if err != nil {
panic(err)
}
parentBatch, err := l2Client.GetRollupBatchByIndex(context.Background(), batchIndex-1)
if err != nil {
log.Fatalf("failed to get batch, index: %d, err: %v", batchIndex-1, err)
}
batch, err := l2Client.GetRollupBatchByIndex(context.Background(), batchIndex)
if err != nil {
log.Fatalf("failed to get batch, index: %d, err: %v", batchIndex, err)
panic(err)
}
batchInfo := new(derivation.BatchInfo)
if err = batchInfo.ParseBatch(*batch, &parentBatch.LastBlockNumber); err != nil {
if err = batchInfo.ParseBatch(*batch); err != nil {
panic(err)
}
fmt.Println("batch index: ", batchIndex)
Expand Down
3 changes: 1 addition & 2 deletions oracle/oracle/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions oracle/oracle/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
Loading