Skip to content

Commit c67888c

Browse files
author
colinlyguo
committed
address comments
1 parent 604e33a commit c67888c

2 files changed

Lines changed: 17 additions & 37 deletions

File tree

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,29 +1036,8 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*db
10361036
}
10371037

10381038
// Check L1 message queue hash consistency
1039-
var totalL1MessagesInBatch uint64
1040-
for _, c := range b.Chunks {
1041-
totalL1MessagesInBatch += c.TotalL1MessagesPoppedInChunk
1042-
}
1043-
1044-
// Check L1 message queue hash consistency
1045-
firstChunk := b.Chunks[0]
1046-
lastChunk := b.Chunks[len(b.Chunks)-1]
1047-
1048-
// If there were L1 messages processed before this batch, prev hash should not be zero
1049-
if firstChunk.TotalL1MessagesPoppedBefore > 0 && encodingBatch.PrevL1MessageQueueHash == (common.Hash{}) {
1050-
return nil, nil, 0, 0, fmt.Errorf("batch %d prev L1 message queue hash is zero but %d L1 messages were processed before", b.Batch.Index, firstChunk.TotalL1MessagesPoppedBefore)
1051-
}
1052-
1053-
// If there are any L1 messages processed up to this batch, post hash should not be zero
1054-
totalL1MessagesProcessed := lastChunk.TotalL1MessagesPoppedBefore + lastChunk.TotalL1MessagesPoppedInChunk
1055-
if totalL1MessagesProcessed > 0 && encodingBatch.PostL1MessageQueueHash == (common.Hash{}) {
1056-
return nil, nil, 0, 0, fmt.Errorf("batch %d post L1 message queue hash is zero but %d L1 messages were processed in total", b.Batch.Index, totalL1MessagesProcessed)
1057-
}
1058-
1059-
// If L1 messages were processed in this batch, prev and post hashes should be different
1060-
if totalL1MessagesInBatch > 0 && encodingBatch.PrevL1MessageQueueHash == encodingBatch.PostL1MessageQueueHash {
1061-
return nil, nil, 0, 0, fmt.Errorf("batch %d has same prev and post L1 message queue hashes but processed %d L1 messages in this batch", b.Batch.Index, totalL1MessagesInBatch)
1039+
if err := r.validateMessageQueueConsistency(encodingBatch.Index, b.Chunks, encodingBatch.PrevL1MessageQueueHash, encodingBatch.PostL1MessageQueueHash); err != nil {
1040+
return nil, nil, 0, 0, err
10621041
}
10631042

10641043
codec, err := encoding.CodecFromVersion(version)

rollup/internal/controller/relayer/l2_relayer_sanity.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,38 @@ import (
1010
)
1111

1212
// validateMessageQueueConsistency validates L1 message queue hash consistency
13-
func (r *Layer2Relayer) validateMessageQueueConsistency(batch *dbBatchWithChunks) error {
14-
if batch.Batch.Index == 0 {
13+
func (r *Layer2Relayer) validateMessageQueueConsistency(batchIndex uint64, chunks []*orm.Chunk, prevL1MsgQueueHash common.Hash, postL1MsgQueueHash common.Hash) error {
14+
if batchIndex == 0 {
1515
return nil
1616
}
1717

18-
firstChunk := batch.Chunks[0]
19-
lastChunk := batch.Chunks[len(batch.Chunks)-1]
18+
if len(chunks) == 0 {
19+
return fmt.Errorf("batch %d has no chunks for message queue validation", batchIndex)
20+
}
2021

21-
prevL1MsgQueueHash := common.HexToHash(batch.Batch.PrevL1MessageQueueHash)
22-
postL1MsgQueueHash := common.HexToHash(batch.Batch.PostL1MessageQueueHash)
22+
firstChunk := chunks[0]
23+
lastChunk := chunks[len(chunks)-1]
2324

2425
// Calculate total L1 messages in this batch
25-
var batchTotalL1MessagesInBatch uint64
26-
for _, chunk := range batch.Chunks {
27-
batchTotalL1MessagesInBatch += chunk.TotalL1MessagesPoppedInChunk
26+
var totalL1MessagesInBatch uint64
27+
for _, chunk := range chunks {
28+
totalL1MessagesInBatch += chunk.TotalL1MessagesPoppedInChunk
2829
}
2930

3031
// If there were L1 messages processed before this batch, prev hash should not be zero
3132
if firstChunk.TotalL1MessagesPoppedBefore > 0 && prevL1MsgQueueHash == (common.Hash{}) {
32-
return fmt.Errorf("batch %d prev L1 message queue hash is zero but %d L1 messages were processed before", batch.Batch.Index, firstChunk.TotalL1MessagesPoppedBefore)
33+
return fmt.Errorf("batch %d prev L1 message queue hash is zero but %d L1 messages were processed before", batchIndex, firstChunk.TotalL1MessagesPoppedBefore)
3334
}
3435

3536
// If there are any L1 messages processed up to this batch, post hash should not be zero
3637
totalL1MessagesProcessed := lastChunk.TotalL1MessagesPoppedBefore + lastChunk.TotalL1MessagesPoppedInChunk
3738
if totalL1MessagesProcessed > 0 && postL1MsgQueueHash == (common.Hash{}) {
38-
return fmt.Errorf("batch %d post L1 message queue hash is zero but %d L1 messages were processed in total", batch.Batch.Index, totalL1MessagesProcessed)
39+
return fmt.Errorf("batch %d post L1 message queue hash is zero but %d L1 messages were processed in total", batchIndex, totalL1MessagesProcessed)
3940
}
4041

4142
// Prev and post queue hashes should be different if L1 messages were processed in this batch
42-
if batchTotalL1MessagesInBatch > 0 && prevL1MsgQueueHash == postL1MsgQueueHash {
43-
return fmt.Errorf("batch %d has same prev and post L1 message queue hashes but processed %d L1 messages in this batch", batch.Batch.Index, batchTotalL1MessagesInBatch)
43+
if totalL1MessagesInBatch > 0 && prevL1MsgQueueHash == postL1MsgQueueHash {
44+
return fmt.Errorf("batch %d has same prev and post L1 message queue hashes but processed %d L1 messages in this batch", batchIndex, totalL1MessagesInBatch)
4445
}
4546

4647
return nil
@@ -135,7 +136,7 @@ func (r *Layer2Relayer) validateSingleBatch(batch *dbBatchWithChunks, i int, all
135136
}
136137

137138
// Validate message queue consistency
138-
if err := r.validateMessageQueueConsistency(batch); err != nil {
139+
if err := r.validateMessageQueueConsistency(batch.Batch.Index, batch.Chunks, common.HexToHash(batch.Batch.PrevL1MessageQueueHash), common.HexToHash(batch.Batch.PostL1MessageQueueHash)); err != nil {
139140
return err
140141
}
141142

0 commit comments

Comments
 (0)