2020-04-28 15:55:46 +00:00
|
|
|
# Ethereum 2.0 Phase 1 -- Beacon Chain + Shard Chain Fork Choice
|
|
|
|
|
|
|
|
**Notice**: This document is a work-in-progress for researchers and implementers.
|
|
|
|
|
|
|
|
## Table of contents
|
|
|
|
|
|
|
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
2020-06-03 14:31:16 +00:00
|
|
|
|
2020-04-28 15:55:46 +00:00
|
|
|
- [Introduction](#introduction)
|
|
|
|
- [Fork choice](#fork-choice)
|
|
|
|
- [Helpers](#helpers)
|
2020-05-01 02:33:23 +00:00
|
|
|
- [`get_forkchoice_shard_store`](#get_forkchoice_shard_store)
|
2020-04-28 15:55:46 +00:00
|
|
|
- [`get_shard_latest_attesting_balance`](#get_shard_latest_attesting_balance)
|
|
|
|
- [`get_shard_head`](#get_shard_head)
|
|
|
|
- [`get_shard_ancestor`](#get_shard_ancestor)
|
2020-06-04 13:24:17 +00:00
|
|
|
- [`get_pending_shard_blocks`](#get_pending_shard_blocks)
|
2020-04-28 15:55:46 +00:00
|
|
|
- [Handlers](#handlers)
|
|
|
|
- [`on_shard_block`](#on_shard_block)
|
|
|
|
|
|
|
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
2020-06-03 14:31:16 +00:00
|
|
|
This document is the shard chain fork choice spec for part of Ethereum 2.0 Phase 1. It assumes the [beacon chain fork choice spec](./fork-choice.md).
|
2020-04-28 15:55:46 +00:00
|
|
|
|
|
|
|
## Fork choice
|
|
|
|
|
|
|
|
### Helpers
|
|
|
|
|
2020-05-01 02:33:23 +00:00
|
|
|
#### `get_forkchoice_shard_store`
|
2020-04-28 15:55:46 +00:00
|
|
|
|
|
|
|
```python
|
2020-05-01 02:25:58 +00:00
|
|
|
def get_forkchoice_shard_store(anchor_state: BeaconState, shard: Shard) -> ShardStore:
|
|
|
|
return ShardStore(
|
|
|
|
shard=shard,
|
2020-06-24 04:50:27 +00:00
|
|
|
signed_blocks={
|
|
|
|
anchor_state.shard_states[shard].latest_block_root: SignedShardBlock(
|
2020-07-16 08:34:21 +00:00
|
|
|
message=ShardBlock(slot=compute_previous_slot(anchor_state.slot), shard=shard)
|
2020-06-24 04:50:27 +00:00
|
|
|
)
|
|
|
|
},
|
2020-05-01 02:25:58 +00:00
|
|
|
block_states={anchor_state.shard_states[shard].latest_block_root: anchor_state.copy().shard_states[shard]},
|
2020-04-28 15:55:46 +00:00
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `get_shard_latest_attesting_balance`
|
|
|
|
|
|
|
|
```python
|
2020-07-14 09:36:27 +00:00
|
|
|
def get_shard_latest_attesting_balance(store: Store, shard: Shard, root: Root) -> Gwei:
|
|
|
|
shard_store = store.shard_stores[shard]
|
2020-04-28 15:55:46 +00:00
|
|
|
state = store.checkpoint_states[store.justified_checkpoint]
|
|
|
|
active_indices = get_active_validator_indices(state, get_current_epoch(state))
|
|
|
|
return Gwei(sum(
|
|
|
|
state.validators[i].effective_balance for i in active_indices
|
|
|
|
if (
|
2020-07-03 02:49:19 +00:00
|
|
|
i in shard_store.latest_messages
|
2020-06-04 12:40:09 +00:00
|
|
|
# TODO: check the latest message logic: currently, validator's previous vote of another shard
|
|
|
|
# would be ignored once their newer vote is accepted. Check if it makes sense.
|
2020-06-03 14:31:16 +00:00
|
|
|
and get_shard_ancestor(
|
2020-07-03 02:49:19 +00:00
|
|
|
store,
|
2020-07-14 09:36:27 +00:00
|
|
|
shard,
|
2020-07-03 02:49:19 +00:00
|
|
|
shard_store.latest_messages[i].root,
|
|
|
|
shard_store.signed_blocks[root].message.slot,
|
2020-04-28 15:55:46 +00:00
|
|
|
) == root
|
|
|
|
)
|
|
|
|
))
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `get_shard_head`
|
|
|
|
|
|
|
|
```python
|
2020-07-14 09:36:27 +00:00
|
|
|
def get_shard_head(store: Store, shard: Shard) -> Root:
|
2020-04-28 15:55:46 +00:00
|
|
|
# Execute the LMD-GHOST fork choice
|
2020-07-14 09:36:27 +00:00
|
|
|
"""
|
|
|
|
Execute the LMD-GHOST fork choice.
|
|
|
|
"""
|
|
|
|
shard_store = store.shard_stores[shard]
|
2020-06-08 16:13:27 +00:00
|
|
|
beacon_head_root = get_head(store)
|
2020-07-14 09:36:27 +00:00
|
|
|
shard_head_state = store.block_states[beacon_head_root].shard_states[shard]
|
2020-06-08 16:13:27 +00:00
|
|
|
shard_head_root = shard_head_state.latest_block_root
|
|
|
|
shard_blocks = {
|
2020-06-24 04:50:27 +00:00
|
|
|
root: signed_shard_block.message for root, signed_shard_block in shard_store.signed_blocks.items()
|
|
|
|
if signed_shard_block.message.slot > shard_head_state.slot
|
2020-06-08 16:13:27 +00:00
|
|
|
}
|
2020-04-28 15:55:46 +00:00
|
|
|
while True:
|
2020-06-02 10:08:28 +00:00
|
|
|
# Find the valid child block roots
|
2020-04-28 15:55:46 +00:00
|
|
|
children = [
|
2020-06-08 16:13:27 +00:00
|
|
|
root for root, shard_block in shard_blocks.items()
|
|
|
|
if shard_block.shard_parent_root == shard_head_root
|
2020-04-28 15:55:46 +00:00
|
|
|
]
|
|
|
|
if len(children) == 0:
|
2020-06-03 17:00:52 +00:00
|
|
|
return shard_head_root
|
2020-04-28 15:55:46 +00:00
|
|
|
# Sort by latest attesting balance with ties broken lexicographically
|
2020-06-03 17:00:52 +00:00
|
|
|
shard_head_root = max(
|
2020-07-14 09:36:27 +00:00
|
|
|
children, key=lambda root: (get_shard_latest_attesting_balance(store, shard, root), root)
|
2020-05-01 02:25:58 +00:00
|
|
|
)
|
2020-04-28 15:55:46 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
#### `get_shard_ancestor`
|
|
|
|
|
|
|
|
```python
|
2020-07-14 09:36:27 +00:00
|
|
|
def get_shard_ancestor(store: Store, shard: Shard, root: Root, slot: Slot) -> Root:
|
|
|
|
shard_store = store.shard_stores[shard]
|
2020-06-24 04:50:27 +00:00
|
|
|
block = shard_store.signed_blocks[root].message
|
2020-04-28 15:55:46 +00:00
|
|
|
if block.slot > slot:
|
2020-07-14 09:36:27 +00:00
|
|
|
return get_shard_ancestor(store, shard, block.shard_parent_root, slot)
|
2020-04-28 15:55:46 +00:00
|
|
|
elif block.slot == slot:
|
|
|
|
return root
|
|
|
|
else:
|
2020-06-08 16:13:27 +00:00
|
|
|
# root is older than queried slot, thus a skip slot. Return most recent root prior to slot
|
2020-04-28 15:55:46 +00:00
|
|
|
return root
|
|
|
|
```
|
|
|
|
|
2020-06-04 13:24:17 +00:00
|
|
|
#### `get_pending_shard_blocks`
|
2020-06-04 12:31:54 +00:00
|
|
|
|
|
|
|
```python
|
2020-07-14 09:36:27 +00:00
|
|
|
def get_pending_shard_blocks(store: Store, shard: Shard) -> Sequence[SignedShardBlock]:
|
2020-06-04 12:31:54 +00:00
|
|
|
"""
|
2020-06-08 16:13:27 +00:00
|
|
|
Return the canonical shard block branch that has not yet been crosslinked.
|
2020-06-04 12:31:54 +00:00
|
|
|
"""
|
2020-07-14 09:36:27 +00:00
|
|
|
shard_store = store.shard_stores[shard]
|
2020-06-04 12:31:54 +00:00
|
|
|
|
|
|
|
beacon_head_root = get_head(store)
|
|
|
|
beacon_head_state = store.block_states[beacon_head_root]
|
|
|
|
latest_shard_block_root = beacon_head_state.shard_states[shard].latest_block_root
|
|
|
|
|
2020-07-14 09:36:27 +00:00
|
|
|
shard_head_root = get_shard_head(store, shard)
|
2020-06-04 12:31:54 +00:00
|
|
|
root = shard_head_root
|
2020-06-24 04:50:27 +00:00
|
|
|
signed_shard_blocks = []
|
2020-06-04 12:31:54 +00:00
|
|
|
while root != latest_shard_block_root:
|
2020-06-24 04:50:27 +00:00
|
|
|
signed_shard_block = shard_store.signed_blocks[root]
|
|
|
|
signed_shard_blocks.append(signed_shard_block)
|
|
|
|
root = signed_shard_block.message.shard_parent_root
|
2020-06-04 12:31:54 +00:00
|
|
|
|
2020-06-24 04:50:27 +00:00
|
|
|
signed_shard_blocks.reverse()
|
|
|
|
return signed_shard_blocks
|
2020-06-04 12:31:54 +00:00
|
|
|
```
|
|
|
|
|
2020-04-28 15:55:46 +00:00
|
|
|
### Handlers
|
|
|
|
|
|
|
|
#### `on_shard_block`
|
|
|
|
|
|
|
|
```python
|
2020-07-22 14:48:52 +00:00
|
|
|
def on_shard_block(store: Store, signed_shard_block: SignedShardBlock) -> None:
|
2020-04-28 15:55:46 +00:00
|
|
|
shard_block = signed_shard_block.message
|
2020-07-22 14:48:52 +00:00
|
|
|
shard = shard_block.shard
|
|
|
|
shard_store = store.shard_stores[shard]
|
2020-06-03 14:31:16 +00:00
|
|
|
|
|
|
|
# Check shard parent exists
|
2020-04-30 12:06:20 +00:00
|
|
|
assert shard_block.shard_parent_root in shard_store.block_states
|
2020-06-05 08:19:25 +00:00
|
|
|
shard_parent_state = shard_store.block_states[shard_block.shard_parent_root]
|
2020-04-28 15:55:46 +00:00
|
|
|
|
2020-06-03 14:31:16 +00:00
|
|
|
# Check beacon parent exists
|
2020-04-28 15:55:46 +00:00
|
|
|
assert shard_block.beacon_parent_root in store.block_states
|
2020-06-05 08:19:25 +00:00
|
|
|
beacon_parent_state = store.block_states[shard_block.beacon_parent_root]
|
2020-04-28 15:55:46 +00:00
|
|
|
|
2020-06-03 14:31:16 +00:00
|
|
|
# Check that block is later than the finalized shard state slot (optimization to reduce calls to get_ancestor)
|
2020-06-02 10:08:28 +00:00
|
|
|
finalized_beacon_state = store.block_states[store.finalized_checkpoint.root]
|
|
|
|
finalized_shard_state = finalized_beacon_state.shard_states[shard]
|
|
|
|
assert shard_block.slot > finalized_shard_state.slot
|
2020-04-28 15:55:46 +00:00
|
|
|
|
2020-06-03 14:31:16 +00:00
|
|
|
# Check block is a descendant of the finalized block at the checkpoint finalized slot
|
2020-06-02 10:08:28 +00:00
|
|
|
finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
|
2020-04-28 15:55:46 +00:00
|
|
|
assert (
|
2020-06-02 10:08:28 +00:00
|
|
|
get_ancestor(store, shard_block.beacon_parent_root, finalized_slot) == store.finalized_checkpoint.root
|
2020-04-28 15:55:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check the block is valid and compute the post-state
|
2020-06-22 15:11:21 +00:00
|
|
|
shard_state = shard_parent_state.copy()
|
2020-06-25 03:14:25 +00:00
|
|
|
shard_state_transition(shard_state, signed_shard_block, beacon_parent_state, validate_result=True)
|
2020-06-05 21:19:46 +00:00
|
|
|
|
2020-04-28 15:55:46 +00:00
|
|
|
# Add new block to the store
|
2020-06-24 04:50:27 +00:00
|
|
|
# Note: storing `SignedShardBlock` format for computing `ShardTransition.proposer_signature_aggregate`
|
|
|
|
shard_store.signed_blocks[hash_tree_root(shard_block)] = signed_shard_block
|
2020-04-28 15:55:46 +00:00
|
|
|
|
|
|
|
# Add new state for this block to the store
|
2020-06-22 15:11:21 +00:00
|
|
|
shard_store.block_states[hash_tree_root(shard_block)] = shard_state
|
2020-04-28 15:55:46 +00:00
|
|
|
```
|