8.7 KiB
8.7 KiB
Ethereum 2.0 Phase 0 -- Beacon Chain Fork Choice
Notice: This document is a work-in-progress for researchers and implementers.
Table of contents
Introduction
This document is the beacon chain fork choice spec, part of Ethereum 2.0 Phase 0. It assumes the beacon chain state transition function spec.
Configuration
Time parameters
Name | Value | Unit | Duration |
---|---|---|---|
SECONDS_PER_SLOT |
6 |
seconds | 6 seconds |
Fork choice
The head block root associated with a store
is defined as get_head(store)
. At genesis let store = get_genesis_store(genesis_state)
and update store
by running:
on_tick(time)
whenevertime > store.time
wheretime
is the current Unix timeon_block(block)
whenever a blockblock
is receivedon_attestation(attestation)
whenever an attestationattestation
is received
Notes:
- Leap seconds: Slots will last
SECONDS_PER_SLOT + 1
orSECONDS_PER_SLOT - 1
seconds around leap seconds. This is automatically handled by UNIX time. - Honest clocks: Honest nodes are assumed to have clocks synchronized within
SECONDS_PER_SLOT
seconds of each other. - Eth1 data: The large
ETH1_FOLLOW_DISTANCE
specified in the honest validator document should ensure thatstate.latest_eth1_data
of the canonical Ethereum 2.0 chain remains consistent with the canonical Ethereum 1.0 chain. If not, emergency manual intervention will be required. - Manual forks: Manual forks may arbitrarily change the fork choice rule but are expected to be enacted at epoch transitions, with the fork details reflected in
state.fork
. - Implementation: The implementation found in this specification is constructed for ease of understanding rather than for optimization in computation, space, or any other resource. A number of optimized alternatives can be found here.
Helpers
Checkpoint
@dataclass
class Checkpoint(object):
epoch: Epoch
root: Hash
Store
@dataclass
class Store(object):
blocks: Dict[Hash, BeaconBlock] = field(default_factory=dict)
block_states: Dict[Hash, BeaconState] = field(default_factory=dict)
checkpoint_states: Dict[Checkpoint, BeaconState] = field(default_factory=dict)
time: int
latest_targets: Dict[ValidatorIndex, Checkpoint] = field(default_factory=dict)
justified_checkpoint: Checkpoint
finalized_checkpoint: Checkpoint
get_genesis_store
def get_genesis_store(genesis_state: BeaconState) -> Store:
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
root = signing_root(genesis_block)
justified_checkpoint = Checkpoint(GENESIS_EPOCH, root)
finalized_checkpoint = Checkpoint(GENESIS_EPOCH, root)
return Store(
blocks={root: genesis_block},
block_states={root: genesis_state},
checkpoint_states={justified_checkpoint: genesis_state.copy()},
time=genesis_state.genesis_time,
justified_checkpoint=justified_checkpoint,
finalized_checkpoint=finalized_checkpoint,
)
get_ancestor
def get_ancestor(store: Store, root: Hash, slot: Slot) -> Hash:
block = store.blocks[root]
assert block.slot >= slot
return root if block.slot == slot else get_ancestor(store, block.parent_root, slot)
get_latest_attesting_balance
def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
state = store.checkpoint_states[store.justified_checkpoint]
active_indices = get_active_validator_indices(state.validator_registry, get_current_epoch(state))
return Gwei(sum(
state.validator_registry[i].effective_balance for i in active_indices
if get_ancestor(store, store.latest_targets[i].root, store.blocks[root].slot) == root
))
get_head
def get_head(store: Store) -> Hash:
# Execute the LMD-GHOST fork choice
head = store.justified_checkpoint.root
justified_slot = get_epoch_start_slot(store.justified_checkpoint.epoch)
while True:
children = [
root for root in store.blocks.keys()
if store.blocks[root].parent_root == head and store.blocks[root].slot > justified_slot
]
if len(children) == 0:
return head
# Sort by latest attesting balance with ties broken lexicographically
head = max(children, key=lambda root: (get_latest_attesting_balance(store, root), root))
Handlers
on_tick
def on_tick(store: Store, time: int) -> None:
store.time = time
on_block
def on_block(store: Store, block: BeaconBlock) -> None:
# Make a copy of the state to avoid mutability issues
parent_block = store.blocks[block.parent_root]
pre_state = store.block_states[parent_block.root].copy()
# Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
# Add new block to the store
store.blocks[signing_root(block)] = block
# Check block is a descendant of the finalized block
assert get_ancestor(store, signing_root(block), store.blocks[get_epoch_start_slot(store.finalized_checkpoint)].slot) == store.finalized_checkpoint.root
# Check that block is later than the finalized epoch slot
assert block.slot > get_epoch_start_slot(store.finalized_checkpoint.epoch)
# Check the block is valid and compute the post-state
state = state_transition(pre_state, block)
# Add new state for this block to the store
store.block_states[signing_root(block)] = state
# Update justified checkpoint
if state.current_justified_epoch > store.justified_checkpoint.epoch:
store.justified_checkpoint = Checkpoint(state.current_justified_epoch, state.current_justified_root)
elif state.previous_justified_epoch > store.justified_epoch:
store.justified_checkpoint = Checkpoint(state.previous_justified_epoch, state.previous_justified_root)
# Update finalized checkpoint
if state.finalized_epoch > state.finalized_epoch:
store.finalized_checkpoint = Checkpoint(state.finalized_epoch, state.finalized_root)
on_attestation
def on_attestation(store: Store, attestation: Attestation) -> None:
target = Checkpoint(attestation.data.target_epoch, attestation.data.target_root)
# Cannot calculate the current shuffling if have not seen the target
assert target.root in store.blocks
# Attestations cannot be from future epochs. If they are, their consideration must be delayed until the are in the past.
assert store.time >= pre_state.genesis_time + get_epoch_start_slot(target.epoch) * SECONDS_PER_SLOT
# Store target checkpoint state if not yet seen
if target not in store.checkpoint_states:
base_state = store.block_states[target.root].copy()
store.checkpoint_states[target] = process_slots(base_state, get_epoch_start_slot(target.epoch))
target_state = store.checkpoint_states[target]
# Attestations can only affect the fork choice of subsequent slots.
# Delay consideration in the fork choice until their slot is in the past.
attestation_slot = get_attestation_data_slot(target_state, attestation.data)
assert store.time >= (attestation_slot + 1) * SECONDS_PER_SLOT
# Get state at the `target` to validate attestation and calculate the committees
indexed_attestation = convert_to_indexed(target_state, attestation)
validate_indexed_attestation(target_state, indexed_attestation)
# Update latest targets
for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices:
if i not in store.latest_targets or target.epoch > store.latest_targets[i].epoch:
store.latest_targets[i] = target