create decorator for tests with matching config

This commit is contained in:
Etan Kissling 2022-06-24 01:05:11 +02:00
parent 840733ffd2
commit 9a253e437d
No known key found for this signature in database
GPG Key ID: B21DA824C5A3D03D
4 changed files with 69 additions and 58 deletions

View File

@ -1,4 +1,9 @@
from eth2spec.test.context import spec_configured_state_test, with_phases
from eth2spec.test.context import (
spec_configured_state_test,
spec_state_test_with_matching_config,
with_all_phases,
with_phases
)
from eth2spec.test.helpers.constants import ALTAIR
@ -17,3 +22,28 @@ def test_config_override(spec, state):
# TODO: it would be nice if the create_genesis_state actually outputs a state
# for the fork with a slot that matches at least the fork boundary.
# assert spec.get_current_epoch(state) >= 4
@with_all_phases
@spec_state_test_with_matching_config
def test_override_config_fork_epoch(spec, state):
if state.fork.current_version == spec.config.GENESIS_FORK_VERSION:
return
assert spec.config.ALTAIR_FORK_EPOCH == spec.GENESIS_EPOCH
if state.fork.current_version == spec.config.ALTAIR_FORK_VERSION:
return
assert spec.config.BELLATRIX_FORK_EPOCH == spec.GENESIS_EPOCH
if state.fork.current_version == spec.config.BELLATRIX_FORK_VERSION:
return
assert spec.config.CAPELLA_FORK_EPOCH == spec.GENESIS_EPOCH
if state.fork.current_version == spec.config.CAPELLA_FORK_VERSION:
return
assert spec.config.SHARDING_FORK_EPOCH == spec.GENESIS_EPOCH
if state.fork.current_version == spec.config.SHARDING_FORK_VERSION:
return
assert False # Fork is missing

View File

@ -1,7 +1,7 @@
from copy import deepcopy
from eth2spec.test.context import (
spec_state_test,
spec_state_test_with_matching_config,
with_presets,
with_altair_and_later,
)
@ -16,7 +16,6 @@ from eth2spec.test.helpers.constants import MINIMAL
from eth2spec.test.helpers.light_client import (
get_sync_aggregate,
initialize_light_client_store,
override_config_fork_epochs,
)
from eth2spec.test.helpers.state import (
next_slots,
@ -26,11 +25,8 @@ from eth2spec.test.helpers.merkle import build_proof
@with_altair_and_later
@spec_state_test
@spec_state_test_with_matching_config
def test_process_light_client_update_not_timeout(spec, state):
old_config = spec.config
override_config_fork_epochs(spec, state)
store = initialize_light_client_store(spec, state)
# Block at slot 1 doesn't increase sync committee period, so it won't force update store.finalized_header
@ -73,16 +69,11 @@ def test_process_light_client_update_not_timeout(spec, state):
assert store.finalized_header == pre_store.finalized_header
assert store.best_valid_update == update
spec.config = old_config
@with_altair_and_later
@spec_state_test
@spec_state_test_with_matching_config
@with_presets([MINIMAL], reason="too slow")
def test_process_light_client_update_at_period_boundary(spec, state):
old_config = spec.config
override_config_fork_epochs(spec, state)
store = initialize_light_client_store(spec, state)
# Forward to slot before next sync committee period so that next block is final one in period
@ -128,16 +119,11 @@ def test_process_light_client_update_at_period_boundary(spec, state):
assert store.best_valid_update == update
assert store.finalized_header == pre_store.finalized_header
spec.config = old_config
@with_altair_and_later
@spec_state_test
@spec_state_test_with_matching_config
@with_presets([MINIMAL], reason="too slow")
def test_process_light_client_update_timeout(spec, state):
old_config = spec.config
override_config_fork_epochs(spec, state)
store = initialize_light_client_store(spec, state)
# Forward to next sync committee period
@ -184,16 +170,11 @@ def test_process_light_client_update_timeout(spec, state):
assert store.best_valid_update == update
assert store.finalized_header == pre_store.finalized_header
spec.config = old_config
@with_altair_and_later
@spec_state_test
@spec_state_test_with_matching_config
@with_presets([MINIMAL], reason="too slow")
def test_process_light_client_update_finality_updated(spec, state):
old_config = spec.config
override_config_fork_epochs(spec, state)
store = initialize_light_client_store(spec, state)
# Change finality
@ -245,5 +226,3 @@ def test_process_light_client_update_finality_updated(spec, state):
assert store.optimistic_header == update.attested_header
assert store.finalized_header == update.finalized_header
assert store.best_valid_update is None
spec.config = old_config

View File

@ -277,6 +277,39 @@ def spec_configured_state_test(conf):
return decorator
def config_fork_epoch_overrides(spec, state):
overrides = {}
if state.fork.current_version == spec.config.GENESIS_FORK_VERSION:
pass
elif state.fork.current_version == spec.config.ALTAIR_FORK_VERSION:
overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.BELLATRIX_FORK_VERSION:
overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.CAPELLA_FORK_VERSION:
overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['CAPELLA_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.SHARDING_FORK_VERSION:
overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['CAPELLA_FORK_EPOCH'] = spec.GENESIS_EPOCH
overrides['SHARDING_FORK_EPOCH'] = spec.GENESIS_EPOCH
else:
assert False # Fork is missing
return overrides
def spec_state_test_with_matching_config(fn):
def decorator(fn):
def wrapper(*args, spec: Spec, **kw):
conf = config_fork_epoch_overrides(spec, kw['state'])
overrides = with_config_overrides(conf)
return overrides(fn)(*args, spec=spec, **kw)
return wrapper
return spec_test(with_state(decorator(single_phase(fn))))
def expect_assertion_error(fn):
bad = False
try:

View File

@ -1,5 +1,3 @@
from copy import deepcopy
from eth2spec.test.helpers.state import (
transition_to,
)
@ -9,35 +7,6 @@ from eth2spec.test.helpers.sync_committee import (
)
def override_config_fork_epochs(spec, state):
# Test framework adjusts state fork but leaves spec config constants inconsistent
config_overrides = {}
if state.fork.current_version == spec.config.GENESIS_FORK_VERSION:
pass
elif state.fork.current_version == spec.config.ALTAIR_FORK_VERSION:
config_overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.BELLATRIX_FORK_VERSION:
config_overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.CAPELLA_FORK_VERSION:
config_overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['CAPELLA_FORK_EPOCH'] = spec.GENESIS_EPOCH
elif state.fork.current_version == spec.config.SHARDING_FORK_VERSION:
config_overrides['ALTAIR_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['BELLATRIX_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['CAPELLA_FORK_EPOCH'] = spec.GENESIS_EPOCH
config_overrides['SHARDING_FORK_EPOCH'] = spec.GENESIS_EPOCH
else:
assert False
tmp_config = deepcopy(spec.config._asdict())
tmp_config.update(config_overrides)
config_types = spec.Configuration.__annotations__
test_config = {k: config_types[k](v) for k, v in tmp_config.items()}
spec.config = spec.Configuration(**test_config)
def initialize_light_client_store(spec, state):
return spec.LightClientStore(
finalized_header=spec.BeaconBlockHeader(),