Make `state_transition` not return post state; the given pre state should have been mutated to post state.

This commit is contained in:
Hsiao-Wei Wang 2020-10-15 15:58:26 +08:00
parent 032a93335c
commit 7fb9226ec6
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
3 changed files with 4 additions and 5 deletions

View File

@ -1202,7 +1202,7 @@ Let `genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))`.
The post-state corresponding to a pre-state `state` and a signed block `signed_block` is defined as `state_transition(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.
```python
def state_transition(state: BeaconState, signed_block: SignedBeaconBlock, validate_result: bool=True) -> BeaconState:
def state_transition(state: BeaconState, signed_block: SignedBeaconBlock, validate_result: bool=True) -> None:
block = signed_block.message
# Process slots (including those with no blocks) since block
process_slots(state, block.slot)
@ -1214,8 +1214,6 @@ def state_transition(state: BeaconState, signed_block: SignedBeaconBlock, valida
# Verify state root
if validate_result:
assert block.state_root == hash_tree_root(state)
# Return post-state
return state
```
```python

View File

@ -355,7 +355,8 @@ def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
assert get_ancestor(store, block.parent_root, finalized_slot) == store.finalized_checkpoint.root
# Check the block is valid and compute the post-state
state = state_transition(pre_state, signed_block, True)
state = pre_state.copy()
state_transition(state, signed_block, True)
# Add new block to the store
store.blocks[hash_tree_root(block)] = block
# Add new state for this block to the store

View File

@ -372,7 +372,7 @@ It is useful to be able to run a state transition function (working on a copy of
def compute_new_state_root(state: BeaconState, block: BeaconBlock) -> Root:
temp_state: BeaconState = state.copy()
signed_block = SignedBeaconBlock(message=block)
temp_state = state_transition(temp_state, signed_block, validate_result=False)
state_transition(temp_state, signed_block, validate_result=False)
return hash_tree_root(temp_state)
```