From 33e34ee8bdaff625276b5826ba366edda7f7280e Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Fri, 22 Mar 2024 23:50:21 +0100 Subject: [PATCH] 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` --- beacon_chain/consensus_object_pools/blockchain_dag.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/beacon_chain/consensus_object_pools/blockchain_dag.nim b/beacon_chain/consensus_object_pools/blockchain_dag.nim index 06cb12544..37b02d78c 100644 --- a/beacon_chain/consensus_object_pools/blockchain_dag.nim +++ b/beacon_chain/consensus_object_pools/blockchain_dag.nim @@ -1968,12 +1968,16 @@ proc pruneBlocksDAG(dag: ChainDAGRef) = dagPruneDur = Moment.now() - startTick # 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 = if bid.slot <= dag.finalizedHead.slot: dag.finalizedHead.blck 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 proc markBlockVerified*(dag: ChainDAGRef, blck: BlockRef) =