Refactor `do_fork` with terrifying eval() and `PREVIOUS_FORK_OF`

This commit is contained in:
Hsiao-Wei Wang 2023-12-15 03:24:42 +08:00
parent 1d7c3d4164
commit eb16a77702
No known key found for this signature in database
GPG Key ID: AE3D6B174F971DE4
1 changed files with 21 additions and 36 deletions

View File

@ -11,12 +11,8 @@ from eth2spec.test.helpers.block import (
) )
from eth2spec.test.helpers.bls_to_execution_changes import get_signed_address_change from eth2spec.test.helpers.bls_to_execution_changes import get_signed_address_change
from eth2spec.test.helpers.constants import ( from eth2spec.test.helpers.constants import (
ALTAIR, PHASE0,
BELLATRIX, PREVIOUS_FORK_OF,
CAPELLA,
DENEB,
EIP6110,
EIP7002,
) )
from eth2spec.test.helpers.deposits import ( from eth2spec.test.helpers.deposits import (
prepare_state_and_deposit, prepare_state_and_deposit,
@ -146,45 +142,34 @@ def state_transition_across_slots_with_ignoring_proposers(spec,
next_slot(spec, state) next_slot(spec, state)
def get_upgrade_fn(spec, fork):
try:
# TODO: make all upgrade_to_* function names consistent?
fn = eval(f"spec.upgrade_to_{fork}")
return fn
except Exception:
raise ValueError(f"Unknown fork: {fork}")
def do_fork(state, spec, post_spec, fork_epoch, with_block=True, sync_aggregate=None, operation_dict=None): def do_fork(state, spec, post_spec, fork_epoch, with_block=True, sync_aggregate=None, operation_dict=None):
spec.process_slots(state, state.slot + 1) spec.process_slots(state, state.slot + 1)
assert state.slot % spec.SLOTS_PER_EPOCH == 0 assert state.slot % spec.SLOTS_PER_EPOCH == 0
assert spec.get_current_epoch(state) == fork_epoch assert spec.get_current_epoch(state) == fork_epoch
if post_spec.fork == ALTAIR: state = get_upgrade_fn(post_spec, post_spec.fork)(state)
state = post_spec.upgrade_to_altair(state)
elif post_spec.fork == BELLATRIX:
state = post_spec.upgrade_to_bellatrix(state)
elif post_spec.fork == CAPELLA:
state = post_spec.upgrade_to_capella(state)
elif post_spec.fork == DENEB:
state = post_spec.upgrade_to_deneb(state)
elif post_spec.fork == EIP6110:
state = post_spec.upgrade_to_eip6110(state)
elif post_spec.fork == EIP7002:
state = post_spec.upgrade_to_eip7002(state)
assert state.fork.epoch == fork_epoch assert state.fork.epoch == fork_epoch
if post_spec.fork == ALTAIR: previous_fork = PREVIOUS_FORK_OF[post_spec.fork]
assert state.fork.previous_version == post_spec.config.GENESIS_FORK_VERSION if previous_fork == PHASE0:
assert state.fork.current_version == post_spec.config.ALTAIR_FORK_VERSION previous_version = spec.config.GENESIS_FORK_VERSION
elif post_spec.fork == BELLATRIX: else:
assert state.fork.previous_version == post_spec.config.ALTAIR_FORK_VERSION previous_version = getattr(post_spec.config, f"{previous_fork.upper()}_FORK_VERSION")
assert state.fork.current_version == post_spec.config.BELLATRIX_FORK_VERSION current_version = getattr(post_spec.config, f"{post_spec.fork.upper()}_FORK_VERSION")
elif post_spec.fork == CAPELLA:
assert state.fork.previous_version == post_spec.config.BELLATRIX_FORK_VERSION assert state.fork.previous_version == previous_version
assert state.fork.current_version == post_spec.config.CAPELLA_FORK_VERSION assert state.fork.current_version == current_version
elif post_spec.fork == DENEB:
assert state.fork.previous_version == post_spec.config.CAPELLA_FORK_VERSION
assert state.fork.current_version == post_spec.config.DENEB_FORK_VERSION
elif post_spec.fork == EIP6110:
assert state.fork.previous_version == post_spec.config.DENEB_FORK_VERSION
assert state.fork.current_version == post_spec.config.EIP6110_FORK_VERSION
elif post_spec.fork == EIP7002:
assert state.fork.previous_version == post_spec.config.CAPELLA_FORK_VERSION
assert state.fork.current_version == post_spec.config.EIP7002_FORK_VERSION
if with_block: if with_block:
return state, _state_transition_and_sign_block_at_slot( return state, _state_transition_and_sign_block_at_slot(