mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-19 23:19:28 +00:00
WIP!
This commit is contained in:
parent
334d6c6bc7
commit
d7e628e08f
@ -19,43 +19,43 @@
|
||||
- [Signature domain types](#signature-domain-types)
|
||||
- [Containers](#containers)
|
||||
- [`ShardBlockSignatures`](#shardblocksignatures)
|
||||
- [`ShardBlockData`](#shardblockdata)
|
||||
- [`ShardBlock`](#shardblock)
|
||||
- [`ShardBlockHeaderData`](#shardblockheaderdata)
|
||||
- [`ShardBlockHeader`](#shardblockheader)
|
||||
- [`ShardState`](#shardstate)
|
||||
- [`ShardReceiptDelta`](#shardreceiptdelta)
|
||||
- [`ShardReceipt`](#shardreceipt)
|
||||
- [`ShardCheckpoint`](#shardcheckpoint)
|
||||
- [Helper functions](#helper-functions)
|
||||
- [Misc](#misc-1)
|
||||
- [`pad`](#pad)
|
||||
- [`compute_slot_of_shard_slot`](#compute_slot_of_shard_slot)
|
||||
- [`compute_padded_data`](#compute_padded_data)
|
||||
- [`compute_epoch_of_shard_slot`](#compute_epoch_of_shard_slot)
|
||||
- [`compute_period_start_epoch`](#compute_period_start_epoch)
|
||||
- [`compute_shard_period_start_epoch`](#compute_shard_period_start_epoch)
|
||||
- [`compute_flat_shard_header`](#compute_flat_shard_header)
|
||||
- [`compute_crosslink_data_root`](#compute_crosslink_data_root)
|
||||
- [State accessors](#state-accessors)
|
||||
- [Beacon state accessors](#beacon-state-accessors)
|
||||
- [`get_period_committee`](#get_period_committee)
|
||||
- [`get_persistent_committee`](#get_persistent_committee)
|
||||
- [`get_shard_committee`](#get_shard_committee)
|
||||
- [`get_shard_proposer_index`](#get_shard_proposer_index)
|
||||
- [`get_default_shard_state`](#get_default_shard_state)
|
||||
- [`get_shard_base_reward`](#get_shard_base_reward)
|
||||
- [State mutators](#state-mutators)
|
||||
- [`add_fee`](#add_fee)
|
||||
- [Shard state mutators](#shard-state-mutators)
|
||||
- [`add_reward`](#add_reward)
|
||||
- [`add_fee`](#add_fee)
|
||||
- [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 data fees](#block-data-fees)
|
||||
- [Object validity](#object-validity)
|
||||
- [Shard block validation: preliminary](#shard-block-validation-preliminary)
|
||||
- [Beacon attestations](#beacon-attestations)
|
||||
- [Block size fee](#block-size-fee)
|
||||
- [Shard fork choice rule](#shard-fork-choice-rule)
|
||||
|
||||
<!-- /TOC -->
|
||||
|
||||
## Introduction
|
||||
|
||||
This document describes the shard data layer and the shard fork choice rule in Phase 1 of Ethereum 2.0.
|
||||
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.
|
||||
|
||||
## Custom types
|
||||
|
||||
@ -69,8 +69,8 @@ This document describes the shard data layer and the shard fork choice rule in P
|
||||
|
||||
| Name | Value |
|
||||
| - | - |
|
||||
| `SHARD_SLOTS_PER_EPOCH` | `2**7` (= 128) |
|
||||
| `TARGET_PERSISTENT_COMMITTEE_SIZE` | `2**7` (= 128) |
|
||||
| `MIN_BLOCK_SIZE_PRICE` | `2**0` (= 1) |
|
||||
| `MAX_PERIOD_COMMITTEE_SIZE` | `2**7` (= 128) |
|
||||
| `SHARD_HEADER_SIZE` | `2**9` (= 512) |
|
||||
| `SHARD_BLOCK_SIZE_TARGET` | `2**14` (= 16,384) |
|
||||
| `SHARD_BLOCK_SIZE_LIMIT` | `2**16` (= 65,536) |
|
||||
@ -79,13 +79,13 @@ This document describes the shard data layer and the shard fork choice rule in P
|
||||
|
||||
| Name | Value |
|
||||
| - | - |
|
||||
| `PHASE_1_FORK_EPOCH` | **TBD** |
|
||||
| `SHARD_GENESIS_EPOCH` | **TBD** |
|
||||
|
||||
### Time parameters
|
||||
|
||||
| Name | Value | Unit | Duration |
|
||||
| - | - | :-: | :-: |
|
||||
| `CROSSLINK_LOOKBACK` | `2**0` (= 1) | epochs | 6.4 minutes |
|
||||
| `SHARD_SLOTS_PER_EPOCH` | `2**7` (= 128) | shard slots | 6.4 minutes |
|
||||
| `EPOCHS_PER_SHARD_PERIOD` | `2**8` (= 256) | epochs | ~27 hours |
|
||||
|
||||
### State list lengths
|
||||
@ -98,8 +98,7 @@ This document describes the shard data layer and the shard fork choice rule in P
|
||||
|
||||
| Name | Value |
|
||||
| - | - |
|
||||
| `BASEFEE_ADJUSTMENT_FACTOR` | `2**3` (= 8) |
|
||||
| `REWARD_COEFFICIENT_BASE` | `2**20` (= 1,048,576) |
|
||||
| `BLOCK_SIZE_PRICE_QUOTIENT` | `2**3` (= 8) |
|
||||
|
||||
### Signature domain types
|
||||
|
||||
@ -118,33 +117,45 @@ class ShardBlockSignatures(Container):
|
||||
proposer: BLSSignature
|
||||
```
|
||||
|
||||
### `ShardBlockData`
|
||||
|
||||
```python
|
||||
class ShardBlockData(Container):
|
||||
slot: ShardSlot
|
||||
beacon_block_root: Hash
|
||||
parent_root: Hash
|
||||
state_root: Hash
|
||||
aggregation_bits: Bitvector[2 * MAX_PERIOD_COMMITTEE_SIZE]
|
||||
block_size_sum: uint64
|
||||
body: List[byte, SHARD_BLOCK_SIZE_LIMIT - SHARD_HEADER_SIZE]
|
||||
```
|
||||
|
||||
### `ShardBlock`
|
||||
|
||||
```python
|
||||
class ShardBlock(Container):
|
||||
data: ShardBlockData
|
||||
signatures: ShardBlockSignatures
|
||||
```
|
||||
|
||||
### `ShardBlockHeaderData`
|
||||
|
||||
```python
|
||||
class ShardBlockHeaderData(Container):
|
||||
slot: ShardSlot
|
||||
beacon_chain_root: Hash
|
||||
beacon_block_root: Hash
|
||||
parent_root: Hash
|
||||
state_root: Hash
|
||||
aggregation_bits: Bitvector[TARGET_PERSISTENT_COMMITTEE_SIZE * 2]
|
||||
total_bytes: uint64
|
||||
body: Bytes[SHARD_BLOCK_SIZE_LIMIT - SHARD_HEADER_SIZE]
|
||||
padding: Bytes[32]
|
||||
signatures: ShardBlockSignatures
|
||||
aggregation_bits: Bitvector[2 * MAX_PERIOD_COMMITTEE_SIZE]
|
||||
block_size_sum: uint64
|
||||
body_root: Hash
|
||||
```
|
||||
|
||||
### `ShardBlockHeader`
|
||||
|
||||
```python
|
||||
class ShardBlockHeader(Container):
|
||||
slot: ShardSlot
|
||||
beacon_chain_root: Hash
|
||||
parent_root: Hash
|
||||
state_root: Hash
|
||||
aggregation_bits: Bitvector[TARGET_PERSISTENT_COMMITTEE_SIZE * 2]
|
||||
total_bytes: uint64
|
||||
body_root: Hash
|
||||
padding: Bytes[32]
|
||||
data: ShardBlockHeaderData
|
||||
signatures: ShardBlockSignatures
|
||||
```
|
||||
|
||||
@ -152,51 +163,60 @@ class ShardBlockHeader(Container):
|
||||
|
||||
```python
|
||||
class ShardState(Container):
|
||||
history_accumulator: Vector[Hash, HISTORY_ACCUMULATOR_VECTOR]
|
||||
earlier_committee_rewards: List[uint64, TARGET_PERSISTENT_COMMITTEE_SIZE]
|
||||
later_committee_rewards: List[uint64, TARGET_PERSISTENT_COMMITTEE_SIZE]
|
||||
earlier_committee_fees: List[Gwei, TARGET_PERSISTENT_COMMITTEE_SIZE]
|
||||
later_committee_fees: List[Gwei, TARGET_PERSISTENT_COMMITTEE_SIZE]
|
||||
basefee: Gwei
|
||||
slot: ShardSlot
|
||||
shard: Shard
|
||||
latest_block_header: ShardBlockHeader
|
||||
slot: ShardSlot
|
||||
history_accumulator: Vector[Hash, HISTORY_ACCUMULATOR_VECTOR]
|
||||
latest_block_header_data: ShardBlockHeader
|
||||
receipt_root: Hash
|
||||
total_bytes: uint64
|
||||
block_size_sum: uint64
|
||||
# Rewards and fees
|
||||
block_size_price: Gwei
|
||||
older_committee_rewards: List[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
|
||||
newer_committee_rewards: List[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
|
||||
older_committee_fees: List[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
|
||||
newer_committee_fees: List[Gwei, MAX_PERIOD_COMMITTEE_SIZE]
|
||||
```
|
||||
|
||||
### `ShardReceiptDelta`
|
||||
### `ShardReceipt`
|
||||
|
||||
```python
|
||||
class ShardReceiptDelta(Container):
|
||||
class ShardReceipt(Container):
|
||||
index: ValidatorIndex
|
||||
reward_coefficient: uint64
|
||||
block_fee: Gwei
|
||||
rewards: Gwei
|
||||
fees: Gwei
|
||||
```
|
||||
|
||||
### `ShardCheckpoint`
|
||||
|
||||
```python
|
||||
class ShardCheckpoint(Container):
|
||||
slot: ShardSlot
|
||||
parent_root: Hash
|
||||
```
|
||||
|
||||
## Helper functions
|
||||
|
||||
### Misc
|
||||
|
||||
#### `pad`
|
||||
#### `compute_padded_data`
|
||||
|
||||
```python
|
||||
def pad(x: bytes, length: uint64) -> bytes:
|
||||
assert len(x) <= length
|
||||
return x + b'\x00' * (length - len(x))
|
||||
def compute_padded_data(data: bytes, length: uint64) -> bytes:
|
||||
assert len(data) <= length
|
||||
return data + b'\x00' * (length - len(data))
|
||||
```
|
||||
|
||||
#### `compute_epoch_of_shard_slot`
|
||||
|
||||
```python
|
||||
def compute_epoch_of_shard_slot(slot: ShardSlot) -> Epoch:
|
||||
return compute_epoch_of_slot(compute_slot_of_shard_slot(slot))
|
||||
return compute_epoch_of_slot(slot // SHARD_SLOTS_PER_EPOCH)
|
||||
```
|
||||
|
||||
#### `compute_period_start_epoch`
|
||||
#### `compute_shard_period_start_epoch`
|
||||
|
||||
```python
|
||||
def compute_period_start_epoch(epoch: Epoch, lookback: Epoch=0) -> Epoch:
|
||||
def compute_shard_period_start_epoch(epoch: Epoch, lookback: uint64) -> Epoch:
|
||||
return Epoch(epoch - (epoch % EPOCHS_PER_SHARD_PERIOD) - lookback * EPOCHS_PER_SHARD_PERIOD)
|
||||
```
|
||||
|
||||
@ -205,19 +225,22 @@ def compute_period_start_epoch(epoch: Epoch, lookback: Epoch=0) -> Epoch:
|
||||
```python
|
||||
def compute_flat_shard_header(block: ShardBlock) -> Bytes[SHARD_HEADER_SIZE]:
|
||||
"""
|
||||
Return a flat serialisation of the ``block`` header which preserves hash tree root.
|
||||
Return a flat serialisation of the ``block`` header, preserving hash tree root.
|
||||
"""
|
||||
data = block.data
|
||||
return (
|
||||
pad(int_to_bytes(block.slot, length=8), 32) +
|
||||
block.beacon_chain_root +
|
||||
block.parent_root +
|
||||
hash_tree_root(block.body) +
|
||||
block.state_root +
|
||||
pad(int_to_bytes(block.total_bytes, length=8), 32) +
|
||||
bytes([sum([block.aggregation_bits[i + j] << j for j in range(8)]) for i in range(0, 256, 8)]) +
|
||||
block.padding +
|
||||
pad(block.signatures.attesters, 128) +
|
||||
pad(block.signatures.proposer, 128)
|
||||
# Left half of the hash tree
|
||||
compute_padded_data(int_to_bytes(data.slot, length=8), 32) +
|
||||
data.beacon_block_root +
|
||||
data.parent_root +
|
||||
hash_tree_root(data.body) +
|
||||
data.state_root +
|
||||
compute_padded_data(int_to_bytes(data.block_size_sum, length=8), 32) +
|
||||
bytes([sum([data.aggregation_bits[i + j] << j for j in range(8)]) for i in range(0, 256, 8)]) +
|
||||
Bytes32() + # Padding
|
||||
# Right half of the hash tree
|
||||
compute_padded_data(block.signatures.attesters, 128) +
|
||||
compute_padded_data(block.signatures.proposer, 128)
|
||||
)
|
||||
```
|
||||
|
||||
@ -226,40 +249,32 @@ def compute_flat_shard_header(block: ShardBlock) -> Bytes[SHARD_HEADER_SIZE]:
|
||||
```python
|
||||
def compute_crosslink_data_root(blocks: Sequence[ShardBlock]) -> Hash:
|
||||
headers = b''.join([compute_flat_shard_header(block) for block in blocks])
|
||||
bodies = b''.join([block.body for block in blocks])
|
||||
MAX_SIZE = SHARD_BLOCK_SIZE_LIMIT * SHARD_SLOTS_PER_EPOCH * MAX_EPOCHS_PER_CROSSLINK
|
||||
return hash_tree_root(BytesN[MAX_SIZE](pad(headers + bodies, MAX_SIZE)))
|
||||
bodies = b''.join([block.data.body for block in blocks])
|
||||
MAX_SIZE = MAX_EPOCHS_PER_CROSSLINK * SHARD_SLOTS_PER_EPOCH * SHARD_BLOCK_SIZE_LIMIT
|
||||
return hash_tree_root(BytesN[MAX_SIZE](compute_padded_data(headers + bodies, MAX_SIZE)))
|
||||
```
|
||||
|
||||
### State accessors
|
||||
### Beacon state accessors
|
||||
|
||||
#### `get_period_committee`
|
||||
|
||||
```python
|
||||
def get_period_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> Sequence[ValidatorIndex]:
|
||||
full_committee = compute_committee(
|
||||
indices=get_active_validator_indices(state, epoch),
|
||||
seed=get_seed(state, epoch),
|
||||
index=shard,
|
||||
count=SHARD_COUNT,
|
||||
)
|
||||
|
||||
return full_committee[:TARGET_PERSISTENT_COMMITTEE_SIZE]
|
||||
def get_period_committee(state: BeaconState, shard: Shard, epoch: Epoch) -> Sequence[ValidatorIndex]:
|
||||
active_validator_indices = get_active_validator_indices(state, epoch)
|
||||
seed = get_seed(state, epoch)
|
||||
return compute_committee(active_validator_indices, seed, shard, SHARD_COUNT)[:MAX_PERIOD_COMMITTEE_SIZE]
|
||||
```
|
||||
|
||||
#### `get_persistent_committee`
|
||||
#### `get_shard_committee`
|
||||
|
||||
```python
|
||||
def get_persistent_committee(state: BeaconState, shard: Shard, epoch: Epoch) -> Sequence[ValidatorIndex]:
|
||||
earlier_committee = get_period_committee(state, compute_period_start_epoch(epoch, lookback=2), shard)
|
||||
later_committee = get_period_committee(state, compute_period_start_epoch(epoch, lookback=1), shard)
|
||||
|
||||
# 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(set(
|
||||
[i for i in earlier_committee if epoch % EPOCHS_PER_SHARD_PERIOD < i % EPOCHS_PER_SHARD_PERIOD]
|
||||
+ [i for i in later_committee if epoch % EPOCHS_PER_SHARD_PERIOD >= i % EPOCHS_PER_SHARD_PERIOD]
|
||||
))
|
||||
def get_shard_committee(state: BeaconState, shard: Shard, epoch: Epoch) -> Sequence[ValidatorIndex]:
|
||||
older_committee = get_period_committee(state, shard, compute_shard_period_start_epoch(epoch, 2))
|
||||
newer_committee = get_period_committee(state, shard, compute_shard_period_start_epoch(epoch, 1))
|
||||
# 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]
|
||||
return older_subcommittee + newer_subcommittee
|
||||
```
|
||||
|
||||
#### `get_shard_proposer_index`
|
||||
@ -267,294 +282,219 @@ def get_persistent_committee(state: BeaconState, shard: Shard, epoch: Epoch) ->
|
||||
```python
|
||||
def get_shard_proposer_index(state: BeaconState, shard: Shard, slot: ShardSlot) -> ValidatorIndex:
|
||||
epoch = get_current_epoch(state)
|
||||
persistent_committee = list(get_persistent_committee(state, shard, epoch))
|
||||
active_indices = [i for i in persistent_committee if is_active_validator(state.validators[i], epoch)]
|
||||
assert len(active_indices) > 0
|
||||
|
||||
MAX_RANDOM_BYTE = 2**8 - 1
|
||||
seed = hash(get_seed(state, epoch) + int_to_bytes(shard, length=8) + int_to_bytes(slot, length=8))
|
||||
i = 0
|
||||
while True:
|
||||
candidate_index = active_indices[(slot + i) % len(active_indices)]
|
||||
random_byte = hash(seed + int_to_bytes(i // 32, length=8))[i % 32]
|
||||
effective_balance = state.validators[candidate_index].effective_balance
|
||||
if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte:
|
||||
return ValidatorIndex(candidate_index)
|
||||
i += 1
|
||||
active_indices = [i for i in get_shard_committee(state, shard, epoch) if is_active_validator(state.validators[i], epoch)]
|
||||
seed = hash(get_seed(state, epoch) + int_to_bytes(slot, length=8) + int_to_bytes(shard, length=8))
|
||||
compute_proposer_index(state, active_indices, seed)
|
||||
```
|
||||
|
||||
#### `get_default_shard_state`
|
||||
|
||||
```python
|
||||
def get_default_shard_state(beacon_state: BeaconState, shard: Shard) -> ShardState:
|
||||
earlier_committee = get_period_committee(
|
||||
beacon_state,
|
||||
Epoch(PHASE_1_FORK_EPOCH - EPOCHS_PER_SHARD_PERIOD * 2),
|
||||
shard,
|
||||
)
|
||||
later_committee = get_period_committee(
|
||||
beacon_state,
|
||||
Epoch(PHASE_1_FORK_EPOCH - EPOCHS_PER_SHARD_PERIOD),
|
||||
shard,
|
||||
)
|
||||
return ShardState(
|
||||
basefee=1,
|
||||
shard=shard,
|
||||
slot=ShardSlot(PHASE_1_FORK_EPOCH * SHARD_SLOTS_PER_EPOCH),
|
||||
earlier_committee_rewards=[REWARD_COEFFICIENT_BASE for _ in range(len(earlier_committee))],
|
||||
later_committee_rewards=[REWARD_COEFFICIENT_BASE for _ in range(len(later_committee))],
|
||||
earlier_committee_fees=[Gwei(0) for _ in range(len(earlier_committee))],
|
||||
later_committee_fees=[Gwei(0) for _ in range(len(later_committee))],
|
||||
)
|
||||
```
|
||||
|
||||
#### `get_shard_base_reward`
|
||||
|
||||
```python
|
||||
def get_shard_base_reward(beacon_state: BeaconState) -> Gwei:
|
||||
total_balance_root = integer_squareroot(get_total_active_balance(beacon_state))
|
||||
return Gwei(REWARD_COEFFICIENT_BASE * BASE_REWARD_FACTOR // total_balance_root // BASE_REWARDS_PER_EPOCH)
|
||||
```
|
||||
|
||||
### State mutators
|
||||
### Shard state mutators
|
||||
|
||||
#### `add_reward`
|
||||
|
||||
```python
|
||||
def add_reward(state: ShardState, beacon_state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
||||
def add_reward(state: BeaconState, shard_state: ShardState, index: ValidatorIndex, delta: Gwei) -> None:
|
||||
epoch = compute_epoch_of_shard_slot(state.slot)
|
||||
earlier_committee = get_period_committee(beacon_state, compute_period_start_epoch(epoch, lookback=2), state.shard)
|
||||
later_committee = get_period_committee(beacon_state, compute_period_start_epoch(epoch, lookback=1), state.shard)
|
||||
if index in earlier_committee:
|
||||
state.earlier_committee_rewards[earlier_committee.index(index)] += delta
|
||||
elif index in later_committee:
|
||||
state.later_committee_rewards[later_committee.index(index)] += delta
|
||||
older_committee = get_period_committee(state, shard_state.shard, compute_shard_period_start_epoch(epoch, 2))
|
||||
newer_committee = get_period_committee(state, shard_state.shard, compute_shard_period_start_epoch(epoch, 1))
|
||||
if index in older_committee:
|
||||
shard_state.older_committee_rewards[older_committee.index(index)] += delta
|
||||
elif index in newer_committee:
|
||||
shard_state.newer_committee_rewards[newer_committee.index(index)] += delta
|
||||
```
|
||||
|
||||
#### `add_fee`
|
||||
|
||||
```python
|
||||
def add_fee(state: ShardState, beacon_state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
||||
def add_fee(state: BeaconState, shard_state: ShardState, index: ValidatorIndex, delta: Gwei) -> None:
|
||||
epoch = compute_epoch_of_shard_slot(state.slot)
|
||||
earlier_committee = get_period_committee(beacon_state, compute_period_start_epoch(epoch, lookback=2), state.shard)
|
||||
later_committee = get_period_committee(beacon_state, compute_period_start_epoch(epoch, lookback=1), state.shard)
|
||||
if index in earlier_committee:
|
||||
state.earlier_committee_fees[earlier_committee.index(index)] += delta
|
||||
elif index in later_committee:
|
||||
state.later_committee_fees[later_committee.index(index)] += delta
|
||||
older_committee = get_period_committee(state, shard_state.shard, compute_shard_period_start_epoch(epoch, 2))
|
||||
newer_committee = get_period_committee(state, shard_state.shard, compute_shard_period_start_epoch(epoch, 1))
|
||||
if index in older_committee:
|
||||
shard_state.older_committee_fees[older_committee.index(index)] += delta
|
||||
elif index in newer_committee:
|
||||
shard_state.newer_committee_fees[newer_committee.index(index)] += delta
|
||||
```
|
||||
|
||||
## Genesis
|
||||
|
||||
### `get_genesis_shard_state`
|
||||
|
||||
```python
|
||||
def get_genesis_shard_state(state: BeaconState, shard: Shard) -> ShardState:
|
||||
older_committee = get_period_committee(state, shard, compute_shard_period_start_epoch(SHARD_GENESIS_EPOCH, 2))
|
||||
newer_committee = get_period_committee(state, shard, compute_shard_period_start_epoch(SHARD_GENESIS_EPOCH, 1))
|
||||
return ShardState(
|
||||
shard=shard,
|
||||
slot=ShardSlot(SHARD_GENESIS_EPOCH * SHARD_SLOTS_PER_EPOCH),
|
||||
block_size_price=MIN_BLOCK_SIZE_PRICE,
|
||||
older_committee_rewards=[Gwei(0) for _ in range(len(older_committee))],
|
||||
newer_committee_rewards=[Gwei(0) for _ in range(len(newer_committee))],
|
||||
older_committee_fees=[Gwei(0) for _ in range(len(older_committee))],
|
||||
newer_committee_fees=[Gwei(0) for _ in range(len(newer_committee))],
|
||||
)
|
||||
```
|
||||
|
||||
### `get_genesis_shard_block`
|
||||
|
||||
```python
|
||||
def get_genesis_shard_block(state: BeaconState, shard: Shard) -> ShardBlock:
|
||||
genesis_state = get_genesis_shard_state(state, shard)
|
||||
return ShardBlock(data=ShardBlockData(
|
||||
shard=shard,
|
||||
slot=ShardSlot(SHARD_GENESIS_EPOCH * SHARD_SLOTS_PER_EPOCH),
|
||||
state_root=hash_tree_root(genesis_state),
|
||||
))
|
||||
```
|
||||
|
||||
## Shard state transition function
|
||||
|
||||
The post-state corresponding to a pre-state `state`, a beacon state `beacon_state`, and a block `block` is defined as `shard_state_transition(state, beacon_state, block)`. State transitions that trigger an unhandled exception (e.g. a failed `assert` or an out-of-range list access) are considered invalid.
|
||||
|
||||
```python
|
||||
def shard_state_transition(state: ShardState,
|
||||
beacon_state: BeaconState,
|
||||
def shard_state_transition(state: BeaconState,
|
||||
shard_state: ShardState,
|
||||
block: ShardBlock,
|
||||
validate_state_root: bool=False) -> ShardState:
|
||||
# Process slots (including those with no blocks) since block
|
||||
process_shard_slots(state, beacon_state, block.slot)
|
||||
process_shard_slots(state, shard_state, block.data.slot)
|
||||
# Process block
|
||||
process_shard_block(state, beacon_state, block)
|
||||
process_shard_block(state, shard_state, block)
|
||||
# Validate state root (`validate_state_root == True` in production)
|
||||
if validate_state_root:
|
||||
assert block.state_root == hash_tree_root(state)
|
||||
assert block.data.state_root == hash_tree_root(shard_state)
|
||||
# Return post-state
|
||||
return state
|
||||
return shard_state
|
||||
```
|
||||
|
||||
```python
|
||||
def process_shard_slots(state: ShardState, beacon_state: BeaconState, slot: ShardSlot) -> None:
|
||||
assert state.slot <= slot
|
||||
while state.slot < slot:
|
||||
process_shard_slot(state)
|
||||
def process_shard_slots(state: BeaconState, shard_state: ShardState, slot: ShardSlot) -> None:
|
||||
assert shard_state.slot <= slot
|
||||
while shard_state.slot < slot:
|
||||
process_shard_slot(state, shard_state)
|
||||
# Process period on the start slot of the next period
|
||||
if (state.slot + 1) % (SHARD_SLOTS_PER_EPOCH * EPOCHS_PER_SHARD_PERIOD) == 0:
|
||||
process_shard_period(state)
|
||||
state.slot += ShardSlot(1)
|
||||
if (shard_state.slot + 1) % (SHARD_SLOTS_PER_EPOCH * EPOCHS_PER_SHARD_PERIOD) == 0:
|
||||
process_shard_period(state, shard_state)
|
||||
shard_state.slot += ShardSlot(1)
|
||||
```
|
||||
|
||||
```python
|
||||
def process_shard_slot(state: ShardState, beacon_state: BeaconState, slot: ShardSlot) -> None:
|
||||
def process_shard_slot(state: BeaconState, shard_state: ShardState) -> None:
|
||||
# Cache state root
|
||||
if state.latest_block_header.state_root == Hash():
|
||||
state.latest_block_header.state_root = hash_tree_root(state)
|
||||
# Save state roots in history accumulator
|
||||
previous_state_root = hash_tree_root(state)
|
||||
if state.latest_block_header_data.state_root == Bytes32():
|
||||
state.latest_block_header_data.state_root = previous_state_root
|
||||
# Cache state root in history accumulator
|
||||
depth = 0
|
||||
state_root = hash_tree_root(state)
|
||||
while state.slot % 2**depth == 0 and depth < HISTORY_ACCUMULATOR_VECTOR:
|
||||
state.history_accumulator[depth] = state_root
|
||||
state.history_accumulator[depth] = previous_state_root
|
||||
depth += 1
|
||||
```
|
||||
|
||||
### Period processing
|
||||
|
||||
```python
|
||||
def process_shard_period(state: ShardState, beacon_state: BeaconState) -> None:
|
||||
def process_shard_period(shard_state: ShardState, state: BeaconState) -> None:
|
||||
epoch = compute_epoch_of_shard_slot(state.slot)
|
||||
earlier_committee = get_period_committee(
|
||||
beacon_state,
|
||||
compute_period_start_epoch(epoch, lookback=2),
|
||||
state.shard,
|
||||
)
|
||||
later_committee = get_period_committee(
|
||||
beacon_state,
|
||||
compute_period_start_epoch(epoch, lookback=1),
|
||||
state.shard,
|
||||
)
|
||||
|
||||
state.receipt_root = hash_tree_root(List[ShardReceiptDelta, TARGET_PERSISTENT_COMMITTEE_SIZE]([
|
||||
ShardReceiptDelta(validator_index, state.earlier_committee_rewards[i], state.earlier_committee_fees[i])
|
||||
for i, validator_index in enumerate(earlier_committee)
|
||||
older_committee = get_period_committee(state, state.shard, compute_shard_period_start_epoch(epoch, 2))
|
||||
newer_committee = get_period_committee(state, state.shard, compute_shard_period_start_epoch(epoch, 1))
|
||||
# Compute receipt root for older committee
|
||||
state.receipt_root = hash_tree_root(List[ShardReceipt, MAX_PERIOD_COMMITTEE_SIZE]([
|
||||
ShardReceipt(validator_index, state.older_committee_rewards[i], state.older_committee_fees[i])
|
||||
for i, validator_index in enumerate(older_committee)
|
||||
]))
|
||||
state.earlier_committee_rewards = state.later_committee_rewards
|
||||
state.earlier_committee_fees = state.later_committee_fees
|
||||
state.later_committee_rewards = [REWARD_COEFFICIENT_BASE for _ in range(len(later_committee))]
|
||||
state.later_committee_fees = [Gwei(0) for _ in range(len(later_committee))]
|
||||
# Rotate rewards and fees
|
||||
state.older_committee_rewards = state.newer_committee_rewards
|
||||
state.newer_committee_rewards = [Gwei(0) for _ in range(len(newer_committee))]
|
||||
state.older_committee_fees = state.newer_committee_fees
|
||||
state.newer_committee_fees = [Gwei(0) for _ in range(len(newer_committee))]
|
||||
```
|
||||
|
||||
### Block processing
|
||||
|
||||
```python
|
||||
def process_shard_block(state: ShardState, beacon_state: BeaconState, block: ShardBlock) -> None:
|
||||
process_shard_block_header(state, beacon_state, block)
|
||||
process_shard_attestations(state, beacon_state, block
|
||||
process_shard_block_data_fees(state, beacon_state, block)
|
||||
def process_shard_block(state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
|
||||
process_shard_block_header(state, shard_state, block)
|
||||
process_shard_attestations(state, shard_state, block)
|
||||
process_shard_block_size_fee(state, shard_state, block)
|
||||
```
|
||||
|
||||
#### Block header
|
||||
|
||||
```python
|
||||
def process_shard_block_header(state: ShardState, beacon_state: BeaconState, block: ShardBlock) -> None:
|
||||
def process_shard_block_header(state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
|
||||
# Verify that the slots match
|
||||
assert block.slot == state.slot
|
||||
data = block.data
|
||||
assert data.slot == state.slot
|
||||
# Verify that the beacon chain root matches
|
||||
parent_epoch = compute_epoch_of_shard_slot(state.latest_block_header_data.slot)
|
||||
assert data.beacon_block_root == get_block_root(state, parent_epoch)
|
||||
# Verify that the parent matches
|
||||
if block.parent_root != Hash():
|
||||
assert block.parent_root == signing_root(state.latest_block_header)
|
||||
assert data.parent_root == hash_tree_root(state.latest_block_header_data)
|
||||
# Save current block as the new latest block
|
||||
state.latest_block_header = ShardBlockHeader(
|
||||
slot=block.slot,
|
||||
beacon_chain_root=block.beacon_chain_root,
|
||||
parent_root=block.parent_root,
|
||||
state.latest_block_header_data = ShardBlockHeaderData(
|
||||
slot=data.slot,
|
||||
beacon_block_root=data.beacon_block_root,
|
||||
parent_root=data.parent_root,
|
||||
# `state_root` is zeroed and overwritten in the next `process_shard_slot` call
|
||||
aggregation_bits=block.aggregation_bits,
|
||||
total_bytes=block.total_bytes,
|
||||
body_root=hash_tree_root(block.body),
|
||||
# `signatures` is zeroed
|
||||
aggregation_bits=data.aggregation_bits,
|
||||
block_size_sum=data.block_size_sum,
|
||||
body_root=hash_tree_root(data.body),
|
||||
)
|
||||
# Verify proposer signature
|
||||
proposer_index = get_shard_proposer_index(beacon_state, state.shard, block.slot)
|
||||
pubkey = beacon_state.validators[proposer_index].pubkey
|
||||
domain = get_domain(beacon_state, DOMAIN_SHARD_PROPOSER, compute_epoch_of_shard_slot(block.slot))
|
||||
assert bls_verify(pubkey, signing_root(block), block.signatures.proposer, domain)
|
||||
# Verify total bytes count
|
||||
state.total_bytes += len(block.body)
|
||||
assert block.total_bytes == state.total_bytes
|
||||
proposer_index = get_shard_proposer_index(state, state.shard, data.slot)
|
||||
pubkey = state.validators[proposer_index].pubkey
|
||||
domain = get_domain(state, DOMAIN_SHARD_PROPOSER, compute_epoch_of_shard_slot(data.slot))
|
||||
assert bls_verify(pubkey, hash_tree_root(block.data), block.signatures.proposer, domain)
|
||||
# Verify total body bytes count
|
||||
state.block_size_sum += SHARD_HEADER_SIZE + len(data.body)
|
||||
assert data.block_size_sum == state.block_size_sum
|
||||
```
|
||||
|
||||
#### Attestations
|
||||
|
||||
```python
|
||||
def process_shard_attestations(state: ShardState, beacon_state: BeaconState, block: ShardBlock) -> None:
|
||||
persistent_committee = get_persistent_committee(beacon_state, state.shard, block.slot)
|
||||
def process_shard_attestations(state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
|
||||
data = block.data
|
||||
pubkeys = []
|
||||
attestation_count = 0
|
||||
base_reward = get_shard_base_reward(beacon_state)
|
||||
for i, validator_index in enumerate(persistent_committee):
|
||||
if block.aggregation_bits[i]:
|
||||
pubkeys.append(beacon_state.validators[validator_index].pubkey)
|
||||
add_reward(state, beacon_state, validator_index, base_reward)
|
||||
shard_committee = get_shard_committee(state, state.shard, data.slot)
|
||||
for i, validator_index in enumerate(shard_committee):
|
||||
if data.aggregation_bits[i]:
|
||||
pubkeys.append(state.validators[validator_index].pubkey)
|
||||
add_reward(state, shard_state, validator_index, get_base_reward(state, validator_index))
|
||||
attestation_count += 1
|
||||
for i in range(len(persistent_committee), TARGET_PERSISTENT_COMMITTEE_SIZE):
|
||||
assert block.aggregation_bits[i] is False or block.aggregation_bits[i] == 0 # TODO: Fix Bitvector
|
||||
# Verify aggregate signature
|
||||
domain = get_domain(beacon_state, DOMAIN_SHARD_ATTESTER, compute_epoch_of_shard_slot(block.slot))
|
||||
assert bls_verify(bls_aggregate_pubkeys(pubkeys), block.parent_root, block.signatures.attesters, domain)
|
||||
# Proposer micro-rewards
|
||||
add_reward(state, beacon_state, proposer_index, attestation_count * get_shard_base_reward(beacon_state) // PROPOSER_REWARD_QUOTIENT)
|
||||
# Verify there are no extraneous bits set beyond the shard committee
|
||||
for i in range(len(shard_committee), 2 * MAX_PERIOD_COMMITTEE_SIZE):
|
||||
assert data.aggregation_bits[i] == 0b0
|
||||
# Verify attester aggregate signature
|
||||
domain = get_domain(state, DOMAIN_SHARD_ATTESTER, compute_epoch_of_shard_slot(data.slot))
|
||||
message = hash_tree_root(ShardCheckpoint(shard_state.slot, data.parent_root))
|
||||
assert bls_verify(bls_aggregate_pubkeys(pubkeys), message, block.signatures.attesters, domain)
|
||||
# Proposer micro-reward
|
||||
proposer_index = get_shard_proposer_index(state, state.shard, data.slot)
|
||||
reward = attestation_count * get_base_reward(state, proposer_index) // PROPOSER_REWARD_QUOTIENT
|
||||
add_reward(state, shard_state, proposer_index, reward)
|
||||
```
|
||||
|
||||
#### Block data fees
|
||||
#### Block size fee
|
||||
|
||||
```python
|
||||
def process_shard_block_data_fees(state: ShardState, beacon_state: BeaconState, block: ShardBlock) -> None:
|
||||
base_reward = get_shard_base_reward(beacon_state)
|
||||
add_fee(state, beacon_state, proposer_index, state.basefee * len(block.body) // SHARD_BLOCK_SIZE_LIMIT)
|
||||
QUOTIENT = SHARD_BLOCK_SIZE_LIMIT * BASEFEE_ADJUSTMENT_FACTOR
|
||||
if len(block.body) > SHARD_BLOCK_SIZE_TARGET:
|
||||
state.basefee += Gwei(max(1, state.basefee * (len(block.body) - SHARD_BLOCK_SIZE_TARGET) // QUOTIENT))
|
||||
elif len(block.body) < SHARD_BLOCK_SIZE_TARGET:
|
||||
state.basefee -= Gwei(max(1, state.basefee * (len(block.body) - SHARD_BLOCK_SIZE_TARGET) // QUOTIENT))
|
||||
state.basefee = Gwei(max(1, min( EFFECTIVE_BALANCE_INCREMENT // EPOCHS_PER_SHARD_PERIOD // SHARD_SLOTS_PER_EPOCH,
|
||||
state.basefee,
|
||||
)))
|
||||
```
|
||||
|
||||
## Object validity
|
||||
|
||||
### Shard block validation: preliminary
|
||||
|
||||
Accept a shard block `block` only if all of the following are correct:
|
||||
|
||||
* Either `block.parent_root == Hash()` or a block `parent` such that `signing_root(parent) == block.parent_root` has already been accepted.
|
||||
* `block.beacon_chain_root == get_block_root(head_beacon_state, compute_epoch_of_shard_slot(parent.slot))` where `head_beacon_state` is the current beacon chain head state. Alternatively phrased, a beacon chain block `beacon_ref` such that `signing_root(beacon_ref) == block.beacon_chain_root` has already been accepted and is part of the canonical chain, and no block with slot `beacon_ref.slot < slot <= compute_start_slot_of_epoch(compute_epoch_of_shard_slot(parent.slot))` is part of the canonical chain.
|
||||
* Let `beacon_state` be the state where `beacon_ref.state_root == hash_tree_root(beacon_state)`. Let `prev_state` be the post-state of the `parent` if the `parent` exists, otherwise let it be `get_default_shard_state(beacon_state, shard)` (defined below). `block.state_root` must equal the `hash_tree_root` of the state after applying `shard_state_transition(prev_state, beacon_state, block)`.
|
||||
|
||||
Note that these acceptance conditions depend on the canonical beacon chain; when the canonical beacon chain reorganizes, the eligibility of shard blocks should be re-evaluated.
|
||||
|
||||
### Beacon attestations
|
||||
|
||||
Let:
|
||||
|
||||
- `pre_state` be the `ShardState` before processing any blocks
|
||||
- `shard_blocks_or_state_roots` be the `Union[ShardBlock, Hash]` list such that `shard_blocks[slot]` is the canonical `ShardBlock` for shard `pre_state.shard` at slot `slot` if a block exists, or the post-state-root of processing state up to and including that slot if a block does not exist.
|
||||
- `beacon_state` be the canonical `BeaconState`
|
||||
- `valid_attestations` be the set of valid `Attestation` objects, recursively defined
|
||||
- `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`
|
||||
|
||||
```python
|
||||
def is_valid_beacon_attestation(pre_state: ShardState,
|
||||
shard_blocks_or_state_roots: Sequence[Union[ShardBlock, Hash]],
|
||||
beacon_state: BeaconState,
|
||||
valid_attestations: Set[Attestation],
|
||||
candidate: Attestation) -> bool:
|
||||
# Check if attestation is already determined valid
|
||||
for attestation in valid_attestations:
|
||||
if candidate == attestation:
|
||||
return True
|
||||
|
||||
# Check previous attestation
|
||||
if candidate.data.previous_crosslink.epoch <= PHASE_1_FORK_EPOCH:
|
||||
assert candidate.data.previous_crosslink.data_root == Hash()
|
||||
def process_shard_block_size_fee(state: BeaconState, shard_state: ShardState, block: ShardBlock) -> None:
|
||||
# Charge proposer block size fee
|
||||
proposer_index = get_shard_proposer_index(state, state.shard, block.data.slot)
|
||||
block_size = SHARD_HEADER_SIZE + len(block.data.body)
|
||||
add_fee(state, shard_state, proposer_index, state.block_size_price * block_size // SHARD_BLOCK_SIZE_LIMIT)
|
||||
# Calculate new block size price
|
||||
if block_size > SHARD_BLOCK_SIZE_TARGET:
|
||||
size_delta = block_size - SHARD_BLOCK_SIZE_TARGET
|
||||
price_delta = Gwei(state.block_size_price * size_delta // SHARD_BLOCK_SIZE_LIMIT // BLOCK_SIZE_PRICE_QUOTIENT)
|
||||
# The maximum gas price caps the amount burnt on gas fees within a period to 32 ETH
|
||||
MAX_BLOCK_SIZE_PRICE = MAX_EFFECTIVE_BALANCE // EPOCHS_PER_SHARD_PERIOD // SHARD_SLOTS_PER_EPOCH
|
||||
state.block_size_price = min(MAX_BLOCK_SIZE_PRICE, state.block_size_price + price_delta)
|
||||
else:
|
||||
previous_attestation = next(
|
||||
(attestation for attestation in valid_attestations
|
||||
if attestation.data.crosslink.data_root == candidate.data.previous_crosslink.data_root),
|
||||
None,
|
||||
)
|
||||
assert previous_attestation is not None
|
||||
assert candidate.data.previous_attestation.epoch < compute_epoch_of_slot(candidate.data.slot)
|
||||
|
||||
# Check crosslink data root
|
||||
start_epoch = beacon_state.crosslinks[pre_state.shard].epoch
|
||||
end_epoch = min(compute_epoch_of_slot(candidate.data.slot) - CROSSLINK_LOOKBACK,
|
||||
start_epoch + MAX_EPOCHS_PER_CROSSLINK)
|
||||
blocks = []
|
||||
for slot in range(start_epoch * SLOTS_PER_EPOCH, end_epoch * SLOTS_PER_EPOCH):
|
||||
if isinstance(shard_blocks_or_state_roots[slot], ShardBlock):
|
||||
blocks.append(shard_blocks_or_state_roots[slot])
|
||||
else:
|
||||
blocks.append(ShardBlock(
|
||||
slot=slot,
|
||||
state_root=shard_blocks_or_state_roots[slot],
|
||||
total_bytes=pre_state.total_bytes,
|
||||
))
|
||||
assert candidate.data.crosslink.data_root == compute_crosslink_data_root(blocks)
|
||||
|
||||
return True
|
||||
size_delta = SHARD_BLOCK_SIZE_TARGET - block_size
|
||||
price_delta = Gwei(state.block_size_price * size_delta // SHARD_BLOCK_SIZE_LIMIT // BLOCK_SIZE_PRICE_QUOTIENT)
|
||||
state.block_size_price = max(MIN_BLOCK_SIZE_PRICE, state.block_size_price - price_delta)
|
||||
```
|
||||
|
||||
## Shard fork choice rule
|
||||
|
||||
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.)
|
||||
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. `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.)
|
||||
|
Loading…
x
Reference in New Issue
Block a user