Merge pull request #1925 from ethereum/hwwhww/shard_state_transition

Rework `shard_state_transition` interface + fix #1922
This commit is contained in:
Hsiao-Wei Wang 2020-06-24 12:29:31 +08:00 committed by GitHub
commit 021788a634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 25 deletions

View File

@ -169,14 +169,14 @@ 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(
shard_state, signed_shard_block,
validate=True, beacon_parent_state=beacon_parent_state)
# 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

@ -50,10 +50,10 @@ def verify_shard_block_message(beacon_parent_state: BeaconState,
```
```python
def verify_shard_block_signature(beacon_state: BeaconState,
def verify_shard_block_signature(beacon_parent_state: BeaconState,
signed_block: SignedShardBlock) -> bool:
proposer = beacon_state.validators[signed_block.message.proposer_index]
domain = get_domain(beacon_state, DOMAIN_SHARD_PROPOSAL, compute_epoch_at_slot(signed_block.message.slot))
proposer = beacon_parent_state.validators[signed_block.message.proposer_index]
domain = get_domain(beacon_parent_state, DOMAIN_SHARD_PROPOSAL, compute_epoch_at_slot(signed_block.message.slot))
signing_root = compute_signing_root(signed_block.message, domain)
return bls.Verify(proposer.pubkey, signing_root, signed_block.signature)
```
@ -62,7 +62,21 @@ def verify_shard_block_signature(beacon_state: BeaconState,
```python
def shard_state_transition(shard_state: ShardState,
block: ShardBlock) -> None:
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)
assert verify_shard_block_signature(beacon_parent_state, signed_block)
process_shard_block(shard_state, signed_block.message)
return shard_state
```
```python
def process_shard_block(shard_state: ShardState,
block: ShardBlock) -> None:
"""
Update ``shard_state`` with shard ``block``.
"""
@ -77,19 +91,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
@ -127,7 +128,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(shard_state, block, validate=False)
if shard_state != transition.shard_states[offset_index]:
return True

View File

@ -280,7 +280,6 @@ def get_shard_transition_fields(
beacon_state: BeaconState,
shard: Shard,
shard_blocks: Sequence[SignedShardBlock],
validate_signature: bool=True,
) -> Tuple[Sequence[uint64], Sequence[Root], Sequence[ShardState]]:
shard_states = []
shard_data_roots = []
@ -299,7 +298,8 @@ 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 = shard_state.copy()
shard_state_transition(shard_state, shard_block, validate=False)
shard_states.append(shard_state)
shard_block_lengths.append(len(shard_block.message.body))