2020-05-19 14:18:07 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 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
|
2023-05-06 08:58:50 +00:00
|
|
|
chronicles,
|
2023-04-28 22:41:55 +00:00
|
|
|
std/[options, 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
|
2024-03-21 18:41:05 +00:00
|
|
|
MaxRetriesPerMissingItem = 7
|
|
|
|
## Exponential backoff, double interval between each attempt
|
2021-12-06 09:49:01 +00:00
|
|
|
MaxMissingItems = 1024
|
2022-01-26 12:20:08 +00:00
|
|
|
## Arbitrary
|
|
|
|
MaxOrphans = SLOTS_PER_EPOCH * 3
|
|
|
|
## Enough for finalization in an alternative fork
|
2023-04-13 19:11:40 +00:00
|
|
|
MaxBlobless = SLOTS_PER_EPOCH
|
|
|
|
## Arbitrary
|
2022-01-26 12:20:08 +00:00
|
|
|
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.
|
|
|
|
|
2024-03-24 05:03:51 +00:00
|
|
|
orphans*: OrderedTable[(Eth2Digest, ValidatorSig), ForkedSignedBeaconBlock]
|
2023-04-13 19:11:40 +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. An orphan block may also be "blobless" (see
|
|
|
|
## below) - if so, upon resolving the parent, it should be
|
2023-04-16 08:37:56 +00:00
|
|
|
## added to the blobless table, after verifying its signature.
|
2023-04-13 19:11:40 +00:00
|
|
|
|
2024-04-11 09:31:39 +00:00
|
|
|
blobless*: OrderedTable[Eth2Digest, ForkedSignedBeaconBlock]
|
2023-04-13 19:11:40 +00:00
|
|
|
## Blocks that we don't have blobs for. When we have received
|
|
|
|
## all blobs for this block, we can proceed to resolving the
|
|
|
|
## block as well. A blobless block inserted into this table must
|
|
|
|
## have a resolved parent (i.e., it is not an orphan).
|
2022-01-26 12:20:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-11 16:22:02 +00:00
|
|
|
func checkMissing*(quarantine: var Quarantine, max: int): 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():
|
2024-03-21 18:41:05 +00:00
|
|
|
if v.tries > static(1 shl MaxRetriesPerMissingItem):
|
2020-05-19 14:18:07 +00:00
|
|
|
done.add(k)
|
|
|
|
|
|
|
|
for k in done:
|
|
|
|
quarantine.missing.del(k)
|
|
|
|
|
|
|
|
# simple (simplistic?) exponential backoff for retries..
|
2023-07-11 16:22:02 +00:00
|
|
|
for k, v in quarantine.missing.mpairs:
|
|
|
|
v.tries += 1
|
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))
|
2023-07-11 16:22:02 +00:00
|
|
|
if result.len >= max:
|
|
|
|
break
|
2020-06-18 10:03:36 +00:00
|
|
|
|
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
|
|
|
|
|
2024-03-21 18:41:05 +00:00
|
|
|
var r = root
|
|
|
|
for i in 0 .. MaxOrphans: # Blocks are not trusted, avoid endless loops
|
|
|
|
if r in quarantine.unviable:
|
|
|
|
# Won't get anywhere with this block
|
|
|
|
return
|
|
|
|
|
|
|
|
# It's not really missing if we're keeping it in the quarantine.
|
|
|
|
# In that case, add the next missing parent root instead
|
|
|
|
var found = false
|
|
|
|
for k, blck in quarantine.orphans:
|
|
|
|
if k[0] == r:
|
|
|
|
r = getForkedBlockField(blck, parent_root)
|
|
|
|
found = true
|
|
|
|
break
|
2022-01-26 12:20:08 +00:00
|
|
|
|
2024-03-21 18:41:05 +00:00
|
|
|
# Add if it's not there, but don't update missing counter
|
|
|
|
if not found:
|
|
|
|
discard quarantine.missing.hasKeyOrPut(r, MissingBlock())
|
|
|
|
return
|
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
|
|
|
|
2023-04-13 19:11:40 +00:00
|
|
|
func removeBlobless*(
|
|
|
|
quarantine: var Quarantine, signedBlock: ForkySignedBeaconBlock) =
|
2023-04-16 08:37:56 +00:00
|
|
|
quarantine.blobless.del(signedBlock.root)
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
func isViable(
|
|
|
|
finalizedSlot: Slot, slot: Slot): 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
|
|
|
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)
|
|
|
|
|
2024-03-24 05:03:51 +00:00
|
|
|
func removeUnviableOrphanTree(
|
|
|
|
quarantine: var Quarantine,
|
|
|
|
toCheck: var seq[Eth2Digest],
|
|
|
|
tbl: var OrderedTable[(Eth2Digest, ValidatorSig), ForkedSignedBeaconBlock]
|
|
|
|
): seq[Eth2Digest] =
|
2022-01-26 12:20:08 +00:00
|
|
|
# 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
|
2023-04-13 19:11:40 +00:00
|
|
|
checked: seq[Eth2Digest]
|
2022-01-26 12:20:08 +00:00
|
|
|
while toCheck.len > 0:
|
|
|
|
let root = toCheck.pop()
|
2023-04-13 19:11:40 +00:00
|
|
|
if root notin checked:
|
|
|
|
checked.add(root)
|
|
|
|
for k, v in tbl.mpairs():
|
2023-04-16 08:37:56 +00:00
|
|
|
let blockRoot = getForkedBlockField(v, parent_root)
|
2023-04-13 19:11:40 +00:00
|
|
|
if blockRoot == 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:
|
2023-04-13 19:11:40 +00:00
|
|
|
tbl.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)
|
|
|
|
|
2023-04-13 19:11:40 +00:00
|
|
|
checked
|
|
|
|
|
2024-03-24 05:03:51 +00:00
|
|
|
func removeUnviableBloblessTree(
|
|
|
|
quarantine: var Quarantine,
|
|
|
|
toCheck: var seq[Eth2Digest],
|
2024-04-11 09:31:39 +00:00
|
|
|
tbl: var OrderedTable[Eth2Digest, ForkedSignedBeaconBlock]) =
|
2023-04-16 08:37:56 +00:00
|
|
|
var
|
|
|
|
toRemove: seq[Eth2Digest] # Can't modify while iterating
|
|
|
|
while toCheck.len > 0:
|
|
|
|
let root = toCheck.pop()
|
|
|
|
for k, v in tbl.mpairs():
|
2024-04-11 09:31:39 +00:00
|
|
|
let blockRoot =
|
|
|
|
withBlck(v):
|
|
|
|
forkyBlck.message.parent_root
|
2023-04-16 08:37:56 +00:00
|
|
|
if blockRoot == root:
|
|
|
|
toCheck.add(k)
|
|
|
|
toRemove.add(k)
|
|
|
|
elif k == root:
|
|
|
|
toRemove.add(k)
|
|
|
|
|
|
|
|
for k in toRemove:
|
|
|
|
tbl.del k
|
|
|
|
quarantine.unviable[k] = ()
|
|
|
|
|
|
|
|
toRemove.setLen(0)
|
|
|
|
|
2023-04-13 19:11:40 +00:00
|
|
|
func addUnviable*(quarantine: var Quarantine, root: Eth2Digest) =
|
|
|
|
if root in quarantine.unviable:
|
|
|
|
return
|
|
|
|
|
|
|
|
quarantine.cleanupUnviable()
|
|
|
|
var toCheck = @[root]
|
2023-04-16 08:37:56 +00:00
|
|
|
var checked = quarantine.removeUnviableOrphanTree(toCheck, quarantine.orphans)
|
|
|
|
quarantine.removeUnviableBloblessTree(checked, quarantine.blobless)
|
2023-04-13 19:11:40 +00:00
|
|
|
|
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-04-13 19:11:40 +00:00
|
|
|
if not isViable(finalizedSlot, getForkedBlockField(v, slot)):
|
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]
|
2023-04-11 06:51:01 +00:00
|
|
|
quarantine.orphans.del k
|
2020-08-31 09:00:38 +00:00
|
|
|
|
2023-04-13 19:11:40 +00:00
|
|
|
func cleanupBlobless(quarantine: var Quarantine, finalizedSlot: Slot) =
|
2023-04-16 08:37:56 +00:00
|
|
|
var toDel: seq[Eth2Digest]
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
for k, v in quarantine.blobless:
|
2024-04-11 09:31:39 +00:00
|
|
|
withBlck(v):
|
|
|
|
if not isViable(finalizedSlot, forkyBlck.message.slot):
|
|
|
|
toDel.add k
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
for k in toDel:
|
2023-04-16 08:37:56 +00:00
|
|
|
quarantine.addUnviable k
|
2023-04-13 19:11:40 +00:00
|
|
|
quarantine.blobless.del k
|
|
|
|
|
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-05-21 17:47:00 +00:00
|
|
|
signedBlock: ForkedSignedBeaconBlock): Result[void, cstring] =
|
2021-06-23 14:43:18 +00:00
|
|
|
## Adds block to quarantine's `orphans` and `missing` lists.
|
2023-04-13 19:11:40 +00:00
|
|
|
if not isViable(finalizedSlot, getForkedBlockField(signedBlock, slot)):
|
2022-01-26 12:20:08 +00:00
|
|
|
quarantine.addUnviable(signedBlock.root)
|
2023-05-21 17:47:00 +00:00
|
|
|
return err("block unviable")
|
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] = ()
|
2023-07-11 16:55:51 +00:00
|
|
|
return err("block parent unviable")
|
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:
|
2024-03-24 05:03:51 +00:00
|
|
|
# Evict based on FIFO
|
|
|
|
var oldest_orphan_key: (Eth2Digest, ValidatorSig)
|
|
|
|
for k in quarantine.orphans.keys:
|
|
|
|
oldest_orphan_key = k
|
|
|
|
break
|
|
|
|
quarantine.orphans.del oldest_orphan_key
|
|
|
|
quarantine.blobless.del oldest_orphan_key[0]
|
2020-08-31 09:00:38 +00:00
|
|
|
|
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
|
|
|
|
2023-05-21 17:47:00 +00:00
|
|
|
ok()
|
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
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
proc addBlobless*(
|
|
|
|
quarantine: var Quarantine, finalizedSlot: Slot,
|
2024-11-13 03:29:14 +00:00
|
|
|
signedBlock: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
|
|
|
|
fulu.SignedBeaconBlock): bool =
|
2024-02-25 19:42:44 +00:00
|
|
|
|
2023-04-13 19:11:40 +00:00
|
|
|
if not isViable(finalizedSlot, signedBlock.message.slot):
|
|
|
|
quarantine.addUnviable(signedBlock.root)
|
|
|
|
return false
|
|
|
|
|
|
|
|
quarantine.cleanupBlobless(finalizedSlot)
|
|
|
|
|
|
|
|
if quarantine.blobless.lenu64 >= MaxBlobless:
|
2024-03-24 05:03:51 +00:00
|
|
|
var oldest_blobless_key: Eth2Digest
|
|
|
|
for k in quarantine.blobless.keys:
|
|
|
|
oldest_blobless_key = k
|
|
|
|
break
|
|
|
|
quarantine.blobless.del oldest_blobless_key
|
2023-04-13 19:11:40 +00:00
|
|
|
|
2023-05-06 08:58:50 +00:00
|
|
|
debug "block quarantine: Adding blobless", blck = shortLog(signedBlock)
|
2024-04-11 09:31:39 +00:00
|
|
|
quarantine.blobless[signedBlock.root] =
|
|
|
|
ForkedSignedBeaconBlock.init(signedBlock)
|
2024-02-25 19:42:44 +00:00
|
|
|
quarantine.missing.del(signedBlock.root)
|
2023-04-13 19:11:40 +00:00
|
|
|
true
|
|
|
|
|
2024-03-21 17:37:31 +00:00
|
|
|
func popBlobless*(
|
|
|
|
quarantine: var Quarantine,
|
|
|
|
root: Eth2Digest): Opt[ForkedSignedBeaconBlock] =
|
2024-04-11 09:31:39 +00:00
|
|
|
var blck: ForkedSignedBeaconBlock
|
2023-04-16 08:37:56 +00:00
|
|
|
if quarantine.blobless.pop(root, blck):
|
2024-04-11 09:31:39 +00:00
|
|
|
Opt.some(blck)
|
2023-04-16 08:37:56 +00:00
|
|
|
else:
|
2024-03-21 17:37:31 +00:00
|
|
|
Opt.none(ForkedSignedBeaconBlock)
|
2023-04-28 12:57:35 +00:00
|
|
|
|
2024-04-11 09:31:39 +00:00
|
|
|
iterator peekBlobless*(quarantine: var Quarantine): ForkedSignedBeaconBlock =
|
2023-04-28 12:57:35 +00:00
|
|
|
for k, v in quarantine.blobless.mpairs():
|
|
|
|
yield v
|