2020-05-19 14:18:07 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2020-05-19 14:18:07 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2020-11-27 22:16:13 +00:00
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
import
|
2021-12-06 09:49:01 +00:00
|
|
|
std/[tables],
|
2020-05-19 14:18:07 +00:00
|
|
|
stew/bitops2,
|
2023-02-27 06:10:22 +00:00
|
|
|
../spec/forks
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
export tables, forks
|
2021-12-06 09:49:01 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
MaxMissingItems = 1024
|
2022-01-26 12:20:08 +00:00
|
|
|
## Arbitrary
|
|
|
|
MaxOrphans = SLOTS_PER_EPOCH * 3
|
|
|
|
## Enough for finalization in an alternative fork
|
|
|
|
MaxUnviables = 16 * 1024
|
|
|
|
## About a day of blocks - most likely not needed but it's quite cheap..
|
2021-12-06 09:49:01 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
MissingBlock* = object
|
|
|
|
tries*: int
|
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
FetchRecord* = object
|
|
|
|
root*: Eth2Digest
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
Quarantine* = object
|
|
|
|
## Keeps track of unvalidated blocks coming from the network
|
|
|
|
## and that cannot yet be added to the chain
|
|
|
|
##
|
|
|
|
## This only stores blocks that cannot be linked to the
|
|
|
|
## ChainDAGRef DAG due to missing ancestor(s).
|
|
|
|
##
|
|
|
|
## Trivially invalid blocks may be dropped before reaching this stage.
|
|
|
|
|
2023-02-27 06:10:22 +00:00
|
|
|
orphans*: Table[(Eth2Digest, ValidatorSig), ForkedSignedBeaconBlock]
|
2022-01-26 12:20:08 +00:00
|
|
|
## Blocks that we don't have a parent for - when we resolve the parent, we
|
|
|
|
## can proceed to resolving the block as well - we index this by root and
|
|
|
|
## signature such that a block with invalid signature won't cause a block
|
|
|
|
## with a valid signature to be dropped
|
|
|
|
|
|
|
|
unviable*: OrderedTable[Eth2Digest, tuple[]]
|
|
|
|
## Unviable blocks are those that come from a history that does not
|
|
|
|
## include the finalized checkpoint we're currently following, and can
|
|
|
|
## therefore never be included in our canonical chain - we keep their hash
|
|
|
|
## around so that we can avoid cluttering the orphans table with their
|
|
|
|
## descendants - the ChainDAG only keeps track blocks that make up the
|
|
|
|
## valid and canonical history.
|
|
|
|
##
|
|
|
|
## Entries are evicted in FIFO order - recent entries are more likely to
|
|
|
|
## appear again in attestations and blocks - however, the unviable block
|
|
|
|
## table is not a complete directory of all unviable blocks circulating -
|
|
|
|
## only those we have observed, been able to verify as unviable and fit
|
|
|
|
## in this cache.
|
|
|
|
|
|
|
|
missing*: Table[Eth2Digest, MissingBlock]
|
|
|
|
## Roots of blocks that we would like to have (either parent_root of
|
|
|
|
## unresolved blocks or block roots of attestations)
|
2020-07-16 13:16:51 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func init*(T: type Quarantine): T =
|
|
|
|
T()
|
2021-01-25 18:45:48 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func checkMissing*(quarantine: var Quarantine): seq[FetchRecord] =
|
2020-05-19 14:18:07 +00:00
|
|
|
## Return a list of blocks that we should try to resolve from other client -
|
|
|
|
## to be called periodically but not too often (once per slot?)
|
|
|
|
var done: seq[Eth2Digest]
|
|
|
|
|
|
|
|
for k, v in quarantine.missing.mpairs():
|
|
|
|
if v.tries > 8:
|
|
|
|
done.add(k)
|
|
|
|
else:
|
|
|
|
inc v.tries
|
|
|
|
|
|
|
|
for k in done:
|
|
|
|
quarantine.missing.del(k)
|
|
|
|
|
|
|
|
# simple (simplistic?) exponential backoff for retries..
|
2022-05-10 10:03:40 +00:00
|
|
|
for k, v in quarantine.missing:
|
2020-05-19 14:18:07 +00:00
|
|
|
if countOnes(v.tries.uint64) == 1:
|
2020-06-18 10:03:36 +00:00
|
|
|
result.add(FetchRecord(root: k))
|
|
|
|
|
2020-09-07 15:04:33 +00:00
|
|
|
# TODO stew/sequtils2
|
2020-09-04 06:39:46 +00:00
|
|
|
template anyIt(s, pred: untyped): bool =
|
2023-02-13 11:03:38 +00:00
|
|
|
# https://github.com/nim-lang/Nim/blob/v1.6.10/lib/pure/collections/sequtils.nim#L753-L775
|
2020-09-04 06:39:46 +00:00
|
|
|
# without the items(...)
|
|
|
|
var result = false
|
|
|
|
for it {.inject.} in s:
|
|
|
|
if pred:
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
result
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func addMissing*(quarantine: var Quarantine, root: Eth2Digest) =
|
2020-08-05 14:19:43 +00:00
|
|
|
## Schedule the download a the given block
|
2021-12-06 09:49:01 +00:00
|
|
|
if quarantine.missing.len >= MaxMissingItems:
|
|
|
|
return
|
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
if root in quarantine.unviable:
|
|
|
|
# Won't get anywhere with this block
|
|
|
|
return
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
# It's not really missing if we're keeping it in the quarantine
|
2022-01-26 12:20:08 +00:00
|
|
|
if anyIt(quarantine.orphans.keys, it[0] == root):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Add if it's not there, but don't update missing counter
|
|
|
|
discard quarantine.missing.hasKeyOrPut(root, MissingBlock())
|
2020-06-23 09:29:08 +00:00
|
|
|
|
2020-09-04 06:39:46 +00:00
|
|
|
func removeOrphan*(
|
2021-12-06 09:49:01 +00:00
|
|
|
quarantine: var Quarantine, signedBlock: ForkySignedBeaconBlock) =
|
|
|
|
quarantine.orphans.del((signedBlock.root, signedBlock.signature))
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-06-23 14:43:18 +00:00
|
|
|
func isViableOrphan(
|
2022-01-26 12:20:08 +00:00
|
|
|
finalizedSlot: Slot, signedBlock: ForkedSignedBeaconBlock): bool =
|
2020-11-16 19:15:43 +00:00
|
|
|
# The orphan must be newer than the finalization point so that its parent
|
|
|
|
# either is the finalized block or more recent
|
2022-01-26 12:20:08 +00:00
|
|
|
let
|
|
|
|
slot = getForkedBlockField(signedBlock, slot)
|
|
|
|
slot > finalizedSlot
|
|
|
|
|
|
|
|
func cleanupUnviable(quarantine: var Quarantine) =
|
|
|
|
while quarantine.unviable.len() >= MaxUnviables:
|
|
|
|
var toDel: Eth2Digest
|
|
|
|
for k in quarantine.unviable.keys():
|
|
|
|
toDel = k
|
|
|
|
break # Cannot modify while for-looping
|
|
|
|
quarantine.unviable.del(toDel)
|
|
|
|
|
|
|
|
func addUnviable*(quarantine: var Quarantine, root: Eth2Digest) =
|
|
|
|
if root in quarantine.unviable:
|
|
|
|
return
|
2020-11-16 19:15:43 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
quarantine.cleanupUnviable()
|
|
|
|
|
|
|
|
# Remove the tree of orphans whose ancestor is unviable - they are now also
|
|
|
|
# unviable! This helps avoiding junk in the quarantine, because we don't keep
|
|
|
|
# unviable parents in the DAG and there's no way to tell an orphan from an
|
|
|
|
# unviable block without the parent.
|
|
|
|
var
|
|
|
|
toRemove: seq[(Eth2Digest, ValidatorSig)] # Can't modify while iterating
|
|
|
|
toCheck = @[root]
|
|
|
|
while toCheck.len > 0:
|
|
|
|
let root = toCheck.pop()
|
|
|
|
for k, v in quarantine.orphans.mpairs():
|
2023-02-27 06:10:22 +00:00
|
|
|
if getForkedBlockField(v, parent_root) == root:
|
2022-01-26 12:20:08 +00:00
|
|
|
toCheck.add(k[0])
|
|
|
|
toRemove.add(k)
|
|
|
|
elif k[0] == root:
|
|
|
|
toRemove.add(k)
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
for k in toRemove:
|
|
|
|
quarantine.orphans.del k
|
2022-06-09 09:09:38 +00:00
|
|
|
quarantine.unviable[k[0]] = ()
|
2022-01-26 12:20:08 +00:00
|
|
|
|
|
|
|
toRemove.setLen(0)
|
|
|
|
|
2022-06-09 09:09:38 +00:00
|
|
|
quarantine.unviable[root] = ()
|
2022-01-26 12:20:08 +00:00
|
|
|
|
|
|
|
func cleanupOrphans(quarantine: var Quarantine, finalizedSlot: Slot) =
|
|
|
|
var toDel: seq[(Eth2Digest, ValidatorSig)]
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for k, v in quarantine.orphans:
|
2023-02-27 06:10:22 +00:00
|
|
|
if not isViableOrphan(finalizedSlot, v):
|
2022-01-26 12:20:08 +00:00
|
|
|
toDel.add k
|
2021-06-23 14:43:18 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
for k in toDel:
|
|
|
|
quarantine.addUnviable k[0]
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2022-01-28 10:59:55 +00:00
|
|
|
func clearAfterReorg*(quarantine: var Quarantine) =
|
|
|
|
## Clear missing and orphans to start with a fresh slate in case of a reorg
|
|
|
|
## Unviables remain unviable and are not cleared.
|
|
|
|
quarantine.missing.reset()
|
|
|
|
quarantine.orphans.reset()
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2021-06-23 14:43:18 +00:00
|
|
|
# Typically, blocks will arrive in mostly topological order, with some
|
|
|
|
# out-of-order block pairs. Therefore, it is unhelpful to use either a
|
|
|
|
# FIFO or LIFO discpline, and since by definition each block gets used
|
|
|
|
# either 0 or 1 times it's not a cache either. Instead, stop accepting
|
2022-01-26 12:20:08 +00:00
|
|
|
# new blocks, and rely on syncing to cache up again if necessary.
|
2021-06-23 14:43:18 +00:00
|
|
|
#
|
|
|
|
# For typical use cases, this need not be large, as they're two or three
|
|
|
|
# blocks arriving out of order due to variable network delays. As blocks
|
|
|
|
# for future slots are rejected before reaching quarantine, this usually
|
|
|
|
# will be a block for the last couple of slots for which the parent is a
|
|
|
|
# likely imminent arrival.
|
2022-01-26 12:20:08 +00:00
|
|
|
func addOrphan*(
|
|
|
|
quarantine: var Quarantine, finalizedSlot: Slot,
|
2023-02-27 06:10:22 +00:00
|
|
|
signedBlock: ForkedSignedBeaconBlock): bool =
|
2021-06-23 14:43:18 +00:00
|
|
|
## Adds block to quarantine's `orphans` and `missing` lists.
|
2022-01-26 12:20:08 +00:00
|
|
|
if not isViableOrphan(finalizedSlot, signedBlock):
|
|
|
|
quarantine.addUnviable(signedBlock.root)
|
2020-11-16 19:15:43 +00:00
|
|
|
return false
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
quarantine.cleanupOrphans(finalizedSlot)
|
|
|
|
|
|
|
|
let parent_root = getForkedBlockField(signedBlock, parent_root)
|
|
|
|
|
|
|
|
if parent_root in quarantine.unviable:
|
2022-06-09 09:09:38 +00:00
|
|
|
quarantine.unviable[signedBlock.root] = ()
|
2022-01-26 12:20:08 +00:00
|
|
|
return true
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2020-11-16 19:15:43 +00:00
|
|
|
# Even if the quarantine is full, we need to schedule its parent for
|
|
|
|
# downloading or we'll never get to the bottom of things
|
2022-01-26 12:20:08 +00:00
|
|
|
quarantine.addMissing(parent_root)
|
2020-11-16 19:15:43 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
if quarantine.orphans.lenu64 >= MaxOrphans:
|
2020-08-31 09:00:38 +00:00
|
|
|
return false
|
|
|
|
|
2023-02-27 06:10:22 +00:00
|
|
|
quarantine.orphans[(signedBlock.root, signedBlock.signature)] = signedBlock
|
2020-08-05 14:19:43 +00:00
|
|
|
quarantine.missing.del(signedBlock.root)
|
2020-06-18 10:03:36 +00:00
|
|
|
|
2020-08-31 09:00:38 +00:00
|
|
|
true
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
iterator pop*(quarantine: var Quarantine, root: Eth2Digest):
|
2023-02-27 06:10:22 +00:00
|
|
|
ForkedSignedBeaconBlock =
|
2021-12-06 09:49:01 +00:00
|
|
|
# Pop orphans whose parent is the block identified by `root`
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
var toRemove: seq[(Eth2Digest, ValidatorSig)]
|
|
|
|
defer: # Run even if iterator is not carried to termination
|
|
|
|
for k in toRemove:
|
|
|
|
quarantine.orphans.del k
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2022-01-26 12:20:08 +00:00
|
|
|
for k, v in quarantine.orphans.mpairs():
|
2023-02-27 06:10:22 +00:00
|
|
|
if getForkedBlockField(v, parent_root) == root:
|
2021-12-06 09:49:01 +00:00
|
|
|
toRemove.add(k)
|
|
|
|
yield v
|