Skip to content

Commit eb16a77

Browse files
committed
Refactor do_fork with terrifying eval() and PREVIOUS_FORK_OF
1 parent 1d7c3d4 commit eb16a77

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

tests/core/pyspec/eth2spec/test/helpers/fork_transition.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111
)
1212
from eth2spec.test.helpers.bls_to_execution_changes import get_signed_address_change
1313
from eth2spec.test.helpers.constants import (
14-
ALTAIR,
15-
BELLATRIX,
16-
CAPELLA,
17-
DENEB,
18-
EIP6110,
19-
EIP7002,
14+
PHASE0,
15+
PREVIOUS_FORK_OF,
2016
)
2117
from eth2spec.test.helpers.deposits import (
2218
prepare_state_and_deposit,
@@ -146,45 +142,34 @@ def state_transition_across_slots_with_ignoring_proposers(spec,
146142
next_slot(spec, state)
147143

148144

145+
def get_upgrade_fn(spec, fork):
146+
try:
147+
# TODO: make all upgrade_to_* function names consistent?
148+
fn = eval(f"spec.upgrade_to_{fork}")
149+
return fn
150+
except Exception:
151+
raise ValueError(f"Unknown fork: {fork}")
152+
153+
149154
def do_fork(state, spec, post_spec, fork_epoch, with_block=True, sync_aggregate=None, operation_dict=None):
150155
spec.process_slots(state, state.slot + 1)
151156

152157
assert state.slot % spec.SLOTS_PER_EPOCH == 0
153158
assert spec.get_current_epoch(state) == fork_epoch
154159

155-
if post_spec.fork == ALTAIR:
156-
state = post_spec.upgrade_to_altair(state)
157-
elif post_spec.fork == BELLATRIX:
158-
state = post_spec.upgrade_to_bellatrix(state)
159-
elif post_spec.fork == CAPELLA:
160-
state = post_spec.upgrade_to_capella(state)
161-
elif post_spec.fork == DENEB:
162-
state = post_spec.upgrade_to_deneb(state)
163-
elif post_spec.fork == EIP6110:
164-
state = post_spec.upgrade_to_eip6110(state)
165-
elif post_spec.fork == EIP7002:
166-
state = post_spec.upgrade_to_eip7002(state)
160+
state = get_upgrade_fn(post_spec, post_spec.fork)(state)
167161

168162
assert state.fork.epoch == fork_epoch
169163

170-
if post_spec.fork == ALTAIR:
171-
assert state.fork.previous_version == post_spec.config.GENESIS_FORK_VERSION
172-
assert state.fork.current_version == post_spec.config.ALTAIR_FORK_VERSION
173-
elif post_spec.fork == BELLATRIX:
174-
assert state.fork.previous_version == post_spec.config.ALTAIR_FORK_VERSION
175-
assert state.fork.current_version == post_spec.config.BELLATRIX_FORK_VERSION
176-
elif post_spec.fork == CAPELLA:
177-
assert state.fork.previous_version == post_spec.config.BELLATRIX_FORK_VERSION
178-
assert state.fork.current_version == post_spec.config.CAPELLA_FORK_VERSION
179-
elif post_spec.fork == DENEB:
180-
assert state.fork.previous_version == post_spec.config.CAPELLA_FORK_VERSION
181-
assert state.fork.current_version == post_spec.config.DENEB_FORK_VERSION
182-
elif post_spec.fork == EIP6110:
183-
assert state.fork.previous_version == post_spec.config.DENEB_FORK_VERSION
184-
assert state.fork.current_version == post_spec.config.EIP6110_FORK_VERSION
185-
elif post_spec.fork == EIP7002:
186-
assert state.fork.previous_version == post_spec.config.CAPELLA_FORK_VERSION
187-
assert state.fork.current_version == post_spec.config.EIP7002_FORK_VERSION
164+
previous_fork = PREVIOUS_FORK_OF[post_spec.fork]
165+
if previous_fork == PHASE0:
166+
previous_version = spec.config.GENESIS_FORK_VERSION
167+
else:
168+
previous_version = getattr(post_spec.config, f"{previous_fork.upper()}_FORK_VERSION")
169+
current_version = getattr(post_spec.config, f"{post_spec.fork.upper()}_FORK_VERSION")
170+
171+
assert state.fork.previous_version == previous_version
172+
assert state.fork.current_version == current_version
188173

189174
if with_block:
190175
return state, _state_transition_and_sign_block_at_slot(

0 commit comments

Comments
 (0)