Rework `shard_state_transition` interface

To make `shard_state_transition` similar to phase 0
`state_transition` function

1. Rename old `shard_state_transition` to `process_shard_block`
2. Add `shard_state_transition` with `validate_message` flag, we only
validate it in shard fork choice
This commit is contained in:
Hsiao-Wei Wang 2020-06-22 23:11:21 +08:00
parent e0d09e8685
commit 4f618fc62d
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
3 changed files with 20 additions and 25 deletions

View File

@ -169,14 +169,12 @@ def on_shard_block(store: Store, shard_store: ShardStore, signed_shard_block: Si
)
# Check the block is valid and compute the post-state
assert verify_shard_block_message(beacon_parent_state, shard_parent_state, shard_block)
assert verify_shard_block_signature(beacon_parent_state, signed_shard_block)
post_state = get_post_shard_state(shard_parent_state, shard_block)
shard_state = shard_parent_state.copy()
shard_state_transition(beacon_parent_state, shard_state, shard_block)
# Add new block to the store
shard_store.blocks[hash_tree_root(shard_block)] = shard_block
# Add new state for this block to the store
shard_store.block_states[hash_tree_root(shard_block)] = post_state
shard_store.block_states[hash_tree_root(shard_block)] = shard_state
```

View File

@ -42,8 +42,6 @@ def verify_shard_block_message(beacon_parent_state: BeaconState,
next_slot = Slot(block.slot + 1)
offset_slots = compute_offset_slots(get_latest_slot_for_shard(beacon_parent_state, shard), next_slot)
assert block.slot in offset_slots
# Check `shard` field
assert block.shard == shard
# Check `proposer_index` field
assert block.proposer_index == get_shard_proposer_index(beacon_parent_state, block.slot, shard)
# Check `body` field
@ -63,8 +61,20 @@ def verify_shard_block_signature(beacon_state: BeaconState,
## Shard state transition
```python
def shard_state_transition(shard_state: ShardState,
block: ShardBlock) -> None:
def shard_state_transition(beacon_parent_state: BeaconState,
shard_state: ShardState,
block: ShardBlock,
validate_message: bool = True) -> bool:
if validate_message:
assert verify_shard_block_message(beacon_parent_state, shard_state, block)
process_shard_block(shard_state, block)
return shard_state
```
```python
def process_shard_block(shard_state: ShardState,
block: ShardBlock) -> None:
"""
Update ``shard_state`` with shard ``block``.
"""
@ -79,19 +89,6 @@ def shard_state_transition(shard_state: ShardState,
shard_state.latest_block_root = latest_block_root
```
We have a pure function `get_post_shard_state` for describing the fraud proof verification and honest validator behavior.
```python
def get_post_shard_state(shard_state: ShardState,
block: ShardBlock) -> ShardState:
"""
A pure function that returns a new post ShardState instead of modifying the given `shard_state`.
"""
post_state = shard_state.copy()
shard_state_transition(post_state, block)
return post_state
```
## Fraud proofs
### Verifying the proof
@ -129,7 +126,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 = get_post_shard_state(shard_state, block)
shard_state_transition(beacon_state, shard_state, block, validate_message=False)
if shard_state != transition.shard_states[offset_index]:
return True

View File

@ -286,7 +286,7 @@ def get_shard_transition_fields(
shard_data_roots = []
shard_block_lengths = []
shard_state = beacon_state.shard_states[shard]
shard_state = beacon_state.shard_states[shard].copy()
shard_block_slots = [shard_block.message.slot for shard_block in shard_blocks]
offset_slots = compute_offset_slots(
get_latest_slot_for_shard(beacon_state, shard),
@ -299,7 +299,7 @@ def get_shard_transition_fields(
else:
shard_block = SignedShardBlock(message=ShardBlock(slot=slot, shard=shard))
shard_data_roots.append(Root())
shard_state = get_post_shard_state(shard_state, shard_block.message)
shard_state_transition(beacon_state, shard_state, shard_block.message, validate_message=False)
shard_states.append(shard_state)
shard_block_lengths.append(len(shard_block.message.body))