Implement HW's exception-handling suggestion

This commit is contained in:
Justin Drake 2019-05-07 10:12:33 +01:00
parent f371daeb20
commit 50009ea85b
2 changed files with 14 additions and 8 deletions

View File

@ -5,7 +5,8 @@ import function_puller
def build_phase0_spec(sourcefile, outfile):
code_lines = []
code_lines.append("""
import copy
from typing import (
Any,
Callable,
@ -88,7 +89,7 @@ def apply_constants_preset(preset: Dict[str, Any]):
# Deal with derived constants
global_vars['GENESIS_EPOCH'] = slot_to_epoch(GENESIS_SLOT)
# Initialize SSZ types again, to account for changed lengths
init_SSZ_types()
""")

View File

@ -1262,16 +1262,21 @@ def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
## Beacon chain state transition function
The post-state corresponding to a pre-state `state` and a block `block` is defined as `state_transition(state, block)`. State transitions that trigger an unhandled exception (e.g. a failed `assert` or an out-of-range list access) are considered invalid.
The post-state corresponding to a pre-state `state` and a block `block` is defined as `state_transition(state, block)`.
```python
def state_transition(state: BeaconState, block: BeaconBlock) -> BeaconState:
# Process slots (including those with no blocks) since block
process_slots(state, block.slot)
# Process block
process_block(state, block)
# Return post-state
pre_state = copy.deepcopy(state)
try:
# Process slots (including those with no blocks) since block
process_slots(state, block.slot)
# Process block
process_block(state, block)
except Exception:
# State is not advanced on exceptions (e.g. a failed `assert` or an out-of-range list access)
return pre_state
return state
```
```python