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 )
2021-03-20 13:21:11 +00:00
- [Transition ](#transition )
2021-03-11 12:33:36 +00:00
- [Execution ](#execution )
- [Containers ](#containers )
- [Extended containers ](#extended-containers )
- [`BeaconBlockBody` ](#beaconblockbody )
- [`BeaconState` ](#beaconstate )
- [New containers ](#new-containers )
- [`ApplicationPayload` ](#applicationpayload )
- [Helper functions ](#helper-functions )
- [Misc ](#misc )
2021-03-20 13:21:11 +00:00
- [`is_transition_completed` ](#is_transition_completed )
- [`is_transition_block` ](#is_transition_block )
2021-03-11 12:33:36 +00:00
- [Block processing ](#block-processing )
- [Application payload processing ](#application-payload-processing )
- [`get_application_state` ](#get_application_state )
- [`application_state_transition` ](#application_state_transition )
- [`process_application_payload` ](#process_application_payload )
<!-- 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.
It enshrines application-layer 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
2021-03-20 13:21:11 +00:00
### Transition
2021-03-24 10:30:29 +00:00
2021-03-20 13:21:11 +00:00
| Name | Value |
| - | - |
2021-03-24 10:30:29 +00:00
| `TRANSITION_TOTAL_DIFFICULTY` | **TBD** |
2021-03-20 13:21:11 +00:00
2021-03-11 12:33:36 +00:00
### Execution
| Name | Value |
| - | - |
2021-03-26 19:20:23 +00:00
| `MAX_BYTES_PER_OPAQUE_TRANSACTION` | `uint64(2**20)` (= 1,048,576) |
2021-03-24 10:30:29 +00:00
| `MAX_APPLICATION_TRANSACTIONS` | `uint64(2**14)` (= 16,384) |
| `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`
*Note*: `BeaconBlockBody` fields remain unchanged other than the addition of `application_payload` .
```python
class BeaconBlockBody(phase0.BeaconBlockBody):
2021-03-25 11:49:13 +00:00
application_payload: ApplicationPayload # [New in Merge] application payload
2021-03-11 12:33:36 +00:00
```
#### `BeaconState`
2021-03-24 10:30:29 +00:00
*Note*: `BeaconState` fields remain unchanged other than addition of `application_state_root` and `application_block_hash` .
2021-03-11 12:33:36 +00:00
```python
2021-03-22 15:14:31 +00:00
class BeaconState(phase0.BeaconState):
2021-03-24 10:30:29 +00:00
# Application-layer
2021-03-25 11:49:13 +00:00
application_state_root: Bytes32 # [New in Merge]
application_block_hash: Bytes32 # [New in Merge]
2021-03-11 12:33:36 +00:00
```
### New containers
#### `ApplicationPayload`
2021-03-26 19:20:23 +00:00
The application payload included in a `BeaconBlockBody` .
2021-03-11 12:33:36 +00:00
```python
class ApplicationPayload(Container):
block_hash: Bytes32 # Hash of application block
coinbase: Bytes20
state_root: Bytes32
gas_limit: uint64
gas_used: uint64
receipt_root: Bytes32
2021-03-25 12:41:00 +00:00
logs_bloom: ByteVector[BYTES_PER_LOGS_BLOOM]
2021-03-26 19:20:23 +00:00
transactions: List[OpaqueTransaction, MAX_APPLICATION_TRANSACTIONS]
2021-03-11 12:33:36 +00:00
```
## Helper functions
### Misc
2021-03-20 13:21:11 +00:00
#### `is_transition_completed`
2021-03-11 12:33:36 +00:00
```python
2021-03-22 15:20:05 +00:00
def is_transition_completed(state: BeaconState) -> boolean:
2021-03-24 10:30:29 +00:00
return state.application_block_hash != Bytes32()
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-03-20 13:21:11 +00:00
def is_transition_block(state: BeaconState, block_body: BeaconBlockBody) -> boolean:
2021-03-22 14:54:44 +00:00
return state.application_block_hash == Bytes32() and block_body.application_payload.block_hash != Bytes32()
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-03-17 09:31:21 +00:00
process_application_payload(state, block.body) # [New in Merge]
2021-03-11 12:33:36 +00:00
```
#### Application payload processing
##### `get_application_state`
*Note*: `ApplicationState` class is an abstract class representing ethereum application state.
Let `get_application_state(application_state_root: Bytes32) -> ApplicationState` be the function that given the root hash returns a copy of ethereum application state.
2021-03-17 09:21:47 +00:00
The body of the function is implementation dependent.
2021-03-11 12:33:36 +00:00
##### `application_state_transition`
2021-03-20 13:21:11 +00:00
Let `application_state_transition(application_state: ApplicationState, application_payload: ApplicationPayload) -> None` be the transition function of ethereum application state.
The body of the function is implementation dependent.
2021-03-11 12:33:36 +00:00
2021-03-11 12:52:31 +00:00
*Note*: `application_state_transition` must throw `AssertionError` if either the transition itself or one of the post-transition verifications has failed.
2021-03-11 12:33:36 +00:00
##### `process_application_payload`
```python
def process_application_payload(state: BeaconState, body: BeaconBlockBody) -> None:
"""
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-03-20 13:21:11 +00:00
if is_transition_completed(state):
application_state = get_application_state(state.application_state_root)
application_state_transition(application_state, body.application_payload)
state.application_state_root = body.application_payload.state_root
state.application_block_hash = body.application_payload.block_hash
elif is_transition_block(state, body):
2021-03-24 10:30:29 +00:00
assert body.application_payload == ApplicationPayload(block_hash=body.application_payload.block_hash)
2021-03-20 13:21:11 +00:00
state.application_block_hash = body.application_payload.block_hash
2021-03-22 14:58:34 +00:00
else:
assert body.application_payload == ApplicationPayload()
2021-03-11 12:33:36 +00:00
```