Skip to content

Commit cf85225

Browse files
author
colinlyguo
committed
refactor
1 parent 925ef74 commit cf85225

File tree

2 files changed

+289
-327
lines changed

2 files changed

+289
-327
lines changed

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 15 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,6 @@ func (r *Layer2Relayer) initializeGenesis() error {
278278
}
279279

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

@@ -303,18 +290,19 @@ func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte,
303290
log.Info("Validium importGenesis", "calldata", common.Bytes2Hex(calldata))
304291
} else {
305292
// rollup mode: pass batchHeader and stateRoot
293+
294+
// Check state root is not zero
295+
if stateRoot == (common.Hash{}) {
296+
return fmt.Errorf("state root is zero")
297+
}
298+
306299
calldata, packErr = r.l1RollupABI.Pack("importGenesisBatch", batchHeader, stateRoot)
307300
if packErr != nil {
308301
return fmt.Errorf("failed to pack rollup importGenesisBatch with batch header: %v and state root: %v. error: %v", common.Bytes2Hex(batchHeader), stateRoot, packErr)
309302
}
310303
log.Info("Rollup importGenesis", "calldata", common.Bytes2Hex(calldata), "stateRoot", stateRoot)
311304
}
312305

313-
// Check generated calldata is not empty
314-
if len(calldata) == 0 {
315-
return fmt.Errorf("generated calldata is empty")
316-
}
317-
318306
// submit genesis batch to L1 rollup contract
319307
txHash, _, err := r.commitSender.SendTransaction(batchHash, &r.cfg.RollupContractAddress, calldata, nil)
320308
if err != nil {
@@ -492,12 +480,6 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
492480
log.Info("Forcing submission of batches due to timeout", "batch index", batchesToSubmit[0].Batch.Index, "first block created at", oldestBlockTimestamp)
493481
}
494482

495-
// Sanity checks before constructing the transaction
496-
if err := r.sanityChecksBeforeConstructingTransaction(batchesToSubmit); err != nil {
497-
log.Error("Sanity checks failed before constructing transaction", "batches count", len(batchesToSubmit), "first batch index", batchesToSubmit[0].Batch.Index, "last batch index", batchesToSubmit[len(batchesToSubmit)-1].Batch.Index, "err", err)
498-
return
499-
}
500-
501483
// We have at least 1 batch to commit
502484
firstBatch := batchesToSubmit[0].Batch
503485
lastBatch := batchesToSubmit[len(batchesToSubmit)-1].Batch
@@ -527,9 +509,8 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
527509
return
528510
}
529511

530-
err = r.sanityChecksCommitBatchCodecV7CalldataAndBlobs(calldata, blobs, batchesToSubmit, firstBatch, lastBatch)
531-
if err != nil {
532-
log.Error("Sanity check failed for calldata and blobs", "err", err)
512+
if err = r.sanityChecksCommitBatchCodecV7CalldataAndBlobs(calldata, blobs); err != nil {
513+
log.Error("Sanity check failed for calldata and blobs", "codecVersion", codecVersion, "start index", firstBatch.Index, "end index", lastBatch.Index, "err", err)
533514
return
534515
}
535516
}
@@ -972,25 +953,6 @@ func (r *Layer2Relayer) handleL2RollupRelayerConfirmLoop(ctx context.Context) {
972953
}
973954

974955
func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*dbBatchWithChunks, firstBatch, lastBatch *orm.Batch) ([]byte, []*kzg4844.Blob, uint64, uint64, error) {
975-
// Basic sanity checks
976-
if len(batchesToSubmit) == 0 {
977-
return nil, nil, 0, 0, fmt.Errorf("no batches to submit")
978-
}
979-
if firstBatch == nil {
980-
return nil, nil, 0, 0, fmt.Errorf("first batch is nil")
981-
}
982-
if lastBatch == nil {
983-
return nil, nil, 0, 0, fmt.Errorf("last batch is nil")
984-
}
985-
986-
// Check firstBatch and lastBatch match batchesToSubmit
987-
if firstBatch.Index != batchesToSubmit[0].Batch.Index {
988-
return nil, nil, 0, 0, fmt.Errorf("first batch index mismatch: expected %d, got %d", batchesToSubmit[0].Batch.Index, firstBatch.Index)
989-
}
990-
if lastBatch.Index != batchesToSubmit[len(batchesToSubmit)-1].Batch.Index {
991-
return nil, nil, 0, 0, fmt.Errorf("last batch index mismatch: expected %d, got %d", batchesToSubmit[len(batchesToSubmit)-1].Batch.Index, lastBatch.Index)
992-
}
993-
994956
var maxBlockHeight uint64
995957
var totalGasUsed uint64
996958
blobs := make([]*kzg4844.Blob, 0, len(batchesToSubmit))
@@ -1011,16 +973,6 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*db
1011973
return nil, nil, 0, 0, fmt.Errorf("failed to get blocks in range for batch %d: %w", b.Batch.Index, err)
1012974
}
1013975

1014-
if len(blocks) == 0 {
1015-
return nil, nil, 0, 0, fmt.Errorf("batch %d chunk %d has no blocks in range [%d, %d]", b.Batch.Index, c.Index, c.StartBlockNumber, c.EndBlockNumber)
1016-
}
1017-
1018-
// Check that we got the expected number of blocks
1019-
expectedBlockCount := c.EndBlockNumber - c.StartBlockNumber + 1
1020-
if uint64(len(blocks)) != expectedBlockCount {
1021-
return nil, nil, 0, 0, fmt.Errorf("batch %d chunk %d expected %d blocks but got %d", b.Batch.Index, c.Index, expectedBlockCount, len(blocks))
1022-
}
1023-
1024976
batchBlocks = append(batchBlocks, blocks...)
1025977

1026978
if c.EndBlockNumber > maxBlockHeight {
@@ -1037,16 +989,6 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*db
1037989
Blocks: batchBlocks,
1038990
}
1039991

1040-
// Check encoding batch fields are not zero hashes
1041-
if encodingBatch.ParentBatchHash == (common.Hash{}) {
1042-
return nil, nil, 0, 0, fmt.Errorf("batch %d parent batch hash is zero", b.Batch.Index)
1043-
}
1044-
1045-
// Check L1 message queue hash consistency
1046-
if err := r.validateMessageQueueConsistency(encodingBatch.Index, b.Chunks, encodingBatch.PrevL1MessageQueueHash, encodingBatch.PostL1MessageQueueHash); err != nil {
1047-
return nil, nil, 0, 0, err
1048-
}
1049-
1050992
codec, err := encoding.CodecFromVersion(version)
1051993
if err != nil {
1052994
return nil, nil, 0, 0, fmt.Errorf("failed to get codec from version %d, err: %w", b.Batch.CodecVersion, err)
@@ -1057,42 +999,29 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*db
1057999
return nil, nil, 0, 0, fmt.Errorf("failed to create DA batch: %w", err)
10581000
}
10591001

1060-
blob := daBatch.Blob()
1061-
if blob == nil {
1062-
return nil, nil, 0, 0, fmt.Errorf("batch %d generated nil blob", b.Batch.Index)
1063-
}
1064-
1065-
blobs = append(blobs, blob)
1002+
blobs = append(blobs, daBatch.Blob())
10661003
}
10671004

10681005
calldata, err := r.l1RollupABI.Pack("commitBatches", version, common.HexToHash(firstBatch.ParentBatchHash), common.HexToHash(lastBatch.Hash))
10691006
if err != nil {
10701007
return nil, nil, 0, 0, fmt.Errorf("failed to pack commitBatches: %w", err)
10711008
}
1072-
1073-
if len(calldata) == 0 {
1074-
return nil, nil, 0, 0, fmt.Errorf("generated calldata is empty")
1075-
}
1076-
10771009
return calldata, blobs, maxBlockHeight, totalGasUsed, nil
10781010
}
10791011

10801012
func (r *Layer2Relayer) constructCommitBatchPayloadValidium(batch *dbBatchWithChunks) ([]byte, uint64, uint64, error) {
1081-
// Basic sanity checks
1082-
if batch == nil || batch.Batch == nil {
1083-
return nil, 0, 0, fmt.Errorf("batch is nil")
1084-
}
1085-
1086-
if len(batch.Chunks) == 0 {
1087-
return nil, 0, 0, fmt.Errorf("batch %d has no chunks", batch.Batch.Index)
1088-
}
1089-
10901013
// Check state root is not zero
10911014
stateRoot := common.HexToHash(batch.Batch.StateRoot)
10921015
if stateRoot == (common.Hash{}) {
10931016
return nil, 0, 0, fmt.Errorf("batch %d state root is zero", batch.Batch.Index)
10941017
}
10951018

1019+
// Check parent batch hash is not zero
1020+
parentBatchHash := common.HexToHash(batch.Batch.ParentBatchHash)
1021+
if parentBatchHash == (common.Hash{}) {
1022+
return nil, 0, 0, fmt.Errorf("batch %d parent batch hash is zero", batch.Batch.Index)
1023+
}
1024+
10961025
// Calculate metrics
10971026
var maxBlockHeight uint64
10981027
var totalGasUsed uint64
@@ -1112,44 +1041,17 @@ func (r *Layer2Relayer) constructCommitBatchPayloadValidium(batch *dbBatchWithCh
11121041

11131042
lastChunk := batch.Chunks[len(batch.Chunks)-1]
11141043
commitment := common.HexToHash(lastChunk.EndBlockHash)
1115-
if commitment == (common.Hash{}) {
1116-
return nil, 0, 0, fmt.Errorf("batch %d last chunk end block hash is zero, cannot create commitment", batch.Batch.Index)
1117-
}
1118-
1119-
// Check parent batch hash is not zero
1120-
parentBatchHash := common.HexToHash(batch.Batch.ParentBatchHash)
1121-
if parentBatchHash == (common.Hash{}) {
1122-
return nil, 0, 0, fmt.Errorf("batch %d parent batch hash is zero", batch.Batch.Index)
1123-
}
11241044

11251045
version := encoding.CodecVersion(batch.Batch.CodecVersion)
11261046
calldata, err := r.validiumABI.Pack("commitBatch", version, common.HexToHash(batch.Batch.ParentBatchHash), common.HexToHash(batch.Batch.StateRoot), common.HexToHash(batch.Batch.WithdrawRoot), commitment[:])
11271047
if err != nil {
11281048
return nil, 0, 0, fmt.Errorf("failed to pack commitBatch: %w", err)
11291049
}
1130-
1131-
if len(calldata) == 0 {
1132-
return nil, 0, 0, fmt.Errorf("generated calldata is empty for batch %d", batch.Batch.Index)
1133-
}
1134-
11351050
log.Info("Validium commitBatch", "maxBlockHeight", maxBlockHeight, "commitment", commitment.Hex())
11361051
return calldata, maxBlockHeight, totalGasUsed, nil
11371052
}
11381053

11391054
func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV7(dbBatch *orm.Batch, endChunk *orm.Chunk, aggProof *message.OpenVMBundleProof) ([]byte, error) {
1140-
// Basic sanity checks
1141-
if dbBatch == nil {
1142-
return nil, fmt.Errorf("batch is nil")
1143-
}
1144-
if endChunk == nil {
1145-
return nil, fmt.Errorf("end chunk is nil")
1146-
}
1147-
1148-
// Check batch header
1149-
if len(dbBatch.BatchHeader) == 0 {
1150-
return nil, fmt.Errorf("batch %d header is empty", dbBatch.Index)
1151-
}
1152-
11531055
// Check state root is not zero
11541056
stateRoot := common.HexToHash(dbBatch.StateRoot)
11551057
if stateRoot == (common.Hash{}) {
@@ -1173,11 +1075,6 @@ func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV7(dbBatch *orm.Batch
11731075
if packErr != nil {
11741076
return nil, fmt.Errorf("failed to pack finalizeBundlePostEuclidV2 with proof: %w", packErr)
11751077
}
1176-
1177-
if len(calldata) == 0 {
1178-
return nil, fmt.Errorf("generated calldata with proof is empty")
1179-
}
1180-
11811078
return calldata, nil
11821079
}
11831080

@@ -1194,28 +1091,10 @@ func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV7(dbBatch *orm.Batch
11941091
if packErr != nil {
11951092
return nil, fmt.Errorf("failed to pack finalizeBundlePostEuclidV2NoProof: %w", packErr)
11961093
}
1197-
1198-
if len(calldata) == 0 {
1199-
return nil, fmt.Errorf("generated calldata without proof is empty")
1200-
}
1201-
12021094
return calldata, nil
12031095
}
12041096

12051097
func (r *Layer2Relayer) constructFinalizeBundlePayloadValidium(dbBatch *orm.Batch, endChunk *orm.Chunk, aggProof *message.OpenVMBundleProof) ([]byte, error) {
1206-
// Basic sanity checks
1207-
if dbBatch == nil {
1208-
return nil, fmt.Errorf("batch is nil")
1209-
}
1210-
if endChunk == nil {
1211-
return nil, fmt.Errorf("end chunk is nil")
1212-
}
1213-
1214-
// Check batch header is not empty
1215-
if len(dbBatch.BatchHeader) == 0 {
1216-
return nil, fmt.Errorf("batch %d header is empty", dbBatch.Index)
1217-
}
1218-
12191098
// Check proof if present
12201099
if aggProof != nil && len(aggProof.Proof()) == 0 {
12211100
return nil, fmt.Errorf("aggregate proof is empty")
@@ -1237,11 +1116,6 @@ func (r *Layer2Relayer) constructFinalizeBundlePayloadValidium(dbBatch *orm.Batc
12371116
if packErr != nil {
12381117
return nil, fmt.Errorf("failed to pack validium finalizeBundle: %w", packErr)
12391118
}
1240-
1241-
if len(calldata) == 0 {
1242-
return nil, fmt.Errorf("generated calldata is empty for batch %d", dbBatch.Index)
1243-
}
1244-
12451119
return calldata, nil
12461120
}
12471121

0 commit comments

Comments
 (0)