2020-08-12 12:48:52 +00:00
|
|
|
import pytest
|
2022-12-07 20:10:09 +00:00
|
|
|
from copy import deepcopy
|
2021-10-29 15:17:57 +00:00
|
|
|
from dataclasses import dataclass
|
2021-12-07 16:51:32 +00:00
|
|
|
import importlib
|
2021-10-29 15:17:57 +00:00
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
from eth2spec.phase0 import mainnet as spec_phase0_mainnet, minimal as spec_phase0_minimal
|
|
|
|
from eth2spec.altair import mainnet as spec_altair_mainnet, minimal as spec_altair_minimal
|
2021-12-22 07:46:18 +00:00
|
|
|
from eth2spec.bellatrix import mainnet as spec_bellatrix_mainnet, minimal as spec_bellatrix_minimal
|
2021-12-01 18:37:30 +00:00
|
|
|
from eth2spec.capella import mainnet as spec_capella_mainnet, minimal as spec_capella_minimal
|
2023-01-23 14:08:34 +00:00
|
|
|
from eth2spec.deneb import mainnet as spec_deneb_mainnet, minimal as spec_deneb_minimal
|
2023-03-27 12:58:43 +00:00
|
|
|
from eth2spec.eip6110 import mainnet as spec_eip6110_mainnet, minimal as spec_eip6110_minimal
|
2019-05-13 21:15:02 +00:00
|
|
|
from eth2spec.utils import bls
|
2019-05-05 22:31:57 +00:00
|
|
|
|
2020-08-12 12:48:52 +00:00
|
|
|
from .exceptions import SkippedTest
|
2021-04-12 14:20:27 +00:00
|
|
|
from .helpers.constants import (
|
2023-01-23 14:08:34 +00:00
|
|
|
PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB,
|
2023-03-27 12:58:43 +00:00
|
|
|
EIP6110,
|
2021-12-01 18:37:30 +00:00
|
|
|
MINIMAL, MAINNET,
|
2022-10-30 11:50:56 +00:00
|
|
|
ALL_PHASES,
|
2021-11-02 11:01:32 +00:00
|
|
|
ALL_FORK_UPGRADES,
|
2021-04-12 14:20:27 +00:00
|
|
|
)
|
2022-11-10 12:26:00 +00:00
|
|
|
from .helpers.forks import is_post_fork
|
2021-10-29 15:17:57 +00:00
|
|
|
from .helpers.typing import SpecForkName, PresetBaseName
|
2019-05-17 18:31:21 +00:00
|
|
|
from .helpers.genesis import create_genesis_state
|
2021-10-29 15:17:57 +00:00
|
|
|
from .utils import (
|
|
|
|
vector_test,
|
|
|
|
with_meta_tags,
|
|
|
|
)
|
2019-05-06 15:10:43 +00:00
|
|
|
|
2020-03-20 19:38:36 +00:00
|
|
|
from random import Random
|
2021-05-18 11:59:26 +00:00
|
|
|
from typing import Any, Callable, Sequence, TypedDict, Protocol, Dict
|
2019-05-31 08:41:39 +00:00
|
|
|
|
2020-05-18 23:55:17 +00:00
|
|
|
from lru import LRU
|
2019-11-19 19:16:40 +00:00
|
|
|
|
2021-05-18 14:17:02 +00:00
|
|
|
# Without pytest CLI arg or pyspec-test-generator 'preset' argument, this will be the config to apply.
|
|
|
|
DEFAULT_TEST_PRESET = MINIMAL
|
|
|
|
|
2021-10-18 09:42:47 +00:00
|
|
|
# Without pytest CLI arg or pyspec-test-generator 'run-phase' argument, this will be the config to apply.
|
|
|
|
DEFAULT_PYTEST_FORKS = ALL_PHASES
|
|
|
|
|
2019-10-23 08:33:48 +00:00
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
# TODO: currently phases are defined as python modules.
|
|
|
|
# It would be better if they would be more well-defined interfaces for stronger typing.
|
2020-04-02 06:54:48 +00:00
|
|
|
|
2021-05-18 14:08:30 +00:00
|
|
|
class Configuration(Protocol):
|
|
|
|
PRESET_BASE: str
|
|
|
|
|
2020-04-02 06:54:48 +00:00
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
class Spec(Protocol):
|
2021-05-18 14:08:30 +00:00
|
|
|
fork: str
|
|
|
|
config: Configuration
|
2020-01-05 22:37:05 +00:00
|
|
|
|
|
|
|
|
2020-04-02 06:54:48 +00:00
|
|
|
class SpecPhase0(Spec):
|
2020-01-05 22:37:05 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
|
2021-03-15 15:53:13 +00:00
|
|
|
class SpecAltair(Spec):
|
2020-12-15 05:18:20 +00:00
|
|
|
...
|
2020-01-05 22:37:05 +00:00
|
|
|
|
|
|
|
|
2021-12-22 07:46:18 +00:00
|
|
|
class SpecBellatrix(Spec):
|
2021-04-05 14:59:09 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
|
2021-12-01 18:37:30 +00:00
|
|
|
class SpecCapella(Spec):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
class ForkMeta:
|
|
|
|
pre_fork_name: str
|
|
|
|
post_fork_name: str
|
|
|
|
fork_epoch: int
|
|
|
|
|
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
spec_targets: Dict[PresetBaseName, Dict[SpecForkName, Spec]] = {
|
|
|
|
MINIMAL: {
|
|
|
|
PHASE0: spec_phase0_minimal,
|
|
|
|
ALTAIR: spec_altair_minimal,
|
2021-12-22 07:46:18 +00:00
|
|
|
BELLATRIX: spec_bellatrix_minimal,
|
2021-12-01 18:37:30 +00:00
|
|
|
CAPELLA: spec_capella_minimal,
|
2023-01-23 14:08:34 +00:00
|
|
|
DENEB: spec_deneb_minimal,
|
2023-03-27 12:58:43 +00:00
|
|
|
EIP6110: spec_eip6110_minimal,
|
2021-05-18 11:59:26 +00:00
|
|
|
},
|
|
|
|
MAINNET: {
|
|
|
|
PHASE0: spec_phase0_mainnet,
|
|
|
|
ALTAIR: spec_altair_mainnet,
|
2021-12-22 07:46:18 +00:00
|
|
|
BELLATRIX: spec_bellatrix_mainnet,
|
2021-12-01 18:37:30 +00:00
|
|
|
CAPELLA: spec_capella_mainnet,
|
2023-03-27 12:58:43 +00:00
|
|
|
DENEB: spec_deneb_mainnet,
|
|
|
|
EIP6110: spec_eip6110_mainnet,
|
2021-05-18 11:59:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
class SpecForks(TypedDict, total=False):
|
2020-04-02 06:54:48 +00:00
|
|
|
PHASE0: SpecPhase0
|
2021-03-15 15:53:13 +00:00
|
|
|
ALTAIR: SpecAltair
|
2021-12-22 07:46:18 +00:00
|
|
|
BELLATRIX: SpecBellatrix
|
2021-12-01 18:37:30 +00:00
|
|
|
CAPELLA: SpecCapella
|
2020-01-05 22:37:05 +00:00
|
|
|
|
|
|
|
|
2020-05-18 23:55:17 +00:00
|
|
|
def _prepare_state(balances_fn: Callable[[Any], Sequence[int]], threshold_fn: Callable[[Any], int],
|
2021-03-09 23:18:30 +00:00
|
|
|
spec: Spec, phases: SpecForks):
|
2021-12-07 17:50:00 +00:00
|
|
|
balances = balances_fn(spec)
|
|
|
|
activation_threshold = threshold_fn(spec)
|
|
|
|
state = create_genesis_state(spec=spec, validator_balances=balances,
|
2020-05-18 23:55:17 +00:00
|
|
|
activation_threshold=activation_threshold)
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
|
|
|
_custom_state_cache_dict = LRU(size=10)
|
|
|
|
|
|
|
|
|
2019-10-23 08:33:48 +00:00
|
|
|
def with_custom_state(balances_fn: Callable[[Any], Sequence[int]],
|
|
|
|
threshold_fn: Callable[[Any], int]):
|
|
|
|
def deco(fn):
|
2020-01-05 22:37:05 +00:00
|
|
|
def entry(*args, spec: Spec, phases: SpecForks, **kw):
|
2021-05-18 14:08:30 +00:00
|
|
|
# make a key for the state, unique to the fork + config (incl preset choice) and balances/activations
|
|
|
|
key = (spec.fork, spec.config.__hash__(), spec.__file__, balances_fn, threshold_fn)
|
2020-05-18 23:55:17 +00:00
|
|
|
global _custom_state_cache_dict
|
|
|
|
if key not in _custom_state_cache_dict:
|
2021-03-09 23:18:30 +00:00
|
|
|
state = _prepare_state(balances_fn, threshold_fn, spec, phases)
|
2020-05-18 23:55:17 +00:00
|
|
|
_custom_state_cache_dict[key] = state.get_backing()
|
|
|
|
|
2020-05-19 01:42:58 +00:00
|
|
|
# Take an entry out of the LRU.
|
2020-05-18 23:55:17 +00:00
|
|
|
# No copy is necessary, as we wrap the immutable backing with a new view.
|
|
|
|
state = spec.BeaconState(backing=_custom_state_cache_dict[key])
|
|
|
|
kw['state'] = state
|
2020-01-05 22:37:05 +00:00
|
|
|
return fn(*args, spec=spec, phases=phases, **kw)
|
2019-10-23 08:33:48 +00:00
|
|
|
return entry
|
|
|
|
return deco
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def default_activation_threshold(spec: Spec):
|
2019-10-24 07:31:43 +00:00
|
|
|
"""
|
|
|
|
Helper method to use the default balance activation threshold for state creation for tests.
|
|
|
|
Usage: `@with_custom_state(threshold_fn=default_activation_threshold, ...)`
|
|
|
|
"""
|
2019-10-23 08:33:48 +00:00
|
|
|
return spec.MAX_EFFECTIVE_BALANCE
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def zero_activation_threshold(spec: Spec):
|
2020-03-16 17:19:21 +00:00
|
|
|
"""
|
2020-03-18 15:55:09 +00:00
|
|
|
Helper method to use 0 gwei as the activation threshold for state creation for tests.
|
|
|
|
Usage: `@with_custom_state(threshold_fn=zero_activation_threshold, ...)`
|
2020-03-16 17:19:21 +00:00
|
|
|
"""
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def default_balances(spec: Spec):
|
2019-10-24 07:31:43 +00:00
|
|
|
"""
|
|
|
|
Helper method to create a series of default balances.
|
|
|
|
Usage: `@with_custom_state(balances_fn=default_balances, ...)`
|
|
|
|
"""
|
2020-02-20 19:34:37 +00:00
|
|
|
num_validators = spec.SLOTS_PER_EPOCH * 8
|
2019-10-23 08:33:48 +00:00
|
|
|
return [spec.MAX_EFFECTIVE_BALANCE] * num_validators
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def scaled_churn_balances(spec: Spec):
|
2021-09-08 02:34:28 +00:00
|
|
|
"""
|
|
|
|
Helper method to create enough validators to scale the churn limit.
|
|
|
|
(This is *firmly* over the churn limit -- thus the +2 instead of just +1)
|
|
|
|
See the second argument of ``max`` in ``get_validator_churn_limit``.
|
|
|
|
Usage: `@with_custom_state(balances_fn=scaled_churn_balances, ...)`
|
|
|
|
"""
|
|
|
|
num_validators = spec.config.CHURN_LIMIT_QUOTIENT * (2 + spec.config.MIN_PER_EPOCH_CHURN_LIMIT)
|
|
|
|
return [spec.MAX_EFFECTIVE_BALANCE] * num_validators
|
|
|
|
|
|
|
|
|
2019-10-23 08:33:48 +00:00
|
|
|
with_state = with_custom_state(default_balances, default_activation_threshold)
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def low_balances(spec: Spec):
|
2019-10-23 08:33:48 +00:00
|
|
|
"""
|
2019-10-24 07:31:43 +00:00
|
|
|
Helper method to create a series of low balances.
|
|
|
|
Usage: `@with_custom_state(balances_fn=low_balances, ...)`
|
2019-10-23 08:33:48 +00:00
|
|
|
"""
|
|
|
|
num_validators = spec.SLOTS_PER_EPOCH * 8
|
|
|
|
# Technically the balances cannot be this low starting from genesis, but it is useful for testing
|
|
|
|
low_balance = 18 * 10 ** 9
|
|
|
|
return [low_balance] * num_validators
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def misc_balances(spec: Spec):
|
2019-10-23 08:33:48 +00:00
|
|
|
"""
|
|
|
|
Helper method to create a series of balances that includes some misc. balances.
|
2019-10-24 07:31:43 +00:00
|
|
|
Usage: `@with_custom_state(balances_fn=misc_balances, ...)`
|
2019-10-23 08:33:48 +00:00
|
|
|
"""
|
|
|
|
num_validators = spec.SLOTS_PER_EPOCH * 8
|
2020-03-20 19:38:36 +00:00
|
|
|
balances = [spec.MAX_EFFECTIVE_BALANCE * 2 * i // num_validators for i in range(num_validators)]
|
|
|
|
rng = Random(1234)
|
|
|
|
rng.shuffle(balances)
|
|
|
|
return balances
|
2019-05-06 15:10:43 +00:00
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def misc_balances_in_default_range_with_many_validators(spec: Spec):
|
2021-08-21 23:59:54 +00:00
|
|
|
"""
|
|
|
|
Helper method to create a series of balances that includes some misc. balances but
|
|
|
|
none that are below the ``EJECTION_BALANCE``.
|
|
|
|
"""
|
2021-08-22 16:58:54 +00:00
|
|
|
# Double validators to facilitate randomized testing
|
|
|
|
num_validators = spec.SLOTS_PER_EPOCH * 8 * 2
|
2021-08-21 23:59:54 +00:00
|
|
|
floor = spec.config.EJECTION_BALANCE + spec.EFFECTIVE_BALANCE_INCREMENT
|
|
|
|
balances = [
|
|
|
|
max(spec.MAX_EFFECTIVE_BALANCE * 2 * i // num_validators, floor) for i in range(num_validators)
|
|
|
|
]
|
|
|
|
rng = Random(1234)
|
|
|
|
rng.shuffle(balances)
|
|
|
|
return balances
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def low_single_balance(spec: Spec):
|
2020-03-16 17:19:21 +00:00
|
|
|
"""
|
|
|
|
Helper method to create a single of balance of 1 Gwei.
|
|
|
|
Usage: `@with_custom_state(balances_fn=low_single_balance, ...)`
|
|
|
|
"""
|
|
|
|
return [1]
|
2019-05-06 15:10:43 +00:00
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def large_validator_set(spec: Spec):
|
2020-08-05 19:04:13 +00:00
|
|
|
"""
|
2020-09-17 22:22:21 +00:00
|
|
|
Helper method to create a large series of default balances.
|
2020-08-05 19:04:13 +00:00
|
|
|
Usage: `@with_custom_state(balances_fn=default_balances, ...)`
|
|
|
|
"""
|
|
|
|
num_validators = 2 * spec.SLOTS_PER_EPOCH * spec.MAX_COMMITTEES_PER_SLOT * spec.TARGET_COMMITTEE_SIZE
|
|
|
|
return [spec.MAX_EFFECTIVE_BALANCE] * num_validators
|
|
|
|
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
def single_phase(fn):
|
|
|
|
"""
|
|
|
|
Decorator that filters out the phases data.
|
|
|
|
most state tests only focus on behavior of a single phase (the "spec").
|
|
|
|
This decorator is applied as part of spec_state_test(fn).
|
|
|
|
"""
|
|
|
|
def entry(*args, **kw):
|
|
|
|
if 'phases' in kw:
|
|
|
|
kw.pop('phases')
|
2020-01-05 23:06:57 +00:00
|
|
|
return fn(*args, **kw)
|
2020-01-05 22:37:05 +00:00
|
|
|
return entry
|
|
|
|
|
|
|
|
|
2020-05-11 17:18:49 +00:00
|
|
|
# BLS is turned on by default, it can be disabled in tests by overriding this, or using `--disable-bls`.
|
|
|
|
# *This is for performance purposes during TESTING, DO NOT DISABLE IN PRODUCTION*.
|
2019-05-21 19:51:28 +00:00
|
|
|
# The runner of the test can indicate the preferred setting (test generators prefer BLS to be ON).
|
|
|
|
# - Some tests are marked as BLS-requiring, and ignore this setting.
|
|
|
|
# (tests that express differences caused by BLS, e.g. invalid signatures being rejected)
|
|
|
|
# - Some other tests are marked as BLS-ignoring, and ignore this setting.
|
|
|
|
# (tests that are heavily performance impacted / require unsigned state transitions)
|
|
|
|
# - Most tests respect the BLS setting.
|
2020-05-11 17:18:49 +00:00
|
|
|
DEFAULT_BLS_ACTIVE = True
|
2019-05-21 19:51:28 +00:00
|
|
|
|
|
|
|
|
2020-08-12 12:48:52 +00:00
|
|
|
is_pytest = True
|
|
|
|
|
|
|
|
|
|
|
|
def dump_skipping_message(reason: str) -> None:
|
|
|
|
message = f"[Skipped test] {reason}"
|
|
|
|
if is_pytest:
|
|
|
|
pytest.skip(message)
|
|
|
|
else:
|
|
|
|
raise SkippedTest(message)
|
|
|
|
|
|
|
|
|
2022-12-13 07:13:44 +00:00
|
|
|
def description(case_description: str):
|
|
|
|
def entry(fn):
|
|
|
|
return with_meta_tags({'description': case_description})(fn)
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
2019-07-26 21:50:11 +00:00
|
|
|
def spec_test(fn):
|
|
|
|
# Bls switch must be wrapped by vector_test,
|
2019-07-26 17:19:36 +00:00
|
|
|
# to fully go through the yielded bls switch data, before setting back the BLS setting.
|
2019-07-26 21:50:11 +00:00
|
|
|
# A test may apply BLS overrides such as @always_bls,
|
|
|
|
# but if it yields data (n.b. @always_bls yields the bls setting), it should be wrapped by this decorator.
|
|
|
|
# This is why @alway_bls has its own bls switch, since the override is beyond the reach of the outer switch.
|
|
|
|
return vector_test()(bls_switch(fn))
|
2019-06-22 04:02:03 +00:00
|
|
|
|
|
|
|
|
2022-12-13 07:13:44 +00:00
|
|
|
# shorthand for decorating @spec_test @with_state @single_phase
|
2019-05-06 15:10:43 +00:00
|
|
|
def spec_state_test(fn):
|
2020-01-05 22:37:05 +00:00
|
|
|
return spec_test(with_state(single_phase(fn)))
|
2019-05-08 15:33:09 +00:00
|
|
|
|
|
|
|
|
2021-05-18 14:08:30 +00:00
|
|
|
def spec_configured_state_test(conf):
|
|
|
|
overrides = with_config_overrides(conf)
|
|
|
|
|
|
|
|
def decorator(fn):
|
|
|
|
return spec_test(overrides(with_state(single_phase(fn))))
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2022-07-15 16:01:33 +00:00
|
|
|
def _check_current_version(spec, state, version_name):
|
|
|
|
fork_version_field = version_name.upper() + '_FORK_VERSION'
|
|
|
|
try:
|
|
|
|
fork_version = getattr(spec.config, fork_version_field)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return state.fork.current_version == fork_version
|
|
|
|
|
|
|
|
|
2022-06-23 23:05:11 +00:00
|
|
|
def config_fork_epoch_overrides(spec, state):
|
|
|
|
if state.fork.current_version == spec.config.GENESIS_FORK_VERSION:
|
2022-11-16 15:39:06 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
for fork in ALL_PHASES:
|
|
|
|
if fork != PHASE0 and _check_current_version(spec, state, fork):
|
|
|
|
overrides = {}
|
|
|
|
for f in ALL_PHASES:
|
|
|
|
if f != PHASE0 and is_post_fork(fork, f):
|
|
|
|
overrides[f.upper() + '_FORK_EPOCH'] = spec.GENESIS_EPOCH
|
|
|
|
return overrides
|
2022-06-23 23:05:11 +00:00
|
|
|
|
|
|
|
|
2022-12-07 20:10:09 +00:00
|
|
|
def with_matching_spec_config(emitted_fork=None):
|
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, spec: Spec, **kw):
|
|
|
|
overrides = config_fork_epoch_overrides(spec, kw['state'])
|
|
|
|
deco = with_config_overrides(overrides, emitted_fork)
|
|
|
|
return deco(fn)(*args, spec=spec, **kw)
|
|
|
|
return wrapper
|
2022-11-19 11:57:13 +00:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2022-06-23 23:05:11 +00:00
|
|
|
def spec_state_test_with_matching_config(fn):
|
2022-12-07 20:10:09 +00:00
|
|
|
return spec_test(with_state(with_matching_spec_config()(single_phase(fn))))
|
2022-06-23 23:05:11 +00:00
|
|
|
|
|
|
|
|
2019-05-08 15:33:09 +00:00
|
|
|
def expect_assertion_error(fn):
|
2019-05-11 15:51:02 +00:00
|
|
|
bad = False
|
2019-05-08 15:33:09 +00:00
|
|
|
try:
|
|
|
|
fn()
|
2019-05-11 15:51:02 +00:00
|
|
|
bad = True
|
2019-05-08 15:33:09 +00:00
|
|
|
except AssertionError:
|
|
|
|
pass
|
2019-05-11 15:01:12 +00:00
|
|
|
except IndexError:
|
|
|
|
# Index errors are special; the spec is not explicit on bound checking, an IndexError is like a failed assert.
|
|
|
|
pass
|
2019-05-11 15:51:02 +00:00
|
|
|
if bad:
|
|
|
|
raise AssertionError('expected an assertion error, but got none.')
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
2019-05-20 17:38:18 +00:00
|
|
|
def never_bls(fn):
|
|
|
|
"""
|
|
|
|
Decorator to apply on ``bls_switch`` decorator to force BLS de-activation. Useful to mark tests as BLS-ignorant.
|
2019-07-26 21:50:11 +00:00
|
|
|
This decorator may only be applied to yielding spec test functions, and should be wrapped by vector_test,
|
|
|
|
as the yielding needs to complete before setting back the BLS setting.
|
2019-05-20 17:38:18 +00:00
|
|
|
"""
|
|
|
|
def entry(*args, **kw):
|
|
|
|
# override bls setting
|
|
|
|
kw['bls_active'] = False
|
2019-07-26 21:50:11 +00:00
|
|
|
return bls_switch(fn)(*args, **kw)
|
|
|
|
return with_meta_tags({'bls_setting': 2})(entry)
|
2019-05-20 17:38:18 +00:00
|
|
|
|
|
|
|
|
2019-05-13 21:15:02 +00:00
|
|
|
def always_bls(fn):
|
|
|
|
"""
|
|
|
|
Decorator to apply on ``bls_switch`` decorator to force BLS activation. Useful to mark tests as BLS-dependent.
|
2019-07-26 21:50:11 +00:00
|
|
|
This decorator may only be applied to yielding spec test functions, and should be wrapped by vector_test,
|
|
|
|
as the yielding needs to complete before setting back the BLS setting.
|
2019-05-13 21:15:02 +00:00
|
|
|
"""
|
|
|
|
def entry(*args, **kw):
|
|
|
|
# override bls setting
|
|
|
|
kw['bls_active'] = True
|
2019-07-26 21:50:11 +00:00
|
|
|
return bls_switch(fn)(*args, **kw)
|
|
|
|
return with_meta_tags({'bls_setting': 1})(entry)
|
2019-05-13 21:15:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def bls_switch(fn):
|
|
|
|
"""
|
|
|
|
Decorator to make a function execute with BLS ON, or BLS off.
|
|
|
|
Based on an optional bool argument ``bls_active``, passed to the function at runtime.
|
2019-07-26 21:50:11 +00:00
|
|
|
This decorator may only be applied to yielding spec test functions, and should be wrapped by vector_test,
|
|
|
|
as the yielding needs to complete before setting back the BLS setting.
|
2019-05-13 21:15:02 +00:00
|
|
|
"""
|
|
|
|
def entry(*args, **kw):
|
|
|
|
old_state = bls.bls_active
|
2019-05-21 19:51:28 +00:00
|
|
|
bls.bls_active = kw.pop('bls_active', DEFAULT_BLS_ACTIVE)
|
2019-10-28 08:35:19 +00:00
|
|
|
res = fn(*args, **kw)
|
|
|
|
if res is not None:
|
|
|
|
yield from res
|
2019-05-13 21:15:02 +00:00
|
|
|
bls.bls_active = old_state
|
|
|
|
return entry
|
2019-05-30 20:57:18 +00:00
|
|
|
|
|
|
|
|
2020-09-07 05:34:23 +00:00
|
|
|
def disable_process_reveal_deadlines(fn):
|
|
|
|
"""
|
|
|
|
Decorator to make a function execute with `process_reveal_deadlines` OFF.
|
|
|
|
This is for testing long-range epochs transition without considering the reveal-deadline slashing effect.
|
|
|
|
"""
|
|
|
|
def entry(*args, spec: Spec, **kw):
|
|
|
|
if hasattr(spec, 'process_reveal_deadlines'):
|
|
|
|
old_state = spec.process_reveal_deadlines
|
|
|
|
spec.process_reveal_deadlines = lambda state: None
|
|
|
|
|
|
|
|
yield from fn(*args, spec=spec, **kw)
|
|
|
|
|
|
|
|
if hasattr(spec, 'process_reveal_deadlines'):
|
|
|
|
spec.process_reveal_deadlines = old_state
|
|
|
|
|
|
|
|
return with_meta_tags({'reveal_deadlines_setting': 1})(entry)
|
|
|
|
|
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
def with_all_phases(fn):
|
|
|
|
"""
|
2019-08-19 11:05:44 +00:00
|
|
|
A decorator for running a test with every phase
|
2019-06-06 21:30:40 +00:00
|
|
|
"""
|
2020-04-02 06:54:48 +00:00
|
|
|
return with_phases(ALL_PHASES)(fn)
|
2019-06-06 21:30:40 +00:00
|
|
|
|
|
|
|
|
2022-10-30 11:50:56 +00:00
|
|
|
def with_all_phases_from(earliest_phase):
|
|
|
|
"""
|
|
|
|
A decorator factory for running a tests with every phase except the ones listed
|
|
|
|
"""
|
|
|
|
def decorator(fn):
|
|
|
|
return with_phases([phase for phase in ALL_PHASES if is_post_fork(phase, earliest_phase)])(fn)
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-06-06 21:30:40 +00:00
|
|
|
def with_all_phases_except(exclusion_phases):
|
|
|
|
"""
|
|
|
|
A decorator factory for running a tests with every phase except the ones listed
|
|
|
|
"""
|
|
|
|
def decorator(fn):
|
2020-04-02 06:54:48 +00:00
|
|
|
return with_phases([phase for phase in ALL_PHASES if phase not in exclusion_phases])(fn)
|
2019-06-06 21:30:40 +00:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2022-10-30 11:50:56 +00:00
|
|
|
with_altair_and_later = with_all_phases_from(ALTAIR)
|
|
|
|
with_bellatrix_and_later = with_all_phases_from(BELLATRIX)
|
|
|
|
with_capella_and_later = with_all_phases_from(CAPELLA)
|
2023-01-23 14:08:34 +00:00
|
|
|
with_deneb_and_later = with_all_phases_from(DENEB)
|
2023-03-28 06:06:10 +00:00
|
|
|
with_eip6110_and_later = with_all_phases_from(EIP6110)
|
2022-10-30 11:50:56 +00:00
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
def _get_preset_targets(kw):
|
|
|
|
preset_name = DEFAULT_TEST_PRESET
|
|
|
|
if 'preset' in kw:
|
|
|
|
preset_name = kw.pop('preset')
|
|
|
|
return spec_targets[preset_name]
|
|
|
|
|
|
|
|
|
|
|
|
def _get_run_phases(phases, kw):
|
|
|
|
"""
|
|
|
|
Return the fork names for the base `spec` in test cases
|
|
|
|
"""
|
|
|
|
if 'phase' in kw:
|
|
|
|
# Limit phases if one explicitly specified
|
|
|
|
phase = kw.pop('phase')
|
|
|
|
if phase not in phases:
|
|
|
|
dump_skipping_message(f"doesn't support this fork: {phase}")
|
|
|
|
return None
|
|
|
|
run_phases = [phase]
|
|
|
|
else:
|
2021-11-02 10:38:24 +00:00
|
|
|
# If pytest `--fork` flag is set, filter out the rest of the forks
|
2021-10-29 15:17:57 +00:00
|
|
|
run_phases = set(phases).intersection(DEFAULT_PYTEST_FORKS)
|
|
|
|
|
|
|
|
return run_phases
|
|
|
|
|
|
|
|
|
|
|
|
def _get_available_phases(run_phases, other_phases):
|
|
|
|
"""
|
2021-11-02 10:38:24 +00:00
|
|
|
Return the available fork names for multi-phase tests
|
2021-10-29 15:17:57 +00:00
|
|
|
"""
|
|
|
|
available_phases = set(run_phases)
|
|
|
|
if other_phases is not None:
|
|
|
|
available_phases |= set(other_phases)
|
|
|
|
return available_phases
|
|
|
|
|
|
|
|
|
|
|
|
def _run_test_case_with_phases(fn, phases, other_phases, kw, args, is_fork_transition=False):
|
|
|
|
run_phases = _get_run_phases(phases, kw)
|
|
|
|
|
|
|
|
if len(run_phases) == 0:
|
|
|
|
if not is_fork_transition:
|
|
|
|
dump_skipping_message("none of the recognized phases are executable, skipping test.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
available_phases = _get_available_phases(run_phases, other_phases)
|
|
|
|
|
|
|
|
targets = _get_preset_targets(kw)
|
|
|
|
|
|
|
|
# Populate all phases for multi-phase tests
|
|
|
|
phase_dir = {}
|
|
|
|
for phase in available_phases:
|
|
|
|
phase_dir[phase] = targets[phase]
|
|
|
|
|
|
|
|
# Return is ignored whenever multiple phases are ran.
|
|
|
|
# This return is for test generators to emit python generators (yielding test vector outputs)
|
|
|
|
for phase in run_phases:
|
|
|
|
ret = fn(spec=targets[phase], phases=phase_dir, *args, **kw)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
def with_phases(phases, other_phases=None):
|
2019-05-30 20:57:18 +00:00
|
|
|
"""
|
2020-01-05 22:37:05 +00:00
|
|
|
Decorator factory that returns a decorator that runs a test for the appropriate phases.
|
|
|
|
Additional phases that do not initially run, but are made available through the test, are optional.
|
2019-05-30 20:57:18 +00:00
|
|
|
"""
|
2019-06-06 14:23:30 +00:00
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, **kw):
|
2021-10-29 15:17:57 +00:00
|
|
|
if 'fork_metas' in kw:
|
|
|
|
fork_metas = kw.pop('fork_metas')
|
|
|
|
if 'phase' in kw:
|
|
|
|
# When running test generator, it sets specific `phase`
|
|
|
|
phase = kw['phase']
|
|
|
|
_phases = [phase]
|
2021-11-02 11:01:32 +00:00
|
|
|
_other_phases = [ALL_FORK_UPGRADES[phase]]
|
2021-10-29 15:17:57 +00:00
|
|
|
ret = _run_test_case_with_phases(fn, _phases, _other_phases, kw, args, is_fork_transition=True)
|
|
|
|
else:
|
2021-11-15 16:15:39 +00:00
|
|
|
# When running pytest, go through `fork_metas` instead of using `phases`
|
2021-10-29 15:17:57 +00:00
|
|
|
for fork_meta in fork_metas:
|
2021-11-15 16:15:39 +00:00
|
|
|
_phases = [fork_meta.pre_fork_name]
|
|
|
|
_other_phases = [fork_meta.post_fork_name]
|
2021-10-29 15:17:57 +00:00
|
|
|
ret = _run_test_case_with_phases(fn, _phases, _other_phases, kw, args, is_fork_transition=True)
|
|
|
|
else:
|
|
|
|
ret = _run_test_case_with_phases(fn, phases, other_phases, kw, args)
|
2019-06-11 02:05:43 +00:00
|
|
|
return ret
|
2019-06-06 14:23:30 +00:00
|
|
|
return wrapper
|
|
|
|
return decorator
|
2020-08-12 13:50:36 +00:00
|
|
|
|
|
|
|
|
2021-05-07 03:30:45 +00:00
|
|
|
def with_presets(preset_bases, reason=None):
|
2021-05-18 11:59:26 +00:00
|
|
|
available_presets = set(preset_bases)
|
2021-05-07 03:30:45 +00:00
|
|
|
|
2020-08-12 12:48:52 +00:00
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, spec: Spec, **kw):
|
2021-05-18 11:59:26 +00:00
|
|
|
if spec.config.PRESET_BASE not in available_presets:
|
|
|
|
message = f"doesn't support this preset base: {spec.config.PRESET_BASE}."
|
2020-09-03 18:41:09 +00:00
|
|
|
if reason is not None:
|
|
|
|
message = f"{message} Reason: {reason}"
|
|
|
|
dump_skipping_message(message)
|
2020-08-12 12:48:52 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
return fn(*args, spec=spec, **kw)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2022-07-15 19:37:18 +00:00
|
|
|
class quoted_str(str):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-12-01 16:46:19 +00:00
|
|
|
def _get_basic_dict(ssz_dict: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
"""
|
2022-07-15 19:37:18 +00:00
|
|
|
Get dict of basic types from a dict of SSZ objects.
|
2021-12-01 16:46:19 +00:00
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
for k, v in ssz_dict.items():
|
|
|
|
if isinstance(v, int):
|
|
|
|
value = int(v)
|
|
|
|
elif isinstance(v, bytes):
|
2022-07-15 19:37:18 +00:00
|
|
|
value = bytes(bytearray(v))
|
2021-12-01 16:46:19 +00:00
|
|
|
else:
|
2022-07-15 19:37:18 +00:00
|
|
|
value = quoted_str(v)
|
2021-12-01 16:46:19 +00:00
|
|
|
result[k] = value
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-05-05 15:03:25 +00:00
|
|
|
def get_copy_of_spec(spec):
|
2021-12-07 16:51:32 +00:00
|
|
|
fork = spec.fork
|
|
|
|
preset = spec.config.PRESET_BASE
|
2021-12-07 17:11:22 +00:00
|
|
|
module_path = f"eth2spec.{fork}.{preset}"
|
|
|
|
module_spec = importlib.util.find_spec(module_path)
|
2021-12-07 16:51:32 +00:00
|
|
|
module = importlib.util.module_from_spec(module_spec)
|
2021-12-07 17:11:22 +00:00
|
|
|
module_spec.loader.exec_module(module)
|
2022-12-07 20:10:09 +00:00
|
|
|
|
|
|
|
# Preserve existing config overrides
|
|
|
|
module.config = deepcopy(spec.config)
|
|
|
|
|
2021-12-07 16:51:32 +00:00
|
|
|
return module
|
|
|
|
|
|
|
|
|
2022-11-19 11:57:13 +00:00
|
|
|
def spec_with_config_overrides(spec, config_overrides):
|
|
|
|
# apply our overrides to a copy of it, and apply it to the spec
|
|
|
|
config = spec.config._asdict()
|
2022-12-07 20:10:09 +00:00
|
|
|
config.update((k, config_overrides[k]) for k in config.keys() & config_overrides.keys())
|
2022-11-19 11:57:13 +00:00
|
|
|
config_types = spec.Configuration.__annotations__
|
|
|
|
modified_config = {k: config_types[k](v) for k, v in config.items()}
|
|
|
|
|
|
|
|
spec.config = spec.Configuration(**modified_config)
|
|
|
|
|
|
|
|
# To output the changed config in a format compatible with yaml test vectors,
|
|
|
|
# the dict SSZ objects have to be converted into Python built-in types.
|
|
|
|
output_config = _get_basic_dict(modified_config)
|
|
|
|
|
|
|
|
return spec, output_config
|
|
|
|
|
|
|
|
|
2022-12-07 20:10:09 +00:00
|
|
|
def with_config_overrides(config_overrides, emitted_fork=None, emit=True):
|
2021-05-07 03:30:45 +00:00
|
|
|
"""
|
2021-05-18 14:08:30 +00:00
|
|
|
WARNING: the spec_test decorator must wrap this, to ensure the decorated test actually runs.
|
|
|
|
This decorator forces the test to yield, and pytest doesn't run generator tests, and instead silently passes it.
|
|
|
|
Use 'spec_configured_state_test' instead of 'spec_state_test' if you are unsure.
|
|
|
|
|
|
|
|
This is a decorator that applies a dict of config value overrides to the spec during execution.
|
2021-05-07 03:30:45 +00:00
|
|
|
"""
|
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, spec: Spec, **kw):
|
2022-12-11 22:41:08 +00:00
|
|
|
# Apply config overrides to spec
|
2023-05-05 15:03:25 +00:00
|
|
|
spec, output_config = spec_with_config_overrides(get_copy_of_spec(spec), config_overrides)
|
2021-05-18 14:08:30 +00:00
|
|
|
|
2022-12-11 22:41:08 +00:00
|
|
|
# Apply config overrides to additional phases, if present
|
2022-11-19 11:57:13 +00:00
|
|
|
if 'phases' in kw:
|
2022-12-11 22:41:08 +00:00
|
|
|
phases = {}
|
2022-11-19 11:57:13 +00:00
|
|
|
for fork in kw['phases']:
|
2022-12-13 11:34:14 +00:00
|
|
|
phases[fork], output = spec_with_config_overrides(
|
2023-05-05 15:03:25 +00:00
|
|
|
get_copy_of_spec(kw['phases'][fork]), config_overrides)
|
2022-12-11 22:41:08 +00:00
|
|
|
if emitted_fork == fork:
|
|
|
|
output_config = output
|
|
|
|
kw['phases'] = phases
|
2021-05-07 03:30:45 +00:00
|
|
|
|
2022-12-11 22:41:08 +00:00
|
|
|
# Emit requested spec (with overrides)
|
|
|
|
if emit:
|
|
|
|
yield 'config', 'cfg', output_config
|
|
|
|
|
2022-12-12 11:15:27 +00:00
|
|
|
# Run the function
|
|
|
|
out = fn(*args, spec=spec, **kw)
|
|
|
|
|
2021-05-07 03:30:45 +00:00
|
|
|
# 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
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2021-08-24 21:57:56 +00:00
|
|
|
def only_generator(reason):
|
|
|
|
def _decorator(inner):
|
|
|
|
def _wrapper(*args, **kwargs):
|
|
|
|
if is_pytest:
|
|
|
|
dump_skipping_message(reason)
|
|
|
|
return None
|
|
|
|
return inner(*args, **kwargs)
|
|
|
|
return _wrapper
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
2022-10-28 18:32:33 +00:00
|
|
|
def with_test_suite_name(suite_name: str):
|
|
|
|
def _decorator(inner):
|
|
|
|
inner.suite_name = suite_name
|
|
|
|
return inner
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
#
|
|
|
|
# Fork transition state tests
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def set_fork_metas(fork_metas: Sequence[ForkMeta]):
|
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return fn(*args, fork_metas=fork_metas, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def with_fork_metas(fork_metas: Sequence[ForkMeta]):
|
2021-04-28 00:24:15 +00:00
|
|
|
"""
|
2021-10-29 15:17:57 +00:00
|
|
|
A decorator to construct a "transition" test from one fork to another.
|
|
|
|
|
|
|
|
Decorator takes a list of `ForkMeta` and each item defines `pre_fork_name`,
|
|
|
|
`post_fork_name`, and `fork_epoch`.
|
2021-04-28 00:24:15 +00:00
|
|
|
|
|
|
|
Decorator assumes a transition from the `pre_fork_name` fork to the
|
|
|
|
`post_fork_name` fork. The user can supply a `fork_epoch` at which the
|
|
|
|
fork occurs or they must compute one (yielding to the generator) during the test
|
|
|
|
if more custom behavior is desired.
|
|
|
|
|
|
|
|
A test using this decorator should expect to receive as parameters:
|
|
|
|
`state`: the default state constructed for the `pre_fork_name` fork
|
|
|
|
according to the `with_state` decorator.
|
|
|
|
`fork_epoch`: the `fork_epoch` provided to this decorator, if given.
|
|
|
|
`spec`: the version of the eth2 spec corresponding to `pre_fork_name`.
|
|
|
|
`post_spec`: the version of the eth2 spec corresponding to `post_fork_name`.
|
|
|
|
`pre_tag`: a function to tag data as belonging to `pre_fork_name` fork.
|
|
|
|
Used to discriminate data during consumption of the generated spec tests.
|
|
|
|
`post_tag`: a function to tag data as belonging to `post_fork_name` fork.
|
|
|
|
Used to discriminate data during consumption of the generated spec tests.
|
|
|
|
"""
|
2021-10-29 15:17:57 +00:00
|
|
|
run_yield_fork_meta = yield_fork_meta(fork_metas)
|
|
|
|
run_with_phases = with_phases(ALL_PHASES)
|
|
|
|
run_set_fork_metas = set_fork_metas(fork_metas)
|
|
|
|
|
|
|
|
def decorator(fn):
|
|
|
|
return run_set_fork_metas(run_with_phases(spec_test(with_state(run_yield_fork_meta(fn)))))
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def yield_fork_meta(fork_metas: Sequence[ForkMeta]):
|
|
|
|
"""
|
|
|
|
Yield meta fields to `meta.yaml` and pass post spec and meta fields to `fn`.
|
|
|
|
"""
|
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, **kw):
|
|
|
|
phases = kw.pop('phases')
|
|
|
|
spec = kw["spec"]
|
2021-11-12 09:06:10 +00:00
|
|
|
try:
|
|
|
|
fork_meta = next(filter(lambda m: m.pre_fork_name == spec.fork, fork_metas))
|
|
|
|
except StopIteration:
|
|
|
|
dump_skipping_message(f"doesn't support this fork: {spec.fork}")
|
|
|
|
|
2021-10-29 15:17:57 +00:00
|
|
|
post_spec = phases[fork_meta.post_fork_name]
|
|
|
|
|
|
|
|
# Reset counter
|
|
|
|
pre_fork_counter = 0
|
|
|
|
|
|
|
|
def pre_tag(obj):
|
|
|
|
nonlocal pre_fork_counter
|
|
|
|
pre_fork_counter += 1
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def post_tag(obj):
|
|
|
|
return obj
|
|
|
|
|
|
|
|
yield "post_fork", "meta", fork_meta.post_fork_name
|
|
|
|
|
|
|
|
has_fork_epoch = False
|
|
|
|
if fork_meta.fork_epoch:
|
|
|
|
kw["fork_epoch"] = fork_meta.fork_epoch
|
|
|
|
has_fork_epoch = True
|
|
|
|
yield "fork_epoch", "meta", fork_meta.fork_epoch
|
|
|
|
|
|
|
|
result = fn(
|
|
|
|
*args,
|
|
|
|
post_spec=post_spec,
|
|
|
|
pre_tag=pre_tag,
|
|
|
|
post_tag=post_tag,
|
|
|
|
**kw,
|
|
|
|
)
|
|
|
|
if result is not None:
|
|
|
|
for part in result:
|
|
|
|
if part[0] == "fork_epoch":
|
|
|
|
has_fork_epoch = True
|
|
|
|
yield part
|
|
|
|
assert has_fork_epoch
|
|
|
|
|
|
|
|
if pre_fork_counter > 0:
|
|
|
|
yield "fork_block", "meta", pre_fork_counter - 1
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
return decorator
|