handle case of unreachable block in `is_optimstic` helper (#6124)

* handle case of unreachable block in `is_optimstic` helper

When a non-canonical block is still in the DB, it can be accessed via
`BlockId`, but `BlockRef` may be unavailable if the block was not
properly cleaned when it got orphaned. Report it as optimistic.

* `template` -> `func`
This commit is contained in:
Etan Kissling 2024-03-22 23:50:21 +01:00 committed by GitHub
parent 2d9586a5a8
commit 33e34ee8bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 2 deletions

View File

@ -1968,12 +1968,16 @@ proc pruneBlocksDAG(dag: ChainDAGRef) =
dagPruneDur = Moment.now() - startTick dagPruneDur = Moment.now() - startTick
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/sync/optimistic.md#helpers # https://github.com/ethereum/consensus-specs/blob/v1.4.0/sync/optimistic.md#helpers
template is_optimistic*(dag: ChainDAGRef, bid: BlockId): bool = func is_optimistic*(dag: ChainDAGRef, bid: BlockId): bool =
let blck = let blck =
if bid.slot <= dag.finalizedHead.slot: if bid.slot <= dag.finalizedHead.slot:
dag.finalizedHead.blck dag.finalizedHead.blck
else: else:
dag.getBlockRef(bid.root).expect("Non-finalized block is known") dag.getBlockRef(bid.root).valueOr:
# The block is part of the DB but is not reachable via `BlockRef`;
# it could have been orphaned or the DB is slightly inconsistent.
# Report it as optimistic until it becomes reachable or gets deleted
return true
not blck.executionValid not blck.executionValid
proc markBlockVerified*(dag: ChainDAGRef, blck: BlockRef) = proc markBlockVerified*(dag: ChainDAGRef, blck: BlockRef) =