Skip to content

Commit f225d25

Browse files
author
colinlyguo
committed
add state root checks
1 parent 18b6b65 commit f225d25

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ func (r *Layer2Relayer) initializeGenesis() error {
277277
}
278278

279279
func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte, stateRoot common.Hash) error {
280+
// Basic sanity checks
281+
if batchHash == "" {
282+
return fmt.Errorf("batch hash is empty")
283+
}
284+
285+
if len(batchHeader) == 0 {
286+
return fmt.Errorf("batch header is empty")
287+
}
288+
289+
if stateRoot == (common.Hash{}) {
290+
return fmt.Errorf("state root is zero")
291+
}
292+
280293
var calldata []byte
281294
var packErr error
282295

@@ -296,6 +309,11 @@ func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte,
296309
log.Info("Rollup importGenesis", "calldata", common.Bytes2Hex(calldata), "stateRoot", stateRoot)
297310
}
298311

312+
// Check generated calldata is not empty
313+
if len(calldata) == 0 {
314+
return fmt.Errorf("generated calldata is empty")
315+
}
316+
299317
// submit genesis batch to L1 rollup contract
300318
txHash, _, err := r.commitSender.SendTransaction(batchHash, &r.cfg.RollupContractAddress, calldata, nil)
301319
if err != nil {
@@ -1083,6 +1101,12 @@ func (r *Layer2Relayer) constructCommitBatchPayloadValidium(batch *dbBatchWithCh
10831101
return nil, 0, 0, fmt.Errorf("batch %d has no chunks", batch.Batch.Index)
10841102
}
10851103

1104+
// Check state root is not zero
1105+
stateRoot := common.HexToHash(batch.Batch.StateRoot)
1106+
if stateRoot == (common.Hash{}) {
1107+
return nil, 0, 0, fmt.Errorf("batch %d state root is zero", batch.Batch.Index)
1108+
}
1109+
10861110
// Calculate metrics
10871111
var maxBlockHeight uint64
10881112
var totalGasUsed uint64
@@ -1102,7 +1126,6 @@ func (r *Layer2Relayer) constructCommitBatchPayloadValidium(batch *dbBatchWithCh
11021126

11031127
lastChunk := batch.Chunks[len(batch.Chunks)-1]
11041128
commitment := common.HexToHash(lastChunk.EndBlockHash)
1105-
11061129
if commitment == (common.Hash{}) {
11071130
return nil, 0, 0, fmt.Errorf("batch %d last chunk end block hash is zero, cannot create commitment", batch.Batch.Index)
11081131
}
@@ -1141,6 +1164,12 @@ func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV7(dbBatch *orm.Batch
11411164
return nil, fmt.Errorf("batch %d header is empty", dbBatch.Index)
11421165
}
11431166

1167+
// Check state root is not zero
1168+
stateRoot := common.HexToHash(dbBatch.StateRoot)
1169+
if stateRoot == (common.Hash{}) {
1170+
return nil, fmt.Errorf("batch %d state root is zero", dbBatch.Index)
1171+
}
1172+
11441173
// Check proof if present
11451174
if aggProof != nil && len(aggProof.Proof()) == 0 {
11461175
return nil, fmt.Errorf("aggregate proof is empty")
@@ -1530,6 +1559,11 @@ func (r *Layer2Relayer) validateBatchFields(batch *dbBatchWithChunks, i int, all
15301559
return fmt.Errorf("batch %d parent batch hash is zero", batch.Batch.Index)
15311560
}
15321561

1562+
stateRoot := common.HexToHash(batch.Batch.StateRoot)
1563+
if stateRoot == (common.Hash{}) {
1564+
return fmt.Errorf("batch %d state root is zero", batch.Batch.Index)
1565+
}
1566+
15331567
return nil
15341568
}
15351569

rollup/internal/controller/relayer/l2_relayer_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@ func testL2RelayerProcessPendingBatches(t *testing.T) {
7070
_, err = chunkOrm.InsertChunk(context.Background(), chunk2, encoding.CodecV7, rutils.ChunkMetrics{})
7171
assert.NoError(t, err)
7272

73+
batchOrm := orm.NewBatch(db)
74+
genesisBatch, err := batchOrm.GetBatchByIndex(context.Background(), 0)
75+
assert.NoError(t, err)
76+
7377
batch := &encoding.Batch{
7478
Index: 1,
7579
TotalL1MessagePoppedBefore: 0,
76-
ParentBatchHash: common.Hash{},
80+
ParentBatchHash: common.HexToHash(genesisBatch.Hash),
7781
Chunks: []*encoding.Chunk{chunk1, chunk2},
7882
Blocks: []*encoding.Block{block1, block2},
7983
}
8084

81-
batchOrm := orm.NewBatch(db)
8285
dbBatch, err := batchOrm.InsertBatch(context.Background(), batch, encoding.CodecV7, rutils.BatchMetrics{})
8386
assert.NoError(t, err)
8487

0 commit comments

Comments
 (0)