2020-05-19 14:18:07 +00:00
|
|
|
# beacon_chain
|
2021-06-23 14:43:18 +00:00
|
|
|
# Copyright (c) 2018-2021 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.
|
|
|
|
|
2020-11-27 22:16:13 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
import
|
2021-12-06 09:49:01 +00:00
|
|
|
std/[tables],
|
2020-11-27 22:16:13 +00:00
|
|
|
chronicles,
|
2020-05-19 14:18:07 +00:00
|
|
|
stew/bitops2,
|
2021-11-05 07:34:34 +00:00
|
|
|
../spec/forks,
|
2020-11-27 22:16:13 +00:00
|
|
|
./block_pools_types
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
export tables, forks, block_pools_types
|
|
|
|
|
|
|
|
const
|
|
|
|
MaxMissingItems = 1024
|
|
|
|
|
|
|
|
type
|
|
|
|
MissingBlock* = object
|
|
|
|
tries*: int
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
orphans*: Table[(Eth2Digest, ValidatorSig), ForkedSignedBeaconBlock] ##\
|
|
|
|
## 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
|
|
|
|
|
|
|
|
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-06-18 10:03:36 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
logScope:
|
|
|
|
topics = "quarant"
|
|
|
|
|
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..
|
|
|
|
for k, v in quarantine.missing.pairs():
|
|
|
|
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 =
|
|
|
|
# https://github.com/nim-lang/Nim/blob/version-1-2/lib/pure/collections/sequtils.nim#L682-L704
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# It's not really missing if we're keeping it in the quarantine
|
|
|
|
if (not anyIt(quarantine.orphans.keys, it[0] == root)):
|
2020-08-05 14:19:43 +00:00
|
|
|
# If the block is in orphans, we no longer need it
|
|
|
|
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(
|
2021-12-06 09:49:01 +00:00
|
|
|
dag: ChainDAGRef, 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
|
2021-12-06 09:49:01 +00:00
|
|
|
let slot = withBlck(signedBlock): blck.message.slot
|
|
|
|
slot > dag.finalizedHead.slot
|
2020-11-16 19:15:43 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func removeOldBlocks(quarantine: var Quarantine, dag: ChainDAGRef) =
|
2020-09-04 06:39:46 +00:00
|
|
|
var oldBlocks: seq[(Eth2Digest, ValidatorSig)]
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2021-06-23 14:43:18 +00:00
|
|
|
template removeNonviableOrphans(orphans: untyped) =
|
|
|
|
for k, v in orphans.pairs():
|
|
|
|
if not isViableOrphan(dag, v):
|
|
|
|
oldBlocks.add k
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2021-06-23 14:43:18 +00:00
|
|
|
for k in oldBlocks:
|
|
|
|
orphans.del k
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
removeNonviableOrphans(quarantine.orphans)
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func clearQuarantine*(quarantine: var Quarantine) =
|
|
|
|
quarantine.orphans.clear()
|
2020-08-31 09:00:38 +00:00
|
|
|
quarantine.missing.clear()
|
|
|
|
|
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
|
|
|
|
# new blocks, and rely on syncing to cache up again if necessary. When
|
|
|
|
# using forward sync, blocks only arrive in an order not requiring the
|
|
|
|
# quarantine.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# Since we start forward sync when about one epoch is missing, that's as
|
|
|
|
# good a number as any.
|
|
|
|
const MAX_QUARANTINE_ORPHANS = SLOTS_PER_EPOCH
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
func add*(quarantine: var Quarantine, dag: ChainDAGRef,
|
|
|
|
signedBlock: ForkedSignedBeaconBlock): bool =
|
2021-06-23 14:43:18 +00:00
|
|
|
## Adds block to quarantine's `orphans` and `missing` lists.
|
2020-11-16 19:15:43 +00:00
|
|
|
if not isViableOrphan(dag, signedBlock):
|
|
|
|
return false
|
2020-08-31 09:00:38 +00:00
|
|
|
|
|
|
|
quarantine.removeOldBlocks(dag)
|
|
|
|
|
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
|
2021-12-06 09:49:01 +00:00
|
|
|
withBlck(signedBlock): quarantine.addMissing(blck.message.parent_root)
|
2020-11-16 19:15:43 +00:00
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
if quarantine.orphans.lenu64 >= MAX_QUARANTINE_ORPHANS:
|
2020-08-31 09:00:38 +00:00
|
|
|
return false
|
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
quarantine.orphans[(signedBlock.root, signedBlock.signature)] =
|
2021-06-23 14:43:18 +00:00
|
|
|
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):
|
|
|
|
ForkedSignedBeaconBlock =
|
|
|
|
# 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
|
|
|
|
2021-12-06 09:49:01 +00:00
|
|
|
for k, v in quarantine.orphans:
|
|
|
|
if getForkedBlockField(v, parent_root) == root:
|
|
|
|
toRemove.add(k)
|
|
|
|
yield v
|