Skip to content

Commit

Permalink
remove block number -> batch index mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Oct 26, 2023
1 parent 624749c commit a4682ba
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 62 deletions.
35 changes: 0 additions & 35 deletions core/rawdb/accessors_rollup_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,3 @@ func ReadFinalizedL2BlockNumber(db ethdb.Reader) *uint64 {
finalizedL2BlockNumber := number.Uint64()
return &finalizedL2BlockNumber
}

// WriteBlockNumberToBatchIndex stores the mapping from a block number to a batch index in the database.
func WriteBlockNumberToBatchIndex(db ethdb.KeyValueWriter, startBlockNumber uint64, endBlockNumber uint64, batchIndex uint64) {
if startBlockNumber > endBlockNumber {
log.Crit("Start block number cannot be greater than end block number", "startBlockNumber", startBlockNumber, "endBlockNumber", endBlockNumber)
return
}

value := big.NewInt(0).SetUint64(batchIndex).Bytes()
for blockNumber := startBlockNumber; blockNumber <= endBlockNumber; blockNumber++ {
if err := db.Put(blockNumberToBatchIndexKey(blockNumber), value); err != nil {
log.Crit("Failed to add block number to batch index mapping to batch", "startBlockNumber", startBlockNumber, "endBlockNumber", endBlockNumber, "batchIndex", batchIndex, "err", err)
return
}
}
}

// ReadBlockNumberToBatchIndex fetches the batch index associated with a block number from the database.
func ReadBlockNumberToBatchIndex(db ethdb.Reader, blockNumber uint64) *uint64 {
data, err := db.Get(blockNumberToBatchIndexKey(blockNumber))
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read batch index for block number from database", "blockNumber", blockNumber, "err", err)
}

number := new(big.Int).SetBytes(data)
if !number.IsUint64() {
log.Crit("unexpected batch index for block number in database", "blockNumber", blockNumber, "number", number)
}

batchIndex := number.Uint64()
return &batchIndex
}
6 changes: 0 additions & 6 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ var (
batchChunkRangesPrefix = []byte("R-bcr")
batchMetaPrefix = []byte("R-bm")
finalizedL2BlockNumberKey = []byte("R-finalized")
blockNumberToBatchIndexPrefix = []byte("R-bn2bi")

// Row consumption
rowConsumptionPrefix = []byte("rc") // rowConsumptionPrefix + hash -> row consumption by block
Expand Down Expand Up @@ -309,8 +308,3 @@ func batchChunkRangesKey(batchIndex uint64) []byte {
func batchMetaKey(batchIndex uint64) []byte {
return append(batchMetaPrefix, encodeBigEndian(batchIndex)...)
}

// blockNumberToBatchIndexKey = blockNumberToBatchIndexPrefix + block number
func blockNumberToBatchIndexKey(blockNumber uint64) []byte {
return append(blockNumberToBatchIndexPrefix, encodeBigEndian(blockNumber)...)
}
29 changes: 12 additions & 17 deletions rollup/rollup_sync_service/rollup_sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,13 @@ func (s *RollupSyncService) parseAndUpdateRollupEventLogs(logs []types.Log, endB
return fmt.Errorf("failed to get local node info, batch index: %v, err: %w", batchIndex, err)
}

startBlock, endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks)
endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks)
if err != nil {
return fmt.Errorf("fatal: validateBatch failed: finalize event: %v, err: %w", event, err)
}

batchWriter := s.db.NewBatch()
rawdb.WriteFinalizedL2BlockNumber(batchWriter, endBlock)
rawdb.WriteBlockNumberToBatchIndex(batchWriter, startBlock, endBlock, batchIndex)
rawdb.WriteFinalizedBatchMeta(batchWriter, batchIndex, finalizedBatchMeta)
if err := batchWriter.Write(); err != nil {
log.Crit("Failed to write batch to database", "err", err)
}
rawdb.WriteFinalizedL2BlockNumber(s.db, endBlock)
rawdb.WriteFinalizedBatchMeta(s.db, batchIndex, finalizedBatchMeta)

if batchIndex%100 == 0 {
log.Info("finalized batch progress", "batch index", batchIndex, "finalized l2 block height", endBlock)
Expand Down Expand Up @@ -363,41 +358,41 @@ func (s *RollupSyncService) decodeChunkBlockRanges(txData []byte) ([]*rawdb.Chun
// validateBatch verifies the consistency between the L1 contract and L2 node data.
// The function will terminate the node and exit if any consistency check fails.
// It returns the number of the end block, a finalized batch meta data, and an error if any.
func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.FinalizedBatchMeta, chunks []*Chunk) (uint64, uint64, *rawdb.FinalizedBatchMeta, error) {
func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.FinalizedBatchMeta, chunks []*Chunk) (uint64, *rawdb.FinalizedBatchMeta, error) {
if len(chunks) == 0 {
return 0, 0, nil, fmt.Errorf("invalid argument: length of chunks is 0")
return 0, nil, fmt.Errorf("invalid argument: length of chunks is 0")
}

startChunk := chunks[0]
if len(startChunk.Blocks) == 0 {
return 0, 0, nil, fmt.Errorf("invalid argument: block count of start chunk is 0")
return 0, nil, fmt.Errorf("invalid argument: block count of start chunk is 0")
}
startBlock := startChunk.Blocks[0]

endChunk := chunks[len(chunks)-1]
if len(endChunk.Blocks) == 0 {
return 0, 0, nil, fmt.Errorf("invalid argument: block count of end chunk is 0")
return 0, nil, fmt.Errorf("invalid argument: block count of end chunk is 0")
}
endBlock := endChunk.Blocks[len(endChunk.Blocks)-1]

localStateRoot := endBlock.Header.Root
if localStateRoot != event.StateRoot {
log.Error("State root mismatch", "batch index", event.BatchIndex.Uint64(), "start block", startBlock.Header.Number.Uint64(), "end block", endBlock.Header.Number.Uint64(), "parent batch hash", parentBatchMeta.BatchHash.Hex(), "l1 finalized state root", event.StateRoot.Hex(), "l2 state root", localStateRoot.Hex())
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, 0, nil, fmt.Errorf("state root mismatch")
return 0, nil, fmt.Errorf("state root mismatch")
}

localWithdrawRoot := endBlock.WithdrawRoot
if localWithdrawRoot != event.WithdrawRoot {
log.Error("Withdraw root mismatch", "batch index", event.BatchIndex.Uint64(), "start block", startBlock.Header.Number.Uint64(), "end block", endBlock.Header.Number.Uint64(), "parent batch hash", parentBatchMeta.BatchHash.Hex(), "l1 finalized withdraw root", event.WithdrawRoot.Hex(), "l2 withdraw root", localWithdrawRoot.Hex())
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, 0, nil, fmt.Errorf("withdraw root mismatch")
return 0, nil, fmt.Errorf("withdraw root mismatch")
}

// Note: All params for NewBatchHeader are calculated locally based on the block data.
batchHeader, err := NewBatchHeader(batchHeaderVersion, event.BatchIndex.Uint64(), parentBatchMeta.TotalL1MessagePopped, parentBatchMeta.BatchHash, chunks)
if err != nil {
return 0, 0, nil, fmt.Errorf("failed to construct batch header, err: %w", err)
return 0, nil, fmt.Errorf("failed to construct batch header, err: %w", err)
}

// Note: If the batch headers match, this ensures the consistency of blocks and transactions
Expand All @@ -408,7 +403,7 @@ func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.Finalized
chunksJson, _ := json.Marshal(chunks)
log.Error("Chunks", "chunks", string(chunksJson))
syscall.Kill(os.Getpid(), syscall.SIGTERM)
return 0, 0, nil, fmt.Errorf("batch hash mismatch")
return 0, nil, fmt.Errorf("batch hash mismatch")
}

totalL1MessagePopped := parentBatchMeta.TotalL1MessagePopped
Expand All @@ -421,5 +416,5 @@ func validateBatch(event *L1FinalizeBatchEvent, parentBatchMeta *rawdb.Finalized
StateRoot: localStateRoot,
WithdrawRoot: localWithdrawRoot,
}
return startBlock.Header.Number.Uint64(), endBlock.Header.Number.Uint64(), finalizedBatchMeta, nil
return endBlock.Header.Number.Uint64(), finalizedBatchMeta, nil
}
6 changes: 2 additions & 4 deletions rollup/rollup_sync_service/rollup_sync_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ func TestValidateBatch(t *testing.T) {
StateRoot: chunk3.Blocks[len(chunk3.Blocks)-1].Header.Root,
WithdrawRoot: chunk3.Blocks[len(chunk3.Blocks)-1].WithdrawRoot,
}
startBlock1, endBlock1, finalizedBatchMeta1, err := validateBatch(event1, parentBatchMeta1, []*Chunk{chunk1, chunk2, chunk3})
endBlock1, finalizedBatchMeta1, err := validateBatch(event1, parentBatchMeta1, []*Chunk{chunk1, chunk2, chunk3})
assert.NoError(t, err)
assert.Equal(t, uint64(2), startBlock1)
assert.Equal(t, uint64(13), endBlock1)

templateBlockTrace4, err := os.ReadFile("./testdata/blockTrace_05.json")
Expand All @@ -194,9 +193,8 @@ func TestValidateBatch(t *testing.T) {
StateRoot: chunk4.Blocks[len(chunk4.Blocks)-1].Header.Root,
WithdrawRoot: chunk4.Blocks[len(chunk4.Blocks)-1].WithdrawRoot,
}
startBlock2, endBlock2, finalizedBatchMeta2, err := validateBatch(event2, parentBatchMeta2, []*Chunk{chunk4})
endBlock2, finalizedBatchMeta2, err := validateBatch(event2, parentBatchMeta2, []*Chunk{chunk4})
assert.NoError(t, err)
assert.Equal(t, uint64(17), startBlock2)
assert.Equal(t, uint64(17), endBlock2)

parentBatchMeta3 := &rawdb.FinalizedBatchMeta{
Expand Down

0 comments on commit a4682ba

Please sign in to comment.