2021-08-18 23:11:38 +00:00
# The Merge -- Honest Validator
2021-03-11 12:33:36 +00:00
**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 -->
- [Introduction ](#introduction )
- [Prerequisites ](#prerequisites )
2021-10-19 00:10:27 +00:00
- [Helpers ](#helpers )
- [`get_pow_block_at_terminal_total_difficulty` ](#get_pow_block_at_terminal_total_difficulty )
- [`get_terminal_pow_block` ](#get_terminal_pow_block )
2021-05-10 13:48:37 +00:00
- [Protocols ](#protocols )
- [`ExecutionEngine` ](#executionengine )
2021-09-20 14:57:45 +00:00
- [`get_payload` ](#get_payload )
2021-03-11 12:33:36 +00:00
- [Beacon chain responsibilities ](#beacon-chain-responsibilities )
- [Block proposal ](#block-proposal )
- [Constructing the `BeaconBlockBody` ](#constructing-the-beaconblockbody )
2021-09-20 14:57:45 +00:00
- [ExecutionPayload ](#executionpayload )
2021-03-11 12:33:36 +00:00
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
This document represents the changes to be made in the code of an "honest validator" to implement executable beacon chain proposal.
## Prerequisites
2021-07-22 14:36:41 +00:00
This document is an extension of the [Altair -- Honest Validator ](../altair/validator.md ) guide.
All behaviors and definitions defined in this document, and documents it extends, carry over unless explicitly noted or overridden.
2021-03-11 12:33:36 +00:00
2021-07-22 14:36:41 +00:00
All terminology, constants, functions, and protocol mechanics defined in the updated Beacon Chain doc of [The Merge ](./beacon-chain.md ) are requisite for this document and used throughout.
Please see related Beacon Chain doc before continuing and use them as a reference throughout.
2021-03-11 12:33:36 +00:00
2021-10-19 00:10:27 +00:00
## Helpers
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
### `get_pow_block_at_terminal_total_difficulty`
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
```python
2021-10-26 13:59:33 +00:00
def get_pow_block_at_terminal_total_difficulty(pow_chain: Dict[Hash32, PowBlock]) -> Optional[PowBlock]:
2021-10-19 00:10:27 +00:00
# `pow_chain` abstractly represents all blocks in the PoW chain
2021-11-17 09:48:10 +00:00
for block in pow_chain.values():
2021-10-19 00:10:27 +00:00
block_reached_ttd = block.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
2021-11-17 09:48:10 +00:00
# Genesis block
if block.parent_hash == Hash32():
# No parent exists, so reaching TTD alone qualifies as valid terminal block
if block_reached_ttd:
return block
# Continue to iterate the next block
if block.parent_hash not in pow_chain:
continue
2021-11-11 00:47:01 +00:00
parent = pow_chain[block.parent_hash]
2021-10-19 00:10:27 +00:00
parent_reached_ttd = parent.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
if block_reached_ttd and not parent_reached_ttd:
return block
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
return None
```
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
### `get_terminal_pow_block`
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
```python
2021-10-26 13:59:33 +00:00
def get_terminal_pow_block(pow_chain: Dict[Hash32, PowBlock]) -> Optional[PowBlock]:
2021-10-19 00:10:27 +00:00
if TERMINAL_BLOCK_HASH != Hash32():
# Terminal block hash override takes precedence over terminal total difficulty
2021-10-26 13:59:33 +00:00
if TERMINAL_BLOCK_HASH in pow_chain:
return pow_chain[TERMINAL_BLOCK_HASH]
else:
2021-10-19 00:10:27 +00:00
return None
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
return get_pow_block_at_terminal_total_difficulty(pow_chain)
```
2021-05-10 13:48:37 +00:00
2021-10-19 00:10:27 +00:00
*Note*: This function does *not* use simple serialize `hash_tree_root` as to
avoid requiring simple serialize hashing capabilities in the Execution Layer.
## Protocols
### `ExecutionEngine`
2021-10-19 07:30:49 +00:00
*Note*: `get_payload` function is added to the `ExecutionEngine` protocol for use as a validator.
2021-10-19 00:10:27 +00:00
2021-10-19 15:28:52 +00:00
The body of this function is implementation dependent.
The Engine API may be used to implement it with an external execution engine.
2021-10-19 00:10:27 +00:00
2021-09-20 14:57:45 +00:00
#### `get_payload`
2021-09-22 11:45:26 +00:00
Given the `payload_id` , `get_payload` returns the most recent version of the execution payload that
2021-10-19 00:10:27 +00:00
has been built since the corresponding call to `notify_forkchoice_updated` method.
2021-05-10 13:48:37 +00:00
```python
2021-09-23 09:03:43 +00:00
def get_payload(self: ExecutionEngine, payload_id: PayloadId) -> ExecutionPayload:
2021-09-20 14:57:45 +00:00
"""
2021-09-22 17:56:10 +00:00
Return ``execution_payload`` object.
2021-09-20 14:57:45 +00:00
"""
2021-05-10 13:48:37 +00:00
...
```
2021-03-11 12:33:36 +00:00
## Beacon chain responsibilities
2021-04-08 08:29:05 +00:00
All validator responsibilities remain unchanged other than those noted below. Namely, the transition block handling and the addition of `ExecutionPayload` .
2021-03-11 12:33:36 +00:00
2021-11-01 15:00:49 +00:00
*Note*: A validator must not propose on or attest to a block that isn't deemed valid, i.e. hasn't yet passed the beacon chain state transition and execution validations. In future upgrades, an "execution Proof-of-Custody" will be integrated to prevent outsourcing of execution payload validations.
2021-11-01 13:31:51 +00:00
2021-03-11 12:33:36 +00:00
### Block proposal
#### Constructing the `BeaconBlockBody`
2021-09-20 14:57:45 +00:00
##### ExecutionPayload
2021-03-11 12:33:36 +00:00
2021-09-23 09:30:14 +00:00
To obtain an execution payload, a block proposer building a block on top of a `state` must take the following actions:
2021-09-20 14:57:45 +00:00
2021-10-19 00:10:27 +00:00
1. Set `payload_id = prepare_execution_payload(state, pow_chain, finalized_block_hash, fee_recipient, execution_engine)` , where:
2021-09-23 07:50:46 +00:00
* `state` is the state object after applying `process_slots(state, slot)` transition to the resulting state of the parent block processing
2021-10-26 13:59:33 +00:00
* `pow_chain` is a `Dict[Hash32, PowBlock]` dictionary that abstractly represents all blocks in the PoW chain with block hash as the dictionary key
2021-10-19 00:10:27 +00:00
* `finalized_block_hash` is the hash of the latest finalized execution payload (`Hash32()` if none yet finalized)
2021-09-20 14:57:45 +00:00
* `fee_recipient` is the value suggested to be used for the `coinbase` field of the execution payload
2021-03-11 12:33:36 +00:00
2021-07-17 06:33:06 +00:00
```python
2021-09-20 14:57:45 +00:00
def prepare_execution_payload(state: BeaconState,
2021-10-26 13:59:33 +00:00
pow_chain: Dict[Hash32, PowBlock],
2021-10-19 00:10:27 +00:00
finalized_block_hash: Hash32,
2021-09-23 08:35:55 +00:00
fee_recipient: ExecutionAddress,
2021-09-23 09:03:43 +00:00
execution_engine: ExecutionEngine) -> Optional[PayloadId]:
2021-06-18 10:09:30 +00:00
if not is_merge_complete(state):
2021-10-19 15:24:07 +00:00
is_terminal_block_hash_set = TERMINAL_BLOCK_HASH != Hash32()
2021-11-05 09:00:39 +00:00
is_activation_epoch_reached = get_current_epoch(state.slot) >= TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH
if is_terminal_block_hash_set and not is_activation_epoch_reached:
2021-10-19 15:24:07 +00:00
# Terminal block hash is set but activation epoch is not yet reached, no prepare payload call is needed
2021-10-18 19:38:08 +00:00
return None
2021-09-23 16:52:02 +00:00
terminal_pow_block = get_terminal_pow_block(pow_chain)
2021-07-17 06:33:06 +00:00
if terminal_pow_block is None:
2021-09-20 14:57:45 +00:00
# Pre-merge, no prepare payload call is needed
return None
2021-09-27 17:01:10 +00:00
# Signify merge via producing on top of the terminal PoW block
parent_hash = terminal_pow_block.block_hash
2021-09-20 14:57:45 +00:00
else:
# Post-merge, normal payload
parent_hash = state.latest_execution_payload_header.block_hash
2021-10-19 00:10:27 +00:00
# Set the forkchoice head and initiate the payload build process
payload_attributes = PayloadAttributes(
timestamp=compute_timestamp_at_slot(state, state.slot),
random=get_randao_mix(state, get_current_epoch(state)),
fee_recipient=fee_recipient,
)
2021-11-04 09:58:48 +00:00
return execution_engine.notify_forkchoice_updated(parent_hash, finalized_block_hash, payload_attributes)
2021-03-11 12:33:36 +00:00
```
2021-09-20 14:57:45 +00:00
2. Set `block.body.execution_payload = get_execution_payload(payload_id, execution_engine)` , where:
2021-03-24 10:30:29 +00:00
2021-09-20 14:57:45 +00:00
```python
2021-09-23 09:03:43 +00:00
def get_execution_payload(payload_id: Optional[PayloadId], execution_engine: ExecutionEngine) -> ExecutionPayload:
2021-09-20 14:57:45 +00:00
if payload_id is None:
# Pre-merge, empty payload
return ExecutionPayload()
else:
return execution_engine.get_payload(payload_id)
2021-03-11 12:33:36 +00:00
```
2021-09-20 14:57:45 +00:00
*Note*: It is recommended for a validator to call `prepare_execution_payload` as soon as input parameters become known,
2021-09-22 12:10:57 +00:00
and make subsequent calls to this function when any of these parameters gets updated.