2020-11-15 10:56:24 +00:00
|
|
|
# Ethereum 2.0 Light Client Support
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
## Table of contents
|
|
|
|
|
2020-11-16 09:09:04 +00:00
|
|
|
<!-- TOC -->
|
|
|
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
|
|
|
2020-11-17 02:41:26 +00:00
|
|
|
|
2020-11-12 09:28:05 +00:00
|
|
|
- [Introduction](#introduction)
|
2020-11-15 10:56:24 +00:00
|
|
|
- [Constants](#constants)
|
2020-11-12 09:28:05 +00:00
|
|
|
- [Configuration](#configuration)
|
|
|
|
- [Misc](#misc)
|
2020-11-15 10:56:24 +00:00
|
|
|
- [Time parameters](#time-parameters)
|
|
|
|
- [Domain types](#domain-types)
|
|
|
|
- [Containers](#containers)
|
|
|
|
- [Extended containers](#extended-containers)
|
|
|
|
- [`BeaconBlockBody`](#beaconblockbody)
|
|
|
|
- [`BeaconState`](#beaconstate)
|
|
|
|
- [New containers](#new-containers)
|
|
|
|
- [`SyncCommittee`](#synccommittee)
|
2020-11-12 09:28:05 +00:00
|
|
|
- [Helper functions](#helper-functions)
|
|
|
|
- [Beacon state accessors](#beacon-state-accessors)
|
2020-11-15 10:56:24 +00:00
|
|
|
- [`get_sync_committee_indices`](#get_sync_committee_indices)
|
|
|
|
- [`get_sync_committee`](#get_sync_committee)
|
2020-11-12 09:28:05 +00:00
|
|
|
- [Block processing](#block-processing)
|
2020-11-15 10:56:24 +00:00
|
|
|
- [Sync committee processing](#sync-committee-processing)
|
2020-11-16 09:09:04 +00:00
|
|
|
- [Epoch processing](#epoch-processing)
|
|
|
|
- [Final updates](#final-updates)
|
|
|
|
|
|
|
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- /TOC -->
|
2020-11-15 10:56:24 +00:00
|
|
|
|
2020-11-12 09:28:05 +00:00
|
|
|
## Introduction
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
This is a standalone beacon chain patch adding light client support via sync committees.
|
|
|
|
|
|
|
|
## Constants
|
|
|
|
|
|
|
|
| Name | Value |
|
|
|
|
| - | - |
|
|
|
|
| `BASE_REWARDS_PER_EPOCH` | `uint64(5)` |
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
### Misc
|
|
|
|
|
|
|
|
| Name | Value |
|
|
|
|
| - | - |
|
2020-11-25 11:38:45 +00:00
|
|
|
| `SYNC_COMMITTEE_SIZE` | `uint64(2**10)` (= 1024) |
|
2020-11-25 11:59:01 +00:00
|
|
|
| `SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE` | `uint64(2**6)` (= 64) |
|
2020-11-15 10:56:24 +00:00
|
|
|
|
|
|
|
### Time parameters
|
|
|
|
|
|
|
|
| Name | Value | Unit | Duration |
|
|
|
|
| - | - | :-: | :-: |
|
|
|
|
| `EPOCHS_PER_SYNC_COMMITTEE_PERIOD` | `Epoch(2**8)` (= 256) | epochs | ~27 hours |
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
### Domain types
|
|
|
|
|
|
|
|
| Name | Value |
|
|
|
|
| - | - |
|
2020-11-15 10:56:24 +00:00
|
|
|
| `DOMAIN_SYNC_COMMITTEE` | `DomainType('0x07000000')` |
|
|
|
|
|
|
|
|
## Containers
|
2020-11-12 09:28:05 +00:00
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
### Extended containers
|
2020-11-12 09:28:05 +00:00
|
|
|
|
2020-12-07 15:10:39 +00:00
|
|
|
*Note*: Extended SSZ containers inherit all fields from the parent in the original
|
|
|
|
order and append any additional fields to the end.
|
|
|
|
|
|
|
|
#### `BeaconBlock`
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-12-07 15:10:39 +00:00
|
|
|
class BeaconBlock(phase0.BeaconBlock):
|
2020-11-25 11:59:01 +00:00
|
|
|
# Sync committee aggregate signature
|
2020-11-18 09:19:32 +00:00
|
|
|
sync_committee_bits: Bitvector[SYNC_COMMITTEE_SIZE]
|
2020-11-15 10:56:24 +00:00
|
|
|
sync_committee_signature: BLSSignature
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
2020-12-07 15:10:39 +00:00
|
|
|
#### `BeaconBlockHeader`
|
|
|
|
|
|
|
|
```python
|
|
|
|
class BeaconBlockHeader(phase0.BeaconBlockHeader):
|
|
|
|
# Sync committee aggregate signature
|
|
|
|
sync_committee_bits: Bitvector[SYNC_COMMITTEE_SIZE]
|
|
|
|
sync_committee_signature: BLSSignature
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
#### `BeaconState`
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
|
|
|
class BeaconState(phase0.BeaconState):
|
2020-11-25 11:59:01 +00:00
|
|
|
# Sync committees
|
2020-11-15 10:56:24 +00:00
|
|
|
current_sync_committee: SyncCommittee
|
|
|
|
next_sync_committee: SyncCommittee
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
### New containers
|
2020-11-12 09:28:05 +00:00
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
#### `SyncCommittee`
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-11-15 10:56:24 +00:00
|
|
|
class SyncCommittee(Container):
|
2020-11-18 09:19:32 +00:00
|
|
|
pubkeys: Vector[BLSPubkey, SYNC_COMMITTEE_SIZE]
|
2020-11-25 11:59:01 +00:00
|
|
|
pubkey_aggregates: Vector[BLSPubkey, SYNC_COMMITTEE_SIZE // SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE]
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Helper functions
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
### Beacon state accessors
|
|
|
|
|
|
|
|
#### `get_sync_committee_indices`
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-11-15 10:56:24 +00:00
|
|
|
def get_sync_committee_indices(state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]:
|
2020-11-12 09:28:05 +00:00
|
|
|
"""
|
2020-11-15 10:56:24 +00:00
|
|
|
Return the sync committee indices for a given state and epoch.
|
2020-11-12 09:28:05 +00:00
|
|
|
"""
|
2020-12-07 15:10:39 +00:00
|
|
|
MAX_RANDOM_BYTE = 2**8 - 1
|
2020-11-15 17:23:44 +00:00
|
|
|
base_epoch = Epoch((max(epoch // EPOCHS_PER_SYNC_COMMITTEE_PERIOD, 1) - 1) * EPOCHS_PER_SYNC_COMMITTEE_PERIOD)
|
2020-11-18 09:19:32 +00:00
|
|
|
active_validator_indices = get_active_validator_indices(state, base_epoch)
|
|
|
|
active_validator_count = uint64(len(active_validator_indices))
|
2020-11-15 10:56:24 +00:00
|
|
|
seed = get_seed(state, base_epoch, DOMAIN_SYNC_COMMITTEE)
|
2020-11-18 09:19:32 +00:00
|
|
|
i, sync_committee_indices = 0, []
|
|
|
|
while len(sync_committee_indices) < SYNC_COMMITTEE_SIZE:
|
|
|
|
shuffled_index = compute_shuffled_index(uint64(i % active_validator_count), active_validator_count, seed)
|
|
|
|
candidate_index = active_validator_indices[shuffled_index]
|
2020-11-18 08:15:30 +00:00
|
|
|
random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32]
|
|
|
|
effective_balance = state.validators[candidate_index].effective_balance
|
|
|
|
if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte:
|
2020-11-18 09:19:32 +00:00
|
|
|
sync_committee_indices.append(candidate_index)
|
2020-11-18 08:15:30 +00:00
|
|
|
i += 1
|
2020-11-18 09:19:32 +00:00
|
|
|
return sync_committee_indices
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 17:23:44 +00:00
|
|
|
#### `get_sync_committee`
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-11-15 10:56:24 +00:00
|
|
|
def get_sync_committee(state: BeaconState, epoch: Epoch) -> SyncCommittee:
|
2020-11-12 09:28:05 +00:00
|
|
|
"""
|
2020-11-15 10:56:24 +00:00
|
|
|
Return the sync committee for a given state and epoch.
|
2020-11-12 09:28:05 +00:00
|
|
|
"""
|
2020-11-15 10:56:24 +00:00
|
|
|
indices = get_sync_committee_indices(state, epoch)
|
|
|
|
validators = [state.validators[index] for index in indices]
|
|
|
|
pubkeys = [validator.pubkey for validator in validators]
|
2020-11-25 11:38:45 +00:00
|
|
|
aggregates = [
|
2020-11-25 11:59:01 +00:00
|
|
|
bls.AggregatePKs(pubkeys[i:i + SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE])
|
|
|
|
for i in range(0, len(pubkeys), SYNC_COMMITTEE_PUBKEY_AGGREGATES_SIZE)
|
2020-11-25 11:38:45 +00:00
|
|
|
]
|
|
|
|
return SyncCommittee(pubkeys, aggregates)
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Block processing
|
|
|
|
|
|
|
|
```python
|
|
|
|
def process_block(state: BeaconState, block: BeaconBlock) -> None:
|
|
|
|
phase0.process_block(state, block)
|
2020-12-07 15:10:39 +00:00
|
|
|
process_sync_committee(state, block)
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
#### Sync committee processing
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-12-07 15:10:39 +00:00
|
|
|
def process_sync_committee(state: BeaconState, block: BeaconBlock) -> None:
|
2020-11-15 10:56:24 +00:00
|
|
|
# Verify sync committee aggregate signature signing over the previous slot block root
|
2020-12-07 15:10:39 +00:00
|
|
|
previous_slot = Slot(max(state.slot, 1) - 1)
|
2020-11-18 09:19:32 +00:00
|
|
|
committee_indices = get_sync_committee_indices(state, get_current_epoch(state))
|
2020-12-07 15:10:39 +00:00
|
|
|
participant_indices = [index for index, bit in zip(committee_indices, body.sync_committee_bits) if bit]
|
|
|
|
committee_pubkeys = state.current_sync_committee.pubkeys
|
|
|
|
participant_pubkeys = [pubkey for pubkey, bit in zip(committee_pubkeys, body.sync_committee_bits) if bit]
|
2020-11-15 10:56:24 +00:00
|
|
|
domain = get_domain(state, DOMAIN_SYNC_COMMITTEE, compute_epoch_at_slot(previous_slot))
|
|
|
|
signing_root = compute_signing_root(get_block_root_at_slot(state, previous_slot), domain)
|
2020-12-07 15:10:39 +00:00
|
|
|
assert bls.FastAggregateVerify(participant_pubkeys, signing_root, block.sync_committee_signature)
|
2020-11-15 10:56:24 +00:00
|
|
|
|
|
|
|
# Reward sync committee participants
|
|
|
|
participant_rewards = Gwei(0)
|
|
|
|
active_validator_count = uint64(len(get_active_validator_indices(state, get_current_epoch(state))))
|
|
|
|
for participant_index in participant_indices:
|
|
|
|
base_reward = get_base_reward(state, participant_index)
|
|
|
|
reward = Gwei(base_reward * active_validator_count // len(committee_indices) // SLOTS_PER_EPOCH)
|
|
|
|
increase_balance(state, participant_index, reward)
|
|
|
|
participant_rewards += reward
|
|
|
|
|
|
|
|
# Reward beacon proposer
|
|
|
|
increase_balance(state, get_beacon_proposer_index(state), Gwei(participant_rewards // PROPOSER_REWARD_QUOTIENT))
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Epoch processing
|
|
|
|
|
2020-11-15 10:56:24 +00:00
|
|
|
#### Final updates
|
2020-11-12 09:28:05 +00:00
|
|
|
|
|
|
|
```python
|
2020-11-15 10:56:24 +00:00
|
|
|
def process_final_updates(state: BeaconState) -> None:
|
|
|
|
phase0.process_final_updates(state)
|
|
|
|
next_epoch = get_current_epoch(state) + Epoch(1)
|
|
|
|
if next_epoch % EPOCHS_PER_SYNC_COMMITTEE_PERIOD == 0:
|
|
|
|
state.current_sync_committee = state.next_sync_committee
|
|
|
|
state.next_sync_committee = get_sync_committee(state, next_epoch + EPOCHS_PER_SYNC_COMMITTEE_PERIOD)
|
2020-11-12 09:28:05 +00:00
|
|
|
```
|