2021-03-11 12:33:36 +00:00
# Ethereum 2.0 The Merge
2021-03-24 10:30:29 +00:00
**Warning:** This document is currently based on [Phase 0 ](../phase0/beacon-chain.md ) but will be rebased to [Altair ](../altair/beacon-chain.md ) once the latter is shipped.
2021-03-17 16:39:35 +00:00
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 )
2021-03-29 18:56:43 +00:00
- [Custom types ](#custom-types )
2021-03-11 12:33:36 +00:00
- [Constants ](#constants )
- [Execution ](#execution )
- [Containers ](#containers )
- [Extended containers ](#extended-containers )
- [`BeaconBlockBody` ](#beaconblockbody )
- [`BeaconState` ](#beaconstate )
- [New containers ](#new-containers )
2021-04-08 08:29:05 +00:00
- [`ExecutionPayload` ](#executionpayload )
- [`ExecutionPayloadHeader` ](#executionpayloadheader )
2021-05-10 13:48:37 +00:00
- [Protocols ](#protocols )
- [`ExecutionEngine` ](#executionengine )
- [`new_block` ](#new_block )
2021-03-11 12:33:36 +00:00
- [Helper functions ](#helper-functions )
- [Misc ](#misc )
2021-05-06 00:21:52 +00:00
- [`is_execution_enabled` ](#is_execution_enabled )
2021-03-20 13:21:11 +00:00
- [`is_transition_completed` ](#is_transition_completed )
- [`is_transition_block` ](#is_transition_block )
2021-04-08 09:26:32 +00:00
- [`compute_time_at_slot` ](#compute_time_at_slot )
2021-03-11 12:33:36 +00:00
- [Block processing ](#block-processing )
2021-04-08 08:29:05 +00:00
- [Execution payload processing ](#execution-payload-processing )
- [`process_execution_payload` ](#process_execution_payload )
2021-06-08 12:44:53 +00:00
- [Initialize state for pure Merge testnets and test vectors ](#initialize-state-for-pure-merge-testnets-and-test-vectors )
2021-03-11 12:33:36 +00:00
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
2021-03-24 10:30:29 +00:00
This is a patch implementing the executable beacon chain proposal.
2021-04-08 08:29:05 +00:00
It enshrines transaction execution and validity as a first class citizen at the core of the beacon chain.
2021-03-11 12:33:36 +00:00
2021-03-26 19:20:23 +00:00
## Custom types
We define the following Python custom types for type hinting and readability:
| Name | SSZ equivalent | Description |
| - | - | - |
| `OpaqueTransaction` | `ByteList[MAX_BYTES_PER_OPAQUE_TRANSACTION]` | a byte-list containing a single [typed transaction envelope ](https://eips.ethereum.org/EIPS/eip-2718#opaque-byte-array-rather-than-an-rlp-array ) structured as `TransactionType \|\| TransactionPayload` |
2021-03-11 12:33:36 +00:00
## Constants
### Execution
| Name | Value |
| - | - |
2021-03-26 19:20:23 +00:00
| `MAX_BYTES_PER_OPAQUE_TRANSACTION` | `uint64(2**20)` (= 1,048,576) |
2021-04-19 05:24:06 +00:00
| `MAX_EXECUTION_TRANSACTIONS` | `uint64(2**14)` (= 16,384) |
2021-03-24 10:30:29 +00:00
| `BYTES_PER_LOGS_BLOOM` | `uint64(2**8)` (= 256) |
2021-03-11 12:33:36 +00:00
## Containers
### Extended containers
*Note*: Extended SSZ containers inherit all fields from the parent in the original
order and append any additional fields to the end.
#### `BeaconBlockBody`
2021-04-08 08:29:05 +00:00
*Note*: `BeaconBlockBody` fields remain unchanged other than the addition of `execution_payload` .
2021-03-11 12:33:36 +00:00
```python
class BeaconBlockBody(phase0.BeaconBlockBody):
2021-04-08 08:29:05 +00:00
execution_payload: ExecutionPayload # [New in Merge]
2021-03-11 12:33:36 +00:00
```
#### `BeaconState`
2021-04-08 08:29:05 +00:00
*Note*: `BeaconState` fields remain unchanged other than addition of `latest_execution_payload_header` .
2021-03-11 12:33:36 +00:00
```python
2021-03-22 15:14:31 +00:00
class BeaconState(phase0.BeaconState):
2021-04-08 08:29:05 +00:00
# Execution-layer
latest_execution_payload_header: ExecutionPayloadHeader # [New in Merge]
2021-03-11 12:33:36 +00:00
```
### New containers
2021-04-08 08:29:05 +00:00
#### `ExecutionPayload`
2021-03-11 12:33:36 +00:00
2021-04-08 08:29:05 +00:00
The execution payload included in a `BeaconBlockBody` .
2021-03-11 12:33:36 +00:00
```python
2021-04-08 08:29:05 +00:00
class ExecutionPayload(Container):
2021-04-08 08:48:03 +00:00
block_hash: Hash32 # Hash of execution block
parent_hash: Hash32
2021-03-11 12:33:36 +00:00
coinbase: Bytes20
state_root: Bytes32
2021-03-31 09:35:55 +00:00
number: uint64
2021-03-11 12:33:36 +00:00
gas_limit: uint64
gas_used: uint64
2021-04-13 13:08:47 +00:00
timestamp: uint64
2021-03-11 12:33:36 +00:00
receipt_root: Bytes32
2021-03-25 12:41:00 +00:00
logs_bloom: ByteVector[BYTES_PER_LOGS_BLOOM]
2021-06-11 10:05:19 +00:00
randao: Bytes32 # 'difficulty' in the yellow paper
2021-04-19 05:24:06 +00:00
transactions: List[OpaqueTransaction, MAX_EXECUTION_TRANSACTIONS]
2021-03-11 12:33:36 +00:00
```
2021-04-08 08:29:05 +00:00
#### `ExecutionPayloadHeader`
2021-04-02 14:48:02 +00:00
2021-04-08 08:29:05 +00:00
The execution payload header included in a `BeaconState` .
2021-04-02 14:48:02 +00:00
2021-04-08 08:29:05 +00:00
*Note:* Holds execution payload data without transaction bodies.
2021-04-02 14:48:02 +00:00
```python
2021-04-08 08:29:05 +00:00
class ExecutionPayloadHeader(Container):
2021-04-08 08:48:03 +00:00
block_hash: Hash32 # Hash of execution block
parent_hash: Hash32
2021-04-02 14:48:02 +00:00
coinbase: Bytes20
state_root: Bytes32
number: uint64
gas_limit: uint64
gas_used: uint64
2021-04-13 13:08:47 +00:00
timestamp: uint64
2021-04-02 14:48:02 +00:00
receipt_root: Bytes32
logs_bloom: ByteVector[BYTES_PER_LOGS_BLOOM]
2021-06-11 10:05:19 +00:00
randao: Bytes32
2021-04-06 05:48:33 +00:00
transactions_root: Root
2021-04-02 14:48:02 +00:00
```
2021-05-10 13:48:37 +00:00
## Protocols
### `ExecutionEngine`
The `ExecutionEngine` protocol separates the consensus and execution sub-systems.
The consensus implementation references an instance of this sub-system with `EXECUTION_ENGINE` .
The following methods are added to the `ExecutionEngine` protocol for use in the state transition:
#### `new_block`
2021-05-13 23:07:22 +00:00
Verifies the given `execution_payload` with respect to execution state transition, and persists changes if valid.
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
def new_block(self: ExecutionEngine, execution_payload: ExecutionPayload) -> bool:
"""
Returns True if the ``execution_payload`` was verified and processed successfully, False otherwise.
"""
...
```
2021-03-11 12:33:36 +00:00
## Helper functions
### Misc
2021-05-06 00:21:52 +00:00
#### `is_execution_enabled`
```python
def is_execution_enabled(state: BeaconState, block: BeaconBlock) -> bool:
return is_transition_completed(state) or is_transition_block(state, block)
```
2021-03-20 13:21:11 +00:00
#### `is_transition_completed`
2021-03-11 12:33:36 +00:00
```python
2021-04-14 06:54:49 +00:00
def is_transition_completed(state: BeaconState) -> bool:
2021-04-08 14:14:01 +00:00
return state.latest_execution_payload_header != ExecutionPayloadHeader()
2021-03-11 12:33:36 +00:00
```
2021-03-20 13:21:11 +00:00
#### `is_transition_block`
2021-03-11 12:33:36 +00:00
```python
2021-05-04 22:17:10 +00:00
def is_transition_block(state: BeaconState, block: BeaconBlock) -> bool:
return not is_transition_completed(state) and block.body.execution_payload != ExecutionPayload()
2021-03-11 12:33:36 +00:00
```
2021-04-08 09:26:32 +00:00
#### `compute_time_at_slot`
2021-04-14 06:53:30 +00:00
*Note*: This function is unsafe with respect to overflows and underflows.
2021-04-08 09:26:32 +00:00
```python
def compute_time_at_slot(state: BeaconState, slot: Slot) -> uint64:
slots_since_genesis = slot - GENESIS_SLOT
return uint64(state.genesis_time + slots_since_genesis * SECONDS_PER_SLOT)
```
2021-03-11 12:33:36 +00:00
### Block processing
```python
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
process_randao(state, block.body)
2021-03-20 13:21:11 +00:00
process_eth1_data(state, block.body)
2021-03-11 12:33:36 +00:00
process_operations(state, block.body)
2021-06-11 16:50:11 +00:00
# [New in Merge] Pre-merge, skip execution payload processing
2021-05-06 00:21:52 +00:00
if is_execution_enabled(state, block):
2021-06-11 10:05:19 +00:00
randao_mix = get_randao_mix(state, get_current_epoch(state))
process_execution_payload(state, block.body.execution_payload, randao_mix, EXECUTION_ENGINE)
2021-03-11 12:33:36 +00:00
```
2021-04-08 08:29:05 +00:00
#### Execution payload processing
2021-03-11 12:33:36 +00:00
2021-04-08 08:29:05 +00:00
##### `process_execution_payload`
2021-03-11 12:33:36 +00:00
```python
2021-05-12 00:40:23 +00:00
def process_execution_payload(state: BeaconState,
execution_payload: ExecutionPayload,
2021-06-11 10:05:19 +00:00
randao_mix: Bytes32,
2021-05-12 00:40:23 +00:00
execution_engine: ExecutionEngine) -> None:
2021-03-11 12:33:36 +00:00
"""
2021-03-24 10:30:29 +00:00
Note: This function is designed to be able to be run in parallel with the other `process_block` sub-functions
2021-03-11 12:33:36 +00:00
"""
2021-04-08 14:14:34 +00:00
if is_transition_completed(state):
2021-04-08 14:14:01 +00:00
assert execution_payload.parent_hash == state.latest_execution_payload_header.block_hash
assert execution_payload.number == state.latest_execution_payload_header.number + 1
2021-06-11 10:05:19 +00:00
assert execution_payload.randao == randao_mix
2021-04-08 08:29:05 +00:00
2021-04-13 13:08:47 +00:00
assert execution_payload.timestamp == compute_time_at_slot(state, state.slot)
2021-05-10 13:48:37 +00:00
assert execution_engine.new_block(execution_payload)
2021-04-08 08:29:05 +00:00
state.latest_execution_payload_header = ExecutionPayloadHeader(
block_hash=execution_payload.block_hash,
parent_hash=execution_payload.parent_hash,
coinbase=execution_payload.coinbase,
state_root=execution_payload.state_root,
number=execution_payload.number,
gas_limit=execution_payload.gas_limit,
gas_used=execution_payload.gas_used,
2021-04-13 13:08:47 +00:00
timestamp=execution_payload.timestamp,
2021-04-08 08:29:05 +00:00
receipt_root=execution_payload.receipt_root,
logs_bloom=execution_payload.logs_bloom,
2021-06-11 10:05:19 +00:00
randao=execution_payload.randao,
2021-04-08 08:29:05 +00:00
transactions_root=hash_tree_root(execution_payload.transactions),
2021-04-02 14:48:02 +00:00
)
2021-03-11 12:33:36 +00:00
```
2021-06-08 12:44:53 +00:00
## Initialize state for pure Merge testnets and test vectors
This helper function is only for initializing the state for pure Merge testnets and tests.
*Note*: The function `initialize_beacon_state_from_eth1` is modified: (1) using `MERGE_FORK_VERSION` as the current fork version, (2) utilizing the Merge `BeaconBlockBody` when constructing the initial `latest_block_header` , and (3) adding initial `latest_execution_payload_header` .
```python
def initialize_beacon_state_from_eth1(eth1_block_hash: Bytes32,
eth1_timestamp: uint64,
deposits: Sequence[Deposit]) -> BeaconState:
2021-06-11 16:50:11 +00:00
randao_seed = eth1_block_hash
2021-06-08 12:44:53 +00:00
fork = Fork(
previous_version=GENESIS_FORK_VERSION,
current_version=MERGE_FORK_VERSION, # [Modified in Merge]
epoch=GENESIS_EPOCH,
)
state = BeaconState(
genesis_time=eth1_timestamp + GENESIS_DELAY,
fork=fork,
eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=uint64(len(deposits))),
latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
2021-06-11 16:50:11 +00:00
randao_mixes=[randao_seed] * EPOCHS_PER_HISTORICAL_VECTOR, # Seed RANDAO with Eth1 entropy
2021-06-08 12:44:53 +00:00
)
# Process deposits
leaves = list(map(lambda deposit: deposit.data, deposits))
for index, deposit in enumerate(deposits):
deposit_data_list = List[DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH](*leaves[:index + 1])
state.eth1_data.deposit_root = hash_tree_root(deposit_data_list)
process_deposit(state, deposit)
# Process activations
for index, validator in enumerate(state.validators):
balance = state.balances[index]
validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
if validator.effective_balance == MAX_EFFECTIVE_BALANCE:
validator.activation_eligibility_epoch = GENESIS_EPOCH
validator.activation_epoch = GENESIS_EPOCH
# Set genesis validators root for domain separation and chain versioning
state.genesis_validators_root = hash_tree_root(state.validators)
# [New in Merge] Construct execution payload header
# Note: initialized with zero block height
state.latest_execution_payload_header = ExecutionPayloadHeader(
block_hash=eth1_block_hash,
parent_hash=Hash32(),
coinbase=Bytes20(),
state_root=Bytes32(),
number=uint64(0),
gas_limit=uint64(0),
gas_used=uint64(0),
timestamp=eth1_timestamp,
receipt_root=Bytes32(),
logs_bloom=ByteVector[BYTES_PER_LOGS_BLOOM](),
2021-06-11 16:50:11 +00:00
randao=randao_seed,
2021-06-08 12:44:53 +00:00
transactions_root=Root(),
)
return state
```