Make `ShardStore` an independent object

This commit is contained in:
Hsiao-Wei Wang 2020-05-01 10:25:58 +08:00
parent cddf9cf114
commit 8fafb6a9e5
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
2 changed files with 43 additions and 74 deletions

View File

@ -31,75 +31,38 @@ This document is the shard chain fork choice spec for part of Ethereum 2.0 Phase
### Helpers ### Helpers
#### Extended `Store` #### `ShardStore`
```python ```python
@dataclass @dataclass
class Store: class ShardStore:
shard: Shard
@dataclass blocks: Dict[Root, ShardBlock] = field(default_factory=dict)
class ShardStore: block_states: Dict[Root, ShardState] = field(default_factory=dict)
blocks: Dict[Root, ShardBlock] = field(default_factory=dict)
block_states: Dict[Root, ShardState] = field(default_factory=dict)
time: uint64
genesis_time: uint64
justified_checkpoint: Checkpoint
finalized_checkpoint: Checkpoint
best_justified_checkpoint: Checkpoint
blocks: Dict[Root, BeaconBlock] = field(default_factory=dict)
block_states: Dict[Root, BeaconState] = field(default_factory=dict)
checkpoint_states: Dict[Checkpoint, BeaconState] = field(default_factory=dict)
latest_messages: Dict[ValidatorIndex, LatestMessage] = field(default_factory=dict)
# shard chain
shards: Dict[Shard, ShardStore] = field(default_factory=dict) # noqa: F821
``` ```
#### Updated `get_forkchoice_store` #### Updated `get_forkchoice_shard_store`
```python ```python
def get_forkchoice_store(anchor_state: BeaconState) -> Store: def get_forkchoice_shard_store(anchor_state: BeaconState, shard: Shard) -> ShardStore:
shard_count = len(anchor_state.shard_states) return ShardStore(
anchor_block_header = anchor_state.latest_block_header.copy() shard=shard,
if anchor_block_header.state_root == Bytes32(): blocks={anchor_state.shard_states[shard].latest_block_root: ShardBlock(slot=anchor_state.slot)},
anchor_block_header.state_root = hash_tree_root(anchor_state) block_states={anchor_state.shard_states[shard].latest_block_root: anchor_state.copy().shard_states[shard]},
anchor_root = hash_tree_root(anchor_block_header)
anchor_epoch = get_current_epoch(anchor_state)
justified_checkpoint = Checkpoint(epoch=anchor_epoch, root=anchor_root)
finalized_checkpoint = Checkpoint(epoch=anchor_epoch, root=anchor_root)
shard_stores = {}
for shard in map(Shard, range(shard_count)):
shard_stores[shard] = Store.ShardStore(
blocks={anchor_state.shard_states[shard].latest_block_root: ShardBlock(slot=anchor_state.slot)},
block_states={anchor_state.shard_states[shard].latest_block_root: anchor_state.copy().shard_states[shard]},
)
return Store(
time=anchor_state.genesis_time + SECONDS_PER_SLOT * anchor_state.slot,
genesis_time=anchor_state.genesis_time,
justified_checkpoint=justified_checkpoint,
finalized_checkpoint=finalized_checkpoint,
best_justified_checkpoint=justified_checkpoint,
blocks={anchor_root: anchor_block_header},
block_states={anchor_root: anchor_state.copy()},
checkpoint_states={justified_checkpoint: anchor_state.copy()},
# shard chain
shards=shard_stores,
) )
``` ```
#### `get_shard_latest_attesting_balance` #### `get_shard_latest_attesting_balance`
```python ```python
def get_shard_latest_attesting_balance(store: Store, shard: Shard, root: Root) -> Gwei: def get_shard_latest_attesting_balance(store: Store, shard_store: ShardStore, root: Root) -> Gwei:
state = store.checkpoint_states[store.justified_checkpoint] state = store.checkpoint_states[store.justified_checkpoint]
active_indices = get_active_validator_indices(state, get_current_epoch(state)) active_indices = get_active_validator_indices(state, get_current_epoch(state))
return Gwei(sum( return Gwei(sum(
state.validators[i].effective_balance for i in active_indices state.validators[i].effective_balance for i in active_indices
if ( if (
i in store.latest_messages and get_shard_ancestor( i in store.latest_messages and get_shard_ancestor(
store, shard, store.latest_messages[i].root, store.shards[shard].blocks[root].slot store, shard_store, store.latest_messages[i].root, shard_store.blocks[root].slot
) == root ) == root
) )
)) ))
@ -108,13 +71,13 @@ def get_shard_latest_attesting_balance(store: Store, shard: Shard, root: Root) -
#### `get_shard_head` #### `get_shard_head`
```python ```python
def get_shard_head(store: Store, shard: Shard) -> Root: def get_shard_head(store: Store, shard_store: ShardStore) -> Root:
# Get filtered block tree that only includes viable branches # Get filtered block tree that only includes viable branches
blocks = get_filtered_shard_block_tree(store, shard) blocks = get_filtered_shard_block_tree(store, shard_store)
# Execute the LMD-GHOST fork choice # Execute the LMD-GHOST fork choice
head_beacon_root = get_head(store) head_beacon_root = get_head(store)
head_shard_root = store.block_states[head_beacon_root].shard_states[shard].latest_block_root head_shard_root = store.block_states[head_beacon_root].shard_states[shard_store.shard].latest_block_root
while True: while True:
children = [ children = [
root for root in blocks.keys() root for root in blocks.keys()
@ -123,16 +86,18 @@ def get_shard_head(store: Store, shard: Shard) -> Root:
if len(children) == 0: if len(children) == 0:
return head_shard_root return head_shard_root
# Sort by latest attesting balance with ties broken lexicographically # Sort by latest attesting balance with ties broken lexicographically
head_shard_root = max(children, key=lambda root: (get_shard_latest_attesting_balance(store, shard, root), root)) head_shard_root = max(
children, key=lambda root: (get_shard_latest_attesting_balance(store, shard_store, root), root)
)
``` ```
#### `get_shard_ancestor` #### `get_shard_ancestor`
```python ```python
def get_shard_ancestor(store: Store, shard: Shard, root: Root, slot: Slot) -> Root: def get_shard_ancestor(store: Store, shard_store: ShardStore, root: Root, slot: Slot) -> Root:
block = store.shards[shard].blocks[root] block = shard_store.blocks[root]
if block.slot > slot: if block.slot > slot:
return get_shard_ancestor(store, shard, block.shard_parent_root, slot) return get_shard_ancestor(store, shard_store, block.shard_parent_root, slot)
elif block.slot == slot: elif block.slot == slot:
return root return root
else: else:
@ -143,8 +108,10 @@ def get_shard_ancestor(store: Store, shard: Shard, root: Root, slot: Slot) -> Ro
#### `filter_shard_block_tree` #### `filter_shard_block_tree`
```python ```python
def filter_shard_block_tree(store: Store, shard: Shard, block_root: Root, blocks: Dict[Root, ShardBlock]) -> bool: def filter_shard_block_tree(store: Store,
shard_store = store.shards[shard] shard_store: ShardStore,
block_root: Root,
blocks: Dict[Root, ShardBlock]) -> bool:
block = shard_store.blocks[block_root] block = shard_store.blocks[block_root]
children = [ children = [
root for root in shard_store.blocks.keys() root for root in shard_store.blocks.keys()
@ -152,7 +119,7 @@ def filter_shard_block_tree(store: Store, shard: Shard, block_root: Root, blocks
] ]
if any(children): if any(children):
filter_block_tree_result = [filter_shard_block_tree(store, shard, child, blocks) for child in children] filter_block_tree_result = [filter_shard_block_tree(store, shard_store, child, blocks) for child in children]
if any(filter_block_tree_result): if any(filter_block_tree_result):
blocks[block_root] = block blocks[block_root] = block
return True return True
@ -164,11 +131,12 @@ def filter_shard_block_tree(store: Store, shard: Shard, block_root: Root, blocks
#### `get_filtered_block_tree` #### `get_filtered_block_tree`
```python ```python
def get_filtered_shard_block_tree(store: Store, shard: Shard) -> Dict[Root, ShardBlock]: def get_filtered_shard_block_tree(store: Store, shard_store: ShardStore) -> Dict[Root, ShardBlock]:
shard = shard_store.shard
base_beacon_block_root = get_head(store) base_beacon_block_root = get_head(store)
base_shard_block_root = store.block_states[base_beacon_block_root].shard_states[shard].latest_block_root base_shard_block_root = store.block_states[base_beacon_block_root].shard_states[shard].latest_block_root
blocks: Dict[Root, ShardBlock] = {} blocks: Dict[Root, ShardBlock] = {}
filter_shard_block_tree(store, shard, base_shard_block_root, blocks) filter_shard_block_tree(store, shard_store, base_shard_block_root, blocks)
return blocks return blocks
``` ```
@ -177,10 +145,9 @@ def get_filtered_shard_block_tree(store: Store, shard: Shard) -> Dict[Root, Shar
#### `on_shard_block` #### `on_shard_block`
```python ```python
def on_shard_block(store: Store, shard: Shard, signed_shard_block: SignedShardBlock) -> None: def on_shard_block(store: Store, shard_store: ShardStore, signed_shard_block: SignedShardBlock) -> None:
shard_block = signed_shard_block.message shard_block = signed_shard_block.message
shard_store = store.shards[shard] shard = shard_store.shard
# 1. Check shard parent exists # 1. Check shard parent exists
assert shard_block.shard_parent_root in shard_store.block_states assert shard_block.shard_parent_root in shard_store.block_states
pre_shard_state = shard_store.block_states[shard_block.shard_parent_root] pre_shard_state = shard_store.block_states[shard_block.shard_parent_root]

View File

@ -11,20 +11,21 @@ from eth2spec.test.helpers.state import next_slot, state_transition_and_sign_blo
from eth2spec.test.helpers.block import build_empty_block from eth2spec.test.helpers.block import build_empty_block
def run_on_shard_block(spec, store, shard, signed_block, valid=True): def run_on_shard_block(spec, store, shard_store, signed_block, valid=True):
if not valid: if not valid:
try: try:
spec.on_shard_block(store, shard, signed_block) spec.on_shard_block(store, shard_store, signed_block)
except AssertionError: except AssertionError:
return return
else: else:
assert False assert False
spec.on_shard_block(store, shard, signed_block) spec.on_shard_block(store, shard_store, signed_block)
assert store.shards[shard].blocks[hash_tree_root(signed_block.message)] == signed_block.message assert shard_store.blocks[hash_tree_root(signed_block.message)] == signed_block.message
def run_apply_shard_and_beacon(spec, state, store, shard, committee_index): def run_apply_shard_and_beacon(spec, state, store, shard_store, committee_index):
shard = shard_store.shard
store.time = store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH store.time = store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH
# Create SignedShardBlock # Create SignedShardBlock
@ -57,11 +58,11 @@ def run_apply_shard_and_beacon(spec, state, store, shard, committee_index):
beacon_block.body.shard_transitions = shard_transitions beacon_block.body.shard_transitions = shard_transitions
signed_beacon_block = state_transition_and_sign_block(spec, state, beacon_block) signed_beacon_block = state_transition_and_sign_block(spec, state, beacon_block)
run_on_shard_block(spec, store, shard, shard_block) run_on_shard_block(spec, store, shard_store, shard_block)
add_block_to_store(spec, store, signed_beacon_block) add_block_to_store(spec, store, signed_beacon_block)
assert spec.get_head(store) == beacon_block.hash_tree_root() assert spec.get_head(store) == beacon_block.hash_tree_root()
assert spec.get_shard_head(store, shard) == shard_block.message.hash_tree_root() assert spec.get_shard_head(store, shard_store) == shard_block.message.hash_tree_root()
@with_all_phases_except([PHASE0]) @with_all_phases_except([PHASE0])
@ -78,6 +79,7 @@ def test_basic(spec, state):
committee_index = spec.CommitteeIndex(0) committee_index = spec.CommitteeIndex(0)
shard = spec.compute_shard_from_committee_index(state, committee_index, state.slot) shard = spec.compute_shard_from_committee_index(state, committee_index, state.slot)
shard_store = spec.get_forkchoice_shard_store(state, shard)
run_apply_shard_and_beacon(spec, state, store, shard, committee_index) run_apply_shard_and_beacon(spec, state, store, shard_store, committee_index)
run_apply_shard_and_beacon(spec, state, store, shard, committee_index) run_apply_shard_and_beacon(spec, state, store, shard_store, committee_index)