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-05-10 13:48:37 +00:00
- [Protocols ](#protocols )
- [`ExecutionEngine` ](#executionengine )
- [`assemble_block` ](#assemble_block )
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-04-08 08:29:05 +00:00
- [Execution Payload ](#execution-payload )
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-05-10 13:48:37 +00:00
## Protocols
### `ExecutionEngine`
The following methods are added to the `ExecutionEngine` protocol for use as a validator:
#### `assemble_block`
2021-05-13 23:07:22 +00:00
Produces a new instance of an execution payload, with the specified `timestamp` ,
on top of the execution payload chain tip identified by `block_hash` .
2021-05-10 13:48:37 +00:00
The body of this function is implementation dependent.
The Consensus API may be used to implement this with an external execution engine.
```python
2021-06-17 15:20:17 +00:00
def assemble_block(self: ExecutionEngine, block_hash: Hash32, timestamp: uint64, random: Bytes32) -> ExecutionPayload:
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
### Block proposal
#### Constructing the `BeaconBlockBody`
2021-04-08 08:29:05 +00:00
##### Execution Payload
2021-03-11 12:33:36 +00:00
2021-09-17 10:20:25 +00:00
* Set `block.body.execution_payload = get_execution_payload(state, execution_engine, pow_chain)` where:
2021-03-11 12:33:36 +00:00
2021-07-17 06:33:06 +00:00
```python
2021-07-20 11:37:52 +00:00
def get_pow_block_at_total_difficulty(total_difficulty: uint256, pow_chain: Sequence[PowBlock]) -> Optional[PowBlock]:
2021-07-17 06:33:06 +00:00
# `pow_chain` abstractly represents all blocks in the PoW chain
for block in pow_chain:
parent = get_pow_block(block.parent_hash)
if block.total_difficulty >= total_difficulty and parent.total_difficulty < total_difficulty:
return block
return None
2021-03-11 12:33:36 +00:00
2021-09-22 18:18:41 +00:00
def get_terminal_pow_block(total_difficulty: uint256, block_hash_override: Hash32, pow_chain: Sequence[PowBlock]) -> Optional[PowBlock]:
# check block hash override prior to total difficulty
pow_block_overrides = [pow_block for pow_block in pow_chain if pow_block.block_hash == block_hash_override]
if len(pow_block_overrides) != 0:
return pow_block_overrides[0]
return get_pow_block_at_total_difficulty(total_difficulty, pow_chain)
2021-03-11 12:33:36 +00:00
2021-06-11 10:05:19 +00:00
def produce_execution_payload(state: BeaconState,
parent_hash: Hash32,
execution_engine: ExecutionEngine) -> ExecutionPayload:
2021-06-18 18:28:38 +00:00
timestamp = compute_timestamp_at_slot(state, state.slot)
2021-09-01 12:42:58 +00:00
randao_mix = get_randao_mix(state, get_current_epoch(state))
2021-06-17 15:20:17 +00:00
return execution_engine.assemble_block(parent_hash, timestamp, randao_mix)
2021-06-11 10:05:19 +00:00
2021-06-01 11:44:58 +00:00
def get_execution_payload(state: BeaconState,
2021-07-17 06:33:06 +00:00
execution_engine: ExecutionEngine,
pow_chain: Sequence[PowBlock]) -> ExecutionPayload:
2021-06-18 10:09:30 +00:00
if not is_merge_complete(state):
2021-09-22 18:18:41 +00:00
terminal_pow_block = get_terminal_pow_block(TERMINAL_TOTAL_DIFFICULTY, TERMINAL_BLOCK_HASH, pow_chain)
2021-07-17 06:33:06 +00:00
if terminal_pow_block is None:
2021-03-25 11:49:13 +00:00
# Pre-merge, empty payload
2021-04-08 08:29:05 +00:00
return ExecutionPayload()
2021-03-25 11:49:13 +00:00
else:
2021-03-31 09:35:55 +00:00
# Signify merge via producing on top of the last PoW block
2021-09-16 05:28:04 +00:00
return produce_execution_payload(state, terminal_pow_block.block_hash, execution_engine)
2021-03-24 10:30:29 +00:00
# Post-merge, normal payload
2021-06-11 10:05:19 +00:00
parent_hash = state.latest_execution_payload_header.block_hash
2021-09-16 05:28:04 +00:00
return produce_execution_payload(state, parent_hash, execution_engine)
2021-03-11 12:33:36 +00:00
```