diff --git a/nimbus/beacon/api_handler/api_forkchoice.nim b/nimbus/beacon/api_handler/api_forkchoice.nim index 8db677296..aab5df878 100644 --- a/nimbus/beacon/api_handler/api_forkchoice.nim +++ b/nimbus/beacon/api_handler/api_forkchoice.nim @@ -155,8 +155,7 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef, # probably resyncing. Ignore the update. # See point 2 of fCUV1 specification # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.4/src/engine/paris.md#specification-1 - var canonHash: common.Hash256 - if db.getBlockHash(header.number, canonHash) and canonHash == blockHash: + if ben.chain.isCanonicalAncestor(header.number, blockHash): notice "Ignoring beacon update to old head", blockHash=blockHash.short, blockNumber=header.number diff --git a/nimbus/core/chain/forked_chain.nim b/nimbus/core/chain/forked_chain.nim index 43723b8f9..bd3baa390 100644 --- a/nimbus/core/chain/forked_chain.nim +++ b/nimbus/core/chain/forked_chain.nim @@ -632,3 +632,28 @@ func isCanonical*(c: ForkedChainRef, blockHash: Hash256): bool = if blockHash == prevHash: return true prevHash = item.blk.header.parentHash + +proc isCanonicalAncestor*(c: ForkedChainRef, + blockNumber: BlockNumber, + blockHash: Hash256): bool = + if blockNumber >= c.cursorHeader.number: + return false + + if blockHash == c.cursorHash: + return false + + if c.baseHeader.number < c.cursorHeader.number: + # The current canonical chain in memory is headed by + # cursorHeader + shouldNotKeyError: + var prevHash = c.cursorHeader.parentHash + while prevHash != c.baseHash: + var header = c.blocks[prevHash].blk.header + if prevHash == blockHash and blockNumber == header.number: + return true + prevHash = header.parentHash + + # canonical chain in database should have a marker + # and the marker is block number + var canonHash: common.Hash256 + c.db.getBlockHash(blockNumber, canonHash) and canonHash == blockHash