2021-12-22 07:46:18 +00:00
# Bellatrix -- Fork Choice
2021-03-11 12:33:36 +00:00
## 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 -->
- [Introduction ](#introduction )
2021-11-04 10:30:23 +00:00
- [Custom types ](#custom-types )
2021-05-10 13:48:37 +00:00
- [Protocols ](#protocols )
- [`ExecutionEngine` ](#executionengine )
2021-09-23 07:37:52 +00:00
- [`notify_forkchoice_updated` ](#notify_forkchoice_updated )
2022-03-22 14:41:42 +00:00
- [`safe_block_hash` ](#safe_block_hash )
2021-06-01 10:28:30 +00:00
- [Helpers ](#helpers )
2021-10-19 00:10:27 +00:00
- [`PayloadAttributes` ](#payloadattributes )
2021-06-01 10:28:30 +00:00
- [`PowBlock` ](#powblock )
- [`get_pow_block` ](#get_pow_block )
2021-06-02 09:00:01 +00:00
- [`is_valid_terminal_pow_block` ](#is_valid_terminal_pow_block )
2021-10-28 08:00:01 +00:00
- [`validate_merge_block` ](#validate_merge_block )
2021-05-10 13:48:37 +00:00
- [Updated fork-choice handlers ](#updated-fork-choice-handlers )
2021-06-01 10:28:30 +00:00
- [`on_block` ](#on_block )
2021-03-11 12:33:36 +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 executable beacon chain proposal.
2021-03-20 13:21:11 +00:00
*Note*: It introduces the process of transition from the last PoW block to the first PoS block.
2021-03-11 12:33:36 +00:00
2021-11-04 09:58:48 +00:00
## Custom types
| Name | SSZ equivalent | Description |
| - | - | - |
| `PayloadId` | `Bytes8` | Identifier of a payload building process |
2021-05-10 13:48:37 +00:00
## Protocols
### `ExecutionEngine`
2021-09-23 07:37:52 +00:00
*Note*: The `notify_forkchoice_updated` function is added to the `ExecutionEngine` protocol to signal the fork choice updates.
2021-05-10 13:48:37 +00:00
The body of this function is implementation dependent.
2021-09-22 12:10:57 +00:00
The Engine API may be used to implement it with an external execution engine.
2021-05-10 13:48:37 +00:00
2021-09-23 07:37:52 +00:00
#### `notify_forkchoice_updated`
2021-05-10 13:48:37 +00:00
2022-03-14 13:09:21 +00:00
This function performs three actions *atomically* :
2021-09-20 14:57:45 +00:00
* Re-organizes the execution payload chain and corresponding state to make `head_block_hash` the head.
2022-03-14 13:09:21 +00:00
* Updates safe block hash with the value provided by `safe_block_hash` parameter.
2021-09-20 14:57:45 +00:00
* Applies finality to the execution state: it irreversibly persists the chain of all execution payloads
and corresponding state, up to and including `finalized_block_hash` .
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
Additionally, if `payload_attributes` is provided, this function sets in motion a payload build process on top of
2021-11-04 09:58:48 +00:00
`head_block_hash` and returns an identifier of initiated process.
2021-10-19 00:10:27 +00:00
2021-05-10 13:48:37 +00:00
```python
2021-10-19 00:10:27 +00:00
def notify_forkchoice_updated(self: ExecutionEngine,
head_block_hash: Hash32,
2022-03-14 13:09:21 +00:00
safe_block_hash: Hash32,
2021-10-19 00:10:27 +00:00
finalized_block_hash: Hash32,
2021-11-04 09:58:48 +00:00
payload_attributes: Optional[PayloadAttributes]) -> Optional[PayloadId]:
2021-05-10 13:48:37 +00:00
...
```
2022-03-14 13:09:21 +00:00
*Note*: The `(head_block_hash, finalized_block_hash)` values of the `notify_forkchoice_updated` function call maps on the `POS_FORKCHOICE_UPDATED` event defined in the [EIP-3675 ](https://eips.ethereum.org/EIPS/eip-3675#definitions ).
2021-11-25 10:33:30 +00:00
As per EIP-3675, before a post-transition block is finalized, `notify_forkchoice_updated` MUST be called with `finalized_block_hash = Hash32()` .
2021-09-24 10:31:09 +00:00
2021-11-26 13:47:05 +00:00
*Note*: Client software MUST NOT call this function until the transition conditions are met on the PoW network, i.e. there exists a block for which `is_valid_terminal_pow_block` function returns `True` .
2021-11-24 14:32:27 +00:00
2021-11-25 10:27:59 +00:00
*Note*: Client software MUST call this function to initiate the payload build process to produce the merge transition block; the `head_block_hash` parameter MUST be set to the hash of a terminal PoW block in this case.
2021-11-24 14:32:27 +00:00
2022-03-22 14:26:09 +00:00
##### `safe_block_hash`
2022-03-30 09:36:01 +00:00
The `safe_block_hash` parameter MUST be set to return value of
[`get_safe_execution_payload_hash(store: Store)` ](../../fork_choice/safe-block.md#get_safe_execution_payload_hash ) function.
2022-03-14 13:09:21 +00:00
2021-06-01 10:28:30 +00:00
## Helpers
2021-03-11 12:33:36 +00:00
2021-10-19 00:10:27 +00:00
### `PayloadAttributes`
Used to signal to initiate the payload build process via `notify_forkchoice_updated` .
```python
@dataclass
class PayloadAttributes(object):
timestamp: uint64
2022-02-22 11:16:33 +00:00
prev_randao: Bytes32
2021-11-18 13:23:52 +00:00
suggested_fee_recipient: ExecutionAddress
2021-10-19 00:10:27 +00:00
```
2021-06-01 10:28:30 +00:00
### `PowBlock`
```python
2021-09-23 19:22:34 +00:00
class PowBlock(Container):
2021-04-08 08:48:03 +00:00
block_hash: Hash32
2021-07-16 13:16:32 +00:00
parent_hash: Hash32
2021-03-22 15:55:35 +00:00
total_difficulty: uint256
```
2021-06-01 10:28:30 +00:00
### `get_pow_block`
2021-03-22 15:55:35 +00:00
2021-10-18 05:32:29 +00:00
Let `get_pow_block(block_hash: Hash32) -> Optional[PowBlock]` be the function that given the hash of the PoW block returns its data.
2021-10-19 20:35:30 +00:00
It may result in `None` if the requested block is not yet available.
2021-03-11 12:33:36 +00:00
2021-09-10 10:07:26 +00:00
*Note*: The `eth_getBlockByHash` JSON-RPC method may be used to pull this information from an execution client.
2021-03-11 12:33:36 +00:00
2021-06-02 09:00:01 +00:00
### `is_valid_terminal_pow_block`
2021-03-11 12:33:36 +00:00
2021-03-24 10:30:29 +00:00
Used by fork-choice handler, `on_block` .
2021-03-11 12:33:36 +00:00
```python
2021-09-17 10:20:25 +00:00
def is_valid_terminal_pow_block(block: PowBlock, parent: PowBlock) -> bool:
is_total_difficulty_reached = block.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
is_parent_total_difficulty_valid = parent.total_difficulty < TERMINAL_TOTAL_DIFFICULTY
2021-09-10 10:07:26 +00:00
return is_total_difficulty_reached and is_parent_total_difficulty_valid
2021-03-11 12:33:36 +00:00
```
2021-10-28 08:00:01 +00:00
### `validate_merge_block`
```python
def validate_merge_block(block: BeaconBlock) -> None:
"""
Check the parent PoW block of execution payload is a valid terminal PoW block.
2021-10-28 08:06:58 +00:00
Note: Unavailable PoW block(s) may later become available,
and a client software MAY delay a call to ``validate_merge_block``
until the PoW block(s) become available.
2021-10-28 08:00:01 +00:00
"""
2021-11-02 14:20:40 +00:00
if TERMINAL_BLOCK_HASH != Hash32():
# If `TERMINAL_BLOCK_HASH` is used as an override, the activation epoch must be reached.
assert compute_epoch_at_slot(block.slot) >= TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH
2021-11-04 03:31:31 +00:00
assert block.body.execution_payload.parent_hash == TERMINAL_BLOCK_HASH
2021-11-04 13:59:13 +00:00
return
2021-11-02 14:20:40 +00:00
2021-10-28 08:00:01 +00:00
pow_block = get_pow_block(block.body.execution_payload.parent_hash)
2021-10-28 08:06:58 +00:00
# Check if `pow_block` is available
2021-10-28 08:00:01 +00:00
assert pow_block is not None
pow_parent = get_pow_block(pow_block.parent_hash)
2021-10-28 08:06:58 +00:00
# Check if `pow_parent` is available
2021-10-28 08:00:01 +00:00
assert pow_parent is not None
# Check if `pow_block` is a valid terminal PoW block
assert is_valid_terminal_pow_block(pow_block, pow_parent)
```
2021-05-10 13:48:37 +00:00
## Updated fork-choice handlers
2021-03-11 12:33:36 +00:00
2021-06-01 10:28:30 +00:00
### `on_block`
2021-03-11 12:33:36 +00:00
2021-03-20 13:21:11 +00:00
*Note*: The only modification is the addition of the verification of transition block conditions.
2021-03-11 12:33:36 +00:00
```python
2021-09-17 10:20:25 +00:00
def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
2021-10-18 05:32:29 +00:00
"""
Run ``on_block`` upon receiving a new block.
2021-10-26 13:38:22 +00:00
A block that is asserted as invalid due to unavailable PoW block may be valid at a later time,
2021-10-18 05:32:29 +00:00
consider scheduling it for later processing in such case.
"""
2021-03-11 12:33:36 +00:00
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])
2022-07-29 07:13:32 +00:00
# Blocks cannot be in the future. If they are, their consideration must be delayed until they are in the past.
2021-03-11 12:33:36 +00:00
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-04-18 03:26:16 +00:00
finalized_checkpoint_block = get_checkpoint_block(
2023-04-05 03:38:20 +00:00
store,
block.parent_root,
store.finalized_checkpoint.epoch,
)
2023-04-18 03:26:16 +00:00
assert store.finalized_checkpoint.root == finalized_checkpoint_block
2021-09-22 18:18:41 +00:00
2021-09-10 10:07:26 +00:00
# Check the block is valid and compute the post-state
state = pre_state.copy()
2023-03-09 23:17:11 +00:00
block_root = hash_tree_root(block)
2021-09-10 10:07:26 +00:00
state_transition(state, signed_block, True)
2021-12-22 07:46:18 +00:00
# [New in Bellatrix]
2021-11-23 21:31:35 +00:00
if is_merge_transition_block(pre_state, block.body):
2021-10-28 08:00:01 +00:00
validate_merge_block(block)
2021-03-11 12:33:36 +00:00
# Add new block to the store
2023-03-09 23:17:11 +00:00
store.blocks[block_root] = block
2021-03-11 12:33:36 +00:00
# Add new state for this block to the store
2023-03-09 23:17:11 +00:00
store.block_states[block_root] = state
2021-03-11 12:33:36 +00:00
2021-11-19 20:35:59 +00:00
# Add proposer score boost if the block is timely
2021-11-23 15:02:04 +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
2021-11-22 18:29:12 +00:00
if get_current_slot(store) == block.slot and is_before_attesting_interval:
2021-11-22 22:44:52 +00:00
store.proposer_boost_root = hash_tree_root(block)
2021-11-19 20:35:59 +00:00
2023-03-09 23:17:11 +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)
2021-03-11 12:33:36 +00:00
```