update tests with new config util

This commit is contained in:
protolambda
2021-05-07 05:30:45 +02:00
parent 79d0fa037f
commit d0fef3bf3f
14 changed files with 95 additions and 61 deletions

View File

@@ -17,7 +17,7 @@ from eth2spec.test.helpers.sync_committee import (
from eth2spec.test.context import (
expect_assertion_error,
with_altair_and_later,
with_configs,
with_presets,
spec_state_test,
always_bls,
)
@@ -213,7 +213,7 @@ def run_successful_sync_committee_test(spec, state, committee_indices, committee
@with_altair_and_later
@with_configs([MINIMAL], reason="to create nonduplicate committee")
@with_presets([MINIMAL], reason="to create nonduplicate committee")
@spec_state_test
def test_sync_committee_rewards_nonduplicate_committee(spec, state):
committee_indices = get_committee_indices(spec, state, duplicates=False)
@@ -229,7 +229,7 @@ def test_sync_committee_rewards_nonduplicate_committee(spec, state):
@with_altair_and_later
@with_configs([MAINNET], reason="to create duplicate committee")
@with_presets([MAINNET], reason="to create duplicate committee")
@spec_state_test
def test_sync_committee_rewards_duplicate_committee(spec, state):
committee_indices = get_committee_indices(spec, state, duplicates=True)
@@ -305,7 +305,7 @@ def test_invalid_signature_past_block(spec, state):
@with_altair_and_later
@with_configs([MINIMAL], reason="to produce different committee sets")
@with_presets([MINIMAL], reason="to produce different committee sets")
@spec_state_test
@always_bls
def test_invalid_signature_previous_committee(spec, state):
@@ -343,7 +343,7 @@ def test_invalid_signature_previous_committee(spec, state):
@with_altair_and_later
@spec_state_test
@always_bls
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_valid_signature_future_committee(spec, state):
# NOTE: the `state` provided is at genesis and the process to select
# sync committees currently returns the same committee for the first and second

View File

@@ -3,7 +3,7 @@ from eth2spec.test.context import (
spec_state_test,
spec_test,
with_altair_and_later,
with_configs,
with_presets,
with_custom_state,
single_phase,
misc_balances,
@@ -48,7 +48,7 @@ def run_sync_committees_progress_test(spec, state):
@with_altair_and_later
@spec_state_test
@always_bls
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_sync_committees_progress_genesis(spec, state):
# Genesis epoch period has an exceptional case
assert spec.get_current_epoch(state) == spec.GENESIS_EPOCH
@@ -59,7 +59,7 @@ def test_sync_committees_progress_genesis(spec, state):
@with_altair_and_later
@spec_state_test
@always_bls
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_sync_committees_progress_not_genesis(spec, state):
# Transition out of the genesis epoch period to test non-exceptional case
assert spec.get_current_epoch(state) == spec.GENESIS_EPOCH
@@ -74,6 +74,6 @@ def test_sync_committees_progress_not_genesis(spec, state):
@spec_test
@single_phase
@always_bls
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_sync_committees_progress_misc_balances(spec, state):
yield from run_sync_committees_progress_test(spec, state)

View File

@@ -1,7 +1,7 @@
from eth2spec.test.context import (
with_phases,
with_custom_state,
with_configs,
with_presets,
spec_test, with_state,
low_balances, misc_balances, large_validator_set,
)
@@ -73,7 +73,7 @@ def test_fork_random_misc_balances(spec, phases, state):
@with_phases(phases=[PHASE0], other_phases=[ALTAIR])
@with_configs([MINIMAL],
@with_presets([MINIMAL],
reason="mainnet config leads to larger validator set than limit of public/private keys pre-generated")
@with_custom_state(balances_fn=large_validator_set, threshold_fn=lambda spec: spec.EJECTION_BALANCE)
@spec_test

View File

@@ -1,6 +1,6 @@
from eth2spec.test.context import (
spec_state_test,
with_configs,
with_presets,
with_phases,
)
from eth2spec.test.helpers.attestations import next_epoch_with_attestations
@@ -83,7 +83,7 @@ def test_process_light_client_update_not_updated(spec, state):
@with_phases([ALTAIR])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_process_light_client_update_timeout(spec, state):
pre_snapshot = spec.LightClientSnapshot(
header=spec.BeaconBlockHeader(),
@@ -149,7 +149,7 @@ def test_process_light_client_update_timeout(spec, state):
@with_phases([ALTAIR])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_process_light_client_update_finality_updated(spec, state):
pre_snapshot = spec.LightClientSnapshot(
header=spec.BeaconBlockHeader(),

View File

@@ -1,5 +1,6 @@
import pytest
from copy import deepcopy
from eth2spec.phase0 import spec as spec_phase0
from eth2spec.altair import spec as spec_altair
from eth2spec.merge import spec as spec_merge
@@ -17,7 +18,7 @@ from random import Random
from typing import Any, Callable, Sequence, TypedDict, Protocol
from lru import LRU
from eth2spec.config import config_util
from importlib import reload
@@ -349,12 +350,13 @@ def with_phases(phases, other_phases=None):
return decorator
def with_configs(configs, reason=None):
def with_presets(preset_bases, reason=None):
available_configs = set(preset_bases)
def decorator(fn):
def wrapper(*args, spec: Spec, **kw):
available_configs = set(configs)
if spec.CONFIG_NAME not in available_configs:
message = f"doesn't support this config: {spec.CONFIG_NAME}."
if spec.PRESET_BASE not in available_configs:
message = f"doesn't support this preset base: {spec.PRESET_BASE}."
if reason is not None:
message = f"{message} Reason: {reason}"
dump_skipping_message(message)
@@ -365,6 +367,38 @@ def with_configs(configs, reason=None):
return decorator
def with_config_overrides(config_overrides):
"""
Decorator that applies a dict of config value overrides to the spec during execution.
This may be slow due to having to reload the spec modules,
since the specs uses globals instead of a configuration object.
"""
def decorator(fn):
def wrapper(*args, spec: Spec, **kw):
# remember the old config
old_config = config_util.config
# apply our overrides to a copy of it, and apply it to the spec
tmp_config = deepcopy(old_config)
tmp_config.update(config_overrides)
config_util.config = tmp_config
reload_specs() # Note this reloads the same module instance(s) that we passed into the test
# 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
config_util.config = old_config
reload_specs()
return wrapper
return decorator
def is_post_altair(spec):
if spec.fork == MERGE: # TODO: remove parallel Altair-Merge condition after rebase.
return False

View File

@@ -16,7 +16,7 @@ from eth2spec.test.context import (
disable_process_reveal_deadlines,
spec_state_test,
with_phases,
with_configs,
with_presets,
)
from eth2spec.test.phase0.block_processing.test_process_attestation import run_attestation_processing
@@ -72,7 +72,7 @@ def run_custody_chunk_response_processing(spec, state, custody_response, valid=T
@with_phases([CUSTODY_GAME])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
@disable_process_reveal_deadlines
def test_challenge_appended(spec, state):
transition_to_valid_shard_slot(spec, state)
@@ -97,7 +97,7 @@ def test_challenge_appended(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_challenge_empty_element_replaced(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + 1) # Make len(offset_slots) == 1
@@ -123,7 +123,7 @@ def test_challenge_empty_element_replaced(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_duplicate_challenge(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + 1) # Make len(offset_slots) == 1
@@ -149,7 +149,7 @@ def test_duplicate_challenge(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_second_challenge(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + 1) # Make len(offset_slots) == 1
@@ -177,7 +177,7 @@ def test_second_challenge(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_multiple_epochs_custody(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 3)
@@ -202,7 +202,7 @@ def test_multiple_epochs_custody(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_many_epochs_custody(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 20)
@@ -227,7 +227,7 @@ def test_many_epochs_custody(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_off_chain_attestation(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH)
@@ -248,7 +248,7 @@ def test_off_chain_attestation(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_custody_response(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH)
@@ -280,7 +280,7 @@ def test_custody_response(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_custody_response_chunk_index_2(spec, state):
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH)
@@ -311,7 +311,7 @@ def test_custody_response_chunk_index_2(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_custody_response_multiple_epochs(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 3)
@@ -343,7 +343,7 @@ def test_custody_response_multiple_epochs(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_custody_response_many_epochs(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 20)

View File

@@ -17,7 +17,7 @@ from eth2spec.test.context import (
spec_state_test,
expect_assertion_error,
disable_process_reveal_deadlines,
with_configs,
with_presets,
)
from eth2spec.test.phase0.block_processing.test_process_attestation import run_attestation_processing
@@ -117,7 +117,7 @@ def run_standard_custody_slashing_test(spec,
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_custody_slashing(spec, state):
yield from run_standard_custody_slashing_test(spec, state)
@@ -125,7 +125,7 @@ def test_custody_slashing(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_incorrect_custody_slashing(spec, state):
yield from run_standard_custody_slashing_test(spec, state, correct=False)
@@ -133,7 +133,7 @@ def test_incorrect_custody_slashing(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_multiple_epochs_custody(spec, state):
yield from run_standard_custody_slashing_test(spec, state, shard_lateness=spec.SLOTS_PER_EPOCH * 3)
@@ -141,7 +141,7 @@ def test_multiple_epochs_custody(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_many_epochs_custody(spec, state):
yield from run_standard_custody_slashing_test(spec, state, shard_lateness=spec.SLOTS_PER_EPOCH * 5)
@@ -149,7 +149,7 @@ def test_many_epochs_custody(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@disable_process_reveal_deadlines
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_invalid_custody_slashing(spec, state):
yield from run_standard_custody_slashing_test(
spec,

View File

@@ -9,7 +9,7 @@ from eth2spec.test.helpers.state import transition_to, transition_to_valid_shard
from eth2spec.test.context import (
spec_state_test,
with_phases,
with_configs,
with_presets,
)
from eth2spec.test.phase0.block_processing.test_process_attestation import run_attestation_processing
from eth2spec.test.helpers.constants import (
@@ -29,7 +29,7 @@ def run_process_challenge_deadlines(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_validator_slashed_after_chunk_challenge(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + 1) # Make len(offset_slots) == 1

View File

@@ -4,7 +4,7 @@ from eth2spec.test.helpers.custody import (
from eth2spec.test.helpers.state import transition_to
from eth2spec.test.context import (
with_phases,
with_configs,
with_presets,
spec_state_test,
)
from eth2spec.test.helpers.constants import (
@@ -23,7 +23,7 @@ def run_process_challenge_deadlines(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_validator_slashed_after_reveal_deadline(spec, state):
assert state.validators[0].slashed == 0
transition_to(spec, state, spec.get_randao_epoch_for_custody_period(0, 0) * spec.SLOTS_PER_EPOCH)
@@ -43,7 +43,7 @@ def test_validator_slashed_after_reveal_deadline(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_validator_not_slashed_after_reveal(spec, state):
transition_to(spec, state, spec.EPOCHS_PER_CUSTODY_PERIOD * spec.SLOTS_PER_EPOCH)
custody_key_reveal = get_valid_custody_key_reveal(spec, state)

View File

@@ -3,7 +3,7 @@ from typing import Dict, Sequence
from eth2spec.test.context import (
with_phases,
spec_state_test,
with_configs,
with_presets,
)
from eth2spec.test.helpers.attestations import get_valid_on_time_attestation
from eth2spec.test.helpers.block import build_empty_block
@@ -83,7 +83,7 @@ def test_with_shard_transition_with_custody_challenge_and_response(spec, state):
@with_phases([CUSTODY_GAME])
@spec_state_test
@with_configs([MINIMAL])
@with_presets([MINIMAL])
def test_custody_key_reveal(spec, state):
transition_to_valid_shard_slot(spec, state)
transition_to(spec, state, state.slot + spec.EPOCHS_PER_CUSTODY_PERIOD * spec.SLOTS_PER_EPOCH)

View File

@@ -4,7 +4,7 @@ from eth2spec.test.context import (
is_post_altair,
spec_state_test,
with_all_phases,
with_configs,
with_presets,
)
from eth2spec.test.helpers.attestations import get_valid_attestation, next_epoch_with_attestations
from eth2spec.test.helpers.block import build_empty_block_for_next_slot
@@ -174,7 +174,7 @@ def test_shorter_chain_but_heavier_weight(spec, state):
@with_all_phases
@spec_state_test
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_filtered_block_tree(spec, state):
test_steps = []
# Initialization

View File

@@ -2,7 +2,7 @@ from eth2spec.test.context import (
is_post_altair,
single_phase,
spec_test,
with_configs,
with_presets,
with_all_phases,
)
from eth2spec.test.helpers.constants import MINIMAL
@@ -26,7 +26,7 @@ def eth1_init_data(eth1_block_hash, eth1_timestamp):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_initialize_beacon_state_from_eth1(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -62,7 +62,7 @@ def test_initialize_beacon_state_from_eth1(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_initialize_beacon_state_some_small_balances(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -106,7 +106,7 @@ def test_initialize_beacon_state_some_small_balances(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_initialize_beacon_state_one_topup_activation(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -155,7 +155,7 @@ def test_initialize_beacon_state_one_topup_activation(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_initialize_beacon_state_random_invalid_genesis(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -182,7 +182,7 @@ def test_initialize_beacon_state_random_invalid_genesis(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_initialize_beacon_state_random_valid_genesis(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)

View File

@@ -2,7 +2,7 @@ from eth2spec.test.context import (
is_post_altair,
spec_test,
single_phase,
with_configs,
with_presets,
with_all_phases,
)
from eth2spec.test.helpers.constants import MINIMAL
@@ -44,7 +44,7 @@ def run_is_valid_genesis_state(spec, state, valid=True):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_is_valid_genesis_state_true(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -57,7 +57,7 @@ def test_is_valid_genesis_state_true(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_is_valid_genesis_state_false_invalid_timestamp(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -71,7 +71,7 @@ def test_is_valid_genesis_state_false_invalid_timestamp(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_is_valid_genesis_state_true_more_balance(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -85,7 +85,7 @@ def test_is_valid_genesis_state_true_more_balance(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_is_valid_genesis_state_true_one_more_validator(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)
@@ -108,7 +108,7 @@ def test_is_valid_genesis_state_true_one_more_validator(spec):
@with_all_phases
@spec_test
@single_phase
@with_configs([MINIMAL], reason="too slow")
@with_presets([MINIMAL], reason="too slow")
def test_is_valid_genesis_state_false_not_enough_validator(spec):
if is_post_altair(spec):
yield 'description', 'meta', get_post_altair_description(spec)

View File

@@ -30,7 +30,7 @@ from eth2spec.test.context import (
with_phases, with_all_phases, single_phase,
expect_assertion_error, always_bls,
disable_process_reveal_deadlines,
with_configs,
with_presets,
with_custom_state,
large_validator_set,
is_post_altair,
@@ -98,7 +98,7 @@ def test_empty_block_transition(spec, state):
@with_all_phases
@with_configs([MINIMAL],
@with_presets([MINIMAL],
reason="mainnet config leads to larger validator set than limit of public/private keys pre-generated")
@spec_test
@with_custom_state(balances_fn=large_validator_set, threshold_fn=lambda spec: spec.EJECTION_BALANCE)
@@ -326,7 +326,7 @@ def test_empty_epoch_transition(spec, state):
@with_all_phases
@with_configs([MINIMAL],
@with_presets([MINIMAL],
reason="mainnet config leads to larger validator set than limit of public/private keys pre-generated")
@spec_test
@with_custom_state(balances_fn=large_validator_set, threshold_fn=lambda spec: spec.EJECTION_BALANCE)