2020-06-24 10:08:26 +00:00
# Ethereum 2.0 Phase 1 -- The Beacon Chain with Shards
2019-10-12 03:05:08 +00:00
**Notice**: This document is a work-in-progress for researchers and implementers.
## Table of contents
2020-01-24 23:43:43 +00:00
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE - RUN doctoc TO UPDATE -->
**Table of Contents**
2019-10-12 03:05:08 +00:00
2020-01-24 23:43:43 +00:00
- [Introduction ](#introduction )
- [Custom types ](#custom-types )
- [Configuration ](#configuration )
- [Misc ](#misc )
2020-06-09 22:38:40 +00:00
- [Shard block configs ](#shard-block-configs )
- [Gwei values ](#gwei-values )
- [Initial values ](#initial-values )
- [Time parameters ](#time-parameters )
2020-04-02 22:48:02 +00:00
- [Domain types ](#domain-types )
2020-01-24 23:43:43 +00:00
- [Updated containers ](#updated-containers )
- [Extended `AttestationData` ](#extended-attestationdata )
- [Extended `Attestation` ](#extended-attestation )
- [Extended `PendingAttestation` ](#extended-pendingattestation )
2020-05-01 18:25:11 +00:00
- [Extended `IndexedAttestation` ](#extended-indexedattestation )
- [Extended `AttesterSlashing` ](#extended-attesterslashing )
2020-01-24 23:43:43 +00:00
- [Extended `Validator` ](#extended-validator )
- [Extended `BeaconBlockBody` ](#extended-beaconblockbody )
- [Extended `BeaconBlock` ](#extended-beaconblock )
- [Extended `SignedBeaconBlock` ](#extended-signedbeaconblock )
- [Extended `BeaconState` ](#extended-beaconstate )
- [New containers ](#new-containers )
2020-03-31 04:37:36 +00:00
- [`ShardBlock` ](#shardblock )
- [`SignedShardBlock` ](#signedshardblock )
- [`ShardBlockHeader` ](#shardblockheader )
2020-01-24 23:43:43 +00:00
- [`ShardState` ](#shardstate )
- [`ShardTransition` ](#shardtransition )
- [`CompactCommittee` ](#compactcommittee )
- [`AttestationCustodyBitWrapper` ](#attestationcustodybitwrapper )
- [Helper functions ](#helper-functions )
- [Misc ](#misc-1 )
2020-04-28 03:28:21 +00:00
- [`compute_previous_slot` ](#compute_previous_slot )
2020-01-24 23:43:43 +00:00
- [`pack_compact_validator` ](#pack_compact_validator )
2020-03-31 10:21:33 +00:00
- [`unpack_compact_validator` ](#unpack_compact_validator )
2020-01-24 23:43:43 +00:00
- [`committee_to_compact_committee` ](#committee_to_compact_committee )
- [`compute_shard_from_committee_index` ](#compute_shard_from_committee_index )
2020-04-03 16:29:35 +00:00
- [`compute_offset_slots` ](#compute_offset_slots )
2020-04-16 11:43:48 +00:00
- [`compute_updated_gasprice` ](#compute_updated_gasprice )
2020-05-29 19:09:42 +00:00
- [`compute_committee_source_epoch` ](#compute_committee_source_epoch )
2020-01-24 23:43:43 +00:00
- [Beacon state accessors ](#beacon-state-accessors )
- [`get_active_shard_count` ](#get_active_shard_count )
- [`get_online_validator_indices` ](#get_online_validator_indices )
- [`get_shard_committee` ](#get_shard_committee )
- [`get_light_client_committee` ](#get_light_client_committee )
2020-04-02 03:20:22 +00:00
- [`get_shard_proposer_index` ](#get_shard_proposer_index )
2020-05-29 15:50:18 +00:00
- [`get_committee_count_delta` ](#get_committee_count_delta )
2020-01-24 23:43:43 +00:00
- [`get_start_shard` ](#get_start_shard )
2020-02-28 19:20:37 +00:00
- [`get_latest_slot_for_shard` ](#get_latest_slot_for_shard )
2020-01-24 23:43:43 +00:00
- [`get_offset_slots` ](#get_offset_slots )
- [Predicates ](#predicates )
2020-05-28 13:49:36 +00:00
- [`is_on_time_attestation` ](#is_on_time_attestation )
2020-04-03 15:46:22 +00:00
- [`is_winning_attestation` ](#is_winning_attestation )
2020-05-14 14:04:13 +00:00
- [`optional_aggregate_verify` ](#optional_aggregate_verify )
- [`optional_fast_aggregate_verify` ](#optional_fast_aggregate_verify )
2020-01-24 23:43:43 +00:00
- [Block processing ](#block-processing )
- [Operations ](#operations )
- [New Attestation processing ](#new-attestation-processing )
- [`validate_attestation` ](#validate_attestation )
2020-05-28 19:20:36 +00:00
- [Updated `process_attestation` ](#updated-process_attestation )
- [Shard transition processing ](#shard-transition-processing )
2020-01-24 23:43:43 +00:00
- [`apply_shard_transition` ](#apply_shard_transition )
- [`process_crosslink_for_shard` ](#process_crosslink_for_shard )
- [`process_crosslinks` ](#process_crosslinks )
2020-05-28 19:20:36 +00:00
- [`verify_empty_shard_transition` ](#verify_empty_shard_transition )
2020-05-28 19:14:43 +00:00
- [`process_shard_transitions` ](#process_shard_transitions )
2020-05-19 14:31:16 +00:00
- [New default validator for deposits ](#new-default-validator-for-deposits )
2020-01-24 23:43:43 +00:00
- [Light client processing ](#light-client-processing )
- [Epoch transition ](#epoch-transition )
2020-05-29 15:50:18 +00:00
- [Phase 1 final updates ](#phase-1-final-updates )
2020-01-24 23:43:43 +00:00
- [Custody game updates ](#custody-game-updates )
- [Online-tracking ](#online-tracking )
- [Light client committee updates ](#light-client-committee-updates )
2019-10-12 03:05:08 +00:00
2020-01-24 23:43:43 +00:00
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2019-10-12 03:05:08 +00:00
## Introduction
2019-11-15 20:11:42 +00:00
This document describes the extensions made to the Phase 0 design of The Beacon Chain
to facilitate the new shards as part of Phase 1 of Eth2.
2019-10-12 03:05:08 +00:00
2019-11-18 23:40:02 +00:00
## Custom types
We define the following Python custom types for type hinting and readability:
| Name | SSZ equivalent | Description |
| - | - | - |
| `Shard` | `uint64` | a shard number |
2020-01-05 23:44:22 +00:00
| `OnlineEpochs` | `uint8` | online countdown epochs |
2019-11-18 23:40:02 +00:00
2019-10-12 03:05:08 +00:00
## Configuration
2019-11-15 20:11:42 +00:00
Configuration is not namespaced. Instead it is strictly an extension;
no constants of phase 0 change, but new constants are adopted for changing behaviors.
2019-10-12 03:05:08 +00:00
### Misc
2020-06-09 20:34:46 +00:00
| Name | Value |
| - | - |
2019-10-12 03:05:08 +00:00
| `MAX_SHARDS` | `2**10` (= 1024) |
2019-10-13 06:52:51 +00:00
| `LIGHT_CLIENT_COMMITTEE_SIZE` | `2**7` (= 128) |
2020-06-09 20:34:46 +00:00
| `GASPRICE_ADJUSTMENT_COEFFICIENT` | `2**3` (= 8) |
### Shard block configs
2020-06-09 22:38:40 +00:00
2020-06-15 07:27:37 +00:00
| Name | Value | Unit |
| - | - | - |
| `MAX_SHARD_BLOCK_SIZE` | `2**20` (= 1,048,576) | bytes |
| `TARGET_SHARD_BLOCK_SIZE` | `2**18` (= 262,144) | bytes |
| `SHARD_BLOCK_OFFSETS` | `[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]` | - |
| `MAX_SHARD_BLOCKS_PER_ATTESTATION` | `len(SHARD_BLOCK_OFFSETS)` | - |
| `BYTES_PER_CUSTODY_CHUNK` | `2**12` (= 4,096) | bytes |
| `CUSTODY_RESPONSE_DEPTH` | `ceillog2(MAX_SHARD_BLOCK_SIZE // BYTES_PER_CUSTODY_CHUNK)` | - |
2020-06-09 20:34:46 +00:00
### Gwei values
| Name | Value |
| - | - |
| `MAX_GASPRICE` | `Gwei(2**14)` (= 16,384) | Gwei |
| `MIN_GASPRICE` | `Gwei(2**3)` (= 8) | Gwei |
### Initial values
| Name | Value |
| - | - |
| `NO_SIGNATURE` | `BLSSignature(b'\x00' * 96)` |
### Time parameters
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
| `ONLINE_PERIOD` | `OnlineEpochs(2**3)` (= 8) | online epochs | ~51 mins |
2019-11-20 04:11:19 +00:00
| `LIGHT_CLIENT_COMMITTEE_PERIOD` | `Epoch(2**8)` (= 256) | epochs | ~27 hours |
2020-04-02 22:48:02 +00:00
### Domain types
| Name | Value |
| - | - |
| `DOMAIN_SHARD_PROPOSAL` | `DomainType('0x80000000')` |
| `DOMAIN_SHARD_COMMITTEE` | `DomainType('0x81000000')` |
| `DOMAIN_LIGHT_CLIENT` | `DomainType('0x82000000')` |
2020-06-15 13:25:40 +00:00
| `DOMAIN_CUSTODY_BIT_SLASHING` | `DomainType('0x83000000')` |
2020-06-15 13:15:11 +00:00
| `DOMAIN_LIGHT_SELECTION_PROOF` | `DomainType('0x84000000')` |
| `DOMAIN_LIGHT_AGGREGATE_AND_PROOF` | `DomainType('0x85000000')` |
2019-10-12 03:05:08 +00:00
2019-11-22 22:45:55 +00:00
## Updated containers
2019-10-12 03:05:08 +00:00
2019-11-22 22:45:55 +00:00
The following containers have updated definitions in Phase 1.
2019-11-03 20:06:19 +00:00
2019-11-22 22:45:55 +00:00
### Extended `AttestationData`
2019-10-27 01:01:10 +00:00
2019-10-29 17:43:13 +00:00
```python
2019-11-22 22:45:55 +00:00
class AttestationData(Container):
2019-10-12 03:05:08 +00:00
slot: Slot
2019-10-27 01:01:10 +00:00
index: CommitteeIndex
2019-10-12 03:05:08 +00:00
# LMD GHOST vote
2019-12-05 19:36:48 +00:00
beacon_block_root: Root
2019-10-12 03:05:08 +00:00
# FFG vote
source: Checkpoint
target: Checkpoint
2020-06-15 16:16:39 +00:00
# Shard vote
shard: Shard
2019-11-12 13:27:34 +00:00
# Current-slot shard block root
2020-05-28 13:38:11 +00:00
shard_head_root: Root
2019-11-01 14:58:13 +00:00
# Shard transition root
2019-12-05 19:36:48 +00:00
shard_transition_root: Root
2019-10-27 01:01:10 +00:00
```
2019-11-22 22:45:55 +00:00
### Extended `Attestation`
2019-10-12 03:05:08 +00:00
```python
class Attestation(Container):
aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE]
data: AttestationData
2019-10-27 01:01:10 +00:00
signature: BLSSignature
```
2019-11-22 22:45:55 +00:00
### Extended `PendingAttestation`
2019-11-01 14:58:13 +00:00
```python
2019-11-19 19:16:40 +00:00
class PendingAttestation(Container):
2019-11-01 14:58:13 +00:00
aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE]
data: AttestationData
inclusion_delay: Slot
proposer_index: ValidatorIndex
2020-05-28 14:39:52 +00:00
# Phase 1
2019-11-18 21:04:29 +00:00
crosslink_success: boolean
2019-11-01 14:58:13 +00:00
```
2020-05-01 18:25:11 +00:00
### Extended `IndexedAttestation`
2019-12-05 22:06:32 +00:00
```python
class IndexedAttestation(Container):
2020-06-12 16:16:08 +00:00
attesting_indices: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE]
data: AttestationData
signature: BLSSignature
2019-12-05 22:06:32 +00:00
```
2020-05-01 18:25:11 +00:00
### Extended `AttesterSlashing`
2019-12-05 22:06:32 +00:00
Note that the `attestation_1` and `attestation_2` have a new `IndexedAttestation` definition.
```python
class AttesterSlashing(Container):
attestation_1: IndexedAttestation
attestation_2: IndexedAttestation
```
2019-11-22 22:45:55 +00:00
### Extended `Validator`
2019-11-15 20:11:42 +00:00
```python
2019-11-19 19:16:40 +00:00
class Validator(Container):
2019-11-15 20:11:42 +00:00
pubkey: BLSPubkey
2019-12-05 19:36:48 +00:00
withdrawal_credentials: Bytes32 # Commitment to pubkey for withdrawals
2019-11-15 20:11:42 +00:00
effective_balance: Gwei # Balance at stake
slashed: boolean
# Status epochs
activation_eligibility_epoch: Epoch # When criteria for activation were met
activation_epoch: Epoch
exit_epoch: Epoch
withdrawable_epoch: Epoch # When validator can withdraw funds
2019-11-16 10:13:47 +00:00
# Custody game
2019-11-15 20:11:42 +00:00
# next_custody_secret_to_reveal is initialised to the custody period
# (of the particular validator) in which the validator is activated
# = get_custody_period_for_validator(...)
next_custody_secret_to_reveal: uint64
2020-04-27 15:08:49 +00:00
# TODO: The max_reveal_lateness doesn't really make sense anymore.
# So how do we incentivise early custody key reveals now?
2020-04-30 23:16:00 +00:00
all_custody_secrets_revealed_epoch: Epoch # to be initialized to FAR_FUTURE_EPOCH
2019-11-15 20:11:42 +00:00
```
2019-10-12 14:59:51 +00:00
2019-11-22 22:45:55 +00:00
### Extended `BeaconBlockBody`
2019-11-15 22:42:28 +00:00
```python
2019-11-19 19:16:40 +00:00
class BeaconBlockBody(Container):
2019-11-15 22:42:28 +00:00
randao_reveal: BLSSignature
eth1_data: Eth1Data # Eth1 data vote
graffiti: Bytes32 # Arbitrary data
# Slashings
proposer_slashings: List[ProposerSlashing, MAX_PROPOSER_SLASHINGS]
attester_slashings: List[AttesterSlashing, MAX_ATTESTER_SLASHINGS]
# Attesting
attestations: List[Attestation, MAX_ATTESTATIONS]
2019-11-16 10:17:39 +00:00
# Entry & exit
2019-11-15 22:42:28 +00:00
deposits: List[Deposit, MAX_DEPOSITS]
2019-12-05 20:49:52 +00:00
voluntary_exits: List[SignedVoluntaryExit, MAX_VOLUNTARY_EXITS]
2019-11-15 22:42:28 +00:00
# Custody game
2020-06-17 22:43:41 +00:00
chunk_challenges: List[CustodyChunkChallenge, MAX_CUSTODY_CHUNK_CHALLENGES]
2020-05-19 13:50:05 +00:00
chunk_challenge_responses: List[CustodyChunkResponse, MAX_CUSTODY_CHUNK_CHALLENGE_RESPONSES]
2019-11-16 10:13:47 +00:00
custody_key_reveals: List[CustodyKeyReveal, MAX_CUSTODY_KEY_REVEALS]
early_derived_secret_reveals: List[EarlyDerivedSecretReveal, MAX_EARLY_DERIVED_SECRET_REVEALS]
2020-05-19 13:50:05 +00:00
custody_slashings: List[SignedCustodySlashing, MAX_CUSTODY_SLASHINGS]
2019-11-15 22:42:28 +00:00
# Shards
shard_transitions: Vector[ShardTransition, MAX_SHARDS]
# Light clients
2020-04-01 18:02:56 +00:00
light_client_bits: Bitvector[LIGHT_CLIENT_COMMITTEE_SIZE]
2019-11-15 22:42:28 +00:00
light_client_signature: BLSSignature
```
2019-11-15 20:11:42 +00:00
2019-11-22 22:45:55 +00:00
### Extended `BeaconBlock`
2019-10-12 14:59:51 +00:00
2019-11-16 10:13:47 +00:00
Note that the `body` has a new `BeaconBlockBody` definition.
2019-10-12 14:59:51 +00:00
```python
2019-11-19 19:16:40 +00:00
class BeaconBlock(Container):
2019-11-15 20:11:42 +00:00
slot: Slot
2020-02-17 19:02:24 +00:00
proposer_index: ValidatorIndex
2019-12-05 19:36:48 +00:00
parent_root: Root
state_root: Root
2019-11-15 20:11:42 +00:00
body: BeaconBlockBody
2019-12-05 20:49:52 +00:00
```
#### Extended `SignedBeaconBlock`
Note that the `message` has a new `BeaconBlock` definition.
```python
class SignedBeaconBlock(Container):
message: BeaconBlock
2019-11-15 20:11:42 +00:00
signature: BLSSignature
```
2019-11-22 22:45:55 +00:00
### Extended `BeaconState`
2019-11-15 20:11:42 +00:00
2019-11-16 10:13:47 +00:00
Note that aside from the new additions, `Validator` and `PendingAttestation` have new definitions.
2019-11-15 20:11:42 +00:00
```python
2019-11-19 19:16:40 +00:00
class BeaconState(Container):
2019-11-15 20:11:42 +00:00
# Versioning
genesis_time: uint64
2020-03-05 16:21:32 +00:00
genesis_validators_root: Root
2019-11-15 20:11:42 +00:00
slot: Slot
fork: Fork
# History
latest_block_header: BeaconBlockHeader
2019-12-05 19:36:48 +00:00
block_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
historical_roots: List[Root, HISTORICAL_ROOTS_LIMIT]
2019-11-15 20:11:42 +00:00
# Eth1
eth1_data: Eth1Data
2020-03-10 17:36:53 +00:00
eth1_data_votes: List[Eth1Data, EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH]
2019-11-15 20:11:42 +00:00
eth1_deposit_index: uint64
# Registry
validators: List[Validator, VALIDATOR_REGISTRY_LIMIT]
balances: List[Gwei, VALIDATOR_REGISTRY_LIMIT]
# Randomness
2019-12-05 19:36:48 +00:00
randao_mixes: Vector[Root, EPOCHS_PER_HISTORICAL_VECTOR]
2019-11-15 20:11:42 +00:00
# Slashings
slashings: Vector[Gwei, EPOCHS_PER_SLASHINGS_VECTOR] # Per-epoch sums of slashed effective balances
# Attestations
previous_epoch_attestations: List[PendingAttestation, MAX_ATTESTATIONS * SLOTS_PER_EPOCH]
current_epoch_attestations: List[PendingAttestation, MAX_ATTESTATIONS * SLOTS_PER_EPOCH]
# Finality
justification_bits: Bitvector[JUSTIFICATION_BITS_LENGTH] # Bit set for every recent justified epoch
previous_justified_checkpoint: Checkpoint # Previous epoch snapshot
current_justified_checkpoint: Checkpoint
finalized_checkpoint: Checkpoint
# Phase 1
2020-05-29 15:50:18 +00:00
current_epoch_start_shard: Shard
2019-11-16 11:23:45 +00:00
shard_states: List[ShardState, MAX_SHARDS]
2020-01-05 23:44:22 +00:00
online_countdown: List[OnlineEpochs, VALIDATOR_REGISTRY_LIMIT] # not a raw byte array, considered its large size.
2019-11-15 20:11:42 +00:00
current_light_committee: CompactCommittee
next_light_committee: CompactCommittee
2019-11-16 11:23:45 +00:00
# Custody game
2019-11-15 20:11:42 +00:00
# Future derived secrets already exposed; contains the indices of the exposed validator
# at RANDAO reveal period % EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS
2019-11-19 19:16:40 +00:00
exposed_derived_secrets: Vector[List[ValidatorIndex, MAX_EARLY_DERIVED_SECRET_REVEALS * SLOTS_PER_EPOCH],
2019-11-15 20:11:42 +00:00
EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS]
2020-04-24 16:06:27 +00:00
custody_chunk_challenge_records: List[CustodyChunkChallengeRecord, MAX_CUSTODY_CHUNK_CHALLENGE_RECORDS]
custody_chunk_challenge_index: uint64
2019-10-12 14:59:51 +00:00
```
2019-11-22 22:45:55 +00:00
## New containers
The following containers are new in Phase 1.
2020-03-31 04:37:36 +00:00
### `ShardBlock`
2019-11-22 22:45:55 +00:00
```python
2020-03-31 04:37:36 +00:00
class ShardBlock(Container):
2019-12-05 19:36:48 +00:00
shard_parent_root: Root
beacon_parent_root: Root
2019-11-22 22:45:55 +00:00
slot: Slot
2020-05-28 13:32:27 +00:00
shard: Shard
2020-04-02 02:43:50 +00:00
proposer_index: ValidatorIndex
2020-01-08 10:19:18 +00:00
body: ByteList[MAX_SHARD_BLOCK_SIZE]
2020-03-31 04:37:36 +00:00
```
### `SignedShardBlock`
```python
class SignedShardBlock(Container):
message: ShardBlock
2019-11-22 22:45:55 +00:00
signature: BLSSignature
```
2020-03-31 04:37:36 +00:00
### `ShardBlockHeader`
2019-11-22 22:45:55 +00:00
```python
2020-03-31 04:37:36 +00:00
class ShardBlockHeader(Container):
2019-12-05 19:36:48 +00:00
shard_parent_root: Root
beacon_parent_root: Root
2019-11-22 22:45:55 +00:00
slot: Slot
2020-05-28 13:32:27 +00:00
shard: Shard
2020-04-02 02:43:50 +00:00
proposer_index: ValidatorIndex
2019-12-05 19:36:48 +00:00
body_root: Root
2019-11-22 22:45:55 +00:00
```
### `ShardState`
```python
class ShardState(Container):
slot: Slot
gasprice: Gwei
2019-12-05 19:36:48 +00:00
latest_block_root: Root
2019-11-22 22:45:55 +00:00
```
### `ShardTransition`
```python
class ShardTransition(Container):
# Starting from slot
start_slot: Slot
# Shard block lengths
shard_block_lengths: List[uint64, MAX_SHARD_BLOCKS_PER_ATTESTATION]
# Shard data roots
2020-05-19 13:52:26 +00:00
# The root is of ByteList[MAX_SHARD_BLOCK_SIZE]
2020-01-08 10:19:18 +00:00
shard_data_roots: List[Bytes32, MAX_SHARD_BLOCKS_PER_ATTESTATION]
2019-11-22 22:45:55 +00:00
# Intermediate shard states
shard_states: List[ShardState, MAX_SHARD_BLOCKS_PER_ATTESTATION]
# Proposer signature aggregate
proposer_signature_aggregate: BLSSignature
```
### `CompactCommittee`
```python
class CompactCommittee(Container):
pubkeys: List[BLSPubkey, MAX_VALIDATORS_PER_COMMITTEE]
compact_validators: List[uint64, MAX_VALIDATORS_PER_COMMITTEE]
```
### `AttestationCustodyBitWrapper`
```python
class AttestationCustodyBitWrapper(Container):
2019-12-05 19:36:48 +00:00
attestation_data_root: Root
2019-11-22 22:45:55 +00:00
block_index: uint64
bit: boolean
```
2019-11-15 20:11:42 +00:00
## Helper functions
### Misc
2020-04-28 03:28:21 +00:00
#### `compute_previous_slot`
2020-01-05 19:11:55 +00:00
```python
2020-04-28 03:28:21 +00:00
def compute_previous_slot(slot: Slot) -> Slot:
2020-01-05 19:11:55 +00:00
if slot > 0:
return Slot(slot - 1)
else:
return Slot(0)
```
2019-11-15 20:11:42 +00:00
#### `pack_compact_validator`
2019-10-13 06:52:51 +00:00
```python
2020-03-31 10:21:33 +00:00
def pack_compact_validator(index: ValidatorIndex, slashed: bool, balance_in_increments: uint64) -> uint64:
2019-10-13 06:52:51 +00:00
"""
2020-03-31 10:21:33 +00:00
Create a compact validator object representing index, slashed status, and compressed balance.
2019-10-13 06:52:51 +00:00
Takes as input balance-in-increments (// EFFECTIVE_BALANCE_INCREMENT) to preserve symmetry with
the unpacking function.
"""
return (index < < 16 ) + ( slashed < < 15 ) + balance_in_increments
```
2020-03-31 10:21:33 +00:00
#### `unpack_compact_validator`
```python
def unpack_compact_validator(compact_validator: uint64) -> Tuple[ValidatorIndex, bool, uint64]:
"""
Return validator index, slashed, balance // EFFECTIVE_BALANCE_INCREMENT
"""
return (
ValidatorIndex(compact_validator >> 16),
bool((compact_validator >> 15) % 2),
compact_validator & (2**15 - 1),
)
```
2019-11-15 20:11:42 +00:00
#### `committee_to_compact_committee`
2019-10-13 06:52:51 +00:00
```python
def committee_to_compact_committee(state: BeaconState, committee: Sequence[ValidatorIndex]) -> CompactCommittee:
"""
2020-05-28 17:24:17 +00:00
Given a state and a list of validator indices, outputs the ``CompactCommittee`` representing them.
2019-10-13 06:52:51 +00:00
"""
validators = [state.validators[i] for i in committee]
compact_validators = [
pack_compact_validator(i, v.slashed, v.effective_balance // EFFECTIVE_BALANCE_INCREMENT)
for i, v in zip(committee, validators)
]
pubkeys = [v.pubkey for v in validators]
return CompactCommittee(pubkeys=pubkeys, compact_validators=compact_validators)
```
2020-01-16 00:43:11 +00:00
#### `compute_shard_from_committee_index`
```python
def compute_shard_from_committee_index(state: BeaconState, index: CommitteeIndex, slot: Slot) -> Shard:
active_shards = get_active_shard_count(state)
return Shard((index + get_start_shard(state, slot)) % active_shards)
```
2020-04-03 16:29:35 +00:00
#### `compute_offset_slots`
```python
def compute_offset_slots(start_slot: Slot, end_slot: Slot) -> Sequence[Slot]:
"""
Return the offset slots that are greater than ``start_slot`` and less than ``end_slot``.
"""
return [Slot(start_slot + x) for x in SHARD_BLOCK_OFFSETS if start_slot + x < end_slot ]
```
2020-04-16 11:43:48 +00:00
#### `compute_updated_gasprice`
```python
2020-06-18 12:34:34 +00:00
def compute_updated_gasprice(prev_gasprice: Gwei, shard_block_length: uint64) -> Gwei:
2020-05-28 14:39:52 +00:00
if shard_block_length > TARGET_SHARD_BLOCK_SIZE:
delta = (prev_gasprice * (shard_block_length - TARGET_SHARD_BLOCK_SIZE)
2020-04-16 11:43:48 +00:00
// TARGET_SHARD_BLOCK_SIZE // GASPRICE_ADJUSTMENT_COEFFICIENT)
return min(prev_gasprice + delta, MAX_GASPRICE)
else:
2020-05-28 14:39:52 +00:00
delta = (prev_gasprice * (TARGET_SHARD_BLOCK_SIZE - shard_block_length)
2020-04-16 11:43:48 +00:00
// TARGET_SHARD_BLOCK_SIZE // GASPRICE_ADJUSTMENT_COEFFICIENT)
return max(prev_gasprice, MIN_GASPRICE + delta) - delta
```
2020-05-29 19:09:42 +00:00
#### `compute_committee_source_epoch`
```python
def compute_committee_source_epoch(epoch: Epoch, period: uint64) -> Epoch:
"""
Return the source epoch for computing the committee.
"""
source_epoch = epoch - epoch % period
if source_epoch >= period:
source_epoch -= period # `period` epochs lookahead
return source_epoch
```
2019-11-15 20:11:42 +00:00
### Beacon state accessors
2020-01-05 19:20:20 +00:00
#### `get_active_shard_count`
```python
def get_active_shard_count(state: BeaconState) -> uint64:
return len(state.shard_states) # May adapt in the future, or change over time.
```
2019-11-16 10:13:47 +00:00
#### `get_online_validator_indices`
2019-11-15 20:11:42 +00:00
```python
2019-11-16 10:13:47 +00:00
def get_online_validator_indices(state: BeaconState) -> Set[ValidatorIndex]:
2019-11-15 20:11:42 +00:00
active_validators = get_active_validator_indices(state, get_current_epoch(state))
return set([i for i in active_validators if state.online_countdown[i] != 0])
```
#### `get_shard_committee`
2019-11-12 14:13:47 +00:00
```python
def get_shard_committee(beacon_state: BeaconState, epoch: Epoch, shard: Shard) -> Sequence[ValidatorIndex]:
2020-05-28 14:39:52 +00:00
"""
Return the shard committee of the given ``epoch`` of the given ``shard``.
"""
2020-05-29 19:09:42 +00:00
source_epoch = compute_committee_source_epoch(epoch, SHARD_COMMITTEE_PERIOD)
2019-11-12 14:13:47 +00:00
active_validator_indices = get_active_validator_indices(beacon_state, source_epoch)
seed = get_seed(beacon_state, source_epoch, DOMAIN_SHARD_COMMITTEE)
2020-04-16 09:18:11 +00:00
active_shard_count = get_active_shard_count(beacon_state)
2020-04-02 03:20:22 +00:00
return compute_committee(
indices=active_validator_indices,
seed=seed,
index=shard,
2020-04-16 09:18:11 +00:00
count=active_shard_count,
2020-04-02 03:20:22 +00:00
)
2019-11-12 14:13:47 +00:00
```
2019-11-15 20:11:42 +00:00
#### `get_light_client_committee`
2019-10-27 01:01:10 +00:00
```python
def get_light_client_committee(beacon_state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]:
2020-05-28 14:39:52 +00:00
"""
2020-06-01 09:47:47 +00:00
Return the light client committee of no more than ``TARGET_COMMITTEE_SIZE`` validators.
2020-05-28 14:39:52 +00:00
"""
2020-05-29 19:09:42 +00:00
source_epoch = compute_committee_source_epoch(epoch, LIGHT_CLIENT_COMMITTEE_PERIOD)
2019-11-01 14:58:13 +00:00
active_validator_indices = get_active_validator_indices(beacon_state, source_epoch)
2019-11-18 23:40:02 +00:00
seed = get_seed(beacon_state, source_epoch, DOMAIN_LIGHT_CLIENT)
2020-04-02 03:20:22 +00:00
return compute_committee(
indices=active_validator_indices,
seed=seed,
index=0,
2020-04-28 02:46:13 +00:00
count=get_active_shard_count(beacon_state),
2020-06-24 23:53:53 +00:00
)[:LIGHT_CLIENT_COMMITTEE_SIZE]
2019-11-12 14:13:47 +00:00
```
2019-11-15 20:11:42 +00:00
#### `get_shard_proposer_index`
2019-11-12 14:13:47 +00:00
```python
def get_shard_proposer_index(beacon_state: BeaconState, slot: Slot, shard: Shard) -> ValidatorIndex:
2020-06-04 03:19:04 +00:00
"""
Return the proposer's index of shard block at ``slot``.
"""
2020-06-03 21:06:04 +00:00
epoch = compute_epoch_at_slot(slot)
2020-06-04 03:19:04 +00:00
committee = get_shard_committee(beacon_state, epoch, shard)
seed = hash(get_seed(beacon_state, epoch, DOMAIN_SHARD_COMMITTEE) + int_to_bytes(slot, length=8))
r = bytes_to_int(seed[:8])
2019-11-12 14:13:47 +00:00
return committee[r % len(committee)]
```
2020-05-29 15:50:18 +00:00
#### `get_committee_count_delta`
```python
def get_committee_count_delta(state: BeaconState, start_slot: Slot, stop_slot: Slot) -> uint64:
"""
2020-06-01 15:22:59 +00:00
Return the sum of committee counts in range ``[start_slot, stop_slot)``.
2020-05-29 15:50:18 +00:00
"""
2020-06-18 15:45:10 +00:00
return sum(
get_committee_count_per_slot(state, compute_epoch_at_slot(Slot(slot)))
for slot in range(start_slot, stop_slot)
)
2020-05-29 15:50:18 +00:00
```
2019-11-18 23:40:02 +00:00
#### `get_start_shard`
```python
def get_start_shard(state: BeaconState, slot: Slot) -> Shard:
2020-05-29 15:50:18 +00:00
"""
Return the start shard at ``slot``.
"""
current_epoch_start_slot = compute_start_slot_at_epoch(get_current_epoch(state))
active_shard_count = get_active_shard_count(state)
if current_epoch_start_slot == slot:
return state.current_epoch_start_shard
2020-06-01 15:15:16 +00:00
elif slot > current_epoch_start_slot:
2020-05-29 15:50:18 +00:00
# Current epoch or the next epoch lookahead
shard_delta = get_committee_count_delta(state, start_slot=current_epoch_start_slot, stop_slot=slot)
return Shard((state.current_epoch_start_shard + shard_delta) % active_shard_count)
else:
# Previous epoch
shard_delta = get_committee_count_delta(state, start_slot=slot, stop_slot=current_epoch_start_slot)
max_committees_per_epoch = MAX_COMMITTEES_PER_SLOT * SLOTS_PER_EPOCH
return Shard(
# Ensure positive
(state.current_epoch_start_shard + max_committees_per_epoch * active_shard_count - shard_delta)
% active_shard_count
)
2019-11-18 23:40:02 +00:00
```
2020-02-28 19:20:37 +00:00
#### `get_latest_slot_for_shard`
2019-11-23 01:09:31 +00:00
```python
2020-02-28 19:20:37 +00:00
def get_latest_slot_for_shard(state: BeaconState, shard: Shard) -> Slot:
2020-05-29 15:50:18 +00:00
"""
2020-06-01 09:56:22 +00:00
Return the latest slot number of the given ``shard``.
2020-05-29 15:50:18 +00:00
"""
2020-02-28 19:20:37 +00:00
return state.shard_states[shard].slot
2019-11-23 01:09:31 +00:00
```
2019-11-15 20:11:42 +00:00
#### `get_offset_slots`
```python
2020-04-03 15:19:56 +00:00
def get_offset_slots(state: BeaconState, shard: Shard) -> Sequence[Slot]:
2020-05-28 14:39:52 +00:00
"""
2020-06-01 09:56:22 +00:00
Return the offset slots of the given ``shard``.
2020-05-29 15:50:18 +00:00
The offset slot are after the latest slot and before current slot.
2020-05-28 14:39:52 +00:00
"""
return compute_offset_slots(get_latest_slot_for_shard(state, shard), state.slot)
2019-11-15 20:11:42 +00:00
```
### Predicates
2020-05-28 13:49:36 +00:00
#### `is_on_time_attestation`
2020-05-01 18:25:11 +00:00
```python
2020-05-28 13:49:36 +00:00
def is_on_time_attestation(state: BeaconState,
2020-06-19 15:46:01 +00:00
attestation_data: AttestationData) -> bool:
2020-05-28 13:49:36 +00:00
"""
2020-06-19 15:46:01 +00:00
Check if the given ``attestation_data`` is on-time.
2020-05-28 13:49:36 +00:00
"""
2020-06-19 15:46:01 +00:00
return attestation_data.slot == compute_previous_slot(state.slot)
2020-05-01 18:25:11 +00:00
```
#### `is_winning_attestation`
```python
def is_winning_attestation(state: BeaconState,
attestation: PendingAttestation,
committee_index: CommitteeIndex,
winning_root: Root) -> bool:
"""
2020-06-19 15:46:01 +00:00
Check if on-time ``attestation`` helped contribute to the successful crosslink of
``winning_root`` formed by ``committee_index`` committee.
2020-05-01 18:25:11 +00:00
"""
return (
2020-06-19 15:46:01 +00:00
is_on_time_attestation(state, attestation.data)
2020-05-01 18:25:11 +00:00
and attestation.data.index == committee_index
and attestation.data.shard_transition_root == winning_root
)
```
2019-11-03 16:17:46 +00:00
2020-05-14 14:04:13 +00:00
#### `optional_aggregate_verify`
```python
def optional_aggregate_verify(pubkeys: Sequence[BLSPubkey],
messages: Sequence[Bytes32],
signature: BLSSignature) -> bool:
2020-05-14 17:05:32 +00:00
"""
If ``pubkeys`` is an empty list, the given ``signature`` should be a stub ``NO_SIGNATURE``.
Otherwise, verify it with standard BLS AggregateVerify API.
"""
2020-05-14 14:04:13 +00:00
if len(pubkeys) == 0:
return signature == NO_SIGNATURE
else:
return bls.AggregateVerify(pubkeys, messages, signature)
2019-10-27 01:01:10 +00:00
```
2020-05-14 14:04:13 +00:00
#### `optional_fast_aggregate_verify`
```python
def optional_fast_aggregate_verify(pubkeys: Sequence[BLSPubkey], message: Bytes32, signature: BLSSignature) -> bool:
2020-05-14 17:05:32 +00:00
"""
If ``pubkeys`` is an empty list, the given ``signature`` should be a stub ``NO_SIGNATURE``.
Otherwise, verify it with standard BLS FastAggregateVerify API.
"""
2020-05-14 14:04:13 +00:00
if len(pubkeys) == 0:
return signature == NO_SIGNATURE
else:
return bls.FastAggregateVerify(pubkeys, message, signature)
```
2019-11-03 16:17:46 +00:00
2019-11-15 20:11:42 +00:00
### Block processing
2019-11-06 22:19:00 +00:00
```python
2019-11-15 20:11:42 +00:00
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
process_randao(state, block.body)
process_eth1_data(state, block.body)
2020-04-01 18:02:56 +00:00
process_light_client_aggregate(state, block.body)
2019-11-15 20:11:42 +00:00
process_operations(state, block.body)
2019-11-12 13:27:34 +00:00
```
2019-11-15 20:11:42 +00:00
#### Operations
2019-10-12 03:05:08 +00:00
2019-10-13 08:42:55 +00:00
```python
2019-11-15 20:11:42 +00:00
def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
# Verify that outstanding deposits are processed up to the maximum number of deposits
assert len(body.deposits) == min(MAX_DEPOSITS, state.eth1_data.deposit_count - state.eth1_deposit_index)
2020-02-06 18:58:21 +00:00
2019-11-20 03:43:32 +00:00
def for_ops(operations: Sequence[Any], fn: Callable[[BeaconState, Any], None]) -> None:
2019-11-15 20:11:42 +00:00
for operation in operations:
fn(state, operation)
2020-02-06 18:58:21 +00:00
2019-11-16 10:13:47 +00:00
for_ops(body.proposer_slashings, process_proposer_slashing)
for_ops(body.attester_slashings, process_attester_slashing)
2019-11-15 20:11:42 +00:00
# New attestation processing
2020-02-06 18:58:21 +00:00
for_ops(body.attestations, process_attestation)
2019-11-16 10:13:47 +00:00
for_ops(body.deposits, process_deposit)
for_ops(body.voluntary_exits, process_voluntary_exit)
2019-11-15 20:11:42 +00:00
2019-11-15 22:42:28 +00:00
# See custody game spec.
process_custody_game_operations(state, body)
2020-05-28 19:14:43 +00:00
process_shard_transitions(state, body.shard_transitions, body.attestations)
2020-02-06 18:58:21 +00:00
2019-11-15 20:11:42 +00:00
# TODO process_operations(body.shard_receipt_proofs, process_shard_receipt_proofs)
2019-10-12 03:05:08 +00:00
```
2019-11-15 20:11:42 +00:00
##### New Attestation processing
2019-11-03 16:17:46 +00:00
2019-11-15 20:11:42 +00:00
###### `validate_attestation`
2019-10-12 03:05:08 +00:00
```python
2019-11-05 18:33:26 +00:00
def validate_attestation(state: BeaconState, attestation: Attestation) -> None:
2019-10-12 03:05:08 +00:00
data = attestation.data
2020-06-17 18:04:40 +00:00
assert data.index < get_committee_count_per_slot ( state , data . target . epoch )
2020-01-05 19:20:20 +00:00
assert data.index < get_active_shard_count ( state )
2019-11-23 01:09:31 +00:00
assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state))
2020-02-06 18:58:21 +00:00
assert data.target.epoch == compute_epoch_at_slot(data.slot)
2019-11-23 01:09:31 +00:00
assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY < = state.slot < = data.slot + SLOTS_PER_EPOCH
committee = get_beacon_committee(state, data.slot, data.index)
assert len(attestation.aggregation_bits) == len(committee)
if attestation.data.target.epoch == get_current_epoch(state):
assert attestation.data.source == state.current_justified_checkpoint
else:
assert attestation.data.source == state.previous_justified_checkpoint
2020-06-12 16:16:08 +00:00
# Type 1: on-time attestations
2020-06-19 15:46:01 +00:00
if is_on_time_attestation(state, attestation.data):
2019-10-12 03:05:08 +00:00
# Correct parent block root
2020-04-28 03:28:21 +00:00
assert data.beacon_block_root == get_block_root_at_slot(state, compute_previous_slot(state.slot))
2020-06-17 01:47:39 +00:00
# Correct shard number
2020-06-18 15:20:24 +00:00
shard = compute_shard_from_committee_index(state, attestation.data.index, attestation.data.slot)
assert attestation.data.shard == shard
2020-06-23 10:22:18 +00:00
# On-time attestations should have a non-empty shard transition root
assert attestation.data.shard_transition_root != hash_tree_root(ShardTransition())
2020-06-12 16:16:08 +00:00
# Type 2: no shard transition
2019-10-12 03:05:08 +00:00
else:
2020-02-22 03:44:54 +00:00
# Ensure delayed attestation
2020-06-01 15:15:16 +00:00
assert data.slot < compute_previous_slot ( state . slot )
2020-02-22 03:44:54 +00:00
# Late attestations cannot have a shard transition root
2019-12-05 19:36:48 +00:00
assert data.shard_transition_root == Root()
2020-02-22 03:44:54 +00:00
# Signature check
assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))
2019-11-03 16:17:46 +00:00
```
2019-10-12 03:05:08 +00:00
2020-05-28 19:20:36 +00:00
###### Updated `process_attestation`
```python
def process_attestation(state: BeaconState, attestation: Attestation) -> None:
validate_attestation(state, attestation)
# Store pending attestation for epoch processing
pending_attestation = PendingAttestation(
aggregation_bits=attestation.aggregation_bits,
data=attestation.data,
inclusion_delay=state.slot - attestation.data.slot,
proposer_index=get_beacon_proposer_index(state),
crosslink_success=False, # To be filled in during process_shard_transitions
)
if attestation.data.target.epoch == get_current_epoch(state):
state.current_epoch_attestations.append(pending_attestation)
else:
state.previous_epoch_attestations.append(pending_attestation)
```
##### Shard transition processing
2019-11-15 20:11:42 +00:00
###### `apply_shard_transition`
2019-11-03 16:17:46 +00:00
```python
def apply_shard_transition(state: BeaconState, shard: Shard, transition: ShardTransition) -> None:
2020-04-20 08:57:29 +00:00
# TODO: only need to check it once when phase 1 starts
assert state.slot > PHASE_1_GENESIS_SLOT
2019-11-03 20:06:19 +00:00
2019-11-03 16:17:46 +00:00
# Correct data root count
2020-04-03 15:19:56 +00:00
offset_slots = get_offset_slots(state, shard)
2019-11-18 22:07:50 +00:00
assert (
len(transition.shard_data_roots)
== len(transition.shard_states)
== len(transition.shard_block_lengths)
== len(offset_slots)
)
2020-02-28 19:20:37 +00:00
assert transition.start_slot == offset_slots[0]
2019-11-03 16:17:46 +00:00
2019-11-03 20:06:19 +00:00
headers = []
proposers = []
2019-11-03 16:17:46 +00:00
prev_gasprice = state.shard_states[shard].gasprice
2019-11-12 13:27:34 +00:00
shard_parent_root = state.shard_states[shard].latest_block_root
2020-05-28 14:39:52 +00:00
for i, offset_slot in enumerate(offset_slots):
2020-04-06 11:30:32 +00:00
shard_block_length = transition.shard_block_lengths[i]
2019-11-18 22:07:50 +00:00
shard_state = transition.shard_states[i]
2020-05-01 17:56:25 +00:00
# Verify correct calculation of gas prices and slots
assert shard_state.gasprice == compute_updated_gasprice(prev_gasprice, shard_block_length)
2020-05-28 14:39:52 +00:00
assert shard_state.slot == offset_slot
2020-05-01 17:56:25 +00:00
# Collect the non-empty proposals result
is_empty_proposal = shard_block_length == 0
2020-04-06 11:30:32 +00:00
if not is_empty_proposal:
2020-05-28 14:39:52 +00:00
proposal_index = get_shard_proposer_index(state, offset_slot, shard)
2020-04-16 11:43:48 +00:00
# Reconstruct shard headers
header = ShardBlockHeader(
2019-11-05 18:33:41 +00:00
shard_parent_root=shard_parent_root,
2020-05-28 14:39:52 +00:00
beacon_parent_root=get_block_root_at_slot(state, offset_slot),
slot=offset_slot,
2020-05-28 13:32:27 +00:00
shard=shard,
proposer_index=proposal_index,
2020-01-08 10:19:18 +00:00
body_root=transition.shard_data_roots[i]
2020-04-16 11:43:48 +00:00
)
shard_parent_root = hash_tree_root(header)
2020-04-06 11:30:32 +00:00
headers.append(header)
proposers.append(proposal_index)
2020-06-03 18:16:39 +00:00
else:
# Must have a stub for `shard_data_root` if empty slot
assert transition.shard_data_roots[i] == Root()
2019-11-03 20:06:19 +00:00
2019-11-03 16:17:46 +00:00
prev_gasprice = shard_state.gasprice
2020-01-09 13:56:06 +00:00
pubkeys = [state.validators[proposer].pubkey for proposer in proposers]
2020-01-13 17:55:21 +00:00
signing_roots = [
compute_signing_root(header, get_domain(state, DOMAIN_SHARD_PROPOSAL, compute_epoch_at_slot(header.slot)))
for header in headers
]
2019-11-04 16:50:09 +00:00
# Verify combined proposer signature
2020-05-14 14:04:13 +00:00
assert optional_aggregate_verify(pubkeys, signing_roots, transition.proposer_signature_aggregate)
2019-11-03 20:06:19 +00:00
2019-11-03 16:17:46 +00:00
# Save updated state
2020-04-06 11:30:32 +00:00
state.shard_states[shard] = transition.shard_states[len(transition.shard_states) - 1]
2020-05-23 22:22:49 +00:00
state.shard_states[shard].slot = compute_previous_slot(state.slot)
2019-11-03 16:17:46 +00:00
```
2019-10-12 14:59:51 +00:00
2019-11-23 01:09:31 +00:00
###### `process_crosslink_for_shard`
2019-10-12 03:05:08 +00:00
2019-11-03 16:17:46 +00:00
```python
2019-11-23 01:09:31 +00:00
def process_crosslink_for_shard(state: BeaconState,
2020-04-01 18:20:32 +00:00
committee_index: CommitteeIndex,
2019-11-23 01:09:31 +00:00
shard_transition: ShardTransition,
2019-12-05 19:36:48 +00:00
attestations: Sequence[Attestation]) -> Root:
2020-06-01 15:15:16 +00:00
on_time_attestation_slot = compute_previous_slot(state.slot)
committee = get_beacon_committee(state, on_time_attestation_slot, committee_index)
2019-11-16 10:13:47 +00:00
online_indices = get_online_validator_indices(state)
2020-06-18 19:39:48 +00:00
shard = compute_shard_from_committee_index(state, committee_index, on_time_attestation_slot)
2019-11-23 01:09:31 +00:00
# Loop over all shard transition roots
shard_transition_roots = set([a.data.shard_transition_root for a in attestations])
for shard_transition_root in sorted(shard_transition_roots):
transition_attestations = [a for a in attestations if a.data.shard_transition_root == shard_transition_root]
transition_participants: Set[ValidatorIndex] = set()
for attestation in transition_attestations:
participants = get_attesting_indices(state, attestation.data, attestation.aggregation_bits)
transition_participants = transition_participants.union(participants)
enough_online_stake = (
get_total_balance(state, online_indices.intersection(transition_participants)) * 3 >=
get_total_balance(state, online_indices.intersection(committee)) * 2
)
# If not enough stake, try next transition root
if not enough_online_stake:
continue
# Attestation < - > shard transition consistency
assert shard_transition_root == hash_tree_root(shard_transition)
2020-06-23 10:22:18 +00:00
# Check `shard_head_root` of the winning root
last_offset_index = len(shard_transition.shard_states) - 1
shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root
for attestation in transition_attestations:
assert attestation.data.shard_head_root == shard_head_root
2019-11-23 01:09:31 +00:00
# Apply transition
2020-06-18 19:39:48 +00:00
apply_shard_transition(state, shard, shard_transition)
2019-11-23 01:09:31 +00:00
# Apply proposer reward and cost
beacon_proposer_index = get_beacon_proposer_index(state)
estimated_attester_reward = sum([get_base_reward(state, attester) for attester in transition_participants])
proposer_reward = Gwei(estimated_attester_reward // PROPOSER_REWARD_QUOTIENT)
increase_balance(state, beacon_proposer_index, proposer_reward)
states_slots_lengths = zip(
shard_transition.shard_states,
2020-06-18 19:39:48 +00:00
get_offset_slots(state, shard),
2019-11-23 01:09:31 +00:00
shard_transition.shard_block_lengths
)
for shard_state, slot, length in states_slots_lengths:
2020-06-18 19:39:48 +00:00
proposer_index = get_shard_proposer_index(state, slot, shard)
2019-11-23 01:09:31 +00:00
decrease_balance(state, proposer_index, shard_state.gasprice * length)
# Return winning transition root
return shard_transition_root
# No winning transition root, ensure empty and return empty root
assert shard_transition == ShardTransition()
2019-12-05 19:36:48 +00:00
return Root()
2019-11-23 01:09:31 +00:00
```
###### `process_crosslinks`
```python
def process_crosslinks(state: BeaconState,
2020-02-06 18:58:21 +00:00
shard_transitions: Sequence[ShardTransition],
attestations: Sequence[Attestation]) -> None:
2020-06-01 15:15:16 +00:00
on_time_attestation_slot = compute_previous_slot(state.slot)
2020-06-17 18:04:40 +00:00
committee_count = get_committee_count_per_slot(state, compute_epoch_at_slot(on_time_attestation_slot))
2020-01-16 00:43:11 +00:00
for committee_index in map(CommitteeIndex, range(committee_count)):
2020-02-06 18:58:21 +00:00
# All attestations in the block for this committee/shard and current slot
2020-06-18 15:32:23 +00:00
shard = compute_shard_from_committee_index(state, committee_index, on_time_attestation_slot)
# Since the attestations are validated, all `shard_attestations` satisfy `attestation.data.shard == shard`
2019-11-23 01:09:31 +00:00
shard_attestations = [
2019-11-18 22:07:50 +00:00
attestation for attestation in attestations
2020-06-19 15:46:01 +00:00
if is_on_time_attestation(state, attestation.data) and attestation.data.index == committee_index
2019-11-18 22:07:50 +00:00
]
2020-06-18 15:32:23 +00:00
winning_root = process_crosslink_for_shard(
state, committee_index, shard_transitions[shard], shard_attestations
)
2019-12-05 19:36:48 +00:00
if winning_root != Root():
2020-02-06 18:58:21 +00:00
# Mark relevant pending attestations as creating a successful crosslink
for pending_attestation in state.current_epoch_attestations:
2020-04-03 15:46:22 +00:00
if is_winning_attestation(state, pending_attestation, committee_index, winning_root):
2020-02-06 18:58:21 +00:00
pending_attestation.crosslink_success = True
```
2020-05-28 19:20:36 +00:00
###### `verify_empty_shard_transition`
2020-02-06 18:58:21 +00:00
```python
2020-05-28 19:20:36 +00:00
def verify_empty_shard_transition(state: BeaconState, shard_transitions: Sequence[ShardTransition]) -> bool:
"""
Verify that a `shard_transition` in a block is empty if an attestation was not processed for it.
"""
for shard in range(get_active_shard_count(state)):
if state.shard_states[shard].slot != compute_previous_slot(state.slot):
if shard_transitions[shard] != ShardTransition():
return False
return True
```
2020-05-28 19:14:43 +00:00
###### `process_shard_transitions`
```python
def process_shard_transitions(state: BeaconState,
shard_transitions: Sequence[ShardTransition],
attestations: Sequence[Attestation]) -> None:
# Process crosslinks
process_crosslinks(state, shard_transitions, attestations)
# Verify the empty proposal shard states
assert verify_empty_shard_transition(state, shard_transitions)
2019-10-12 03:05:08 +00:00
```
2020-05-19 14:14:04 +00:00
##### New default validator for deposits
2019-12-05 22:06:32 +00:00
```python
2020-05-19 14:14:04 +00:00
def get_validator_from_deposit(state: BeaconState, deposit: Deposit) -> Validator:
amount = deposit.data.amount
effective_balance = min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
next_custody_secret_to_reveal = get_custody_period_for_validator(
ValidatorIndex(len(state.validators)),
get_current_epoch(state),
2019-12-05 22:06:32 +00:00
)
2020-05-19 14:14:04 +00:00
return Validator(
pubkey=deposit.data.pubkey,
withdrawal_credentials=deposit.data.withdrawal_credentials,
activation_eligibility_epoch=FAR_FUTURE_EPOCH,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawable_epoch=FAR_FUTURE_EPOCH,
effective_balance=effective_balance,
next_custody_secret_to_reveal=next_custody_secret_to_reveal,
all_custody_secrets_revealed_epoch=FAR_FUTURE_EPOCH,
2019-12-05 22:06:32 +00:00
)
```
2019-11-15 20:11:42 +00:00
#### Light client processing
2019-10-13 06:52:51 +00:00
```python
2020-04-01 18:02:56 +00:00
def process_light_client_aggregate(state: BeaconState, block_body: BeaconBlockBody) -> None:
2019-11-01 14:58:13 +00:00
committee = get_light_client_committee(state, get_current_epoch(state))
2020-06-03 16:43:42 +00:00
previous_slot = compute_previous_slot(state.slot)
2020-04-01 18:02:56 +00:00
previous_block_root = get_block_root_at_slot(state, previous_slot)
2019-11-05 20:16:56 +00:00
total_reward = Gwei(0)
2020-01-09 13:56:06 +00:00
signer_pubkeys = []
2020-01-05 23:17:48 +00:00
for bit_index, participant_index in enumerate(committee):
2020-04-01 18:02:56 +00:00
if block_body.light_client_bits[bit_index]:
2020-01-09 13:56:06 +00:00
signer_pubkeys.append(state.validators[participant_index].pubkey)
2020-04-01 18:02:56 +00:00
if not state.validators[participant_index].slashed:
increase_balance(state, participant_index, get_base_reward(state, participant_index))
total_reward += get_base_reward(state, participant_index)
2019-11-01 14:58:13 +00:00
2019-11-20 04:11:19 +00:00
increase_balance(state, get_beacon_proposer_index(state), Gwei(total_reward // PROPOSER_REWARD_QUOTIENT))
2020-04-01 18:02:56 +00:00
signing_root = compute_signing_root(previous_block_root,
get_domain(state, DOMAIN_LIGHT_CLIENT, compute_epoch_at_slot(previous_slot)))
2020-05-14 14:04:13 +00:00
assert optional_fast_aggregate_verify(signer_pubkeys, signing_root, block_body.light_client_signature)
2019-10-13 06:52:51 +00:00
```
2019-10-12 14:59:51 +00:00
### Epoch transition
2019-11-15 20:11:42 +00:00
This epoch transition overrides the phase0 epoch transition:
2019-10-12 14:59:51 +00:00
```python
2019-11-15 20:11:42 +00:00
def process_epoch(state: BeaconState) -> None:
process_justification_and_finalization(state)
process_rewards_and_penalties(state)
process_registry_updates(state)
2019-11-15 22:42:28 +00:00
process_reveal_deadlines(state)
2020-05-19 14:31:01 +00:00
process_challenge_deadlines(state)
2019-11-15 20:11:42 +00:00
process_slashings(state)
2020-06-01 15:15:16 +00:00
process_final_updates(state) # phase 0 final updates
2020-05-29 15:50:18 +00:00
process_phase_1_final_updates(state)
```
#### Phase 1 final updates
```python
def process_phase_1_final_updates(state: BeaconState) -> None:
2019-11-15 22:42:28 +00:00
process_custody_final_updates(state)
process_online_tracking(state)
process_light_client_committee_updates(state)
2020-05-29 15:50:18 +00:00
# Update current_epoch_start_shard
2020-06-01 15:15:16 +00:00
state.current_epoch_start_shard = get_start_shard(state, Slot(state.slot + 1))
2019-11-15 22:42:28 +00:00
```
2019-11-15 20:11:42 +00:00
2019-11-15 22:52:57 +00:00
#### Custody game updates
2020-06-10 16:20:42 +00:00
`process_reveal_deadlines` , `process_challenge_deadlines` and `process_custody_final_updates` are defined in [the Custody Game spec ](./custody-game.md ),
2019-11-15 22:52:57 +00:00
2019-11-15 22:42:28 +00:00
#### Online-tracking
```python
def process_online_tracking(state: BeaconState) -> None:
2019-10-27 01:01:10 +00:00
# Slowly remove validators from the "online" set if they do not show up
for index in range(len(state.validators)):
if state.online_countdown[index] != 0:
state.online_countdown[index] = state.online_countdown[index] - 1
2019-11-06 22:19:00 +00:00
# Process pending attestations
for pending_attestation in state.current_epoch_attestations + state.previous_epoch_attestations:
for index in get_attesting_indices(state, pending_attestation.data, pending_attestation.aggregation_bits):
state.online_countdown[index] = ONLINE_PERIOD
2019-10-12 14:59:51 +00:00
```
2019-11-15 22:42:28 +00:00
#### Light client committee updates
```python
def process_light_client_committee_updates(state: BeaconState) -> None:
2020-05-28 14:39:52 +00:00
"""
Update light client committees.
"""
2019-11-15 22:42:28 +00:00
if get_current_epoch(state) % LIGHT_CLIENT_COMMITTEE_PERIOD == 0:
state.current_light_committee = state.next_light_committee
new_committee = get_light_client_committee(state, get_current_epoch(state) + LIGHT_CLIENT_COMMITTEE_PERIOD)
state.next_light_committee = committee_to_compact_committee(state, new_committee)
```