eth2.0-specs/specs/merge/validator.md

158 lines
6.2 KiB
Markdown
Raw Normal View History

# 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-09-23 09:03:43 +00:00
- [Custom types](#custom-types)
2021-05-10 13:48:37 +00:00
- [Protocols](#protocols)
- [`ExecutionEngine`](#executionengine)
2021-09-20 14:57:45 +00:00
- [`prepare_payload`](#prepare_payload)
- [`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
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
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-09-23 09:03:43 +00:00
## Custom types
| Name | SSZ equivalent | Description |
| - | - | - |
| `PayloadId` | `uint64` | Identifier of a payload building process |
2021-05-10 13:48:37 +00:00
## Protocols
### `ExecutionEngine`
*Note*: `prepare_payload` and `get_payload` functions are added to the `ExecutionEngine` protocol for use as a validator.
2021-05-10 13:48:37 +00:00
The body of each of these functions is implementation dependent.
The Engine API may be used to implement them with an external execution engine.
2021-05-10 13:48:37 +00:00
2021-09-20 14:57:45 +00:00
#### `prepare_payload`
2021-05-10 13:48:37 +00:00
Given the set of execution payload attributes, `prepare_payload` initiates a process of building an execution payload
2021-09-20 14:57:45 +00:00
on top of the execution chain tip identified by `parent_hash`.
2021-05-10 13:48:37 +00:00
```python
2021-09-20 14:57:45 +00:00
def prepare_payload(self: ExecutionEngine,
parent_hash: Hash32,
timestamp: uint64,
random: Bytes32,
2021-09-23 09:03:43 +00:00
fee_recipient: ExecutionAddress) -> PayloadId:
2021-09-20 14:57:45 +00:00
"""
Return ``payload_id`` that is used to obtain the execution payload in a subsequent ``get_payload`` call.
2021-09-20 14:57:45 +00:00
"""
...
```
#### `get_payload`
Given the `payload_id`, `get_payload` returns the most recent version of the execution payload that
2021-09-20 14:57:45 +00:00
has been built since the corresponding call to `prepare_payload` 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
"""
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
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
### 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
1. Set `payload_id = prepare_execution_payload(state, pow_chain, fee_recipient, execution_engine)`, where:
* `state` is the state object after applying `process_slots(state, slot)` transition to the resulting state of the parent block processing
2021-09-20 14:57:45 +00:00
* `pow_chain` is a list that abstractly represents all blocks in the PoW chain
* `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
```python
def get_pow_block_at_terminal_total_difficulty(pow_chain: Sequence[PowBlock]) -> Optional[PowBlock]:
# `pow_chain` abstractly represents all blocks in the PoW chain
for block in pow_chain:
parent = get_pow_block(block.parent_hash)
2021-09-24 12:32:08 +00:00
block_reached_ttd = block.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
parent_reached_ttd = parent.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
if block_reached_ttd and not parent_reached_ttd:
return block
return None
2021-03-11 12:33:36 +00:00
2021-09-24 12:32:08 +00:00
def get_terminal_pow_block(pow_chain: Sequence[PowBlock]) -> Optional[PowBlock]:
if TERMINAL_BLOCK_HASH != Hash32():
2021-09-24 18:57:58 +00:00
# Terminal block hash override takes precedence over terminal total difficulty
2021-09-24 12:32:08 +00:00
pow_block_overrides = [block for block in pow_chain if block.block_hash == TERMINAL_BLOCK_HASH]
if not any(pow_block_overrides):
return None
return pow_block_overrides[0]
2021-09-22 18:18:41 +00:00
return get_pow_block_at_terminal_total_difficulty(pow_chain)
2021-09-22 18:18:41 +00:00
2021-03-11 12:33:36 +00:00
2021-09-20 14:57:45 +00:00
def prepare_execution_payload(state: BeaconState,
pow_chain: Sequence[PowBlock],
2021-09-23 08:35:55 +00:00
fee_recipient: ExecutionAddress,
2021-09-23 09:03:43 +00:00
execution_engine: ExecutionEngine) -> Optional[PayloadId]:
polish merge/beacon-chain.md (#2472) Polish `merge/beacon-chain.md` with mostly non-substantive changes. **Non-substantive changes** * rename `MAX_EXECUTION_TRANSACTIONS` to `MAX_TRANSACTIONS_PER_PAYLOAD` - rename "execution transaction" to just "transaction" as per discussion with Danny * rename `compute_time_at_slot` to `compute_timestamp_at_slot` - the function returns a Unix timestamp - "timestamp" matches `execution_payload.timestamp` * be explicit about `ExecutionEngine.execution_state` for clarity * rename `ExecutionPayload.number` to `ExecutionPayload.block_number` - more specific ("number" is pretty vague) - consistent with `ExecutionPayload.block_hash` * rename `new_block` to `on_payload` - the `on_` prefix is consistent with other event handlers (e.g. see `on_tick`, `on_block`, `on_attestation` [here](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/fork-choice.md#handlers)) - the `_payload` suffix is more to the point given the function accepts an `execution_payload` - avoids conflict with `on_block` which is already used in the fork choice * rework the table of contents for consistency * order `is_execution_enabled` after `is_transition_completed` and `is_transition_block` - `is_execution_enabled` refers to `is_transition_completed` and `is_transition_block` * rename "transition" to "merge" - "transition" is a bit vague—we will have other transitions at future hard forks - there is no need for two words to refer to the same concept * add a bunch of inline comments, e.g. in `process_execution_payload` * make the `process_execution_payload` signature consistent with the other `process_` functions in `process_block` which take as arguments `state` and `block.body` * remove `TRANSITION_TOTAL_DIFFICULTY` - to be put in `merge/fork-choice.md` where it is used * various misc cleanups **Substantive changes** * reorder `ExecutionPayload` fields - for consistency with yellow paper and Eth1 - same for `ExecutionPayloadHeader` - added comments separating out the execution block header fields from the extra fields (cosmetic)
2021-06-18 10:09:30 +00:00
if not is_merge_complete(state):
terminal_pow_block = get_terminal_pow_block(pow_chain)
if terminal_pow_block is None:
2021-09-20 14:57:45 +00:00
# Pre-merge, no prepare payload call is needed
return None
# 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
timestamp = compute_timestamp_at_slot(state, state.slot)
random = get_randao_mix(state, get_current_epoch(state))
return execution_engine.prepare_payload(parent_hash, timestamp, random, fee_recipient)
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-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,
and make subsequent calls to this function when any of these parameters gets updated.