Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions tests/core/pyspec/eth2spec/test/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from copy import deepcopy
from dataclasses import dataclass
import importlib
from eth_utils import encode_hex

from eth2spec.phase0 import mainnet as spec_phase0_mainnet, minimal as spec_phase0_minimal
Expand Down Expand Up @@ -86,10 +86,9 @@ class SpecForks(TypedDict, total=False):

def _prepare_state(balances_fn: Callable[[Any], Sequence[int]], threshold_fn: Callable[[Any], int],
spec: Spec, phases: SpecForks):
phase = phases[spec.fork]
balances = balances_fn(phase)
activation_threshold = threshold_fn(phase)
state = create_genesis_state(spec=phase, validator_balances=balances,
balances = balances_fn(spec)
activation_threshold = threshold_fn(spec)
state = create_genesis_state(spec=spec, validator_balances=balances,
activation_threshold=activation_threshold)
return state

Expand Down Expand Up @@ -481,6 +480,16 @@ def _get_basic_dict(ssz_dict: Dict[str, Any]) -> Dict[str, Any]:
return result


def _get_copy_of_spec(spec):
fork = spec.fork
preset = spec.config.PRESET_BASE
module_path = f"eth2spec.{fork}.{preset}"
module_spec = importlib.util.find_spec(module_path)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return module


def with_config_overrides(config_overrides):
"""
WARNING: the spec_test decorator must wrap this, to ensure the decorated test actually runs.
Expand All @@ -491,33 +500,27 @@ def with_config_overrides(config_overrides):
"""
def decorator(fn):
def wrapper(*args, spec: Spec, **kw):
# remember the old config
old_config = spec.config
spec = _get_copy_of_spec(spec)

# apply our overrides to a copy of it, and apply it to the spec
tmp_config = deepcopy(old_config._asdict()) # not a private method, there are multiple
tmp_config.update(config_overrides)
config = spec.config._asdict()
config.update(config_overrides)
config_types = spec.Configuration.__annotations__
# Retain types of all config values
test_config = {k: config_types[k](v) for k, v in tmp_config.items()}
modified_config = {k: config_types[k](v) for k, v in config.items()}

# To output the changed config to could be serialized with yaml test vectors,
# the dict SSZ objects have to be converted into Python built-in types.
output_config = _get_basic_dict(test_config)
output_config = _get_basic_dict(modified_config)
yield 'config', 'data', output_config

spec.config = spec.Configuration(**test_config)
spec.config = spec.Configuration(**modified_config)

# Run the function
out = fn(*args, spec=spec, **kw)
# If it's not returning None like a normal test function,
# it's generating things, and we need to complete it before setting back the config.
if out is not None:
yield from out

# Restore the old config and apply it
spec.config = old_config

return wrapper
return decorator

Expand Down