PR feedback from Danny: nuke optional beacon_parent_state

This commit is contained in:
Hsiao-Wei Wang 2020-06-25 11:14:25 +08:00
parent 79b6bc616d
commit 2a7b5f7e68
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
4 changed files with 13 additions and 19 deletions

View File

@ -170,9 +170,7 @@ def on_shard_block(store: Store, shard_store: ShardStore, signed_shard_block: Si
# Check the block is valid and compute the post-state
shard_state = shard_parent_state.copy()
shard_state_transition(
shard_state, signed_shard_block,
validate=True, beacon_parent_state=beacon_parent_state)
shard_state_transition(shard_state, signed_shard_block, beacon_parent_state, validate_result=True)
# Add new block to the store
shard_store.blocks[hash_tree_root(shard_block)] = shard_block

View File

@ -66,16 +66,16 @@ def verify_shard_block_signature(beacon_parent_state: BeaconState,
## Shard state transition function
The post-state corresponding to a pre-state `shard_state` and a signed block `signed_block` is defined as `shard_state_transition(shard_state, signed_block)`. State transitions that trigger an unhandled exception (e.g. a failed `assert` or an out-of-range list access) are considered invalid. State transitions that cause a `uint64` overflow or underflow are also considered invalid.
The post-state corresponding to a pre-state `shard_state` and a signed block `signed_block` is defined as `shard_state_transition(shard_state, signed_block, beacon_parent_state)`, where `beacon_parent_state` is the parent beacon state of the `signed_block`. State transitions that trigger an unhandled exception (e.g. a failed `assert` or an out-of-range list access) are considered invalid. State transitions that cause a `uint64` overflow or underflow are also considered invalid.
```python
def shard_state_transition(shard_state: ShardState,
signed_block: SignedShardBlock,
validate: bool = True,
beacon_parent_state: Optional[BeaconState] = None) -> ShardState:
if validate:
assert beacon_parent_state is not None
assert verify_shard_block_message(beacon_parent_state, shard_state, signed_block.message)
beacon_parent_state: BeaconState,
validate_result: bool = True) -> ShardState:
assert verify_shard_block_message(beacon_parent_state, shard_state, signed_block.message)
if validate_result:
assert verify_shard_block_signature(beacon_parent_state, signed_block)
process_shard_block(shard_state, signed_block.message)
@ -133,7 +133,7 @@ def is_valid_fraud_proof(beacon_state: BeaconState,
else:
shard_state = transition.shard_states[offset_index - 1] # Not doing the actual state updates here.
shard_state_transition(shard_state, block, validate=False)
process_shard_block(shard_state, block.message)
if shard_state != transition.shard_states[offset_index]:
return True

View File

@ -299,7 +299,7 @@ def get_shard_transition_fields(
shard_block = SignedShardBlock(message=ShardBlock(slot=slot, shard=shard))
shard_data_roots.append(Root())
shard_state = shard_state.copy()
shard_state_transition(shard_state, shard_block, validate=False)
process_shard_block(shard_state, shard_block.message)
shard_states.append(shard_state)
shard_block_lengths.append(len(shard_block.message.body))

View File

@ -13,25 +13,21 @@ from eth2spec.test.helpers.shard_transitions import is_full_crosslink
from eth2spec.test.helpers.state import transition_to_valid_shard_slot
def run_shard_blocks(spec, shard_state, signed_shard_block,
beacon_parent_state,
validate=True, valid=True):
def run_shard_blocks(spec, shard_state, signed_shard_block, beacon_parent_state, valid=True):
pre_shard_state = shard_state.copy()
yield 'pre', pre_shard_state
yield 'signed_shard_block', signed_shard_block
yield 'validate', validate
yield 'beacon_parent_state', beacon_parent_state
if not valid:
expect_assertion_error(lambda: spec.shard_state_transition(
shard_state, signed_shard_block, validate=validate, beacon_parent_state=beacon_parent_state)
expect_assertion_error(
lambda: spec.shard_state_transition(shard_state, signed_shard_block, beacon_parent_state)
)
yield 'post', None
return
spec.shard_state_transition(shard_state, signed_shard_block,
validate=validate, beacon_parent_state=beacon_parent_state)
spec.shard_state_transition(shard_state, signed_shard_block, beacon_parent_state)
yield 'post', shard_state
# Verify `process_shard_block`