2020-04-09 16:15:00 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
|
|
|
# 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-04-15 09:21:22 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2020-07-25 19:41:12 +00:00
|
|
|
std/[tables, options],
|
2020-06-10 06:58:12 +00:00
|
|
|
# Status
|
2020-07-30 15:48:25 +00:00
|
|
|
stew/results,
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
chronicles,
|
2020-04-09 16:15:00 +00:00
|
|
|
# Internal
|
2020-07-25 19:41:12 +00:00
|
|
|
../spec/[datatypes, digest],
|
2020-07-30 19:18:17 +00:00
|
|
|
../block_pools/block_pools_types
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/fork-choice.md
|
|
|
|
# This is a port of https://github.com/sigp/lighthouse/pull/804
|
|
|
|
# which is a port of "Proto-Array": https://github.com/protolambda/lmd-ghost
|
|
|
|
# See also:
|
|
|
|
# - Protolambda port of Lighthouse: https://github.com/protolambda/eth2-py-hacks/blob/ae286567/proto_array.py
|
|
|
|
# - Prysmatic writeup: https://hackmd.io/bABJiht3Q9SyV3Ga4FT9lQ#High-level-concept
|
|
|
|
# - Gasper Whitepaper: https://arxiv.org/abs/2003.03052
|
|
|
|
|
|
|
|
# ProtoArray low-level types
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
type
|
2020-07-30 15:48:25 +00:00
|
|
|
fcKind* = enum
|
2020-04-09 16:15:00 +00:00
|
|
|
## Fork Choice Error Kinds
|
2020-07-30 15:48:25 +00:00
|
|
|
fcFinalizedNodeUnknown
|
|
|
|
fcJustifiedNodeUnknown
|
2020-08-17 18:36:13 +00:00
|
|
|
fcInvalidFinalizedRootChange
|
2020-07-30 15:48:25 +00:00
|
|
|
fcInvalidNodeIndex
|
|
|
|
fcInvalidParentIndex
|
|
|
|
fcInvalidBestChildIndex
|
|
|
|
fcInvalidJustifiedIndex
|
|
|
|
fcInvalidBestDescendant
|
|
|
|
fcInvalidParentDelta
|
|
|
|
fcInvalidNodeDelta
|
|
|
|
fcDeltaUnderflow
|
|
|
|
fcIndexUnderflow
|
|
|
|
fcInvalidDeltaLen
|
|
|
|
fcRevertedFinalizedEpoch
|
|
|
|
fcInvalidBestNode
|
2020-08-17 18:36:13 +00:00
|
|
|
fcInconsistentTick
|
2020-06-10 06:58:12 +00:00
|
|
|
# -------------------------
|
|
|
|
# TODO: Extra error modes beyond Proto/Lighthouse to be reviewed
|
2020-07-30 15:48:25 +00:00
|
|
|
fcUnknownParent
|
2020-08-26 15:23:34 +00:00
|
|
|
fcPruningFromOutdatedFinalizedRoot
|
2020-04-09 16:15:00 +00:00
|
|
|
|
2020-08-17 18:36:13 +00:00
|
|
|
AttErrorKind* = enum
|
|
|
|
attFromFuture
|
|
|
|
attFromPast
|
|
|
|
attBadTargetEpoch
|
|
|
|
attUnkownTarget
|
|
|
|
attUnknownBlock
|
|
|
|
attWrongTarget
|
|
|
|
attFutureSlot
|
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
FcUnderflowKind* = enum
|
|
|
|
## Fork Choice Overflow Kinds
|
|
|
|
fcUnderflowIndices = "Indices Overflow"
|
|
|
|
fcUnderflowBestChild = "Best Child Overflow"
|
|
|
|
fcUnderflowBestDescendant = "Best Descendant Overflow"
|
|
|
|
|
|
|
|
Index* = int
|
2020-08-17 18:36:13 +00:00
|
|
|
Delta* = int64
|
|
|
|
## Delta balances
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
ForkChoiceError* = object
|
2020-07-30 15:48:25 +00:00
|
|
|
case kind*: fcKind
|
|
|
|
of fcFinalizedNodeUnknown,
|
|
|
|
fcJustifiedNodeUnknown:
|
2020-04-09 16:15:00 +00:00
|
|
|
block_root*: Eth2Digest
|
2020-08-17 18:36:13 +00:00
|
|
|
of fcInvalidFinalizedRootChange,
|
|
|
|
fcInconsistentTick:
|
2020-04-09 16:15:00 +00:00
|
|
|
discard
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcInvalidNodeIndex,
|
|
|
|
fcInvalidParentIndex,
|
|
|
|
fcInvalidBestChildIndex,
|
|
|
|
fcInvalidJustifiedIndex,
|
|
|
|
fcInvalidBestDescendant,
|
|
|
|
fcInvalidParentDelta,
|
|
|
|
fcInvalidNodeDelta,
|
|
|
|
fcDeltaUnderflow:
|
2020-04-09 16:15:00 +00:00
|
|
|
index*: Index
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcIndexUnderflow:
|
2020-04-09 16:15:00 +00:00
|
|
|
underflowKind*: FcUnderflowKind
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcInvalidDeltaLen:
|
2020-04-09 16:15:00 +00:00
|
|
|
deltasLen*: int
|
|
|
|
indicesLen*: int
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcRevertedFinalizedEpoch:
|
2020-04-09 16:15:00 +00:00
|
|
|
current_finalized_epoch*: Epoch
|
|
|
|
new_finalized_epoch*: Epoch
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcInvalidBestNode:
|
2020-04-09 16:15:00 +00:00
|
|
|
start_root*: Eth2Digest
|
|
|
|
justified_epoch*: Epoch
|
|
|
|
finalized_epoch*: Epoch
|
|
|
|
head_root*: Eth2Digest
|
|
|
|
head_justified_epoch*: Epoch
|
|
|
|
head_finalized_epoch*: Epoch
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcUnknownParent:
|
2020-06-10 06:58:12 +00:00
|
|
|
child_root*: Eth2Digest
|
|
|
|
parent_root*: Eth2Digest
|
2020-08-26 15:23:34 +00:00
|
|
|
of fcPruningFromOutdatedFinalizedRoot:
|
|
|
|
finalizedRoot*: Eth2Digest
|
2020-04-09 16:15:00 +00:00
|
|
|
|
2020-07-30 15:48:25 +00:00
|
|
|
FcResult*[T] = Result[T, ForkChoiceError]
|
|
|
|
|
2020-08-26 15:23:34 +00:00
|
|
|
ProtoNodes* = object
|
|
|
|
buf*: seq[ProtoNode]
|
|
|
|
offset*: int ##\
|
|
|
|
## Substracted from logical Index
|
|
|
|
## to get the physical index
|
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
ProtoArray* = object
|
|
|
|
justified_epoch*: Epoch
|
|
|
|
finalized_epoch*: Epoch
|
2020-08-26 15:23:34 +00:00
|
|
|
nodes*: Protonodes
|
2020-04-09 16:15:00 +00:00
|
|
|
indices*: Table[Eth2Digest, Index]
|
|
|
|
|
|
|
|
ProtoNode* = object
|
|
|
|
root*: Eth2Digest
|
|
|
|
parent*: Option[Index]
|
|
|
|
justified_epoch*: Epoch
|
|
|
|
finalized_epoch*: Epoch
|
|
|
|
weight*: int64
|
|
|
|
best_child*: Option[Index]
|
|
|
|
best_descendant*: Option[Index]
|
|
|
|
|
2020-07-25 19:41:12 +00:00
|
|
|
BalanceCheckpoint* = object
|
|
|
|
blck*: BlockRef
|
2020-08-12 04:49:52 +00:00
|
|
|
epochRef*: EpochRef
|
2020-07-25 19:41:12 +00:00
|
|
|
|
2020-08-17 18:36:13 +00:00
|
|
|
Checkpoints* = object
|
|
|
|
time*: Slot
|
2020-07-25 19:41:12 +00:00
|
|
|
justified*: BalanceCheckpoint
|
|
|
|
finalized*: Checkpoint
|
2020-08-17 18:36:13 +00:00
|
|
|
best_justified*: Checkpoint
|
2020-07-25 19:41:12 +00:00
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
# Fork choice high-level types
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
type
|
|
|
|
VoteTracker* = object
|
|
|
|
current_root*: Eth2Digest
|
|
|
|
next_root*: Eth2Digest
|
|
|
|
next_epoch*: Epoch
|
|
|
|
|
2020-07-25 19:41:12 +00:00
|
|
|
ForkChoiceBackend* = object
|
2020-04-09 16:15:00 +00:00
|
|
|
proto_array*: ProtoArray
|
|
|
|
votes*: seq[VoteTracker]
|
|
|
|
balances*: seq[Gwei]
|
2020-06-10 06:58:12 +00:00
|
|
|
|
2020-08-17 18:36:13 +00:00
|
|
|
QueuedAttestation* = object
|
|
|
|
slot*: Slot
|
|
|
|
attesting_indices*: seq[ValidatorIndex]
|
|
|
|
block_root*: Eth2Digest
|
|
|
|
target_epoch*: Epoch
|
|
|
|
|
2020-07-25 19:41:12 +00:00
|
|
|
ForkChoice* = object
|
|
|
|
backend*: ForkChoiceBackend
|
|
|
|
checkpoints*: Checkpoints
|
|
|
|
finalizedBlock*: BlockRef ## Any finalized block used at startup
|
2020-08-17 18:36:13 +00:00
|
|
|
queuedAttestations*: seq[QueuedAttestation]
|
2020-07-25 19:41:12 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
func shortlog*(vote: VoteTracker): auto =
|
|
|
|
(
|
|
|
|
current_root: vote.current_root,
|
|
|
|
next_root: vote.next_root,
|
|
|
|
next_epoch: vote.next_epoch
|
|
|
|
)
|
|
|
|
|
|
|
|
chronicles.formatIt VoteTracker: it.shortLog
|
2020-08-26 15:23:34 +00:00
|
|
|
chronicles.formatIt ForkChoiceError: $it
|