eth2.0-specs/specs/phase1/shard-data-chains.md

445 lines
17 KiB
Markdown
Raw Normal View History

# Ethereum 2.0 Phase 1 -- Shard Data Chains
2019-05-06 15:30:32 +00:00
**Notice**: This document is a work-in-progress for researchers and implementers.
2019-05-06 15:30:32 +00:00
## 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)
- [Custom types](#custom-types)
- [Configuration](#configuration)
- [Misc](#misc)
- [Initial values](#initial-values)
- [Time parameters](#time-parameters)
- [State list lengths](#state-list-lengths)
- [Rewards and penalties](#rewards-and-penalties)
- [Signature domain types](#signature-domain-types)
- [Containers](#containers)
- [`Crosslink`](#crosslink)
- [`ShardBlock`](#shardblock)
- [`ShardBlockHeader`](#shardblockheader)
- [`ShardState`](#shardstate)
- [`ShardAttestationData`](#shardattestationdata)
- [Helper functions](#helper-functions)
- [Misc](#misc-1)
- [`compute_epoch_of_shard_slot`](#compute_epoch_of_shard_slot)
- [`compute_shard_period_start_epoch`](#compute_shard_period_start_epoch)
- [Beacon state accessors](#beacon-state-accessors)
- [`get_period_committee`](#get_period_committee)
- [`get_shard_committee`](#get_shard_committee)
- [`get_shard_proposer_index`](#get_shard_proposer_index)
- [Shard state mutators](#shard-state-mutators)
- [`process_delta`](#process_delta)
- [Genesis](#genesis)
- [`get_genesis_shard_state`](#get_genesis_shard_state)
- [`get_genesis_shard_block`](#get_genesis_shard_block)
- [Shard state transition function](#shard-state-transition-function)
- [Period processing](#period-processing)
- [Block processing](#block-processing)
- [Block header](#block-header)
- [Attestations](#attestations)
- [Block body](#block-body)
- [Shard fork choice rule](#shard-fork-choice-rule)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
2019-08-28 21:57:24 +00:00
This document describes the shard transition function (data layer only) and the shard fork choice rule as part of Phase 1 of Ethereum 2.0.
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
## Custom types
| Name | SSZ equivalent | Description |
| - | - | - |
| `Shard` | `uint64` | a shard number |
| `ShardSlot` | `uint64` | a shard slot number |
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
2019-06-17 21:19:44 +00:00
## Configuration
### Misc
| Name | Value |
| - | - |
| `SHARD_COUNT` | `2**10` (= 1,024) |
2019-09-03 18:17:19 +00:00
| `MIN_BLOCK_BODY_PRICE` | `2**0` (= 1) |
2019-08-28 21:57:24 +00:00
| `MAX_PERIOD_COMMITTEE_SIZE` | `2**7` (= 128) |
| `SHARD_HEADER_SIZE` | `2**10` (= 1024) |
| `SHARD_BLOCK_SIZE_TARGET` | `2**14` (= 16,384) |
2019-09-03 18:17:19 +00:00
| `MAX_SHARD_BLOCK_SIZE` | `2**16` (= 65,536) |
2019-06-17 21:19:44 +00:00
### Initial values
| Name | Value | Unit |
2019-07-24 05:39:04 +00:00
| - | - |
| `SHARD_GENESIS_EPOCH` | **TBD** | Epoch |
### Time parameters
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
2019-08-28 21:57:24 +00:00
| `SHARD_SLOTS_PER_EPOCH` | `2**7` (= 128) | shard slots | 6.4 minutes |
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
| `EPOCHS_PER_SHARD_PERIOD` | `2**8` (= 256) | epochs | ~27 hours |
### State list lengths
2019-09-05 19:07:25 +00:00
| Name | Value |
| - | - |
| `HISTORY_ACCUMULATOR_DEPTH` | `2**6` (= 64) |
### Rewards and penalties
| Name | Value |
| - | - |
2019-09-03 18:17:19 +00:00
| `BLOCK_BODY_PRICE_QUOTIENT` | `2**3` (= 8) |
2019-06-30 20:12:02 +00:00
### Signature domain types
| Name | Value |
| - | - |
2019-12-30 05:52:00 +00:00
| `DOMAIN_SHARD_PROPOSER` | `DomainType('0x80000000')` |
| `DOMAIN_SHARD_ATTESTER` | `DomainType('0x81000000')` |
## Containers
### `Crosslink`
```python
2019-10-18 03:05:43 +00:00
# Crosslink is a placeholder to appease the build script until phase 1 is reworked
class Crosslink(Container):
shard: Shard
```
### `ShardBlock`
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
```python
class ShardBlock(Container):
shard: Shard
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
slot: ShardSlot
beacon_block_root: Root
parent_root: Root
state_root: Root
2019-09-03 18:17:19 +00:00
body: List[byte, MAX_SHARD_BLOCK_SIZE - SHARD_HEADER_SIZE]
2019-09-05 19:07:25 +00:00
block_size_sum: uint64
2019-09-03 18:27:30 +00:00
aggregation_bits: Bitvector[2 * MAX_PERIOD_COMMITTEE_SIZE]
attestations: BLSSignature
signature: BLSSignature
2019-08-28 21:57:24 +00:00
```
### `ShardBlockHeader`
```python
class ShardBlockHeader(Container):
shard: Shard
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
slot: ShardSlot
beacon_block_root: Root
parent_root: Root
state_root: Root
body_root: Root
2019-09-05 19:07:25 +00:00
block_size_sum: uint64
2019-09-03 18:27:30 +00:00
aggregation_bits: Bitvector[2 * MAX_PERIOD_COMMITTEE_SIZE]
attestations: BLSSignature
signature: BLSSignature
```
2019-07-29 20:43:55 +00:00
### `ShardState`
```python
class ShardState(Container):
2019-07-29 20:43:55 +00:00
shard: Shard
2019-08-28 21:57:24 +00:00
slot: ShardSlot
history_accumulator: Vector[Bytes32, HISTORY_ACCUMULATOR_DEPTH]
latest_block_header: ShardBlockHeader
2019-08-28 21:57:24 +00:00
block_size_sum: uint64
2019-08-29 15:36:13 +00:00
# Fees and rewards
block_body_price: Gwei
older_committee_positive_deltas: Vector[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
older_committee_negative_deltas: Vector[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
newer_committee_positive_deltas: Vector[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
newer_committee_negative_deltas: Vector[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
2019-08-28 21:57:24 +00:00
```
### `ShardAttestationData`
2019-08-28 21:57:24 +00:00
```python
class ShardAttestationData(Container):
2019-08-28 21:57:24 +00:00
slot: ShardSlot
parent_root: Root
2019-07-29 20:43:55 +00:00
```
2019-02-08 09:54:02 +00:00
## Helper functions
### Misc
#### `compute_epoch_of_shard_slot`
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
```python
def compute_epoch_of_shard_slot(slot: ShardSlot) -> Epoch:
return Epoch(slot // SHARD_SLOTS_PER_EPOCH)
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
```
2019-08-28 21:57:24 +00:00
#### `compute_shard_period_start_epoch`
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
```python
2019-08-28 21:57:24 +00:00
def compute_shard_period_start_epoch(epoch: Epoch, lookback: uint64) -> Epoch:
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
return Epoch(epoch - (epoch % EPOCHS_PER_SHARD_PERIOD) - lookback * EPOCHS_PER_SHARD_PERIOD)
```
2019-08-28 21:57:24 +00:00
### Beacon state accessors
#### `get_period_committee`
```python
2019-09-03 21:04:59 +00:00
def get_period_committee(beacon_state: BeaconState, shard: Shard, epoch: Epoch) -> Sequence[ValidatorIndex]:
active_validator_indices = get_active_validator_indices(beacon_state, epoch)
seed = get_seed(beacon_state, epoch, DOMAIN_SHARD_ATTESTER)
2019-08-28 21:57:24 +00:00
return compute_committee(active_validator_indices, seed, shard, SHARD_COUNT)[:MAX_PERIOD_COMMITTEE_SIZE]
```
2019-08-28 21:57:24 +00:00
#### `get_shard_committee`
2019-02-08 09:54:02 +00:00
```python
2019-09-03 21:04:59 +00:00
def get_shard_committee(beacon_state: BeaconState, shard: Shard, epoch: Epoch) -> Sequence[ValidatorIndex]:
older_committee = get_period_committee(beacon_state, shard, compute_shard_period_start_epoch(epoch, 2))
newer_committee = get_period_committee(beacon_state, shard, compute_shard_period_start_epoch(epoch, 1))
2019-08-28 21:57:24 +00:00
# Every epoch cycle out validators from the older committee and cycle in validators from the newer committee
older_subcommittee = [i for i in older_committee if i % EPOCHS_PER_SHARD_PERIOD > epoch % EPOCHS_PER_SHARD_PERIOD]
newer_subcommittee = [i for i in newer_committee if i % EPOCHS_PER_SHARD_PERIOD <= epoch % EPOCHS_PER_SHARD_PERIOD]
2019-09-03 21:45:13 +00:00
return older_subcommittee + newer_subcommittee
2019-02-08 09:54:02 +00:00
```
#### `get_shard_proposer_index`
2019-02-08 09:54:02 +00:00
```python
def get_shard_proposer_index(beacon_state: BeaconState, shard: Shard, slot: ShardSlot) -> ValidatorIndex:
2019-09-03 21:04:59 +00:00
epoch = get_current_epoch(beacon_state)
shard_committee = get_shard_committee(beacon_state, shard, epoch)
active_indices = [i for i in shard_committee if is_active_validator(beacon_state.validators[i], epoch)]
assert any(active_indices)
Attestation changes + persistent committee changes (#1294) * Minimal attestation simplification * minor fix * Make the tests pass * Decrease `PLACEHOLDER`, Use `compute_epoch_of_shard_slot` * Fix proposer signature name and use get_seed() to calculate current_shuffling_seed * Fix linter error * Add the WIP `test_is_valid_shard_block` * Add `get_shard_block_attester_committee` * Simplified committee selection * Added some helpers and simplified * Update specs/core/1_shard-data-chains.md * Update 1_shard-data-chains.md * Simplified switchover epochs, changed block structure, changed crosslink structure * Update 1_shard-data-chains.md * Moved balance dependency to proposer selection * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md Co-Authored-By: Danny Ryan <dannyjryan@gmail.com> * Update specs/core/1_shard-data-chains.md * Fixed shard header flattening * Update specs/core/1_shard-data-chains.md * Minor fixes * Update specs/core/1_shard-data-chains.md * Update specs/core/1_shard-data-chains.md Co-Authored-By: Hsiao-Wei Wang <hwwang156@gmail.com> * cleanup testing and lint * return none if not active validators in persistent committee * only allow active validators as shard proposer
2019-07-29 13:47:35 +00:00
epoch_seed = get_seed(beacon_state, epoch, DOMAIN_SHARD_PROPOSER)
seed = hash(epoch_seed + int_to_bytes(slot, length=8) + int_to_bytes(shard, length=8))
return compute_proposer_index(beacon_state, active_indices, seed)
```
2019-08-28 21:57:24 +00:00
### Shard state mutators
#### `process_delta`
2019-07-29 20:43:55 +00:00
```python
def process_delta(beacon_state: BeaconState,
shard_state: ShardState,
index: ValidatorIndex,
delta: Gwei,
positive: bool=True) -> None:
2019-09-27 01:34:19 +00:00
epoch = compute_epoch_of_shard_slot(shard_state.slot)
2019-09-03 21:04:59 +00:00
older_committee = get_period_committee(beacon_state, shard_state.shard, compute_shard_period_start_epoch(epoch, 2))
newer_committee = get_period_committee(beacon_state, shard_state.shard, compute_shard_period_start_epoch(epoch, 1))
2019-08-28 21:57:24 +00:00
if index in older_committee:
if positive:
shard_state.older_committee_positive_deltas[older_committee.index(index)] += delta
else:
shard_state.older_committee_negative_deltas[older_committee.index(index)] += delta
2019-08-28 21:57:24 +00:00
elif index in newer_committee:
if positive:
shard_state.newer_committee_positive_deltas[newer_committee.index(index)] += delta
else:
shard_state.newer_committee_negative_deltas[newer_committee.index(index)] += delta
```
2019-07-29 20:43:55 +00:00
2019-08-28 21:57:24 +00:00
## Genesis
2019-08-28 21:57:24 +00:00
### `get_genesis_shard_state`
```python
def get_genesis_shard_state(shard: Shard) -> ShardState:
2019-08-28 21:57:24 +00:00
return ShardState(
shard=shard,
slot=ShardSlot(SHARD_GENESIS_EPOCH * SHARD_SLOTS_PER_EPOCH),
2019-09-27 04:02:16 +00:00
latest_block_header=ShardBlockHeader(
shard=shard,
slot=ShardSlot(SHARD_GENESIS_EPOCH * SHARD_SLOTS_PER_EPOCH),
body_root=hash_tree_root(List[byte, MAX_SHARD_BLOCK_SIZE - SHARD_HEADER_SIZE]()),
),
2019-09-03 18:17:19 +00:00
block_body_price=MIN_BLOCK_BODY_PRICE,
2019-08-28 21:57:24 +00:00
)
2019-07-29 20:43:55 +00:00
```
2019-08-28 21:57:24 +00:00
### `get_genesis_shard_block`
2019-07-29 20:43:55 +00:00
```python
2019-09-03 21:49:33 +00:00
def get_genesis_shard_block(shard: Shard) -> ShardBlock:
return ShardBlock(
shard=shard,
2019-08-28 21:57:24 +00:00
slot=ShardSlot(SHARD_GENESIS_EPOCH * SHARD_SLOTS_PER_EPOCH),
2019-09-03 21:49:33 +00:00
state_root=hash_tree_root(get_genesis_shard_state(shard)),
)
2019-07-29 20:43:55 +00:00
```
## Shard state transition function
2019-07-29 20:43:55 +00:00
```python
2019-09-03 21:04:59 +00:00
def shard_state_transition(beacon_state: BeaconState,
2019-08-28 21:57:24 +00:00
shard_state: ShardState,
block: ShardBlock,
validate_state_root: bool=False) -> ShardState:
# Process slots (including those with no blocks) since block
2019-09-03 21:09:47 +00:00
process_shard_slots(shard_state, block.slot)
# Process block
2019-09-03 21:04:59 +00:00
process_shard_block(beacon_state, shard_state, block)
# Validate state root (`validate_state_root == True` in production)
if validate_state_root:
assert block.state_root == hash_tree_root(shard_state)
# Return post-state
2019-08-28 21:57:24 +00:00
return shard_state
2019-07-29 20:43:55 +00:00
```
```python
2019-09-03 21:09:47 +00:00
def process_shard_slots(shard_state: ShardState, slot: ShardSlot) -> None:
2019-08-28 21:57:24 +00:00
assert shard_state.slot <= slot
while shard_state.slot < slot:
2019-09-03 21:04:59 +00:00
process_shard_slot(shard_state)
2019-09-05 19:07:25 +00:00
# Process shard period on the start slot of the next shard period
2019-08-28 21:57:24 +00:00
if (shard_state.slot + 1) % (SHARD_SLOTS_PER_EPOCH * EPOCHS_PER_SHARD_PERIOD) == 0:
2019-09-03 21:04:59 +00:00
process_shard_period(shard_state)
2019-08-28 21:57:24 +00:00
shard_state.slot += ShardSlot(1)
```
2019-07-31 10:18:07 +00:00
```python
2019-09-03 21:04:59 +00:00
def process_shard_slot(shard_state: ShardState) -> None:
# Cache state root
2019-09-03 21:04:59 +00:00
previous_state_root = hash_tree_root(shard_state)
if shard_state.latest_block_header.state_root == Bytes32():
shard_state.latest_block_header.state_root = previous_state_root
2019-08-28 21:57:24 +00:00
# Cache state root in history accumulator
2019-07-29 20:43:55 +00:00
depth = 0
2019-09-05 19:07:25 +00:00
while shard_state.slot % 2**depth == 0 and depth < HISTORY_ACCUMULATOR_DEPTH:
2019-09-03 21:04:59 +00:00
shard_state.history_accumulator[depth] = previous_state_root
depth += 1
```
2019-07-31 10:18:07 +00:00
### Period processing
```python
2019-09-03 21:04:59 +00:00
def process_shard_period(shard_state: ShardState) -> None:
2019-09-05 19:07:25 +00:00
# Rotate committee deltas
shard_state.older_committee_positive_deltas = shard_state.newer_committee_positive_deltas
shard_state.older_committee_negative_deltas = shard_state.newer_committee_negative_deltas
shard_state.newer_committee_positive_deltas = [Gwei(0) for _ in range(MAX_PERIOD_COMMITTEE_SIZE)]
shard_state.newer_committee_negative_deltas = [Gwei(0) for _ in range(MAX_PERIOD_COMMITTEE_SIZE)]
2019-07-29 20:43:55 +00:00
```
### Block processing
2019-07-29 20:43:55 +00:00
```python
2019-09-03 21:04:59 +00:00
def process_shard_block(beacon_state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
process_shard_block_header(beacon_state, shard_state, block)
process_shard_attestations(beacon_state, shard_state, block)
process_shard_block_body(beacon_state, shard_state, block)
```
#### Block header
```python
2019-09-03 21:04:59 +00:00
def process_shard_block_header(beacon_state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
2019-09-03 18:27:30 +00:00
# Verify the shard number
assert block.shard == shard_state.shard
2019-09-03 18:27:30 +00:00
# Verify the slot number
assert block.slot == shard_state.slot
2019-09-03 18:27:30 +00:00
# Verify the beacon chain root
epoch = compute_epoch_of_shard_slot(shard_state.slot)
assert epoch * SLOTS_PER_EPOCH == beacon_state.slot
beacon_block_header = BeaconBlockHeader(
slot=beacon_state.latest_block_header.slot,
parent_root=beacon_state.latest_block_header.parent_root,
state_root=beacon_state.latest_block_header.state_root,
body_root=beacon_state.latest_block_header.body_root,
)
if beacon_block_header.state_root == Bytes32():
beacon_block_header.state_root = hash_tree_root(beacon_state)
2019-11-21 22:13:45 +00:00
assert block.beacon_block_root == hash_tree_root(beacon_block_header)
2019-09-03 18:27:30 +00:00
# Verify the parent root
2019-11-21 22:13:45 +00:00
assert block.parent_root == hash_tree_root(shard_state.latest_block_header)
# Save current block as the new latest block
shard_state.latest_block_header = ShardBlockHeader(
shard=block.shard,
slot=block.slot,
beacon_block_root=block.beacon_block_root,
parent_root=block.parent_root,
# `state_root` is zeroed and overwritten in the next `process_shard_slot` call
body_root=hash_tree_root(block.body),
2019-09-05 19:07:25 +00:00
block_size_sum=block.block_size_sum,
2019-09-03 18:27:30 +00:00
aggregation_bits=block.aggregation_bits,
attestations=block.attestations,
# `signature` is zeroed
2019-07-31 10:18:07 +00:00
)
2019-09-03 18:27:30 +00:00
# Verify the sum of the block sizes since genesis
2019-09-05 19:07:25 +00:00
shard_state.block_size_sum += SHARD_HEADER_SIZE + len(block.body)
2019-09-03 21:04:59 +00:00
assert block.block_size_sum == shard_state.block_size_sum
# Verify proposer is not slashed
2019-09-03 21:04:59 +00:00
proposer_index = get_shard_proposer_index(beacon_state, shard_state.shard, block.slot)
proposer = beacon_state.validators[proposer_index]
assert not proposer.slashed
# Verify proposer signature
2019-09-03 21:04:59 +00:00
domain = get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_epoch_of_shard_slot(block.slot))
2020-01-03 06:46:27 +00:00
assert bls.Verify(proposer.pubkey, compute_signing_root(block, domain), block.signature)
```
#### Attestations
2019-07-31 10:18:07 +00:00
```python
2019-09-03 21:04:59 +00:00
def process_shard_attestations(beacon_state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
pubkeys = []
attestation_count = 0
2019-09-03 21:04:59 +00:00
shard_committee = get_shard_committee(beacon_state, shard_state.shard, block.slot)
2019-08-28 21:57:24 +00:00
for i, validator_index in enumerate(shard_committee):
2019-09-03 18:27:30 +00:00
if block.aggregation_bits[i]:
2019-09-03 21:04:59 +00:00
pubkeys.append(beacon_state.validators[validator_index].pubkey)
process_delta(beacon_state, shard_state, validator_index, get_base_reward(beacon_state, validator_index))
attestation_count += 1
2019-08-28 21:57:24 +00:00
# Verify there are no extraneous bits set beyond the shard committee
for i in range(len(shard_committee), 2 * MAX_PERIOD_COMMITTEE_SIZE):
2019-09-03 18:27:30 +00:00
assert block.aggregation_bits[i] == 0b0
2019-08-28 21:57:24 +00:00
# Verify attester aggregate signature
2019-09-03 21:04:59 +00:00
domain = get_domain(beacon_state, DOMAIN_SHARD_ATTESTER, compute_epoch_of_shard_slot(block.slot))
2019-12-17 13:33:37 +00:00
shard_attestation_data = ShardAttestationData(slot=shard_state.slot, parent_root=block.parent_root)
2020-01-03 07:18:34 +00:00
signing_root = compute_signing_root(shard_attestation_data, domain)
assert bls.FastAggregateVerify(pubkeys, signing_root, block.attestations)
2019-08-28 21:57:24 +00:00
# Proposer micro-reward
2019-09-03 21:04:59 +00:00
proposer_index = get_shard_proposer_index(beacon_state, shard_state.shard, block.slot)
reward = attestation_count * get_base_reward(beacon_state, proposer_index) // PROPOSER_REWARD_QUOTIENT
process_delta(beacon_state, shard_state, proposer_index, Gwei(reward))
```
2019-09-03 18:27:30 +00:00
#### Block body
```python
2019-09-03 21:04:59 +00:00
def process_shard_block_body(beacon_state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
2019-09-03 18:27:30 +00:00
# Verify block body size is a multiple of the header size
assert len(block.body) % SHARD_HEADER_SIZE == 0
# Apply proposer block body fee
2019-09-03 21:04:59 +00:00
block_body_fee = shard_state.block_body_price * len(block.body) // MAX_SHARD_BLOCK_SIZE
2019-09-05 19:07:25 +00:00
proposer_index = get_shard_proposer_index(beacon_state, shard_state.shard, block.slot)
process_delta(beacon_state, shard_state, proposer_index, Gwei(block_body_fee), positive=False) # Burn
process_delta(beacon_state, shard_state, proposer_index, Gwei(block_body_fee // PROPOSER_REWARD_QUOTIENT)) # Reward
# Calculate new block body price
block_size = SHARD_HEADER_SIZE + len(block.body)
2019-09-03 18:17:19 +00:00
QUOTIENT = MAX_SHARD_BLOCK_SIZE * BLOCK_BODY_PRICE_QUOTIENT
if block_size > SHARD_BLOCK_SIZE_TARGET:
price_delta = Gwei(shard_state.block_body_price * (block_size - SHARD_BLOCK_SIZE_TARGET) // QUOTIENT)
2019-09-05 19:07:25 +00:00
# The maximum block body price caps the amount burnt on fees within a shard period
2019-09-03 18:17:19 +00:00
MAX_BLOCK_BODY_PRICE = MAX_EFFECTIVE_BALANCE // EPOCHS_PER_SHARD_PERIOD // SHARD_SLOTS_PER_EPOCH
shard_state.block_body_price = Gwei(min(MAX_BLOCK_BODY_PRICE, shard_state.block_body_price + price_delta))
else:
price_delta = Gwei(shard_state.block_body_price * (SHARD_BLOCK_SIZE_TARGET - block_size) // QUOTIENT)
shard_state.block_body_price = Gwei(max(MIN_BLOCK_BODY_PRICE, shard_state.block_body_price + price_delta))
```
## Shard fork choice rule
2019-09-03 21:04:59 +00:00
The fork choice rule for any shard is LMD GHOST using the shard attestations of the shard committee and the beacon chain attestations of the crosslink committee currently assigned to that shard, but instead of being rooted in the genesis it is rooted in the block referenced in the most recent accepted crosslink (i.e. `beacon_state.crosslinks[shard].shard_block_root`). Only blocks whose `beacon_block_root` is the block in the main beacon chain at the specified `slot` should be considered. (If the beacon chain skips a slot, then the block at that slot is considered to be the block in the beacon chain at the highest slot lower than that slot.)