2020-08-12 12:48:52 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-05-30 20:57:18 +00:00
|
|
|
from eth2spec.phase0 import spec as spec_phase0
|
2019-11-19 19:16:40 +00:00
|
|
|
from eth2spec.phase1 import spec as spec_phase1
|
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
|
2019-05-17 18:31:21 +00:00
|
|
|
from .helpers.genesis import create_genesis_state
|
2019-07-26 21:50:11 +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
|
2020-04-02 06:54:48 +00:00
|
|
|
from typing import Any, Callable, NewType, Sequence, TypedDict, Protocol
|
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
|
|
|
from importlib import reload
|
|
|
|
|
2020-01-04 17:33:15 +00:00
|
|
|
|
|
|
|
def reload_specs():
|
|
|
|
reload(spec_phase0)
|
|
|
|
reload(spec_phase1)
|
2019-11-19 19:16:40 +00:00
|
|
|
|
2019-10-23 08:33:48 +00:00
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
# Some of the Spec module functionality is exposed here to deal with phase-specific changes.
|
|
|
|
|
2020-04-02 06:54:48 +00:00
|
|
|
SpecForkName = NewType("SpecForkName", str)
|
2020-08-12 12:48:52 +00:00
|
|
|
ConfigName = NewType("ConfigName", str)
|
2020-04-02 06:54:48 +00:00
|
|
|
|
|
|
|
PHASE0 = SpecForkName('phase0')
|
|
|
|
PHASE1 = SpecForkName('phase1')
|
|
|
|
ALL_PHASES = (PHASE0, PHASE1)
|
|
|
|
|
2020-08-12 12:48:52 +00:00
|
|
|
MAINNET = ConfigName('mainnet')
|
|
|
|
MINIMAL = ConfigName('minimal')
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
class Spec(Protocol):
|
|
|
|
version: str
|
|
|
|
|
|
|
|
|
2020-04-02 06:54:48 +00:00
|
|
|
class SpecPhase0(Spec):
|
2020-01-05 22:37:05 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-04-02 06:54:48 +00:00
|
|
|
class SpecPhase1(Spec):
|
2020-01-05 23:49:32 +00:00
|
|
|
def upgrade_to_phase1(self, state: spec_phase0.BeaconState) -> spec_phase1.BeaconState:
|
|
|
|
...
|
2020-01-05 22:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
# add transfer, bridge, etc. as the spec evolves
|
|
|
|
class SpecForks(TypedDict, total=False):
|
2020-04-02 06:54:48 +00:00
|
|
|
PHASE0: SpecPhase0
|
|
|
|
PHASE1: SpecPhase1
|
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],
|
|
|
|
spec: Spec, phases: SpecForks):
|
|
|
|
|
|
|
|
p0 = phases[PHASE0]
|
|
|
|
balances = balances_fn(p0)
|
|
|
|
activation_threshold = threshold_fn(p0)
|
|
|
|
|
|
|
|
state = create_genesis_state(spec=p0, validator_balances=balances,
|
|
|
|
activation_threshold=activation_threshold)
|
|
|
|
if spec.fork == PHASE1:
|
|
|
|
# TODO: instead of upgrading a test phase0 genesis state we can also write a phase1 state helper.
|
|
|
|
# Decide based on performance/consistency results later.
|
|
|
|
state = phases[PHASE1].upgrade_to_phase1(state)
|
|
|
|
|
|
|
|
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-05-18 23:55:17 +00:00
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
def entry(*args, spec: Spec, phases: SpecForks, **kw):
|
2020-05-19 00:46:27 +00:00
|
|
|
# make a key for the state
|
|
|
|
# genesis fork version separates configs during test-generation runtime.
|
|
|
|
key = (spec.fork, spec.GENESIS_FORK_VERSION, 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:
|
|
|
|
state = _prepare_state(balances_fn, threshold_fn, spec, phases)
|
|
|
|
_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
|
|
|
|
|
|
|
|
|
|
|
|
def default_activation_threshold(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
|
|
|
|
|
|
|
|
|
2020-03-16 17:19:21 +00:00
|
|
|
def zero_activation_threshold(spec):
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
|
|
2019-10-23 08:33:48 +00:00
|
|
|
def default_balances(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
|
|
|
|
|
|
|
|
|
|
|
|
with_state = with_custom_state(default_balances, default_activation_threshold)
|
|
|
|
|
|
|
|
|
|
|
|
def low_balances(spec):
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def misc_balances(spec):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-03-16 17:19:21 +00:00
|
|
|
def low_single_balance(spec):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
# shorthand for decorating @spectest() @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
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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):
|
2019-06-10 23:26:39 +00:00
|
|
|
run_phases = phases
|
|
|
|
|
|
|
|
# limit phases if one explicitly specified
|
|
|
|
if 'phase' in kw:
|
|
|
|
phase = kw.pop('phase')
|
|
|
|
if phase not in phases:
|
2020-08-12 12:48:52 +00:00
|
|
|
dump_skipping_message(f"doesn't support this fork: {phase}")
|
|
|
|
return None
|
2019-06-10 23:26:39 +00:00
|
|
|
run_phases = [phase]
|
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
available_phases = set(run_phases)
|
|
|
|
if other_phases is not None:
|
|
|
|
available_phases += set(other_phases)
|
|
|
|
|
2020-01-05 22:51:02 +00:00
|
|
|
# TODO: test state is dependent on phase0 but is immediately transitioned to phase1.
|
|
|
|
# A new state-creation helper for phase 1 may be in place, and then phase1+ tests can run without phase0
|
2020-04-02 06:54:48 +00:00
|
|
|
available_phases.add(PHASE0)
|
2020-01-05 22:51:02 +00:00
|
|
|
|
2020-01-05 22:37:05 +00:00
|
|
|
phase_dir = {}
|
2020-04-02 06:54:48 +00:00
|
|
|
if PHASE0 in available_phases:
|
|
|
|
phase_dir[PHASE0] = spec_phase0
|
|
|
|
if PHASE1 in available_phases:
|
|
|
|
phase_dir[PHASE1] = spec_phase1
|
2020-01-05 22:37:05 +00:00
|
|
|
|
|
|
|
# return is ignored whenever multiple phases are ran. If
|
2020-04-02 06:54:48 +00:00
|
|
|
if PHASE0 in run_phases:
|
2020-01-05 22:37:05 +00:00
|
|
|
ret = fn(spec=spec_phase0, phases=phase_dir, *args, **kw)
|
2020-04-02 06:54:48 +00:00
|
|
|
if PHASE1 in run_phases:
|
2020-01-05 22:37:05 +00:00
|
|
|
ret = fn(spec=spec_phase1, phases=phase_dir, *args, **kw)
|
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
|
|
|
|
|
|
|
|
|
|
|
def disable_process_reveal_deadlines(fn):
|
|
|
|
def entry(*args, spec: Spec, **kw):
|
|
|
|
if hasattr(spec, 'process_reveal_deadlines'):
|
|
|
|
spec.process_reveal_deadlines = lambda state: None
|
|
|
|
return fn(*args, spec=spec, **kw)
|
|
|
|
return with_meta_tags({'reveal_deadlines_setting': 1})(entry)
|
2020-09-01 16:07:19 +00:00
|
|
|
|
|
|
|
|
2020-09-03 18:41:09 +00:00
|
|
|
def with_configs(configs, reason=None):
|
2020-08-12 12:48:52 +00:00
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, spec: Spec, **kw):
|
|
|
|
available_configs = set(configs)
|
|
|
|
if spec.CONFIG_NAME not in available_configs:
|
2020-09-03 18:41:09 +00:00
|
|
|
message = f"doesn't support this config: {spec.CONFIG_NAME}."
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def only_full_crosslink(fn):
|
|
|
|
def is_full_crosslink(spec, state):
|
|
|
|
epoch = spec.compute_epoch_at_slot(state.slot)
|
|
|
|
return spec.get_committee_count_per_slot(state, epoch) >= spec.get_active_shard_count(state)
|
|
|
|
|
|
|
|
def wrapper(*args, spec: Spec, state: Any, **kw):
|
|
|
|
# TODO: update condition to "phase1+" if we have phase2
|
|
|
|
if spec.fork == PHASE1 and not is_full_crosslink(spec, state):
|
|
|
|
dump_skipping_message("only for full crosslink")
|
|
|
|
return None
|
|
|
|
return fn(*args, spec=spec, state=state, **kw)
|
|
|
|
return wrapper
|