Skip to content

Commit c3659d5

Browse files
author
colinlyguo
committed
refactor(rollup-relayer): remove outdated logic
1 parent 3ab5752 commit c3659d5

File tree

15 files changed

+198
-2316
lines changed

15 files changed

+198
-2316
lines changed

coordinator/internal/logic/provertask/batch_prover_task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ func (bp *BatchProverTask) getBatchTaskDetail(dbBatch *orm.Batch, chunkInfos []*
243243
ChunkProofs: chunkProofs,
244244
}
245245

246-
if encoding.CodecVersion(dbBatch.CodecVersion) != encoding.CodecV3 && encoding.CodecVersion(dbBatch.CodecVersion) != encoding.CodecV4 {
247-
return taskDetail, nil
246+
if encoding.CodecVersion(dbBatch.CodecVersion) != encoding.CodecV4 {
247+
return nil, fmt.Errorf("unsupported codec version: %v, expected at least %v", dbBatch.CodecVersion, encoding.CodecV4)
248248
}
249249

250250
codec, err := encoding.CodecFromVersion(encoding.CodecVersion(dbBatch.CodecVersion))

rollup/cmd/rollup_relayer/app/app.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ func action(ctx *cli.Context) error {
108108

109109
go utils.Loop(subCtx, 2*time.Second, l2relayer.ProcessPendingBatches)
110110

111-
go utils.Loop(subCtx, 15*time.Second, l2relayer.ProcessCommittedBatches)
112-
113111
go utils.Loop(subCtx, 15*time.Second, l2relayer.ProcessPendingBundles)
114112

115113
// Finish start all rollup relayer functions.

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 5 additions & 316 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,8 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
419419
var blob *kzg4844.Blob
420420
codecVersion := encoding.CodecVersion(dbBatch.CodecVersion)
421421
switch codecVersion {
422-
case encoding.CodecV0, encoding.CodecV1, encoding.CodecV2:
423-
calldata, blob, err = r.constructCommitBatchPayloadCodecV0AndV1AndV2(dbBatch, dbParentBatch, dbChunks, chunks)
424-
if err != nil {
425-
log.Error("failed to construct commitBatch payload for V0/V1/V2", "codecVersion", codecVersion, "index", dbBatch.Index, "err", err)
426-
return
427-
}
428-
case encoding.CodecV3, encoding.CodecV4:
429-
calldata, blob, err = r.constructCommitBatchPayloadCodecV3AndV4(dbBatch, dbParentBatch, dbChunks, chunks)
422+
case encoding.CodecV4:
423+
calldata, blob, err = r.constructCommitBatchPayloadCodecV4(dbBatch, dbParentBatch, dbChunks, chunks)
430424
if err != nil {
431425
log.Error("failed to construct commitBatchWithBlobProof payload for V3/V4", "codecVersion", codecVersion, "index", dbBatch.Index, "err", err)
432426
return
@@ -488,69 +482,6 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
488482
}
489483
}
490484

491-
// ProcessCommittedBatches submit proof to layer 1 rollup contract
492-
func (r *Layer2Relayer) ProcessCommittedBatches() {
493-
// retrieves the earliest batch whose rollup status is 'committed'
494-
fields := map[string]interface{}{
495-
"rollup_status": types.RollupCommitted,
496-
}
497-
orderByList := []string{"index ASC"}
498-
limit := 1
499-
batches, err := r.batchOrm.GetBatches(r.ctx, fields, orderByList, limit)
500-
if err != nil {
501-
log.Error("Failed to fetch committed L2 batches", "err", err)
502-
return
503-
}
504-
if len(batches) != 1 {
505-
log.Warn("Unexpected result for GetBlockBatches", "number of batches", len(batches))
506-
return
507-
}
508-
509-
r.metrics.rollupL2RelayerProcessCommittedBatchesTotal.Inc()
510-
511-
batch := batches[0]
512-
status := types.ProvingStatus(batch.ProvingStatus)
513-
switch status {
514-
case types.ProvingTaskUnassigned, types.ProvingTaskAssigned:
515-
if batch.CommittedAt == nil {
516-
log.Error("batch.CommittedAt is nil", "index", batch.Index, "hash", batch.Hash)
517-
return
518-
}
519-
520-
if r.cfg.EnableTestEnvBypassFeatures && utils.NowUTC().Sub(*batch.CommittedAt) > time.Duration(r.cfg.FinalizeBatchWithoutProofTimeoutSec)*time.Second {
521-
if err := r.finalizeBatch(batch, false); err != nil {
522-
log.Error("Failed to finalize timeout batch without proof", "index", batch.Index, "hash", batch.Hash, "err", err)
523-
}
524-
}
525-
526-
case types.ProvingTaskVerified:
527-
r.metrics.rollupL2RelayerProcessCommittedBatchesFinalizedTotal.Inc()
528-
if err := r.finalizeBatch(batch, true); err != nil {
529-
log.Error("Failed to finalize batch with proof", "index", batch.Index, "hash", batch.Hash, "err", err)
530-
}
531-
532-
case types.ProvingTaskFailed:
533-
// We were unable to prove this batch. There are two possibilities:
534-
// (a) Prover bug. In this case, we should fix and redeploy the prover.
535-
// In the meantime, we continue to commit batches to L1 as well as
536-
// proposing and proving chunks and batches.
537-
// (b) Unprovable batch, e.g. proof overflow. In this case we need to
538-
// stop the ledger, fix the limit, revert all the violating blocks,
539-
// chunks and batches and all subsequent ones, and resume, i.e. this
540-
// case requires manual resolution.
541-
log.Error(
542-
"batch proving failed",
543-
"Index", batch.Index,
544-
"Hash", batch.Hash,
545-
"ProvedAt", batch.ProvedAt,
546-
"ProofTimeSec", batch.ProofTimeSec,
547-
)
548-
549-
default:
550-
log.Error("encounter unreachable case in ProcessCommittedBatches", "proving status", status)
551-
}
552-
}
553-
554485
// ProcessPendingBundles submits proof to layer 1 rollup contract
555486
func (r *Layer2Relayer) ProcessPendingBundles() {
556487
r.metrics.rollupL2RelayerProcessPendingBundlesTotal.Inc()
@@ -596,126 +527,6 @@ func (r *Layer2Relayer) ProcessPendingBundles() {
596527
}
597528
}
598529

599-
func (r *Layer2Relayer) finalizeBatch(dbBatch *orm.Batch, withProof bool) error {
600-
// Check batch status before sending `finalizeBatch` tx.
601-
if r.cfg.ChainMonitor.Enabled {
602-
var batchStatus bool
603-
batchStatus, err := r.getBatchStatusByIndex(dbBatch)
604-
if err != nil {
605-
r.metrics.rollupL2ChainMonitorLatestFailedCall.Inc()
606-
log.Warn("failed to get batch status, please check chain_monitor api server", "batch_index", dbBatch.Index, "err", err)
607-
return err
608-
}
609-
if !batchStatus {
610-
r.metrics.rollupL2ChainMonitorLatestFailedBatchStatus.Inc()
611-
log.Error("the batch status is false, stop finalize batch and check the reason", "batch_index", dbBatch.Index)
612-
return errors.New("the batch status is false")
613-
}
614-
}
615-
616-
if dbBatch.Index == 0 {
617-
return errors.New("invalid args: batch index is 0, should only happen in finalizing genesis batch")
618-
}
619-
620-
dbParentBatch, getErr := r.batchOrm.GetBatchByIndex(r.ctx, dbBatch.Index-1)
621-
if getErr != nil {
622-
return fmt.Errorf("failed to get batch, index: %d, err: %w", dbBatch.Index-1, getErr)
623-
}
624-
625-
dbChunks, err := r.chunkOrm.GetChunksInRange(r.ctx, dbBatch.StartChunkIndex, dbBatch.EndChunkIndex)
626-
if err != nil {
627-
return fmt.Errorf("failed to fetch chunks: %w", err)
628-
}
629-
630-
var aggProof *message.BatchProof
631-
if withProof {
632-
aggProof, getErr = r.batchOrm.GetVerifiedProofByHash(r.ctx, dbBatch.Hash)
633-
if getErr != nil {
634-
return fmt.Errorf("failed to get verified proof by hash, index: %d, err: %w", dbBatch.Index, getErr)
635-
}
636-
637-
if err = aggProof.SanityCheck(); err != nil {
638-
return fmt.Errorf("failed to check agg_proof sanity, index: %d, err: %w", dbBatch.Index, err)
639-
}
640-
}
641-
642-
var calldata []byte
643-
codecVersion := encoding.GetCodecVersion(r.chainCfg, dbChunks[0].StartBlockNumber, dbChunks[0].StartBlockTime)
644-
645-
switch codecVersion {
646-
case encoding.CodecV0:
647-
log.Info("Start to roll up zk proof", "batch hash", dbBatch.Hash)
648-
calldata, err = r.constructFinalizeBatchPayloadCodecV0(dbBatch, dbParentBatch, aggProof)
649-
if err != nil {
650-
return fmt.Errorf("failed to construct finalizeBatch payload codecv0, index: %v, err: %w", dbBatch.Index, err)
651-
}
652-
653-
case encoding.CodecV1, encoding.CodecV2:
654-
log.Info("Start to roll up zk proof", "batch hash", dbBatch.Hash)
655-
chunks := make([]*encoding.Chunk, len(dbChunks))
656-
for i, c := range dbChunks {
657-
blocks, dbErr := r.l2BlockOrm.GetL2BlocksInRange(r.ctx, c.StartBlockNumber, c.EndBlockNumber)
658-
if dbErr != nil {
659-
return fmt.Errorf("failed to fetch blocks: %w", dbErr)
660-
}
661-
chunks[i] = &encoding.Chunk{Blocks: blocks}
662-
}
663-
calldata, err = r.constructFinalizeBatchPayloadCodecV1AndV2(dbBatch, dbParentBatch, dbChunks, chunks, aggProof)
664-
if err != nil {
665-
return fmt.Errorf("failed to construct finalizeBatch payload codecv1, index: %v, err: %w", dbBatch.Index, err)
666-
}
667-
668-
case encoding.CodecV3, encoding.CodecV4:
669-
log.Debug("using finalizeBundle instead", "index", dbBatch.Index, "codec version", codecVersion)
670-
return nil
671-
672-
default:
673-
return fmt.Errorf("unsupported codec version: %v", codecVersion)
674-
}
675-
676-
txHash, err := r.finalizeSender.SendTransaction(dbBatch.Hash, &r.cfg.RollupContractAddress, calldata, nil, 0)
677-
if err != nil {
678-
log.Error(
679-
"finalizeBatch in layer1 failed",
680-
"with proof", withProof,
681-
"index", dbBatch.Index,
682-
"hash", dbBatch.Hash,
683-
"RollupContractAddress", r.cfg.RollupContractAddress,
684-
"err", err,
685-
"calldata", common.Bytes2Hex(calldata),
686-
)
687-
return err
688-
}
689-
690-
log.Info("finalizeBatch in layer1", "with proof", withProof, "index", dbBatch.Index, "batch hash", dbBatch.Hash, "tx hash", txHash.String())
691-
692-
// Updating rollup status in database.
693-
if err := r.batchOrm.UpdateFinalizeTxHashAndRollupStatus(r.ctx, dbBatch.Hash, txHash.String(), types.RollupFinalizing); err != nil {
694-
log.Error("UpdateFinalizeTxHashAndRollupStatus failed", "index", dbBatch.Index, "batch hash", dbBatch.Hash, "tx hash", txHash.String(), "err", err)
695-
return err
696-
}
697-
698-
// Updating the proving status when finalizing without proof, thus the coordinator could omit this task.
699-
// it isn't a necessary step, so don't put in a transaction with UpdateFinalizeTxHashAndRollupStatus
700-
if !withProof {
701-
txErr := r.db.Transaction(func(dbTX *gorm.DB) error {
702-
if updateErr := r.batchOrm.UpdateProvingStatus(r.ctx, dbBatch.Hash, types.ProvingTaskVerified, dbTX); updateErr != nil {
703-
return updateErr
704-
}
705-
if updateErr := r.chunkOrm.UpdateProvingStatusByBatchHash(r.ctx, dbBatch.Hash, types.ProvingTaskVerified, dbTX); updateErr != nil {
706-
return updateErr
707-
}
708-
return nil
709-
})
710-
if txErr != nil {
711-
log.Error("Updating chunk and batch proving status when finalizing without proof failure", "batchHash", dbBatch.Hash, "err", txErr)
712-
}
713-
}
714-
715-
r.metrics.rollupL2RelayerProcessCommittedBatchesFinalizedSuccessTotal.Inc()
716-
return nil
717-
}
718-
719530
func (r *Layer2Relayer) finalizeBundle(bundle *orm.Bundle, withProof bool) error {
720531
// Check batch status before sending `finalizeBundle` tx.
721532
if r.cfg.ChainMonitor.Enabled {
@@ -757,7 +568,7 @@ func (r *Layer2Relayer) finalizeBundle(bundle *orm.Bundle, withProof bool) error
757568
}
758569
}
759570

760-
calldata, err := r.constructFinalizeBundlePayloadCodecV3AndV4(dbBatch, aggProof)
571+
calldata, err := r.constructFinalizeBundlePayloadCodecV4(dbBatch, aggProof)
761572
if err != nil {
762573
return fmt.Errorf("failed to construct finalizeBundle payload codecv3, index: %v, err: %w", dbBatch.Index, err)
763574
}
@@ -963,45 +774,7 @@ func (r *Layer2Relayer) handleL2RollupRelayerConfirmLoop(ctx context.Context) {
963774
}
964775
}
965776

966-
func (r *Layer2Relayer) constructCommitBatchPayloadCodecV0AndV1AndV2(dbBatch *orm.Batch, dbParentBatch *orm.Batch, dbChunks []*orm.Chunk, chunks []*encoding.Chunk) ([]byte, *kzg4844.Blob, error) {
967-
codec, err := encoding.CodecFromVersion(encoding.CodecVersion(dbBatch.CodecVersion))
968-
if err != nil {
969-
return nil, nil, fmt.Errorf("failed to get codec from version %d, err: %w", dbBatch.CodecVersion, err)
970-
}
971-
972-
batch := &encoding.Batch{
973-
Index: dbBatch.Index,
974-
TotalL1MessagePoppedBefore: dbChunks[0].TotalL1MessagesPoppedBefore,
975-
ParentBatchHash: common.HexToHash(dbParentBatch.Hash),
976-
Chunks: chunks,
977-
}
978-
979-
daBatch, createErr := codec.NewDABatch(batch)
980-
if createErr != nil {
981-
return nil, nil, fmt.Errorf("failed to create DA batch: %w", createErr)
982-
}
983-
984-
encodedChunks := make([][]byte, len(dbChunks))
985-
for i, c := range dbChunks {
986-
daChunk, createErr := codec.NewDAChunk(chunks[i], c.TotalL1MessagesPoppedBefore)
987-
if createErr != nil {
988-
return nil, nil, fmt.Errorf("failed to create DA chunk: %w", createErr)
989-
}
990-
daChunkBytes, encodeErr := daChunk.Encode()
991-
if encodeErr != nil {
992-
return nil, nil, fmt.Errorf("failed to encode DA chunk: %w", encodeErr)
993-
}
994-
encodedChunks[i] = daChunkBytes
995-
}
996-
997-
calldata, packErr := r.l1RollupABI.Pack("commitBatch", daBatch.Version(), dbParentBatch.BatchHeader, encodedChunks, daBatch.SkippedL1MessageBitmap())
998-
if packErr != nil {
999-
return nil, nil, fmt.Errorf("failed to pack commitBatch: %w", packErr)
1000-
}
1001-
return calldata, daBatch.Blob(), nil
1002-
}
1003-
1004-
func (r *Layer2Relayer) constructCommitBatchPayloadCodecV3AndV4(dbBatch *orm.Batch, dbParentBatch *orm.Batch, dbChunks []*orm.Chunk, chunks []*encoding.Chunk) ([]byte, *kzg4844.Blob, error) {
777+
func (r *Layer2Relayer) constructCommitBatchPayloadCodecV4(dbBatch *orm.Batch, dbParentBatch *orm.Batch, dbChunks []*orm.Chunk, chunks []*encoding.Chunk) ([]byte, *kzg4844.Blob, error) {
1005778
batch := &encoding.Batch{
1006779
Index: dbBatch.Index,
1007780
TotalL1MessagePoppedBefore: dbChunks[0].TotalL1MessagesPoppedBefore,
@@ -1043,91 +816,7 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV3AndV4(dbBatch *orm.Bat
1043816
return calldata, daBatch.Blob(), nil
1044817
}
1045818

1046-
func (r *Layer2Relayer) constructFinalizeBatchPayloadCodecV0(dbBatch *orm.Batch, dbParentBatch *orm.Batch, aggProof *message.BatchProof) ([]byte, error) {
1047-
if aggProof != nil { // finalizeBatch with proof.
1048-
calldata, packErr := r.l1RollupABI.Pack(
1049-
"finalizeBatchWithProof",
1050-
dbBatch.BatchHeader,
1051-
common.HexToHash(dbParentBatch.StateRoot),
1052-
common.HexToHash(dbBatch.StateRoot),
1053-
common.HexToHash(dbBatch.WithdrawRoot),
1054-
aggProof.Proof,
1055-
)
1056-
if packErr != nil {
1057-
return nil, fmt.Errorf("failed to pack finalizeBatchWithProof: %w", packErr)
1058-
}
1059-
return calldata, nil
1060-
}
1061-
1062-
// finalizeBatch without proof.
1063-
calldata, packErr := r.l1RollupABI.Pack(
1064-
"finalizeBatch",
1065-
dbBatch.BatchHeader,
1066-
common.HexToHash(dbParentBatch.StateRoot),
1067-
common.HexToHash(dbBatch.StateRoot),
1068-
common.HexToHash(dbBatch.WithdrawRoot),
1069-
)
1070-
if packErr != nil {
1071-
return nil, fmt.Errorf("failed to pack finalizeBatch: %w", packErr)
1072-
}
1073-
return calldata, nil
1074-
}
1075-
1076-
func (r *Layer2Relayer) constructFinalizeBatchPayloadCodecV1AndV2(dbBatch *orm.Batch, dbParentBatch *orm.Batch, dbChunks []*orm.Chunk, chunks []*encoding.Chunk, aggProof *message.BatchProof) ([]byte, error) {
1077-
batch := &encoding.Batch{
1078-
Index: dbBatch.Index,
1079-
TotalL1MessagePoppedBefore: dbChunks[0].TotalL1MessagesPoppedBefore,
1080-
ParentBatchHash: common.HexToHash(dbParentBatch.Hash),
1081-
Chunks: chunks,
1082-
}
1083-
1084-
codec, err := encoding.CodecFromVersion(encoding.CodecVersion(dbBatch.CodecVersion))
1085-
if err != nil {
1086-
return nil, fmt.Errorf("failed to get codec from version %d, err: %w", dbBatch.CodecVersion, err)
1087-
}
1088-
1089-
daBatch, createErr := codec.NewDABatch(batch)
1090-
if createErr != nil {
1091-
return nil, fmt.Errorf("failed to create DA batch: %w", createErr)
1092-
}
1093-
1094-
blobDataProof, getErr := daBatch.BlobDataProofForPointEvaluation()
1095-
if getErr != nil {
1096-
return nil, fmt.Errorf("failed to get blob data proof: %w", getErr)
1097-
}
1098-
1099-
if aggProof != nil { // finalizeBatch4844 with proof.
1100-
calldata, packErr := r.l1RollupABI.Pack(
1101-
"finalizeBatchWithProof4844",
1102-
dbBatch.BatchHeader,
1103-
common.HexToHash(dbParentBatch.StateRoot),
1104-
common.HexToHash(dbBatch.StateRoot),
1105-
common.HexToHash(dbBatch.WithdrawRoot),
1106-
blobDataProof,
1107-
aggProof.Proof,
1108-
)
1109-
if packErr != nil {
1110-
return nil, fmt.Errorf("failed to pack finalizeBatchWithProof4844: %w", packErr)
1111-
}
1112-
return calldata, nil
1113-
}
1114-
1115-
// finalizeBatch4844 without proof.
1116-
calldata, packErr := r.l1RollupABI.Pack(
1117-
"finalizeBatch4844",
1118-
dbBatch.BatchHeader,
1119-
common.HexToHash(dbParentBatch.StateRoot),
1120-
common.HexToHash(dbBatch.StateRoot),
1121-
common.HexToHash(dbBatch.WithdrawRoot),
1122-
blobDataProof,
1123-
)
1124-
if packErr != nil {
1125-
return nil, fmt.Errorf("failed to pack finalizeBatch4844: %w", packErr)
1126-
}
1127-
return calldata, nil
1128-
}
1129-
1130-
func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV3AndV4(dbBatch *orm.Batch, aggProof *message.BundleProof) ([]byte, error) {
819+
func (r *Layer2Relayer) constructFinalizeBundlePayloadCodecV4(dbBatch *orm.Batch, aggProof *message.BundleProof) ([]byte, error) {
1131820
if aggProof != nil { // finalizeBundle with proof.
1132821
calldata, packErr := r.l1RollupABI.Pack(
1133822
"finalizeBundleWithProof",

0 commit comments

Comments
 (0)