PR review from Danny

This commit is contained in:
Hsiao-Wei Wang 2020-06-02 18:08:28 +08:00
parent 63de59d5f6
commit 142ba17451
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
1 changed files with 17 additions and 8 deletions

View File

@ -59,7 +59,9 @@ def get_shard_latest_attesting_balance(store: Store, shard_store: ShardStore, ro
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
store.latest_messages[i].shard == shard_store.shard and
get_shard_ancestor(
store, shard_store, store.latest_messages[i].root, shard_store.blocks[root].slot store, shard_store, store.latest_messages[i].root, shard_store.blocks[root].slot
) == root ) == root
) )
@ -71,12 +73,18 @@ def get_shard_latest_attesting_balance(store: Store, shard_store: ShardStore, ro
```python ```python
def get_shard_head(store: Store, shard_store: ShardStore) -> Root: def get_shard_head(store: Store, shard_store: ShardStore) -> Root:
# Execute the LMD-GHOST fork choice # Execute the LMD-GHOST fork choice
shard_blocks = shard_store.blocks
head_beacon_root = get_head(store) head_beacon_root = get_head(store)
head_shard_root = store.block_states[head_beacon_root].shard_states[shard_store.shard].latest_block_root head_shard_state = store.block_states[head_beacon_root].shard_states[shard_store.shard]
head_shard_root = head_shard_state.latest_block_root
while True: while True:
# Find the valid child block roots
children = [ children = [
root for root in shard_store.blocks.keys() root for root in shard_store.blocks.keys()
if shard_store.blocks[root].shard_parent_root == head_shard_root if (
shard_blocks[root].shard_parent_root == head_shard_root
and shard_blocks[root].slot > head_shard_state.slot
)
] ]
if len(children) == 0: if len(children) == 0:
return head_shard_root return head_shard_root
@ -116,14 +124,15 @@ def on_shard_block(store: Store, shard_store: ShardStore, signed_shard_block: Si
assert shard_block.beacon_parent_root in store.block_states assert shard_block.beacon_parent_root in store.block_states
beacon_state = store.block_states[shard_block.beacon_parent_root] beacon_state = store.block_states[shard_block.beacon_parent_root]
# 3. Check that block is later than the finalized epoch slot (optimization to reduce calls to get_ancestor) # 3. Check that block is later than the finalized shard state slot (optimization to reduce calls to get_ancestor)
finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch) finalized_beacon_state = store.block_states[store.finalized_checkpoint.root]
assert shard_block.slot > finalized_slot finalized_shard_state = finalized_beacon_state.shard_states[shard]
assert shard_block.slot > finalized_shard_state.slot
# 4. Check block is a descendant of the finalized block at the checkpoint finalized slot # 4. Check block is a descendant of the finalized block at the checkpoint finalized slot
finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
assert ( assert (
shard_block.beacon_parent_root == store.finalized_checkpoint.root get_ancestor(store, shard_block.beacon_parent_root, finalized_slot) == store.finalized_checkpoint.root
or get_ancestor(store, shard_block.beacon_parent_root, finalized_slot) == store.finalized_checkpoint.root
) )
# Add new block to the store # Add new block to the store