Skip to content

Commit

Permalink
Normalize frozenblocks progress for whenever we delete chaindata (#10476
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Giulio2002 authored May 26, 2024
1 parent e83cd70 commit 4098ed5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 10 additions & 1 deletion eth/stagedsync/stage_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,11 @@ func FillDBFromSnapshots(logPrefix string, ctx context.Context, tx kv.RwTx, dirs
if err := blockReader.HeadersRange(ctx, func(header *types.Header) error {
blockNum, blockHash := header.Number.Uint64(), header.Hash()
td.Add(td, header.Difficulty)

// What can happen if chaindata is deleted is that maybe header.seg progress is lower or higher than
// body.seg progress. In this case we need to skip the header, and "normalize" the progress to keep them in sync.
if blockNum > blocksAvailable {
return nil // This can actually happen as FrozenBlocks() is SegmentIdMax() and not the last .seg
}
if err := rawdb.WriteTd(tx, blockHash, blockNum, td); err != nil {
return err
}
Expand Down Expand Up @@ -393,6 +397,11 @@ func FillDBFromSnapshots(logPrefix string, ctx context.Context, tx kv.RwTx, dirs
panic(baseTxNum + txAmount) //uint-underflow
}
maxTxNum := baseTxNum + txAmount - 1
// What can happen if chaindata is deleted is that maybe header.seg progress is lower or higher than
// body.seg progress. In this case we need to skip the header, and "normalize" the progress to keep them in sync.
if blockNum > blocksAvailable {
return nil // This can actually happen as FrozenBlocks() is SegmentIdMax() and not the last .seg
}

if err := rawdbv3.TxNums.Append(tx, blockNum, maxTxNum); err != nil {
return fmt.Errorf("%w. blockNum=%d, maxTxNum=%d", err, blockNum, maxTxNum)
Expand Down
9 changes: 7 additions & 2 deletions turbo/execution/eth1/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original

if err := stages2.ProcessFrozenBlocks(ctx, e.db, e.blockReader, e.executionPipeline); err != nil {
sendForkchoiceErrorWithoutWaiting(outcomeCh, err)
e.logger.Warn("ProcessFrozenBlocks", "error", err)
return
}

Expand Down Expand Up @@ -196,7 +197,7 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
log.Info("[sync] limited big jump", "from", finishProgressBefore, "amount", uint64(e.syncCfg.LoopBlockLimit))
}

canonicalHash, err := e.blockReader.CanonicalHash(ctx, tx, fcuHeader.Number.Uint64())
canonicalHash, err := rawdb.ReadCanonicalHash(tx, fcuHeader.Number.Uint64())
if err != nil {
sendForkchoiceErrorWithoutWaiting(outcomeCh, err)
return
Expand Down Expand Up @@ -334,7 +335,6 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
return
}
}

if len(newCanonicals) > 0 {
if err := rawdbv3.TxNums.Truncate(tx, newCanonicals[0].number); err != nil {
sendForkchoiceErrorWithoutWaiting(outcomeCh, err)
Expand All @@ -347,6 +347,10 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
}
}
if isDomainAheadOfBlocks(tx) {
if err := tx.Commit(); err != nil {
sendForkchoiceErrorWithoutWaiting(outcomeCh, err)
return
}
sendForkchoiceReceiptWithoutWaiting(outcomeCh, &execution.ForkChoiceReceipt{
LatestValidHash: gointerfaces.ConvertHashToH256(common.Hash{}),
Status: execution.ExecutionStatus_TooFarAway,
Expand Down Expand Up @@ -383,6 +387,7 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
initialCycle := limitedBigJump
if _, err := e.executionPipeline.Run(e.db, wrap.TxContainer{Tx: tx}, initialCycle); err != nil {
err = fmt.Errorf("updateForkChoice: %w", err)
e.logger.Warn("Cannot update chain head", "hash", blockHash, "err", err)
sendForkchoiceErrorWithoutWaiting(outcomeCh, err)
return
}
Expand Down

0 comments on commit 4098ed5

Please sign in to comment.