2020-11-17 12:42:09 +00:00
# Minimal Light Client
2020-11-16 07:02:11 +00:00
**Notice**: This document is a work-in-progress for researchers and implementers.
## Table of contents
<!-- TOC -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE - RUN doctoc TO UPDATE -->
- [Introduction ](#introduction )
- [Constants ](#constants )
2020-11-17 12:42:09 +00:00
- [Configuration ](#configuration )
- [Misc ](#misc )
- [Time parameters ](#time-parameters )
2020-11-16 07:02:11 +00:00
- [Containers ](#containers )
2020-11-17 12:42:09 +00:00
- [`LightClientSnapshot` ](#lightclientsnapshot )
- [`LightClientUpdate` ](#lightclientupdate )
- [`LightClientStore` ](#lightclientstore )
2020-12-10 09:04:11 +00:00
- [Helper functions ](#helper-functions )
- [`get_subtree_index` ](#get_subtree_index )
2020-11-16 07:02:11 +00:00
- [Light client state updates ](#light-client-state-updates )
2020-11-17 12:42:09 +00:00
- [`is_valid_light_client_update` ](#is_valid_light_client_update )
2020-11-18 10:33:42 +00:00
- [`apply_light_client_update` ](#apply_light_client_update )
2020-11-17 12:42:09 +00:00
- [`process_light_client_update` ](#process_light_client_update )
2020-11-16 07:02:11 +00:00
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
2020-12-07 15:10:39 +00:00
Eth2 is designed to be light client friendly for constrained environments to
access Eth2 with reasonable safety and liveness.
Such environments include resource-constrained devices (e.g. phones for trust-minimised wallets)
and metered VMs (e.g. blockchain VMs for cross-chain bridges).
2020-11-17 12:42:09 +00:00
2020-12-07 15:10:39 +00:00
This document suggests a minimal light client design for the beacon chain that
uses sync committees introduced in [this beacon chain extension ](./beacon-chain.md ).
2020-11-16 07:02:11 +00:00
## Constants
| Name | Value |
| - | - |
2020-12-09 09:35:22 +00:00
| `FINALIZED_ROOT_INDEX` | `get_generalized_index(BeaconState, 'finalized_checkpoint', 'root')` |
| `NEXT_SYNC_COMMITTEE_INDEX` | `get_generalized_index(BeaconState, 'next_sync_committee')` |
2020-11-17 12:42:09 +00:00
## Configuration
### Misc
| Name | Value |
| - | - |
2020-11-17 15:30:09 +00:00
| `MIN_SYNC_COMMITTEE_PARTICIPANTS` | `1` |
2020-11-17 12:42:09 +00:00
| `MAX_VALID_LIGHT_CLIENT_UPDATES` | `uint64(2**64 - 1)` |
### Time parameters
| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
| `LIGHT_CLIENT_UPDATE_TIMEOUT` | `Slot(2**13)` | slots | ~27 hours |
2020-11-16 07:02:11 +00:00
## Containers
2020-11-17 12:42:09 +00:00
#### `LightClientSnapshot`
2020-11-16 07:02:11 +00:00
```python
2020-11-17 12:42:09 +00:00
class LightClientSnapshot(Container):
# Beacon block header
2020-11-16 07:02:11 +00:00
header: BeaconBlockHeader
2020-11-17 12:42:09 +00:00
# Sync committees corresponding to the header
current_sync_committee: SyncCommittee
next_sync_committee: SyncCommittee
2020-11-16 07:02:11 +00:00
```
2020-11-17 12:42:09 +00:00
#### `LightClientUpdate`
2020-11-16 07:02:11 +00:00
2020-11-17 12:42:09 +00:00
```python
class LightClientUpdate(Container):
2020-11-18 10:33:42 +00:00
# Update beacon block header
header: BeaconBlockHeader
# Next sync committee corresponding to the header
next_sync_committee: SyncCommittee
2020-12-10 09:04:11 +00:00
next_sync_committee_branch: Vector[Bytes32, NEXT_SYNC_COMMITTEE_INDEX.bit_length()]
2020-11-18 10:33:42 +00:00
# Finality proof for the update header
finality_header: BeaconBlockHeader
2020-12-10 09:04:11 +00:00
finality_branch: Vector[Bytes32, FINALIZED_ROOT_INDEX.bit_length()]
2020-11-17 12:42:09 +00:00
# Sync committee aggregate signature
2020-11-18 09:31:41 +00:00
sync_committee_bits: Bitvector[SYNC_COMMITTEE_SIZE]
2020-11-17 12:42:09 +00:00
sync_committee_signature: BLSSignature
2020-11-17 15:30:09 +00:00
# Fork version for the aggregate signature
2020-11-18 08:27:19 +00:00
fork_version: Version
2020-11-17 12:42:09 +00:00
```
#### `LightClientStore`
2020-11-16 07:02:11 +00:00
```python
2020-11-17 12:42:09 +00:00
class LightClientStore(Container):
snapshot: LightClientSnapshot
valid_updates: List[LightClientUpdate, MAX_VALID_LIGHT_CLIENT_UPDATES]
2020-11-16 07:02:11 +00:00
```
2020-12-10 09:04:11 +00:00
## Helper functions
### `get_subtree_index`
```python
def get_subtree_index(generalized_index: GeneralizedIndex) -> uint64:
return uint64(generalized_index % 2**((generalized_index).bit_length()))
```
2020-11-16 07:02:11 +00:00
## Light client state updates
2020-12-15 05:18:20 +00:00
A light client maintains its state in a `store` object of type `LightClientStore` and receives `update` objects of type `LightClientUpdate` . Every `update` triggers `process_light_client_update(store, update, current_slot)` where `current_slot` is the current slot based on some local clock.
2020-11-16 07:02:11 +00:00
2020-11-17 12:42:09 +00:00
#### `is_valid_light_client_update`
2020-11-16 07:02:11 +00:00
```python
2020-11-18 10:33:42 +00:00
def is_valid_light_client_update(snapshot: LightClientSnapshot, update: LightClientUpdate) -> bool:
# Verify update slot is larger than snapshot slot
assert update.header.slot > snapshot.header.slot
2020-11-17 12:42:09 +00:00
# Verify update does not skip a sync committee period
2020-11-18 10:33:42 +00:00
snapshot_period = compute_epoch_at_slot(snapshot.header.slot) // EPOCHS_PER_SYNC_COMMITTEE_PERIOD
update_period = compute_epoch_at_slot(update.header.slot) // EPOCHS_PER_SYNC_COMMITTEE_PERIOD
assert update_period in (snapshot_period, snapshot_period + 1)
# Verify update header root is the finalized root of the finality header, if specified
if update.finality_header == BeaconBlockHeader():
signed_header = update.header
2020-12-10 09:04:11 +00:00
assert update.finality_branch == [Bytes32() for _ in range(FINALIZED_ROOT_INDEX.bit_length())]
2020-11-18 08:27:19 +00:00
else:
2020-11-18 10:33:42 +00:00
signed_header = update.finality_header
2020-11-18 08:27:19 +00:00
assert is_valid_merkle_branch(
2020-11-18 10:33:42 +00:00
leaf=hash_tree_root(update.header),
branch=update.finality_branch,
2020-12-10 09:04:11 +00:00
depth=FINALIZED_ROOT_INDEX.bit_length(),
index=get_subtree_index(FINALIZED_ROOT_INDEX),
2020-11-18 10:33:42 +00:00
root=update.finality_header.state_root,
)
2020-11-17 12:42:09 +00:00
2020-11-18 10:33:42 +00:00
# Verify update next sync committee if the update period incremented
if update_period == snapshot_period:
sync_committee = snapshot.current_sync_committee
2020-12-10 09:04:11 +00:00
assert update.next_sync_committee_branch == [Bytes32() for _ in range(NEXT_SYNC_COMMITTEE_INDEX.bit_length())]
2020-11-18 09:31:41 +00:00
else:
2020-11-18 10:33:42 +00:00
sync_committee = snapshot.next_sync_committee
2020-11-17 12:42:09 +00:00
assert is_valid_merkle_branch(
2020-11-18 10:33:42 +00:00
leaf=hash_tree_root(update.next_sync_committee),
2020-11-17 12:42:09 +00:00
branch=update.next_sync_committee_branch,
2020-12-10 09:04:11 +00:00
depth=NEXT_SYNC_COMMITTEE_INDEX.bit_length(),
index=get_subtree_index(NEXT_SYNC_COMMITTEE_INDEX),
2020-11-18 10:33:42 +00:00
root=update.header.state_root,
2020-11-17 12:42:09 +00:00
)
2020-11-18 10:33:42 +00:00
# Verify sync committee has sufficient participants
2020-11-18 09:31:41 +00:00
assert sum(update.sync_committee_bits) >= MIN_SYNC_COMMITTEE_PARTICIPANTS
2020-11-17 12:42:09 +00:00
# Verify sync committee aggregate signature
participant_pubkeys = [pubkey for (bit, pubkey) in zip(update.sync_committee_bits, sync_committee.pubkeys) if bit]
2020-11-17 14:18:58 +00:00
domain = compute_domain(DOMAIN_SYNC_COMMITTEE, update.fork_version)
2020-11-18 10:33:42 +00:00
signing_root = compute_signing_root(signed_header, domain)
2020-11-17 12:42:09 +00:00
assert bls.FastAggregateVerify(participant_pubkeys, signing_root, update.sync_committee_signature)
2020-11-16 07:02:11 +00:00
return True
```
2020-11-18 10:33:42 +00:00
#### `apply_light_client_update`
```python
def apply_light_client_update(snapshot: LightClientSnapshot, update: LightClientUpdate) -> None:
snapshot_period = compute_epoch_at_slot(snapshot.header.slot) // EPOCHS_PER_SYNC_COMMITTEE_PERIOD
update_period = compute_epoch_at_slot(update.header.slot) // EPOCHS_PER_SYNC_COMMITTEE_PERIOD
if update_period == snapshot_period + 1:
snapshot.current_sync_committee = snapshot.next_sync_committee
snapshot.next_sync_committee = update.next_sync_committee
snapshot.header = update.header
```
#### `process_light_client_update`
2020-11-16 07:02:11 +00:00
2020-11-16 07:07:40 +00:00
```python
2020-11-17 12:42:09 +00:00
def process_light_client_update(store: LightClientStore, update: LightClientUpdate, current_slot: Slot) -> None:
2020-11-17 15:30:09 +00:00
# Validate update
2020-11-18 10:33:42 +00:00
assert is_valid_light_client_update(store.snapshot, update)
store.valid_updates.append(update)
2020-12-09 09:35:22 +00:00
if (
sum(update.sync_committee_bits) * 3 > len(update.sync_committee_bits) * 2
and update.header != update.finality_header
):
2020-11-18 10:33:42 +00:00
# Apply update if 2/3 quorum is reached and we have a finality proof
apply_light_client_update(store, update)
2020-11-18 09:31:41 +00:00
store.valid_updates = []
2020-12-09 09:35:22 +00:00
elif current_slot > store.snapshot.header.slot + LIGHT_CLIENT_UPDATE_TIMEOUT:
2020-11-17 15:30:09 +00:00
# Forced best update when the update timeout has elapsed
2020-11-18 10:33:42 +00:00
apply_light_client_update(store, max(store.valid_updates, key=lambda update: sum(update.sync_committee_bits)))
2020-11-18 09:31:41 +00:00
store.valid_updates = []
2020-11-16 07:02:11 +00:00
```