2018-11-14 21:01:57 +00:00
# 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.
2018-11-14 21:01:57 +00:00
2019-05-06 15:30:32 +00:00
## Table of contents
2019-02-19 11:26:35 +00:00
<!-- TOC -->
2019-05-06 15:30:32 +00:00
- [Ethereum 2.0 Phase 1 -- Shard Data Chains ](#ethereum-20-phase-1----shard-data-chains )
- [Table of contents ](#table-of-contents )
2019-03-28 22:56:43 +00:00
- [Introduction ](#introduction )
- [Constants ](#constants )
- [Misc ](#misc )
- [Time parameters ](#time-parameters )
- [Signature domains ](#signature-domains )
2019-03-17 11:44:19 +00:00
- [Data structures ](#data-structures )
2019-03-28 22:56:43 +00:00
- [`ShardBlockBody` ](#shardblockbody )
2019-05-07 12:23:28 +00:00
- [`ShardAttestation` ](#shardattestation )
2019-03-28 22:56:43 +00:00
- [`ShardBlock` ](#shardblock )
- [`ShardBlockHeader` ](#shardblockheader )
- [Helper functions ](#helper-functions )
- [`get_period_committee` ](#get_period_committee )
2019-03-31 22:49:02 +00:00
- [`get_switchover_epoch` ](#get_switchover_epoch )
2019-03-28 22:56:43 +00:00
- [`get_persistent_committee` ](#get_persistent_committee )
- [`get_shard_proposer_index` ](#get_shard_proposer_index )
- [`get_shard_header` ](#get_shard_header )
- [`verify_shard_attestation_signature` ](#verify_shard_attestation_signature )
- [`compute_crosslink_data_root` ](#compute_crosslink_data_root )
- [Object validity ](#object-validity )
- [Shard blocks ](#shard-blocks )
- [Shard attestations ](#shard-attestations )
- [Beacon attestations ](#beacon-attestations )
- [Shard fork choice rule ](#shard-fork-choice-rule )
2019-02-19 11:26:35 +00:00
<!-- /TOC -->
2019-03-28 22:56:43 +00:00
## Introduction
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
This document describes the shard data layer and the shard fork choice rule in Phase 1 of Ethereum 2.0.
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
## Constants
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
### Misc
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
| Name | Value |
| - | - |
| `BYTES_PER_SHARD_BLOCK_BODY` | `2**14` (= 16,384) |
| `MAX_SHARD_ATTESTIONS` | `2**4` (= 16) |
| `PHASE_1_GENESIS_EPOCH` | **TBD** |
2019-05-26 12:14:48 +00:00
| `PHASE_1_GENESIS_SLOT` | **TBD** |
2019-05-26 17:41:36 +00:00
| `GENESIS_SHARD_SLOT` | 0 |
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
### Time parameters
2018-11-14 21:01:57 +00:00
2019-03-28 22:56:43 +00:00
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
2019-05-07 11:13:22 +00:00
| `CROSSLINK_LOOKBACK` | `2**0` (= 1) | epochs | 6.2 minutes |
| `PERSISTENT_COMMITTEE_PERIOD` | `2**11` (= 2,048) | epochs | ~9 days |
2019-05-26 17:41:36 +00:00
| `SECONDS_PER_SLOT` | `2**1 * 3**1` (= 6) | 6 seconds |
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
### Signature domains
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
| Name | Value |
| - | - |
| `DOMAIN_SHARD_PROPOSER` | `128` |
| `DOMAIN_SHARD_ATTESTER` | `129` |
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
## Data structures
### `ShardBlockBody`
```python
2019-06-05 13:29:26 +00:00
class ShardBlockBody(Container):
data: Vector[bytes, BYTES_PER_SHARD_BLOCK_BODY]
2019-05-07 12:23:28 +00:00
```
### `ShardAttestation`
```python
2019-06-05 13:29:26 +00:00
class ShardAttestation(Container):
class data(Container):
slot: uint64
shard: uint64
shard_block_root: Bytes32
aggregation_bitfield: bytes
aggregate_signature: Bytes96
2019-03-28 22:56:43 +00:00
```
### `ShardBlock`
```python
2019-06-05 13:29:26 +00:00
class ShardBlock(Container):
slot: uint64
shard: uint64
beacon_chain_root: Bytes32
parent_root: Bytes32
data: ShardBlockBody
state_root: Bytes32
attestations: List[ShardAttestation]
signature: Bytes96
2019-03-28 22:56:43 +00:00
```
### `ShardBlockHeader`
```python
2019-06-05 13:29:26 +00:00
class ShardBlockHeader(Container):
slot: uint64
shard: uint64
beacon_chain_root: Bytes32
parent_root: Bytes32
body_root: Bytes32
state_root: Bytes32
attestations: List[ShardAttestation]
signature: Bytes96
2019-03-28 22:56:43 +00:00
```
2019-02-08 09:54:02 +00:00
## Helper functions
2019-03-28 22:56:43 +00:00
### `get_period_committee`
2019-02-12 11:11:45 +00:00
```python
2019-05-26 17:41:36 +00:00
def get_period_committee(state: BeaconState,
epoch: Epoch,
shard: Shard,
index: int,
count: int) -> List[ValidatorIndex]:
2019-02-12 11:11:45 +00:00
"""
2019-03-28 22:56:43 +00:00
Return committee for a period. Used to construct persistent committees.
2019-02-12 11:11:45 +00:00
"""
2019-05-01 14:21:38 +00:00
return compute_committee(
indices=get_active_validator_indices(state, epoch),
seed=generate_seed(state, epoch),
index=shard * count + index,
count=SHARD_COUNT * count,
)
2019-02-12 11:11:45 +00:00
```
2019-03-31 22:49:02 +00:00
### `get_switchover_epoch`
```python
def get_switchover_epoch(state: BeaconState, epoch: Epoch, index: ValidatorIndex):
earlier_start_epoch = epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD * 2
2019-05-28 07:58:51 +00:00
return (bytes_to_int(hash(generate_seed(state, earlier_start_epoch) + int_to_bytes(index, length=3)[0:8]))
2019-05-26 17:41:36 +00:00
% PERSISTENT_COMMITTEE_PERIOD)
2019-03-31 22:49:02 +00:00
```
2019-03-28 22:56:43 +00:00
### `get_persistent_committee`
2019-02-08 09:54:02 +00:00
```python
2019-02-12 11:11:45 +00:00
def get_persistent_committee(state: BeaconState,
2019-02-14 23:02:01 +00:00
shard: Shard,
2019-03-17 11:44:19 +00:00
slot: Slot) -> List[ValidatorIndex]:
2019-02-09 04:10:54 +00:00
"""
2019-03-17 11:44:19 +00:00
Return the persistent committee for the given ``shard`` at the given ``slot``.
2019-02-09 04:10:54 +00:00
"""
2019-04-02 18:17:55 +00:00
epoch = slot_to_epoch(slot)
2019-03-17 11:44:19 +00:00
earlier_start_epoch = epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD * 2
later_start_epoch = epoch - (epoch % PERSISTENT_COMMITTEE_PERIOD) - PERSISTENT_COMMITTEE_PERIOD
committee_count = max(
len(get_active_validator_indices(state.validator_registry, earlier_start_epoch)) //
(SHARD_COUNT * TARGET_COMMITTEE_SIZE),
len(get_active_validator_indices(state.validator_registry, later_start_epoch)) //
(SHARD_COUNT * TARGET_COMMITTEE_SIZE),
) + 1
2019-05-01 08:09:24 +00:00
2019-03-17 11:44:19 +00:00
index = slot % committee_count
2019-03-28 22:56:43 +00:00
earlier_committee = get_period_committee(state, shard, earlier_start_epoch, index, committee_count)
later_committee = get_period_committee(state, shard, later_start_epoch, index, committee_count)
2019-02-12 11:11:45 +00:00
2019-02-10 06:09:34 +00:00
# Take not-yet-cycled-out validators from earlier committee and already-cycled-in validators from
# later committee; return a sorted list of the union of the two, deduplicated
return sorted(list(set(
2019-03-31 22:49:02 +00:00
[i for i in earlier_committee if epoch % PERSISTENT_COMMITTEE_PERIOD < get_switchover_epoch ( state , epoch , i ) ] +
[i for i in later_committee if epoch % PERSISTENT_COMMITTEE_PERIOD >= get_switchover_epoch(state, epoch, i)]
2019-02-10 06:09:34 +00:00
)))
2019-02-08 09:54:02 +00:00
```
2019-03-17 11:44:19 +00:00
2019-03-28 22:56:43 +00:00
### `get_shard_proposer_index`
2019-02-08 09:54:02 +00:00
2019-02-10 21:44:58 +00:00
```python
def get_shard_proposer_index(state: BeaconState,
2019-02-14 23:02:01 +00:00
shard: Shard,
slot: Slot) -> ValidatorIndex:
2019-03-28 22:56:43 +00:00
# Randomly shift persistent committee
2019-03-17 11:44:19 +00:00
persistent_committee = get_persistent_committee(state, shard, slot)
2019-05-07 09:57:41 +00:00
seed = hash(state.current_shuffling_seed + int_to_bytes(shard, length=8) + int_to_bytes(slot, length=8))
2019-03-28 22:56:43 +00:00
random_index = bytes_to_int(seed[0:8]) % len(persistent_committee)
persistent_committee = persistent_committee[random_index:] + persistent_committee[:random_index]
# Search for an active proposer
for index in persistent_committee:
2019-03-17 11:44:19 +00:00
if is_active_validator(state.validator_registry[index], get_current_epoch(state)):
2019-02-10 21:44:58 +00:00
return index
2018-11-25 13:06:37 +00:00
2019-03-28 22:56:43 +00:00
# No block can be proposed if no validator is active
return None
2018-11-14 21:01:57 +00:00
```
2019-03-28 22:56:43 +00:00
### `get_shard_header`
2018-11-14 21:01:57 +00:00
```python
2019-03-28 22:56:43 +00:00
def get_shard_header(block: ShardBlock) -> ShardBlockHeader:
return ShardBlockHeader(
2019-05-07 11:13:22 +00:00
slot=block.slot,
shard=block.shard,
beacon_chain_root=block.beacon_chain_root,
2019-05-09 00:00:25 +00:00
parent_root=block.parent_root,
2019-05-07 11:13:22 +00:00
body_root=hash_tree_root(block.body),
state_root=block.state_root,
attestations=block.attestations,
signature=block.signature,
2019-02-10 16:17:21 +00:00
)
2018-11-14 21:01:57 +00:00
```
2019-03-28 22:56:43 +00:00
### `verify_shard_attestation_signature`
```python
def verify_shard_attestation_signature(state: BeaconState,
attestation: ShardAttestation) -> None:
data = attestation.data
2019-05-24 17:59:22 +00:00
persistent_committee = get_persistent_committee(state, data.shard, data.slot)
2019-03-28 22:56:43 +00:00
assert verify_bitfield(attestation.aggregation_bitfield, len(persistent_committee))
pubkeys = []
for i, index in enumerate(persistent_committee):
2019-05-07 11:13:22 +00:00
if get_bitfield_bit(attestation.aggregation_bitfield, i) == 0b1:
2019-03-28 22:56:43 +00:00
validator = state.validator_registry[index]
assert is_active_validator(validator, get_current_epoch(state))
pubkeys.append(validator.pubkey)
assert bls_verify(
pubkey=bls_aggregate_pubkeys(pubkeys),
2019-05-24 17:59:22 +00:00
message_hash=data.shard_block_root,
2019-03-28 22:56:43 +00:00
signature=attestation.aggregate_signature,
domain=get_domain(state, slot_to_epoch(data.slot), DOMAIN_SHARD_ATTESTER)
)
2019-02-19 11:26:35 +00:00
```
2019-03-28 22:56:43 +00:00
### `compute_crosslink_data_root`
2019-02-19 11:26:35 +00:00
```python
2019-05-21 10:41:24 +00:00
def compute_crosslink_data_root(blocks: List[ShardBlock]) -> Bytes32:
2019-03-28 22:56:43 +00:00
def is_power_of_two(value: int) -> bool:
return (value > 0) and (value & (value - 1) == 0)
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
def pad_to_power_of_2(values: List[bytes]) -> List[bytes]:
while not is_power_of_two(len(values)):
values += [b'\x00' * BYTES_PER_SHARD_BLOCK_BODY]
return values
2019-02-19 11:26:35 +00:00
2019-05-26 17:41:36 +00:00
def hash_tree_root_of_bytes(data: bytes) -> bytes:
return hash_tree_root([data[i:i + 32] for i in range(0, len(data), 32)])
2019-02-19 11:26:35 +00:00
2019-05-28 07:58:51 +00:00
def zpad(data: bytes, length: int) -> bytes:
return data + b'\x00' * (length - len(data))
2019-03-28 22:56:43 +00:00
return hash(
2019-05-26 17:41:36 +00:00
hash_tree_root(pad_to_power_of_2([
2019-05-28 07:58:51 +00:00
hash_tree_root_of_bytes(
zpad(serialize(get_shard_header(block)), BYTES_PER_SHARD_BLOCK_BODY)
) for block in blocks
]))
+ hash_tree_root(pad_to_power_of_2([
2019-06-05 13:29:26 +00:00
hash_tree_root_of_bytes(block.body) for block in blocks
2019-03-28 22:56:43 +00:00
]))
2019-02-19 11:26:35 +00:00
)
```
2019-03-28 22:56:43 +00:00
## Object validity
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
### Shard blocks
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
Let:
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
* `beacon_blocks` be the `BeaconBlock` list such that `beacon_blocks[slot]` is the canonical `BeaconBlock` at slot `slot`
* `beacon_state` be the canonical `BeaconState` after processing `beacon_blocks[-1]`
* `valid_shard_blocks` be the list of valid `ShardBlock` , recursively defined
* `unix_time` be the current unix time
* `candidate` be a candidate `ShardBlock` for which validity is to be determined by running `is_valid_shard_block`
2019-02-19 11:26:35 +00:00
```python
2019-03-28 22:56:43 +00:00
def is_valid_shard_block(beacon_blocks: List[BeaconBlock],
beacon_state: BeaconState,
valid_shard_blocks: List[ShardBlock],
2019-05-07 12:23:28 +00:00
unix_time: int,
2019-05-07 11:13:22 +00:00
candidate: ShardBlock) -> bool:
2019-03-28 22:56:43 +00:00
# Check if block is already determined valid
for _, block in enumerate(valid_shard_blocks):
if candidate == block:
return True
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check slot number
2019-05-24 17:59:22 +00:00
assert candidate.slot >= PHASE_1_GENESIS_SLOT
2019-03-28 22:56:43 +00:00
assert unix_time >= beacon_state.genesis_time + (block.slot - GENESIS_SLOT) * SECONDS_PER_SLOT
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check shard number
2019-05-24 17:59:22 +00:00
assert candidate.shard < = SHARD_COUNT
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check beacon block
2019-05-24 17:59:22 +00:00
beacon_block = beacon_blocks[candidate.slot]
assert candidate.beacon_block_root == signing_root(beacon_block)
2019-05-26 12:14:48 +00:00
assert beacon_block.slot < = candidate.slot
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check state root
2019-05-24 17:59:22 +00:00
assert candidate.state_root == ZERO_HASH # [to be removed in phase 2]
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check parent block
2019-05-24 17:59:22 +00:00
if candidate.slot == PHASE_1_GENESIS_SLOT:
2019-05-06 19:49:46 +00:00
assert candidate.parent_root == ZERO_HASH
2019-03-28 22:56:43 +00:00
else:
parent_block = next(
2019-05-26 17:41:36 +00:00
(block for block in valid_shard_blocks if signing_root(block) == candidate.parent_root),
None
)
assert parent_block is not None
2019-05-24 17:59:22 +00:00
assert parent_block.shard == candidate.shard
assert parent_block.slot < candidate.slot
2019-04-08 01:51:13 +00:00
assert signing_root(beacon_blocks[parent_block.slot]) == parent_block.beacon_chain_root
2019-03-28 22:56:43 +00:00
# Check attestations
2019-05-24 17:59:22 +00:00
assert len(candidate.attestations) < = MAX_SHARD_ATTESTIONS
for _, attestation in enumerate(candidate.attestations):
assert max(GENESIS_SHARD_SLOT, candidate.slot - SLOTS_PER_EPOCH) < = attestation.data.slot
assert attestation.data.slot < = candidate.slot - MIN_ATTESTATION_INCLUSION_DELAY
assert attestation.data.crosslink.shard == candidate.shard
2019-03-28 22:56:43 +00:00
verify_shard_attestation_signature(beacon_state, attestation)
# Check signature
2019-05-24 17:59:22 +00:00
proposer_index = get_shard_proposer_index(beacon_state, candidate.shard, candidate.slot)
2019-03-28 22:56:43 +00:00
assert proposer_index is not None
assert bls_verify(
2019-05-26 17:41:36 +00:00
pubkey=beacon_state.validator_registry[proposer_index].pubkey,
2019-04-08 01:51:13 +00:00
message_hash=signing_root(block),
2019-05-24 17:59:22 +00:00
signature=candidate.signature,
2019-05-26 17:41:36 +00:00
domain=get_domain(beacon_state, slot_to_epoch(candidate.slot), DOMAIN_SHARD_PROPOSER),
2019-03-03 02:36:04 +00:00
)
2019-03-28 22:56:43 +00:00
return True
2019-03-03 02:36:04 +00:00
```
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
### Shard attestations
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
Let:
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
* `valid_shard_blocks` be the list of valid `ShardBlock`
* `beacon_state` be the canonical `BeaconState`
* `candidate` be a candidate `ShardAttestation` for which validity is to be determined by running `is_valid_shard_attestation`
2019-03-03 02:36:04 +00:00
```python
2019-03-28 22:56:43 +00:00
def is_valid_shard_attestation(valid_shard_blocks: List[ShardBlock],
beacon_state: BeaconState,
2019-05-24 17:59:22 +00:00
candidate: ShardAttestation) -> bool:
2019-03-28 22:56:43 +00:00
# Check shard block
shard_block = next(
2019-05-26 12:14:48 +00:00
(block for block in valid_shard_blocks if signing_root(block) == candidate.data.shard_block_root),
None,
)
2019-05-26 17:41:36 +00:00
assert shard_block is not None
2019-05-24 17:59:22 +00:00
assert shard_block.slot == candidate.data.slot
assert shard_block.shard == candidate.data.shard
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
# Check signature
2019-05-24 17:59:22 +00:00
verify_shard_attestation_signature(beacon_state, candidate)
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
return True
2019-03-03 02:36:04 +00:00
```
2019-03-28 22:56:43 +00:00
### Beacon attestations
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
Let:
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
* `shard` be a valid `Shard`
* `shard_blocks` be the `ShardBlock` list such that `shard_blocks[slot]` is the canonical `ShardBlock` for shard `shard` at slot `slot`
* `beacon_state` be the canonical `BeaconState`
* `valid_attestations` be the list of valid `Attestation` , recursively defined
2019-05-06 15:30:32 +00:00
* `candidate` be a candidate `Attestation` which is valid under Phase 0 rules, and for which validity is to be determined under Phase 1 rules by running `is_valid_beacon_attestation`
2019-02-19 11:26:35 +00:00
```python
2019-03-28 22:56:43 +00:00
def is_valid_beacon_attestation(shard: Shard,
shard_blocks: List[ShardBlock],
beacon_state: BeaconState,
valid_attestations: List[Attestation],
candidate: Attestation) -> bool:
# Check if attestation is already determined valid
for _, attestation in enumerate(valid_attestations):
if candidate == attestation:
return True
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check previous attestation
if candidate.data.previous_crosslink.epoch < = PHASE_1_GENESIS_EPOCH:
2019-05-06 17:26:14 +00:00
assert candidate.data.previous_crosslink.data_root == ZERO_HASH
2019-02-19 11:26:35 +00:00
else:
2019-03-28 22:56:43 +00:00
previous_attestation = next(
2019-05-07 11:13:22 +00:00
(attestation for attestation in valid_attestations if
2019-05-26 17:41:36 +00:00
attestation.data.crosslink.data_root == candidate.data.previous_crosslink.data_root),
None,
)
assert previous_attestation is not None
2019-03-28 22:56:43 +00:00
assert candidate.data.previous_attestation.epoch < slot_to_epoch ( candidate . data . slot )
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
# Check crosslink data root
2019-05-26 17:41:36 +00:00
start_epoch = beacon_state.latest_crosslinks[shard].epoch
2019-05-05 11:10:39 +00:00
end_epoch = min(slot_to_epoch(candidate.data.slot) - CROSSLINK_LOOKBACK, start_epoch + MAX_EPOCHS_PER_CROSSLINK)
2019-03-28 22:56:43 +00:00
blocks = []
for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH):
blocks.append(shard_blocks[slot])
2019-05-06 17:26:14 +00:00
assert candidate.data.crosslink.data_root == compute_crosslink_data_root(blocks)
2019-02-19 11:26:35 +00:00
2019-03-28 22:56:43 +00:00
return True
2019-02-19 11:26:35 +00:00
```
2019-03-03 02:36:04 +00:00
2019-03-28 22:56:43 +00:00
## Shard fork choice rule
2019-03-03 02:36:04 +00:00
2019-04-16 17:03:22 +00:00
The fork choice rule for any shard is LMD GHOST using the shard attestations of the persistent 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. `state.crosslinks[shard].shard_block_root` ). Only blocks whose `beacon_chain_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.)