Skip to content

Commit 27168a2

Browse files
committed
address comments
1 parent 842d654 commit 27168a2

File tree

2 files changed

+151
-6
lines changed

2 files changed

+151
-6
lines changed

rollup/rollup_sync_service/rollup_sync_service.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ func (s *RollupSyncService) getCommittedBatchMeta(commitedBatch da.EntryWithBloc
464464
return nil, fmt.Errorf("failed to decode block ranges from chunks, batch index: %v, err: %w", commitedBatch.BatchIndex(), err)
465465
}
466466

467-
// With CodecV7 the batch creation changed. We need to compute and store PostL1MessageQueueHash.
467+
// With >= CodecV7 the batch creation changed. We need to compute and store PostL1MessageQueueHash.
468468
// PrevL1MessageQueueHash of a batch == PostL1MessageQueueHash of the previous batch.
469469
// We need to do this for every committed batch (instead of finalized batch) because the L1MessageQueueHash
470470
// is a continuous hash of all L1 messages over all batches. With bundles we only receive the finalize event
471471
// for the last batch of the bundle.
472472
var lastL1MessageQueueHash common.Hash
473-
if commitedBatch.Version() == encoding.CodecV7 {
473+
if commitedBatch.Version() >= encoding.CodecV7 {
474474
parentCommittedBatchMeta, err := rawdb.ReadCommittedBatchMeta(s.db, commitedBatch.BatchIndex()-1)
475475
if err != nil {
476476
return nil, fmt.Errorf("failed to read parent committed batch meta, batch index: %v, err: %w", commitedBatch.BatchIndex()-1, errors.Join(ErrMissingBatchEvent, err))
@@ -493,11 +493,11 @@ func (s *RollupSyncService) getCommittedBatchMeta(commitedBatch da.EntryWithBloc
493493
return nil, fmt.Errorf("failed to get local node info, batch index: %v, err: %w", commitedBatch.BatchIndex(), err)
494494
}
495495

496-
// There is no chunks encoded in a batch anymore with CodecV7.
496+
// There is no chunks encoded in a batch anymore with >= CodecV7.
497497
// For compatibility reason here we still use a single chunk to store the block ranges of the batch.
498498
// We make sure that there is really only one chunk which contains all blocks of the batch.
499499
if len(chunks) != 1 {
500-
return nil, fmt.Errorf("invalid argument: chunk count is not 1 for CodecV7, batch index: %v", commitedBatch.BatchIndex())
500+
return nil, fmt.Errorf("invalid argument: chunk count is not 1 for CodecV%v, batch index: %v", commitedBatch.Version(), commitedBatch.BatchIndex())
501501
}
502502

503503
lastL1MessageQueueHash, err = encoding.MessageQueueV2ApplyL1MessagesFromBlocks(prevL1MessageQueueHash, chunks[0].Blocks)
@@ -563,11 +563,11 @@ func validateBatch(batchIndex uint64, event *l1.FinalizeBatchEvent, parentFinali
563563
Chunks: chunks,
564564
}
565565
} else {
566-
// With CodecV7 the batch creation changed. There is no chunks encoded in a batch anymore.
566+
// With >= CodecV7 the batch creation changed. There is no chunks encoded in a batch anymore.
567567
// For compatibility reason here we still use a single chunk to store the block ranges of the batch.
568568
// We make sure that there is really only one chunk which contains all blocks of the batch.
569569
if len(chunks) != 1 {
570-
return 0, nil, fmt.Errorf("invalid argument: chunk count is not 1 for CodecV7, batch index: %v", batchIndex)
570+
return 0, nil, fmt.Errorf("invalid argument: chunk count is not 1 for CodecV%v, batch index: %v", committedBatchMeta.Version, batchIndex)
571571
}
572572

573573
batch = &encoding.Batch{

rollup/rollup_sync_service/rollup_sync_service_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,151 @@ func TestValidateBatchCodecV7(t *testing.T) {
816816
}, finalizedBatchMeta4)
817817
}
818818

819+
func TestValidateBatchCodecV8(t *testing.T) {
820+
codecV8 := encoding.NewDACodecV8()
821+
822+
var finalizedBatchMeta1 *rawdb.FinalizedBatchMeta
823+
var committedBatchMeta1 *rawdb.CommittedBatchMeta
824+
{
825+
block1 := replaceBlockNumber(readBlockFromJSON(t, "./testdata/blockTrace_02.json"), 1)
826+
batch1 := &encoding.Batch{
827+
Index: 1,
828+
PrevL1MessageQueueHash: common.Hash{},
829+
PostL1MessageQueueHash: common.Hash{},
830+
Blocks: []*encoding.Block{block1},
831+
}
832+
batch1LastBlock := batch1.Blocks[len(batch1.Blocks)-1]
833+
834+
daBatch1, err := codecV8.NewDABatch(batch1)
835+
require.NoError(t, err)
836+
837+
event1 := l1.NewFinalizeBatchEvent(
838+
new(big.Int).SetUint64(batch1.Index),
839+
daBatch1.Hash(),
840+
batch1LastBlock.Header.Root,
841+
batch1LastBlock.WithdrawRoot,
842+
common.HexToHash("0x1"),
843+
common.HexToHash("0x1"),
844+
1,
845+
)
846+
847+
committedBatchMeta1 = &rawdb.CommittedBatchMeta{
848+
Version: uint8(encoding.CodecV8),
849+
PostL1MessageQueueHash: common.Hash{},
850+
}
851+
852+
var endBlock1 uint64
853+
endBlock1, finalizedBatchMeta1, err = validateBatch(event1.BatchIndex().Uint64(), event1, &rawdb.FinalizedBatchMeta{}, &rawdb.CommittedBatchMeta{}, committedBatchMeta1, []*encoding.Chunk{{Blocks: batch1.Blocks}}, nil)
854+
require.NoError(t, err)
855+
require.EqualValues(t, 1, endBlock1)
856+
require.Equal(t, &rawdb.FinalizedBatchMeta{
857+
BatchHash: daBatch1.Hash(),
858+
TotalL1MessagePopped: 0,
859+
StateRoot: batch1LastBlock.Header.Root,
860+
WithdrawRoot: batch1LastBlock.WithdrawRoot,
861+
}, finalizedBatchMeta1)
862+
}
863+
864+
// finalize 3 batches with CodecV8 at once
865+
block2 := replaceBlockNumber(readBlockFromJSON(t, "./testdata/blockTrace_03.json"), 2)
866+
batch2 := &encoding.Batch{
867+
Index: 2,
868+
ParentBatchHash: finalizedBatchMeta1.BatchHash,
869+
PrevL1MessageQueueHash: common.Hash{},
870+
PostL1MessageQueueHash: common.Hash{},
871+
Blocks: []*encoding.Block{block2},
872+
}
873+
batch2LastBlock := batch2.Blocks[len(batch2.Blocks)-1]
874+
875+
daBatch2, err := codecV8.NewDABatch(batch2)
876+
require.NoError(t, err)
877+
878+
block3 := replaceBlockNumber(readBlockFromJSON(t, "./testdata/blockTrace_06.json"), 3)
879+
LastL1MessageQueueHashBatch3, err := encoding.MessageQueueV2ApplyL1MessagesFromBlocks(common.Hash{}, []*encoding.Block{block3})
880+
require.NoError(t, err)
881+
batch3 := &encoding.Batch{
882+
Index: 3,
883+
ParentBatchHash: daBatch2.Hash(),
884+
PrevL1MessageQueueHash: common.Hash{},
885+
PostL1MessageQueueHash: LastL1MessageQueueHashBatch3,
886+
Blocks: []*encoding.Block{block3},
887+
}
888+
batch3LastBlock := batch3.Blocks[len(batch3.Blocks)-1]
889+
890+
daBatch3, err := codecV8.NewDABatch(batch3)
891+
require.NoError(t, err)
892+
893+
block4 := replaceBlockNumber(readBlockFromJSON(t, "./testdata/blockTrace_07.json"), 4)
894+
LastL1MessageQueueHashBatch4, err := encoding.MessageQueueV2ApplyL1MessagesFromBlocks(LastL1MessageQueueHashBatch3, []*encoding.Block{block4})
895+
require.NoError(t, err)
896+
batch4 := &encoding.Batch{
897+
Index: 4,
898+
ParentBatchHash: daBatch3.Hash(),
899+
PrevL1MessageQueueHash: LastL1MessageQueueHashBatch3,
900+
PostL1MessageQueueHash: LastL1MessageQueueHashBatch4,
901+
Blocks: []*encoding.Block{block4},
902+
}
903+
batch4LastBlock := batch4.Blocks[len(batch4.Blocks)-1]
904+
905+
daBatch4, err := codecV8.NewDABatch(batch4)
906+
require.NoError(t, err)
907+
908+
event2 := l1.NewFinalizeBatchEvent(
909+
new(big.Int).SetUint64(batch4.Index),
910+
daBatch4.Hash(),
911+
batch4LastBlock.Header.Root,
912+
batch4LastBlock.WithdrawRoot,
913+
common.HexToHash("0x1"),
914+
common.HexToHash("0x1"),
915+
1,
916+
)
917+
918+
committedBatchMeta2 := &rawdb.CommittedBatchMeta{
919+
Version: uint8(encoding.CodecV8),
920+
PostL1MessageQueueHash: common.Hash{},
921+
}
922+
923+
committedBatchMeta3 := &rawdb.CommittedBatchMeta{
924+
Version: uint8(encoding.CodecV8),
925+
PostL1MessageQueueHash: LastL1MessageQueueHashBatch3,
926+
}
927+
928+
committedBatchMeta4 := &rawdb.CommittedBatchMeta{
929+
Version: uint8(encoding.CodecV8),
930+
PostL1MessageQueueHash: LastL1MessageQueueHashBatch4,
931+
}
932+
933+
endBlock2, finalizedBatchMeta2, err := validateBatch(2, event2, finalizedBatchMeta1, committedBatchMeta1, committedBatchMeta2, []*encoding.Chunk{{Blocks: batch2.Blocks}}, nil)
934+
require.NoError(t, err)
935+
require.EqualValues(t, 2, endBlock2)
936+
require.Equal(t, &rawdb.FinalizedBatchMeta{
937+
BatchHash: daBatch2.Hash(),
938+
TotalL1MessagePopped: 0,
939+
StateRoot: batch2LastBlock.Header.Root,
940+
WithdrawRoot: batch2LastBlock.WithdrawRoot,
941+
}, finalizedBatchMeta2)
942+
943+
endBlock3, finalizedBatchMeta3, err := validateBatch(3, event2, finalizedBatchMeta2, committedBatchMeta2, committedBatchMeta3, []*encoding.Chunk{{Blocks: batch3.Blocks}}, nil)
944+
require.NoError(t, err)
945+
require.EqualValues(t, 3, endBlock3)
946+
require.Equal(t, &rawdb.FinalizedBatchMeta{
947+
BatchHash: daBatch3.Hash(),
948+
TotalL1MessagePopped: 1,
949+
StateRoot: batch3LastBlock.Header.Root,
950+
WithdrawRoot: batch3LastBlock.WithdrawRoot,
951+
}, finalizedBatchMeta3)
952+
953+
endBlock4, finalizedBatchMeta4, err := validateBatch(4, event2, finalizedBatchMeta3, committedBatchMeta3, committedBatchMeta4, []*encoding.Chunk{{Blocks: batch4.Blocks}}, nil)
954+
require.NoError(t, err)
955+
require.EqualValues(t, 4, endBlock4)
956+
require.Equal(t, &rawdb.FinalizedBatchMeta{
957+
BatchHash: daBatch4.Hash(),
958+
TotalL1MessagePopped: 6,
959+
StateRoot: batch4LastBlock.Header.Root,
960+
WithdrawRoot: batch4LastBlock.WithdrawRoot,
961+
}, finalizedBatchMeta4)
962+
}
963+
819964
func readBlockFromJSON(t *testing.T, filename string) *encoding.Block {
820965
data, err := os.ReadFile(filename)
821966
assert.NoError(t, err)

0 commit comments

Comments
 (0)