use fixed max depth for `BlockRef` (#6070)

In `block_dag` there is a max depth of 100 years configured to detect
internal inconsistencies, e.g., circular references. As `BlockRef` was
changed long ago to only reflect the non-finalized chain segment, the
theoretically supported max depth can be reduced and simplified.
This commit is contained in:
Etan Kissling 2024-03-13 13:01:51 +01:00 committed by GitHub
parent efb1971d7e
commit 4e2ffca44a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 5 deletions

View File

@ -102,17 +102,20 @@ func parentOrSlot*(bs: BlockSlot): BlockSlot =
else:
BlockSlot(blck: bs.blck, slot: bs.slot - 1)
# This is used to detect internal inconsistencies, e.g., circular references.
# Note that `BlockRef` is only used for the non-finalized chain segment.
const defaultMaxDepth = 1'i64 shl 24 # More than enough for years
func getDepth*(a, b: BlockRef): tuple[ancestor: bool, depth: int] =
var b = b
var depth = 0
const maxDepth = (100'i64 * 365 * 24 * 60 * 60 div SECONDS_PER_SLOT.int)
while true:
if a == b:
return (true, depth)
# for now, use an assert for block chain length since a chain this long
# indicates a circular reference here..
doAssert depth < maxDepth
doAssert depth < defaultMaxDepth
depth += 1
if a.slot >= b.slot or b.parent.isNil:
@ -132,9 +135,8 @@ func link*(parent, child: BlockRef) =
child.parent = parent
func get_ancestor*(blck: BlockRef, slot: Slot,
maxDepth = 100'i64 * 365 * 24 * 60 * 60 div SECONDS_PER_SLOT.int):
BlockRef =
func get_ancestor*(
blck: BlockRef, slot: Slot, maxDepth = defaultMaxDepth): BlockRef =
## https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/phase0/fork-choice.md#get_ancestor
## Return the most recent block as of the time at `slot` that not more recent
## than `blck` itself