2020-04-09 16:15:00 +00:00
|
|
|
# beacon_chain
|
2021-02-08 07:27:30 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2020-04-09 16:15:00 +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-04-15 09:21:22 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2020-08-27 07:34:12 +00:00
|
|
|
std/[options, tables],
|
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
|
2021-06-21 08:35:24 +00:00
|
|
|
../spec/datatypes/base,
|
2021-03-04 09:13:44 +00:00
|
|
|
../consensus_object_pools/block_pools_types
|
2020-04-09 16:15:00 +00:00
|
|
|
|
2021-08-20 23:37:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v0.11.1/specs/phase0/fork-choice.md
|
2020-04-09 16:15:00 +00:00
|
|
|
# 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
|
|
|
|
fcInvalidNodeIndex
|
|
|
|
fcInvalidJustifiedIndex
|
|
|
|
fcInvalidBestDescendant
|
|
|
|
fcInvalidParentDelta
|
|
|
|
fcInvalidNodeDelta
|
|
|
|
fcDeltaUnderflow
|
|
|
|
fcInvalidDeltaLen
|
|
|
|
fcInvalidBestNode
|
2020-08-17 18:36:13 +00:00
|
|
|
fcInconsistentTick
|
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
|
|
|
|
|
|
|
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:
|
2021-02-16 18:53:07 +00:00
|
|
|
blockRoot*: Eth2Digest
|
2021-04-26 20:39:44 +00:00
|
|
|
of fcInconsistentTick:
|
2020-04-09 16:15:00 +00:00
|
|
|
discard
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcInvalidNodeIndex,
|
|
|
|
fcInvalidJustifiedIndex,
|
|
|
|
fcInvalidBestDescendant,
|
|
|
|
fcInvalidParentDelta,
|
|
|
|
fcInvalidNodeDelta,
|
|
|
|
fcDeltaUnderflow:
|
2020-04-09 16:15:00 +00:00
|
|
|
index*: Index
|
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 fcInvalidBestNode:
|
2021-02-16 18:53:07 +00:00
|
|
|
startRoot*: Eth2Digest
|
2021-11-25 18:41:39 +00:00
|
|
|
fkChoiceJustifiedCheckpoint*: Checkpoint
|
|
|
|
fkChoiceFinalizedCheckpoint*: Checkpoint
|
2021-02-16 18:53:07 +00:00
|
|
|
headRoot*: Eth2Digest
|
2021-11-25 18:41:39 +00:00
|
|
|
headJustifiedCheckpoint*: Checkpoint
|
|
|
|
headFinalizedCheckpoint*: Checkpoint
|
2020-07-30 15:48:25 +00:00
|
|
|
of fcUnknownParent:
|
2021-02-16 18:53:07 +00:00
|
|
|
childRoot*: Eth2Digest
|
|
|
|
parentRoot*: 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
|
2021-11-25 18:41:39 +00:00
|
|
|
justifiedCheckpoint*: Checkpoint
|
|
|
|
finalizedCheckpoint*: Checkpoint
|
|
|
|
nodes*: ProtoNodes
|
2020-04-09 16:15:00 +00:00
|
|
|
indices*: Table[Eth2Digest, Index]
|
|
|
|
|
|
|
|
ProtoNode* = object
|
|
|
|
root*: Eth2Digest
|
|
|
|
parent*: Option[Index]
|
2021-11-25 18:41:39 +00:00
|
|
|
justifiedCheckpoint*: Checkpoint
|
|
|
|
finalizedCheckpoint*: Checkpoint
|
2020-04-09 16:15:00 +00:00
|
|
|
weight*: int64
|
2021-02-16 18:53:07 +00:00
|
|
|
bestChild*: Option[Index]
|
|
|
|
bestDescendant*: Option[Index]
|
2020-04-09 16:15:00 +00:00
|
|
|
|
2020-07-25 19:41:12 +00:00
|
|
|
BalanceCheckpoint* = object
|
2021-11-25 18:41:39 +00:00
|
|
|
checkpoint*: Checkpoint
|
2020-10-22 10:53:33 +00:00
|
|
|
balances*: seq[Gwei]
|
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
|
2021-02-08 07:27:30 +00:00
|
|
|
attesting_indices*: seq[ValidatorIndex]
|
2020-08-17 18:36:13 +00:00
|
|
|
block_root*: Eth2Digest
|
|
|
|
target_epoch*: Epoch
|
|
|
|
|
2020-07-25 19:41:12 +00:00
|
|
|
ForkChoice* = object
|
|
|
|
backend*: ForkChoiceBackend
|
|
|
|
checkpoints*: Checkpoints
|
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
|