2022-03-16 18:01:42 +00:00
|
|
|
# Capella -- Fork Choice
|
|
|
|
|
|
|
|
**Notice**: This document is a work-in-progress for researchers and implementers.
|
|
|
|
|
|
|
|
## Table of contents
|
|
|
|
<!-- TOC -->
|
|
|
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
|
|
|
2022-03-22 14:22:35 +00:00
|
|
|
- [Introduction](#introduction)
|
|
|
|
- [Custom types](#custom-types)
|
|
|
|
- [Protocols](#protocols)
|
|
|
|
- [`ExecutionEngine`](#executionengine)
|
|
|
|
- [`notify_forkchoice_updated`](#notify_forkchoice_updated)
|
|
|
|
- [Helpers](#helpers)
|
|
|
|
- [Extended `PayloadAttributes`](#extended-payloadattributes)
|
2023-01-27 08:12:39 +00:00
|
|
|
- [Updated fork-choice handlers](#updated-fork-choice-handlers)
|
|
|
|
- [`on_block`](#on_block)
|
2022-03-22 14:22:35 +00:00
|
|
|
|
2022-03-16 18:01:42 +00:00
|
|
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- /TOC -->
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
This is the modification of the fork choice according to the Capella upgrade.
|
|
|
|
|
|
|
|
Unless stated explicitly, all prior functionality from [Bellatrix](../bellatrix/fork-choice.md) is inherited.
|
|
|
|
|
|
|
|
## Custom types
|
|
|
|
|
|
|
|
## Protocols
|
|
|
|
|
|
|
|
### `ExecutionEngine`
|
|
|
|
|
|
|
|
*Note*: The `notify_forkchoice_updated` function is modified in the `ExecutionEngine` protocol at the Capella upgrade.
|
|
|
|
|
|
|
|
#### `notify_forkchoice_updated`
|
|
|
|
|
|
|
|
The only change made is to the `PayloadAttributes` container through the addition of `withdrawals`.
|
|
|
|
Otherwise, `notify_forkchoice_updated` inherits all prior functionality.
|
|
|
|
|
|
|
|
```python
|
|
|
|
def notify_forkchoice_updated(self: ExecutionEngine,
|
|
|
|
head_block_hash: Hash32,
|
2022-03-22 14:00:53 +00:00
|
|
|
safe_block_hash: Hash32,
|
2022-03-16 18:01:42 +00:00
|
|
|
finalized_block_hash: Hash32,
|
2022-03-22 14:14:38 +00:00
|
|
|
payload_attributes: Optional[PayloadAttributes]) -> Optional[PayloadId]:
|
2022-03-16 18:01:42 +00:00
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
## Helpers
|
|
|
|
|
|
|
|
### Extended `PayloadAttributes`
|
|
|
|
|
|
|
|
`PayloadAttributes` is extended with the `withdrawals` field.
|
|
|
|
|
|
|
|
```python
|
|
|
|
@dataclass
|
|
|
|
class PayloadAttributes(object):
|
|
|
|
timestamp: uint64
|
|
|
|
prev_randao: Bytes32
|
|
|
|
suggested_fee_recipient: ExecutionAddress
|
2022-09-20 19:43:38 +00:00
|
|
|
withdrawals: Sequence[Withdrawal] # [New in Capella]
|
2022-03-16 18:01:42 +00:00
|
|
|
```
|
2023-01-27 08:12:39 +00:00
|
|
|
|
|
|
|
## Updated fork-choice handlers
|
|
|
|
|
|
|
|
### `on_block`
|
|
|
|
|
|
|
|
*Note*: The only modification is the deletion of the verification of merge transition block conditions.
|
|
|
|
|
|
|
|
```python
|
|
|
|
def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
|
|
|
|
"""
|
|
|
|
Run ``on_block`` upon receiving a new block.
|
|
|
|
"""
|
|
|
|
block = signed_block.message
|
|
|
|
# Parent block must be known
|
|
|
|
assert block.parent_root in store.block_states
|
|
|
|
# Make a copy of the state to avoid mutability issues
|
|
|
|
pre_state = copy(store.block_states[block.parent_root])
|
|
|
|
# Blocks cannot be in the future. If they are, their consideration must be delayed until they are in the past.
|
|
|
|
assert get_current_slot(store) >= block.slot
|
|
|
|
|
|
|
|
# Check that block is later than the finalized epoch slot (optimization to reduce calls to get_ancestor)
|
|
|
|
finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
|
|
|
|
assert block.slot > finalized_slot
|
|
|
|
# Check block is a descendant of the finalized block at the checkpoint finalized slot
|
2023-05-15 09:25:48 +00:00
|
|
|
finalized_checkpoint_block = get_checkpoint_block(
|
|
|
|
store,
|
|
|
|
block.parent_root,
|
|
|
|
store.finalized_checkpoint.epoch,
|
|
|
|
)
|
|
|
|
assert store.finalized_checkpoint.root == finalized_checkpoint_block
|
2023-01-27 08:12:39 +00:00
|
|
|
|
|
|
|
# Check the block is valid and compute the post-state
|
|
|
|
state = pre_state.copy()
|
2023-05-15 09:25:48 +00:00
|
|
|
block_root = hash_tree_root(block)
|
2023-01-27 08:12:39 +00:00
|
|
|
state_transition(state, signed_block, True)
|
|
|
|
|
|
|
|
# Add new block to the store
|
2023-05-15 09:25:48 +00:00
|
|
|
store.blocks[block_root] = block
|
2023-01-27 08:12:39 +00:00
|
|
|
# Add new state for this block to the store
|
2023-05-15 09:25:48 +00:00
|
|
|
store.block_states[block_root] = state
|
2023-01-27 08:12:39 +00:00
|
|
|
|
2023-10-20 06:41:51 +00:00
|
|
|
# Add block timeliness to the store
|
2023-01-27 08:12:39 +00:00
|
|
|
time_into_slot = (store.time - store.genesis_time) % SECONDS_PER_SLOT
|
|
|
|
is_before_attesting_interval = time_into_slot < SECONDS_PER_SLOT // INTERVALS_PER_SLOT
|
2023-10-20 06:41:51 +00:00
|
|
|
is_timely = get_current_slot(store) == block.slot and is_before_attesting_interval
|
|
|
|
store.block_timeliness[hash_tree_root(block)] = is_timely
|
|
|
|
|
|
|
|
# Add proposer score boost if the block is timely and not conflicting with an existing block
|
2023-08-04 13:58:01 +00:00
|
|
|
is_first_block = store.proposer_boost_root == Root()
|
2023-10-20 06:41:51 +00:00
|
|
|
if is_timely and is_first_block:
|
2023-01-27 08:12:39 +00:00
|
|
|
store.proposer_boost_root = hash_tree_root(block)
|
|
|
|
|
2023-05-15 09:25:48 +00:00
|
|
|
# Update checkpoints in store if necessary
|
|
|
|
update_checkpoints(store, state.current_justified_checkpoint, state.finalized_checkpoint)
|
|
|
|
|
|
|
|
# Eagerly compute unrealized justification and finality.
|
|
|
|
compute_pulled_up_tip(store, block_root)
|
2023-01-27 08:12:39 +00:00
|
|
|
```
|