Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis committed Nov 21, 2024
1 parent a6f914a commit 0123502
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
52 changes: 24 additions & 28 deletions rollup/internal/controller/relayer/full_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
log.Info("Restoring full previous state with", "L1 block height", f.cfg.RecoveryConfig.L1BlockHeight, "latest finalized batch", f.cfg.RecoveryConfig.LatestFinalizedBatch)

// 1. Get latest finalized batch stored in DB
latestDBBatch, err := f.batchORM.GetLatestBatch(context.Background())
latestDBBatch, err := f.batchORM.GetLatestBatch(f.ctx)
if err != nil {
return fmt.Errorf("failed to get latest batch from DB: %w", err)
}

// TODO:
// 1. what if it is a fresh start? -> latest batch is nil
//latestDBBatch.CommitTxHash

log.Info("Latest finalized batch in DB", "batch", latestDBBatch.Index, "hash", latestDBBatch.Hash)

// 2. Get latest finalized L1 block
Expand All @@ -84,23 +80,21 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
log.Info("Latest finalized L1 block number", "latest finalized L1 block", latestFinalizedL1Block)

// 3. Get latest finalized batch from contract (at latest finalized L1 block)
latestFinalizedBatch, err := f.l1Reader.LatestFinalizedBatch(latestFinalizedL1Block)
latestFinalizedBatchContract, err := f.l1Reader.LatestFinalizedBatch(latestFinalizedL1Block)
if err != nil {
return fmt.Errorf("failed to get latest finalized batch: %w", err)
}

// TODO: remove this, just for debugging
latestFinalizedBatch = 82310
log.Info("Latest finalized batch from L1 contract", "latest finalized batch", latestFinalizedBatch, "at latest finalized L1 block", latestFinalizedL1Block)
log.Info("Latest finalized batch from L1 contract", "latest finalized batch", latestFinalizedBatchContract, "at latest finalized L1 block", latestFinalizedL1Block)

// 4. Get batches one by one from stored in DB to latest finalized batch.
receipt, err := f.l1Client.TransactionReceipt(context.Background(), common.HexToHash(latestDBBatch.CommitTxHash))
receipt, err := f.l1Client.TransactionReceipt(f.ctx, common.HexToHash(latestDBBatch.CommitTxHash))
if err != nil {
return fmt.Errorf("failed to get transaction receipt of latest DB batch finalization transaction: %w", err)
}
fromBlock := receipt.BlockNumber.Uint64()

log.Info("Fetching rollup events from L1", "from block", fromBlock, "to block", latestFinalizedL1Block, "from batch", latestDBBatch.Index, "to batch", latestFinalizedBatch)
log.Info("Fetching rollup events from L1", "from block", fromBlock, "to block", latestFinalizedL1Block, "from batch", latestDBBatch.Index, "to batch", latestFinalizedBatchContract)

commitsHeapMap := common.NewHeapMap[uint64, *l1.CommitBatchEvent](func(event *l1.CommitBatchEvent) uint64 {
return event.BatchIndex().Uint64()
Expand All @@ -114,8 +108,6 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
return true
}

fmt.Println("event", event.Type(), event.BatchIndex().Uint64())

switch event.Type() {
case l1.CommitEventType:
commitEvent := event.(*l1.CommitBatchEvent)
Expand All @@ -142,7 +134,7 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
bundles = append(bundles, bundle)

// Stop fetching rollup events if we reached the latest finalized batch.
if finalizeEvent.BatchIndex().Uint64() >= latestFinalizedBatch {
if finalizeEvent.BatchIndex().Uint64() >= latestFinalizedBatchContract {
return false
}

Expand Down Expand Up @@ -173,7 +165,7 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
var lastBatchInBundle *orm.Batch

for _, batch := range bundle {
dbBatch, err := f.batchORM.GetBatchByIndex(context.Background(), batch.commit.BatchIndex().Uint64())
dbBatch, err := f.batchORM.GetBatchByIndex(f.ctx, batch.commit.BatchIndex().Uint64())
if err != nil {
return fmt.Errorf("failed to get batch by index for bundle generation: %w", err)
}
Expand All @@ -191,19 +183,19 @@ func (f *FullRecovery) RestoreFullPreviousState() error {
}

err = f.db.Transaction(func(dbTX *gorm.DB) error {
newBundle, err := f.bundleORM.InsertBundle(context.Background(), dbBatches, encoding.CodecVersion(lastBatchInBundle.CodecVersion), dbTX)
newBundle, err := f.bundleORM.InsertBundle(f.ctx, dbBatches, encoding.CodecVersion(lastBatchInBundle.CodecVersion), dbTX)
if err != nil {
return fmt.Errorf("failed to insert bundle to DB: %w", err)
}
if err = f.batchORM.UpdateBundleHashInRange(context.Background(), newBundle.StartBatchIndex, newBundle.EndBatchIndex, newBundle.Hash, dbTX); err != nil {
if err = f.batchORM.UpdateBundleHashInRange(f.ctx, newBundle.StartBatchIndex, newBundle.EndBatchIndex, newBundle.Hash, dbTX); err != nil {
return fmt.Errorf("failed to update bundle_hash %s for batches (%d to %d): %w", newBundle.Hash, newBundle.StartBatchIndex, newBundle.EndBatchIndex, err)
}

if err = f.bundleORM.UpdateFinalizeTxHashAndRollupStatus(context.Background(), newBundle.Hash, lastBatchInBundle.FinalizeTxHash, types.RollupFinalized, dbTX); err != nil {
if err = f.bundleORM.UpdateFinalizeTxHashAndRollupStatus(f.ctx, newBundle.Hash, lastBatchInBundle.FinalizeTxHash, types.RollupFinalized, dbTX); err != nil {
return fmt.Errorf("failed to update finalize tx hash and rollup status for bundle %s: %w", newBundle.Hash, err)
}

if err = f.bundleORM.UpdateProvingStatus(context.Background(), newBundle.Hash, types.ProvingTaskVerified, dbTX); err != nil {
if err = f.bundleORM.UpdateProvingStatus(f.ctx, newBundle.Hash, types.ProvingTaskVerified, dbTX); err != nil {
return fmt.Errorf("failed to update proving status for bundle %s: %w", newBundle.Hash, err)
}

Expand Down Expand Up @@ -254,7 +246,7 @@ func (f *FullRecovery) processFinalizedBatch(nextBatch *batchEvents) error {
start := daChunkRawTxs.Blocks[0].Number()
end := daChunkRawTxs.Blocks[len(daChunkRawTxs.Blocks)-1].Number()

blocks, err := f.blockORM.GetL2BlocksInRange(context.Background(), start, end)
blocks, err := f.blockORM.GetL2BlocksInRange(f.ctx, start, end)
if err != nil {
return fmt.Errorf("failed to get L2 blocks in range: %w", err)
}
Expand All @@ -272,21 +264,23 @@ func (f *FullRecovery) processFinalizedBatch(nextBatch *batchEvents) error {
}

err = f.db.Transaction(func(dbTX *gorm.DB) error {
dbChunk, err := f.chunkORM.InsertChunk(context.Background(), &chunk, codec.Version(), *metrics, dbTX)
dbChunk, err := f.chunkORM.InsertChunk(f.ctx, &chunk, codec.Version(), *metrics, dbTX)
if err != nil {
return fmt.Errorf("failed to insert chunk to DB: %w", err)
}
if err := f.blockORM.UpdateChunkHashInRange(context.Background(), dbChunk.StartBlockNumber, dbChunk.EndBlockNumber, dbChunk.Hash, dbTX); err != nil {
if err := f.blockORM.UpdateChunkHashInRange(f.ctx, dbChunk.StartBlockNumber, dbChunk.EndBlockNumber, dbChunk.Hash, dbTX); err != nil {
return fmt.Errorf("failed to update chunk_hash for l2_blocks (chunk hash: %s, start block: %d, end block: %d): %w", dbChunk.Hash, dbChunk.StartBlockNumber, dbChunk.EndBlockNumber, err)
}

if err = f.chunkORM.UpdateProvingStatus(context.Background(), dbChunk.Hash, types.ProvingTaskVerified, dbTX); err != nil {
if err = f.chunkORM.UpdateProvingStatus(f.ctx, dbChunk.Hash, types.ProvingTaskVerified, dbTX); err != nil {
return fmt.Errorf("failed to update proving status for chunk %s: %w", dbChunk.Hash, err)
}

daChunks = append(daChunks, &chunk)
dbChunks = append(dbChunks, dbChunk)

log.Info("Inserted chunk", "index", dbChunk.Index, "hash", dbChunk.Hash, "start block", dbChunk.StartBlockNumber, "end block", dbChunk.EndBlockNumber)

return nil
})
if err != nil {
Expand All @@ -295,7 +289,7 @@ func (f *FullRecovery) processFinalizedBatch(nextBatch *batchEvents) error {
}

// 5.4 Reproduce batch.
dbParentBatch, err := f.batchORM.GetLatestBatch(context.Background())
dbParentBatch, err := f.batchORM.GetLatestBatch(f.ctx)
if err != nil {
return fmt.Errorf("failed to get latest batch from DB: %w", err)
}
Expand All @@ -315,21 +309,23 @@ func (f *FullRecovery) processFinalizedBatch(nextBatch *batchEvents) error {
}

err = f.db.Transaction(func(dbTX *gorm.DB) error {
dbBatch, err := f.batchORM.InsertBatch(context.Background(), &batch, codec.Version(), *metrics, dbTX)
dbBatch, err := f.batchORM.InsertBatch(f.ctx, &batch, codec.Version(), *metrics, dbTX)
if err != nil {
return fmt.Errorf("failed to insert batch to DB: %w", err)
}
if err = f.chunkORM.UpdateBatchHashInRange(context.Background(), dbBatch.StartChunkIndex, dbBatch.EndChunkIndex, dbBatch.Hash, dbTX); err != nil {
if err = f.chunkORM.UpdateBatchHashInRange(f.ctx, dbBatch.StartChunkIndex, dbBatch.EndChunkIndex, dbBatch.Hash, dbTX); err != nil {
return fmt.Errorf("failed to update batch_hash for chunks (batch hash: %s, start chunk: %d, end chunk: %d): %w", dbBatch.Hash, dbBatch.StartChunkIndex, dbBatch.EndChunkIndex, err)
}

if err = f.batchORM.UpdateProvingStatus(context.Background(), dbBatch.Hash, types.ProvingTaskVerified, dbTX); err != nil {
if err = f.batchORM.UpdateProvingStatus(f.ctx, dbBatch.Hash, types.ProvingTaskVerified, dbTX); err != nil {
return fmt.Errorf("failed to update proving status for batch %s: %w", dbBatch.Hash, err)
}
if err = f.batchORM.UpdateRollupStatusCommitAndFinalizeTxHash(context.Background(), dbBatch.Hash, types.RollupFinalized, nextBatch.commit.TxHash().Hex(), nextBatch.finalize.TxHash().Hex(), dbTX); err != nil {
if err = f.batchORM.UpdateRollupStatusCommitAndFinalizeTxHash(f.ctx, dbBatch.Hash, types.RollupFinalized, nextBatch.commit.TxHash().Hex(), nextBatch.finalize.TxHash().Hex(), dbTX); err != nil {
return fmt.Errorf("failed to update rollup status for batch %s: %w", dbBatch.Hash, err)
}

log.Info("Inserted batch", "index", dbBatch.Index, "hash", dbBatch.Hash, "start chunk", dbBatch.StartChunkIndex, "end chunk", dbBatch.EndChunkIndex)

return nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/orm/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (o *Batch) UpdateRollupStatusCommitAndFinalizeTxHash(ctx context.Context, h
updateFields["commit_tx_hash"] = commitTxHash
updateFields["committed_at"] = utils.NowUTC()
updateFields["finalize_tx_hash"] = finalizeTxHash
updateFields["finalized_at"] = time.Now()
updateFields["finalized_at"] = utils.NowUTC()

updateFields["rollup_status"] = int(status)

Expand Down
1 change: 0 additions & 1 deletion rollup/internal/orm/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, codecVer
parentChunkStateRoot = parentChunk.StateRoot
}

fmt.Println("insertChunk", totalL1MessagePoppedBefore, chunkIndex, parentChunkHash)
chunkHash, err := utils.GetChunkHash(chunk, totalL1MessagePoppedBefore, codecVersion)
if err != nil {
log.Error("failed to get chunk hash", "err", err)
Expand Down

0 comments on commit 0123502

Please sign in to comment.