From 0c95a802e8b40b4f105c8f18702376f70afebb5a Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Fri, 27 Dec 2024 17:54:56 +0800 Subject: [PATCH 1/3] fix(rollup-relayer): update batch finalizing status and unify db timestamp utc --- common/version/version.go | 2 +- .../internal/controller/relayer/l2_relayer.go | 18 +++++++++++-- rollup/internal/orm/batch.go | 25 +++++++++---------- rollup/internal/orm/bundle.go | 9 +++---- rollup/internal/orm/chunk.go | 8 +++--- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/common/version/version.go b/common/version/version.go index ebbd083707..6fbb114b60 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.4.83" +var tag = "v4.4.84" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/rollup/internal/controller/relayer/l2_relayer.go b/rollup/internal/controller/relayer/l2_relayer.go index 33f6921a9b..772015644b 100644 --- a/rollup/internal/controller/relayer/l2_relayer.go +++ b/rollup/internal/controller/relayer/l2_relayer.go @@ -636,8 +636,22 @@ func (r *Layer2Relayer) finalizeBundle(bundle *orm.Bundle, withProof bool) error log.Info("finalizeBundle in layer1", "with proof", withProof, "index", bundle.Index, "start batch index", bundle.StartBatchIndex, "end batch index", bundle.EndBatchIndex, "tx hash", txHash.String()) // Updating rollup status in database. - if err := r.bundleOrm.UpdateFinalizeTxHashAndRollupStatus(r.ctx, bundle.Hash, txHash.String(), types.RollupFinalizing); err != nil { - log.Error("UpdateFinalizeTxHashAndRollupStatus failed", "index", bundle.Index, "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) + err = r.db.Transaction(func(dbTX *gorm.DB) error { + if err = r.batchOrm.UpdateFinalizeTxHashAndRollupStatusByBundleHash(r.ctx, bundle.Hash, txHash.String(), types.RollupFinalizing, dbTX); err != nil { + log.Warn("UpdateFinalizeTxHashAndRollupStatusByBundleHash failed", "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) + return err + } + + if err = r.bundleOrm.UpdateFinalizeTxHashAndRollupStatus(r.ctx, bundle.Hash, txHash.String(), types.RollupFinalizing, dbTX); err != nil { + log.Warn("UpdateFinalizeTxHashAndRollupStatus failed", "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) + return err + } + + return nil + }) + + if err != nil { + log.Warn("failed to update rollup status of bundle and batches", "err", err) return err } diff --git a/rollup/internal/orm/batch.go b/rollup/internal/orm/batch.go index 6f909f2962..54654822a7 100644 --- a/rollup/internal/orm/batch.go +++ b/rollup/internal/orm/batch.go @@ -13,9 +13,8 @@ import ( "scroll-tech/common/types" "scroll-tech/common/types/message" - "scroll-tech/common/utils" - rutils "scroll-tech/rollup/internal/utils" + "scroll-tech/rollup/internal/utils" ) // Batch represents a batch of chunks. @@ -250,7 +249,7 @@ func (o *Batch) GetBatchByIndex(ctx context.Context, index uint64) (*Batch, erro } // InsertBatch inserts a new batch into the database. -func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVersion encoding.CodecVersion, metrics rutils.BatchMetrics, dbTX ...*gorm.DB) (*Batch, error) { +func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVersion encoding.CodecVersion, metrics utils.BatchMetrics, dbTX ...*gorm.DB) (*Batch, error) { if batch == nil { return nil, errors.New("invalid args: batch is nil") } @@ -271,7 +270,7 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVer startChunkIndex = parentBatch.EndChunkIndex + 1 } - batchMeta, err := rutils.GetBatchMetadata(batch, codecVersion) + batchMeta, err := utils.GetBatchMetadata(batch, codecVersion) if err != nil { log.Error("failed to get batch metadata", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore, "parent hash", batch.ParentBatchHash.Hex(), "number of chunks", numChunks, "err", err) @@ -347,11 +346,11 @@ func (o *Batch) UpdateProvingStatus(ctx context.Context, hash string, status typ switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now() + updateFields["prover_assigned_at"] = time.Now().UTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now() + updateFields["proved_at"] = time.Now().UTC() } db := o.db @@ -375,9 +374,9 @@ func (o *Batch) UpdateRollupStatus(ctx context.Context, hash string, status type switch status { case types.RollupCommitted: - updateFields["committed_at"] = utils.NowUTC() + updateFields["committed_at"] = time.Now().UTC() case types.RollupFinalized: - updateFields["finalized_at"] = utils.NowUTC() + updateFields["finalized_at"] = time.Now().UTC() } db := o.db @@ -400,7 +399,7 @@ func (o *Batch) UpdateCommitTxHashAndRollupStatus(ctx context.Context, hash stri updateFields["commit_tx_hash"] = commitTxHash updateFields["rollup_status"] = int(status) if status == types.RollupCommitted { - updateFields["committed_at"] = utils.NowUTC() + updateFields["committed_at"] = time.Now().UTC() } db := o.db.WithContext(ctx) @@ -419,7 +418,7 @@ func (o *Batch) UpdateFinalizeTxHashAndRollupStatus(ctx context.Context, hash st updateFields["finalize_tx_hash"] = finalizeTxHash updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now() + updateFields["finalized_at"] = time.Now().UTC() } db := o.db.WithContext(ctx) @@ -478,11 +477,11 @@ func (o *Batch) UpdateProvingStatusByBundleHash(ctx context.Context, bundleHash switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now() + updateFields["prover_assigned_at"] = time.Now().UTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now() + updateFields["proved_at"] = time.Now().UTC() } db := o.db @@ -507,7 +506,7 @@ func (o *Batch) UpdateFinalizeTxHashAndRollupStatusByBundleHash(ctx context.Cont switch status { case types.RollupFinalized: - updateFields["finalized_at"] = utils.NowUTC() + updateFields["finalized_at"] = time.Now().UTC() } db := o.db diff --git a/rollup/internal/orm/bundle.go b/rollup/internal/orm/bundle.go index 80825dde23..ac290f4221 100644 --- a/rollup/internal/orm/bundle.go +++ b/rollup/internal/orm/bundle.go @@ -15,7 +15,6 @@ import ( "scroll-tech/common/types" "scroll-tech/common/types/message" - "scroll-tech/common/utils" ) // Bundle represents a bundle of batches. @@ -194,7 +193,7 @@ func (o *Bundle) UpdateFinalizeTxHashAndRollupStatus(ctx context.Context, hash s updateFields["finalize_tx_hash"] = finalizeTxHash updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now() + updateFields["finalized_at"] = time.Now().UTC() } db := o.db @@ -218,7 +217,7 @@ func (o *Bundle) UpdateProvingStatus(ctx context.Context, hash string, status ty switch status { case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now() + updateFields["proved_at"] = time.Now().UTC() } db := o.db @@ -241,7 +240,7 @@ func (o *Bundle) UpdateRollupStatus(ctx context.Context, hash string, status typ updateFields := make(map[string]interface{}) updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now() + updateFields["finalized_at"] = time.Now().UTC() } db := o.db.WithContext(ctx) @@ -271,7 +270,7 @@ func (o *Bundle) UpdateProofAndProvingStatusByHash(ctx context.Context, hash str updateFields["proof"] = proofBytes updateFields["proving_status"] = provingStatus updateFields["proof_time_sec"] = proofTimeSec - updateFields["proved_at"] = utils.NowUTC() + updateFields["proved_at"] = time.Now().UTC() db = db.WithContext(ctx) db = db.Model(&Bundle{}) diff --git a/rollup/internal/orm/chunk.go b/rollup/internal/orm/chunk.go index 893290f757..3be15335f4 100644 --- a/rollup/internal/orm/chunk.go +++ b/rollup/internal/orm/chunk.go @@ -261,11 +261,11 @@ func (o *Chunk) UpdateProvingStatus(ctx context.Context, hash string, status typ switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now() + updateFields["prover_assigned_at"] = time.Now().UTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now() + updateFields["proved_at"] = time.Now().UTC() } db := o.db @@ -289,11 +289,11 @@ func (o *Chunk) UpdateProvingStatusByBatchHash(ctx context.Context, batchHash st switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now() + updateFields["prover_assigned_at"] = time.Now().UTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now() + updateFields["proved_at"] = time.Now().UTC() } db := o.db From 5993aa5f6952f5c7a83686b1ecbc6f6383f85d7a Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Fri, 27 Dec 2024 18:02:17 +0800 Subject: [PATCH 2/3] use utils.NowUTC() --- rollup/internal/orm/batch.go | 25 +++++++++++++------------ rollup/internal/orm/bundle.go | 9 +++++---- rollup/internal/orm/chunk.go | 15 ++++++++------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/rollup/internal/orm/batch.go b/rollup/internal/orm/batch.go index 54654822a7..d965015eb0 100644 --- a/rollup/internal/orm/batch.go +++ b/rollup/internal/orm/batch.go @@ -13,8 +13,9 @@ import ( "scroll-tech/common/types" "scroll-tech/common/types/message" + "scroll-tech/common/utils" - "scroll-tech/rollup/internal/utils" + rutils "scroll-tech/rollup/internal/utils" ) // Batch represents a batch of chunks. @@ -249,7 +250,7 @@ func (o *Batch) GetBatchByIndex(ctx context.Context, index uint64) (*Batch, erro } // InsertBatch inserts a new batch into the database. -func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVersion encoding.CodecVersion, metrics utils.BatchMetrics, dbTX ...*gorm.DB) (*Batch, error) { +func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVersion encoding.CodecVersion, metrics rutils.BatchMetrics, dbTX ...*gorm.DB) (*Batch, error) { if batch == nil { return nil, errors.New("invalid args: batch is nil") } @@ -270,7 +271,7 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, codecVer startChunkIndex = parentBatch.EndChunkIndex + 1 } - batchMeta, err := utils.GetBatchMetadata(batch, codecVersion) + batchMeta, err := rutils.GetBatchMetadata(batch, codecVersion) if err != nil { log.Error("failed to get batch metadata", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore, "parent hash", batch.ParentBatchHash.Hex(), "number of chunks", numChunks, "err", err) @@ -346,11 +347,11 @@ func (o *Batch) UpdateProvingStatus(ctx context.Context, hash string, status typ switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now().UTC() + updateFields["prover_assigned_at"] = utils.NowUTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() } db := o.db @@ -374,9 +375,9 @@ func (o *Batch) UpdateRollupStatus(ctx context.Context, hash string, status type switch status { case types.RollupCommitted: - updateFields["committed_at"] = time.Now().UTC() + updateFields["committed_at"] = utils.NowUTC() case types.RollupFinalized: - updateFields["finalized_at"] = time.Now().UTC() + updateFields["finalized_at"] = utils.NowUTC() } db := o.db @@ -399,7 +400,7 @@ func (o *Batch) UpdateCommitTxHashAndRollupStatus(ctx context.Context, hash stri updateFields["commit_tx_hash"] = commitTxHash updateFields["rollup_status"] = int(status) if status == types.RollupCommitted { - updateFields["committed_at"] = time.Now().UTC() + updateFields["committed_at"] = utils.NowUTC() } db := o.db.WithContext(ctx) @@ -418,7 +419,7 @@ func (o *Batch) UpdateFinalizeTxHashAndRollupStatus(ctx context.Context, hash st updateFields["finalize_tx_hash"] = finalizeTxHash updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now().UTC() + updateFields["finalized_at"] = utils.NowUTC() } db := o.db.WithContext(ctx) @@ -477,11 +478,11 @@ func (o *Batch) UpdateProvingStatusByBundleHash(ctx context.Context, bundleHash switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now().UTC() + updateFields["prover_assigned_at"] = utils.NowUTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() } db := o.db @@ -506,7 +507,7 @@ func (o *Batch) UpdateFinalizeTxHashAndRollupStatusByBundleHash(ctx context.Cont switch status { case types.RollupFinalized: - updateFields["finalized_at"] = time.Now().UTC() + updateFields["finalized_at"] = utils.NowUTC() } db := o.db diff --git a/rollup/internal/orm/bundle.go b/rollup/internal/orm/bundle.go index ac290f4221..54f7cf9fd3 100644 --- a/rollup/internal/orm/bundle.go +++ b/rollup/internal/orm/bundle.go @@ -15,6 +15,7 @@ import ( "scroll-tech/common/types" "scroll-tech/common/types/message" + "scroll-tech/common/utils" ) // Bundle represents a bundle of batches. @@ -193,7 +194,7 @@ func (o *Bundle) UpdateFinalizeTxHashAndRollupStatus(ctx context.Context, hash s updateFields["finalize_tx_hash"] = finalizeTxHash updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now().UTC() + updateFields["finalized_at"] = utils.NowUTC() } db := o.db @@ -217,7 +218,7 @@ func (o *Bundle) UpdateProvingStatus(ctx context.Context, hash string, status ty switch status { case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() } db := o.db @@ -240,7 +241,7 @@ func (o *Bundle) UpdateRollupStatus(ctx context.Context, hash string, status typ updateFields := make(map[string]interface{}) updateFields["rollup_status"] = int(status) if status == types.RollupFinalized { - updateFields["finalized_at"] = time.Now().UTC() + updateFields["finalized_at"] = utils.NowUTC() } db := o.db.WithContext(ctx) @@ -270,7 +271,7 @@ func (o *Bundle) UpdateProofAndProvingStatusByHash(ctx context.Context, hash str updateFields["proof"] = proofBytes updateFields["proving_status"] = provingStatus updateFields["proof_time_sec"] = proofTimeSec - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() db = db.WithContext(ctx) db = db.Model(&Bundle{}) diff --git a/rollup/internal/orm/chunk.go b/rollup/internal/orm/chunk.go index 3be15335f4..d195fbbb44 100644 --- a/rollup/internal/orm/chunk.go +++ b/rollup/internal/orm/chunk.go @@ -11,8 +11,9 @@ import ( "gorm.io/gorm" "scroll-tech/common/types" + "scroll-tech/common/utils" - "scroll-tech/rollup/internal/utils" + rutils "scroll-tech/rollup/internal/utils" ) // Chunk represents a chunk of blocks in the database. @@ -177,7 +178,7 @@ func (o *Chunk) GetChunksByBatchHash(ctx context.Context, batchHash string) ([]* } // InsertChunk inserts a new chunk into the database. -func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, codecVersion encoding.CodecVersion, metrics utils.ChunkMetrics, dbTX ...*gorm.DB) (*Chunk, error) { +func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, codecVersion encoding.CodecVersion, metrics rutils.ChunkMetrics, dbTX ...*gorm.DB) (*Chunk, error) { if chunk == nil || len(chunk.Blocks) == 0 { return nil, errors.New("invalid args") } @@ -202,7 +203,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, codecVer parentChunkStateRoot = parentChunk.StateRoot } - chunkHash, err := utils.GetChunkHash(chunk, totalL1MessagePoppedBefore, codecVersion) + chunkHash, err := rutils.GetChunkHash(chunk, totalL1MessagePoppedBefore, codecVersion) if err != nil { log.Error("failed to get chunk hash", "err", err) return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err) @@ -261,11 +262,11 @@ func (o *Chunk) UpdateProvingStatus(ctx context.Context, hash string, status typ switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now().UTC() + updateFields["prover_assigned_at"] = utils.NowUTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() } db := o.db @@ -289,11 +290,11 @@ func (o *Chunk) UpdateProvingStatusByBatchHash(ctx context.Context, batchHash st switch status { case types.ProvingTaskAssigned: - updateFields["prover_assigned_at"] = time.Now().UTC() + updateFields["prover_assigned_at"] = utils.NowUTC() case types.ProvingTaskUnassigned: updateFields["prover_assigned_at"] = nil case types.ProvingTaskVerified: - updateFields["proved_at"] = time.Now().UTC() + updateFields["proved_at"] = utils.NowUTC() } db := o.db From b01aadea1d3eaf5eb3e0f6c0d2bddc7f6dc4b5f4 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Fri, 27 Dec 2024 18:14:42 +0800 Subject: [PATCH 3/3] add bundle index --- rollup/internal/controller/relayer/l2_relayer.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rollup/internal/controller/relayer/l2_relayer.go b/rollup/internal/controller/relayer/l2_relayer.go index 772015644b..76030ea0aa 100644 --- a/rollup/internal/controller/relayer/l2_relayer.go +++ b/rollup/internal/controller/relayer/l2_relayer.go @@ -529,7 +529,7 @@ func (r *Layer2Relayer) ProcessPendingBundles() { } case types.ProvingTaskVerified: - log.Info("Start to roll up zk proof", "bundle hash", bundle.Hash) + log.Info("Start to roll up zk proof", "index", bundle.Index, "bundle hash", bundle.Hash) r.metrics.rollupL2RelayerProcessPendingBundlesFinalizedTotal.Inc() if err := r.finalizeBundle(bundle, true); err != nil { log.Error("failed to finalize bundle with proof", "bundle index", bundle.Index, "start batch index", bundle.StartBatchIndex, "end batch index", bundle.EndBatchIndex, "err", err) @@ -638,12 +638,12 @@ func (r *Layer2Relayer) finalizeBundle(bundle *orm.Bundle, withProof bool) error // Updating rollup status in database. err = r.db.Transaction(func(dbTX *gorm.DB) error { if err = r.batchOrm.UpdateFinalizeTxHashAndRollupStatusByBundleHash(r.ctx, bundle.Hash, txHash.String(), types.RollupFinalizing, dbTX); err != nil { - log.Warn("UpdateFinalizeTxHashAndRollupStatusByBundleHash failed", "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) + log.Warn("UpdateFinalizeTxHashAndRollupStatusByBundleHash failed", "index", bundle.Index, "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) return err } if err = r.bundleOrm.UpdateFinalizeTxHashAndRollupStatus(r.ctx, bundle.Hash, txHash.String(), types.RollupFinalizing, dbTX); err != nil { - log.Warn("UpdateFinalizeTxHashAndRollupStatus failed", "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) + log.Warn("UpdateFinalizeTxHashAndRollupStatus failed", "index", bundle.Index, "bundle hash", bundle.Hash, "tx hash", txHash.String(), "err", err) return err }