eth2.0-specs/specs/core/0_fork-choice.md

202 lines
8.3 KiB
Markdown
Raw Normal View History

2019-04-22 13:20:48 +00:00
# Ethereum 2.0 Phase 0 -- Beacon Chain Fork Choice
2019-05-06 15:30:32 +00:00
**Notice**: This document is a work-in-progress for researchers and implementers.
2019-04-22 13:20:48 +00:00
## Table of contents
<!-- TOC -->
- [Ethereum 2.0 Phase 0 -- Beacon Chain Fork Choice](#ethereum-20-phase-0----beacon-chain-fork-choice)
- [Table of contents](#table-of-contents)
- [Introduction](#introduction)
- [Constants](#constants)
- [Time parameters](#time-parameters)
2019-06-15 22:42:03 +00:00
- [Fork choice](#fork-choice)
2019-06-20 10:58:05 +00:00
- [Helpers](#helpers)
2019-06-21 18:55:55 +00:00
- [`Checkpoint`](#checkpoint)
2019-06-15 22:42:03 +00:00
- [`Store`](#store)
- [`get_genesis_store`](#get_genesis_store)
- [`get_ancestor`](#get_ancestor)
2019-06-20 10:58:05 +00:00
- [`get_latest_attesting_balance`](#get_latest_attesting_balance)
2019-06-15 22:42:03 +00:00
- [`get_head`](#get_head)
- [Handlers](#handlers)
- [`on_tick`](#on_tick)
- [`on_block`](#on_block)
- [`on_attestation`](#on_attestation)
2019-04-22 13:20:48 +00:00
<!-- /TOC -->
## Introduction
2019-06-15 22:42:03 +00:00
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](./0_beacon-chain.md).
2019-06-17 21:19:44 +00:00
## Configuration
2019-04-22 13:20:48 +00:00
### Time parameters
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
| `SECONDS_PER_SLOT` | `6` | seconds | 6 seconds |
2019-06-15 22:42:03 +00:00
## 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)` whenever `time > store.time` where `time` is the current Unix time
* `on_block(block)` whenever a block `block` is received
* `on_attestation(attestation)` whenever an attestation `attestation` is received
*Notes*:
1) **Leap seconds**: Slots will last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds around leap seconds. This is automatically handled by [UNIX time](https://en.wikipedia.org/wiki/Unix_time).
2019-06-15 22:42:03 +00:00
2) **Honest clocks**: Honest nodes are assumed to have clocks synchronized within `SECONDS_PER_SLOT` seconds of each other.
3) **Eth1 data**: The large `ETH1_FOLLOW_DISTANCE` specified in the [honest validator document](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/validator/0_beacon-chain-validator.md) should ensure that `state.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.
2019-06-20 10:58:05 +00:00
4) **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`.
2019-06-21 17:18:24 +00:00
5) **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](https://github.com/protolambda/lmd-ghost).
2019-06-15 22:42:03 +00:00
2019-06-20 10:58:05 +00:00
### Helpers
2019-04-22 13:20:48 +00:00
2019-06-21 18:55:55 +00:00
#### `Checkpoint`
2019-06-15 22:42:03 +00:00
```python
@dataclass
2019-06-21 18:55:55 +00:00
class Checkpoint(object):
2019-06-15 22:42:03 +00:00
epoch: Epoch
2019-06-20 10:58:05 +00:00
root: Hash
2019-06-15 22:42:03 +00:00
```
#### `Store`
```python
@dataclass
class Store(object):
2019-06-20 10:58:05 +00:00
blocks: Dict[Hash, BeaconBlock] = field(default_factory=dict)
2019-06-21 18:55:55 +00:00
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
2019-06-15 22:42:03 +00:00
```
2019-04-22 13:20:48 +00:00
2019-06-15 22:42:03 +00:00
#### `get_genesis_store`
2019-04-22 13:20:48 +00:00
2019-06-15 22:42:03 +00:00
```python
def get_genesis_store(genesis_state: BeaconState) -> Store:
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
root = signing_root(genesis_block)
2019-06-21 18:55:55 +00:00
justified_checkpoint = Checkpoint(GENESIS_EPOCH, root)
finalized_checkpoint = Checkpoint(GENESIS_EPOCH, root)
return Store(
blocks={root: genesis_block},
2019-06-21 18:55:55 +00:00
block_states={root: genesis_state},
checkpoint_states={justified_checkpoint: genesis_state.copy()},
time=genesis_state.genesis_time,
2019-06-21 18:55:55 +00:00
justified_checkpoint=justified_checkpoint,
finalized_checkpoint=finalized_checkpoint,
)
2019-06-15 22:42:03 +00:00
```
2019-04-22 13:20:48 +00:00
2019-06-15 22:42:03 +00:00
#### `get_ancestor`
2019-04-22 13:20:48 +00:00
2019-06-15 22:42:03 +00:00
```python
2019-06-20 10:58:05 +00:00
def get_ancestor(store: Store, root: Hash, slot: Slot) -> Hash:
2019-06-15 22:42:03 +00:00
block = store.blocks[root]
assert block.slot >= slot
return root if block.slot == slot else get_ancestor(store, block.parent_root, slot)
```
2019-04-22 13:20:48 +00:00
2019-06-20 10:58:05 +00:00
#### `get_latest_attesting_balance`
2019-04-22 13:20:48 +00:00
```python
2019-06-20 10:58:05 +00:00
def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
2019-06-21 18:55:55 +00:00
state = store.checkpoint_states[store.justified_checkpoint]
active_indices = get_active_validator_indices(state.validator_registry, get_current_epoch(state))
return Gwei(sum(
2019-06-15 22:42:03 +00:00
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
))
2019-04-22 13:20:48 +00:00
```
2019-06-15 22:42:03 +00:00
#### `get_head`
2019-04-22 13:20:48 +00:00
```python
2019-06-20 10:58:05 +00:00
def get_head(store: Store) -> Hash:
2019-06-15 22:42:03 +00:00
# Execute the LMD-GHOST fork choice
2019-06-21 18:55:55 +00:00
head = store.justified_checkpoint.root
justified_slot = get_epoch_start_slot(store.justified_checkpoint.epoch)
2019-06-15 22:42:03 +00:00
while True:
children = [
root for root in store.blocks.keys()
if store.blocks[root].parent_root == head and store.blocks[root].slot > justified_slot
]
2019-04-22 13:20:48 +00:00
if len(children) == 0:
return head
2019-06-20 10:58:05 +00:00
# Sort by latest attesting balance with ties broken lexicographically
head = max(children, key=lambda root: (get_latest_attesting_balance(store, root), root))
2019-06-15 22:42:03 +00:00
```
### Handlers
#### `on_tick`
```python
def on_tick(store: Store, time: int) -> None:
store.time = time
2019-04-22 13:20:48 +00:00
```
2019-06-15 22:42:03 +00:00
#### `on_block`
2019-06-15 22:42:03 +00:00
```python
def on_block(store: Store, block: BeaconBlock) -> None:
2019-06-21 11:00:42 +00:00
# Make a copy of the state to avoid mutability issues
parent_block = store.blocks[block.parent_root]
2019-06-21 18:55:55 +00:00
pre_state = store.block_states[parent_block.root].copy()
2019-06-21 10:19:08 +00:00
# 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
2019-06-15 22:42:03 +00:00
# Add new block to the store
store.blocks[signing_root(block)] = block
# Check block is a descendant of the finalized block
2019-06-21 18:55:55 +00:00
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
2019-06-21 18:55:55 +00:00
assert block.slot > get_epoch_start_slot(store.finalized_checkpoint.epoch)
2019-06-15 22:42:03 +00:00
# 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
2019-06-21 18:55:55 +00:00
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:
2019-06-21 18:55:55 +00:00
store.justified_checkpoint = Checkpoint(state.previous_justified_epoch, state.previous_justified_root)
# Update finalized checkpoint
if state.finalized_epoch > state.finalized_epoch:
2019-06-21 18:55:55 +00:00
store.finalized_checkpoint = Checkpoint(state.finalized_epoch, state.finalized_root)
2019-06-15 22:42:03 +00:00
```
2019-06-15 22:42:03 +00:00
#### `on_attestation`
```python
def on_attestation(store: Store, attestation: Attestation) -> None:
2019-06-21 18:55:55 +00:00
target_checkpoint = Checkpoint(attestation.data.target_epoch, attestation.data, target_root)
# Cannot calculate the current shuffling if have not seen the target
assert target_checkpoint.root in store.blocks
2019-06-21 18:55:55 +00:00
# Store target checkpoint state if not yet seen
if target_checkpoint not in store.checkpoint_states:
base_state = store.block_states[target_checkpoint.root].copy()
store.checkpoint_states[target_checkpoint] = process_slots(base_state, get_epoch_start_slot(target_checkpoint.epoch))
2019-06-21 18:55:55 +00:00
# Get state at the `target_checkpoint` to validate attestation and calculate the committees
state = store.checkpoint_states[target_checkpoint]
2019-06-15 22:42:03 +00:00
indexed_attestation = convert_to_indexed(state, attestation)
validate_indexed_attestation(state, indexed_attestation)
2019-06-21 18:55:55 +00:00
# Update latest targets
2019-06-15 22:42:03 +00:00
for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices:
2019-06-21 18:55:55 +00:00
if i not in store.latest_targets or target_checkpoint.epoch > store.latest_targets[i].epoch:
store.latest_targets[i] = target_checkpoint
2019-06-15 22:42:03 +00:00
```