Apply Terence's suggestion: refactor on_shard_block interface

This commit is contained in:
Hsiao-Wei Wang 2020-07-22 22:48:52 +08:00
parent 4dcf5e2498
commit 33e56b5742
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
2 changed files with 10 additions and 12 deletions

View File

@ -142,13 +142,10 @@ def get_pending_shard_blocks(store: Store, shard: Shard) -> Sequence[SignedShard
#### `on_shard_block`
```python
def on_shard_block(store: Store, shard: Shard, signed_shard_block: SignedShardBlock) -> None:
shard_store = store.shard_stores[shard]
def on_shard_block(store: Store, signed_shard_block: SignedShardBlock) -> None:
shard_block = signed_shard_block.message
# Check shard
# TODO: check it in networking spec
assert shard_block.shard == shard
shard = shard_block.shard
shard_store = store.shard_stores[shard]
# Check shard parent exists
assert shard_block.shard_parent_root in shard_store.block_states

View File

@ -13,16 +13,17 @@ from eth2spec.test.helpers.state import state_transition_and_sign_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, signed_block, valid=True):
shard = signed_block.message.shard
if not valid:
try:
spec.on_shard_block(store, shard, signed_block)
spec.on_shard_block(store, signed_block)
except AssertionError:
return
else:
assert False
spec.on_shard_block(store, shard, signed_block)
spec.on_shard_block(store, signed_block)
shard_store = store.shard_stores[shard]
assert shard_store.signed_blocks[hash_tree_root(signed_block.message)] == signed_block
@ -53,7 +54,7 @@ def create_and_apply_shard_block(spec, store, shard, beacon_parent_state, shard_
shard_parent_state=shard_parent_state, slot=beacon_parent_state.slot, body=body, signed=True
)
shard_blocks_buffer.append(shard_block)
run_on_shard_block(spec, store, shard, shard_block)
run_on_shard_block(spec, store, shard_block)
assert spec.get_shard_head(store, shard) == shard_block.message.hash_tree_root()
@ -183,7 +184,7 @@ def create_simple_fork(spec, state, store, shard):
spec, beacon_parent_state, shard,
shard_parent_state=shard_parent_state, slot=beacon_parent_state.slot, body=body, signed=True
)
run_on_shard_block(spec, store, shard, forking_block_child)
run_on_shard_block(spec, store, forking_block_child)
# Shard block B
body = b'\x78' * 4 # different body
@ -191,7 +192,7 @@ def create_simple_fork(spec, state, store, shard):
spec, beacon_parent_state, shard,
shard_parent_state=shard_parent_state, slot=beacon_parent_state.slot, body=body, signed=True
)
run_on_shard_block(spec, store, shard, shard_block_b)
run_on_shard_block(spec, store, shard_block_b)
# Set forking_block
current_head = spec.get_shard_head(store, shard)