From 332ad4cb576c35dbc8e37a9a9999048562b8f450 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Sun, 30 Oct 2022 12:50:56 +0100 Subject: [PATCH] Move `is_post_xxx` functions to new module Moving the `is_post_xxx` functions to a separate module allows `genesis` to also use them (cyclic import from `context` prevented this before). This allows removing `FORKS_BEFORE_ALTAIR` and `FORKS_BEFORE_BELLATRIX` constants and adding a more general `is_post_fork` function that needs less maintenance. This then allows definition of `with_all_phases_from` to streamline the implementation of the `with_xxx_and_later` decorators. --- .../altair/unittests/test_config_override.py | 5 ++- tests/core/pyspec/eth2spec/test/context.py | 40 ++++++++----------- .../eth2spec/test/helpers/attestations.py | 3 +- .../pyspec/eth2spec/test/helpers/block.py | 2 +- .../pyspec/eth2spec/test/helpers/constants.py | 5 +-- .../pyspec/eth2spec/test/helpers/deposits.py | 6 +-- .../eth2spec/test/helpers/epoch_processing.py | 2 +- .../test/helpers/execution_payload.py | 2 +- .../pyspec/eth2spec/test/helpers/genesis.py | 10 +++-- .../pyspec/eth2spec/test/helpers/phases.py | 33 +++++++++++++++ .../test/helpers/proposer_slashings.py | 2 +- .../pyspec/eth2spec/test/helpers/random.py | 2 +- .../pyspec/eth2spec/test/helpers/rewards.py | 2 +- .../pyspec/eth2spec/test/helpers/state.py | 3 +- ..._process_justification_and_finalization.py | 3 +- .../test_process_rewards_and_penalties.py | 2 + .../test_process_slashings.py | 3 +- .../test/phase0/fork_choice/test_get_head.py | 4 +- .../phase0/genesis/test_initialization.py | 4 +- .../test/phase0/genesis/test_validity.py | 4 +- .../test/phase0/sanity/test_blocks.py | 3 +- .../unittests/test_config_invariants.py | 4 +- 22 files changed, 91 insertions(+), 53 deletions(-) create mode 100644 tests/core/pyspec/eth2spec/test/helpers/phases.py diff --git a/tests/core/pyspec/eth2spec/test/altair/unittests/test_config_override.py b/tests/core/pyspec/eth2spec/test/altair/unittests/test_config_override.py index b7df49790..e3836d3c9 100644 --- a/tests/core/pyspec/eth2spec/test/altair/unittests/test_config_override.py +++ b/tests/core/pyspec/eth2spec/test/altair/unittests/test_config_override.py @@ -1,12 +1,13 @@ from eth2spec.test.context import ( - is_post_capella, - is_post_eip4844, spec_configured_state_test, spec_state_test_with_matching_config, with_all_phases, with_phases, ) from eth2spec.test.helpers.constants import ALTAIR +from eth2spec.test.helpers.phases import ( + is_post_capella, is_post_eip4844, +) @with_phases([ALTAIR]) diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index bc04c05f2..7684e74be 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -13,9 +13,10 @@ from .exceptions import SkippedTest from .helpers.constants import ( PHASE0, ALTAIR, BELLATRIX, CAPELLA, EIP4844, SHARDING, MINIMAL, MAINNET, - ALL_PHASES, FORKS_BEFORE_ALTAIR, FORKS_BEFORE_BELLATRIX, + ALL_PHASES, ALL_FORK_UPGRADES, ) +from .helpers.phases import is_post_fork from .helpers.typing import SpecForkName, PresetBaseName from .helpers.genesis import create_genesis_state from .utils import ( @@ -408,6 +409,15 @@ def with_all_phases(fn): return with_phases(ALL_PHASES)(fn) +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 + + def with_all_phases_except(exclusion_phases): """ A decorator factory for running a tests with every phase except the ones listed @@ -417,6 +427,12 @@ def with_all_phases_except(exclusion_phases): return decorator +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) +with_eip4844_and_later = with_all_phases_from(EIP4844) + + def _get_preset_targets(kw): preset_name = DEFAULT_TEST_PRESET if 'preset' in kw: @@ -587,28 +603,6 @@ def with_config_overrides(config_overrides): return decorator -def is_post_altair(spec): - return spec.fork not in FORKS_BEFORE_ALTAIR - - -def is_post_bellatrix(spec): - return spec.fork not in FORKS_BEFORE_BELLATRIX - - -def is_post_capella(spec): - return spec.fork == CAPELLA - - -def is_post_eip4844(spec): - return spec.fork == EIP4844 - - -with_altair_and_later = with_all_phases_except([PHASE0]) -with_bellatrix_and_later = with_all_phases_except([PHASE0, ALTAIR]) -with_capella_and_later = with_all_phases_except([PHASE0, ALTAIR, BELLATRIX, EIP4844]) -with_eip4844_and_later = with_all_phases_except([PHASE0, ALTAIR, BELLATRIX, CAPELLA]) - - def only_generator(reason): def _decorator(inner): def _wrapper(*args, **kwargs): diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index 854dbf59a..c3db16675 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -2,10 +2,11 @@ from lru import LRU from typing import List -from eth2spec.test.context import expect_assertion_error, is_post_altair +from eth2spec.test.context import expect_assertion_error from eth2spec.test.helpers.state import state_transition_and_sign_block, next_epoch, next_slot from eth2spec.test.helpers.block import build_empty_block_for_next_slot from eth2spec.test.helpers.keys import privkeys +from eth2spec.test.helpers.phases import is_post_altair from eth2spec.utils import bls from eth2spec.utils.ssz.ssz_typing import Bitlist diff --git a/tests/core/pyspec/eth2spec/test/helpers/block.py b/tests/core/pyspec/eth2spec/test/helpers/block.py index facaa48b6..24bc68e60 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/block.py +++ b/tests/core/pyspec/eth2spec/test/helpers/block.py @@ -1,6 +1,6 @@ -from eth2spec.test.context import is_post_altair, is_post_bellatrix from eth2spec.test.helpers.execution_payload import build_empty_execution_payload from eth2spec.test.helpers.keys import privkeys +from eth2spec.test.helpers.phases import is_post_altair, is_post_bellatrix from eth2spec.utils import bls from eth2spec.utils.bls import only_with_bls from eth2spec.utils.ssz.ssz_impl import hash_tree_root diff --git a/tests/core/pyspec/eth2spec/test/helpers/constants.py b/tests/core/pyspec/eth2spec/test/helpers/constants.py index 0763c42cc..598b189ef 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/constants.py +++ b/tests/core/pyspec/eth2spec/test/helpers/constants.py @@ -26,9 +26,6 @@ ALL_PHASES = ( # The forks that output to the test vectors. TESTGEN_FORKS = (PHASE0, ALTAIR, BELLATRIX, CAPELLA, EIP4844) -FORKS_BEFORE_ALTAIR = (PHASE0,) -FORKS_BEFORE_BELLATRIX = (PHASE0, ALTAIR) - # TODO: no EIP4844 fork tests now. Should add when we figure out the content of Capella. ALL_FORK_UPGRADES = { # pre_fork_name: post_fork_name @@ -37,7 +34,7 @@ ALL_FORK_UPGRADES = { BELLATRIX: CAPELLA, } ALL_PRE_POST_FORKS = ALL_FORK_UPGRADES.items() -AFTER_BELLATRIX_UPGRADES = {key: value for key, value in ALL_FORK_UPGRADES.items() if key not in FORKS_BEFORE_ALTAIR} +AFTER_BELLATRIX_UPGRADES = {key: value for key, value in ALL_FORK_UPGRADES.items() if key != PHASE0} AFTER_BELLATRIX_PRE_POST_FORKS = AFTER_BELLATRIX_UPGRADES.items() # diff --git a/tests/core/pyspec/eth2spec/test/helpers/deposits.py b/tests/core/pyspec/eth2spec/test/helpers/deposits.py index 4639b22dc..f013fdc7e 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/deposits.py +++ b/tests/core/pyspec/eth2spec/test/helpers/deposits.py @@ -1,10 +1,8 @@ from random import Random -from eth2spec.test.context import ( - is_post_altair, - expect_assertion_error, -) +from eth2spec.test.context import expect_assertion_error from eth2spec.test.helpers.keys import pubkeys, privkeys +from eth2spec.test.helpers.phases import is_post_altair from eth2spec.test.helpers.state import get_balance from eth2spec.utils import bls from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merkle_proof diff --git a/tests/core/pyspec/eth2spec/test/helpers/epoch_processing.py b/tests/core/pyspec/eth2spec/test/helpers/epoch_processing.py index e18c239f1..ebfce5cad 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/epoch_processing.py +++ b/tests/core/pyspec/eth2spec/test/helpers/epoch_processing.py @@ -1,5 +1,5 @@ -from eth2spec.test.context import is_post_altair +from eth2spec.test.helpers.phases import is_post_altair def get_process_calls(spec): diff --git a/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py b/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py index 83162e1c2..f2dd15e39 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py +++ b/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py @@ -1,5 +1,5 @@ from eth2spec.debug.random_value import get_random_bytes_list -from eth2spec.test.context import is_post_capella +from eth2spec.test.helpers.phases import is_post_capella def build_empty_execution_payload(spec, state, randao_mix=None): diff --git a/tests/core/pyspec/eth2spec/test/helpers/genesis.py b/tests/core/pyspec/eth2spec/test/helpers/genesis.py index e497a0ce2..bd4f0e421 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/genesis.py +++ b/tests/core/pyspec/eth2spec/test/helpers/genesis.py @@ -1,6 +1,8 @@ from eth2spec.test.helpers.constants import ( ALTAIR, BELLATRIX, CAPELLA, EIP4844, - FORKS_BEFORE_ALTAIR, FORKS_BEFORE_BELLATRIX, +) +from eth2spec.test.helpers.phases import ( + is_post_altair, is_post_bellatrix, ) from eth2spec.test.helpers.keys import pubkeys @@ -88,7 +90,7 @@ def create_genesis_state(spec, validator_balances, activation_threshold): if validator.effective_balance >= activation_threshold: validator.activation_eligibility_epoch = spec.GENESIS_EPOCH validator.activation_epoch = spec.GENESIS_EPOCH - if spec.fork not in FORKS_BEFORE_ALTAIR: + if is_post_altair(spec): state.previous_epoch_participation.append(spec.ParticipationFlags(0b0000_0000)) state.current_epoch_participation.append(spec.ParticipationFlags(0b0000_0000)) state.inactivity_scores.append(spec.uint64(0)) @@ -96,13 +98,13 @@ def create_genesis_state(spec, validator_balances, activation_threshold): # Set genesis validators root for domain separation and chain versioning state.genesis_validators_root = spec.hash_tree_root(state.validators) - if spec.fork not in FORKS_BEFORE_ALTAIR: + if is_post_altair(spec): # Fill in sync committees # Note: A duplicate committee is assigned for the current and next committee at genesis state.current_sync_committee = spec.get_next_sync_committee(state) state.next_sync_committee = spec.get_next_sync_committee(state) - if spec.fork not in FORKS_BEFORE_BELLATRIX: + if is_post_bellatrix(spec): # Initialize the execution payload header (with block number and genesis time set to 0) state.latest_execution_payload_header = get_sample_genesis_execution_payload_header( spec, diff --git a/tests/core/pyspec/eth2spec/test/helpers/phases.py b/tests/core/pyspec/eth2spec/test/helpers/phases.py new file mode 100644 index 000000000..4bb75413d --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/helpers/phases.py @@ -0,0 +1,33 @@ +from .constants import ( + PHASE0, ALTAIR, BELLATRIX, CAPELLA, EIP4844, +) + + +def is_post_fork(a, b): + if a == EIP4844: + return b in [PHASE0, ALTAIR, BELLATRIX, EIP4844] + if a == CAPELLA: + return b in [PHASE0, ALTAIR, BELLATRIX, CAPELLA] + if a == BELLATRIX: + return b in [PHASE0, ALTAIR, BELLATRIX] + if a == ALTAIR: + return b in [PHASE0, ALTAIR] + if a == PHASE0: + return b in [PHASE0] + assert False # Fork is missing + + +def is_post_altair(spec): + return is_post_fork(spec.fork, ALTAIR) + + +def is_post_bellatrix(spec): + return is_post_fork(spec.fork, BELLATRIX) + + +def is_post_capella(spec): + return is_post_fork(spec.fork, CAPELLA) + + +def is_post_eip4844(spec): + return is_post_fork(spec.fork, EIP4844) diff --git a/tests/core/pyspec/eth2spec/test/helpers/proposer_slashings.py b/tests/core/pyspec/eth2spec/test/helpers/proposer_slashings.py index 517190869..abb8bd62a 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/proposer_slashings.py +++ b/tests/core/pyspec/eth2spec/test/helpers/proposer_slashings.py @@ -1,6 +1,6 @@ -from eth2spec.test.context import is_post_altair, is_post_bellatrix from eth2spec.test.helpers.block_header import sign_block_header from eth2spec.test.helpers.keys import pubkey_to_privkey +from eth2spec.test.helpers.phases import is_post_altair, is_post_bellatrix from eth2spec.test.helpers.state import get_balance from eth2spec.test.helpers.sync_committee import ( compute_committee_indices, diff --git a/tests/core/pyspec/eth2spec/test/helpers/random.py b/tests/core/pyspec/eth2spec/test/helpers/random.py index 0dc446d19..1b093fb41 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/random.py +++ b/tests/core/pyspec/eth2spec/test/helpers/random.py @@ -1,8 +1,8 @@ from random import Random from eth2spec.test.helpers.attestations import cached_prepare_state_with_attestations -from eth2spec.test.context import is_post_altair from eth2spec.test.helpers.deposits import mock_deposit +from eth2spec.test.helpers.phases import is_post_altair from eth2spec.test.helpers.state import next_epoch diff --git a/tests/core/pyspec/eth2spec/test/helpers/rewards.py b/tests/core/pyspec/eth2spec/test/helpers/rewards.py index ec7ff25e3..c3a5cf2a9 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/rewards.py +++ b/tests/core/pyspec/eth2spec/test/helpers/rewards.py @@ -2,7 +2,7 @@ from random import Random from lru import LRU from eth2spec.phase0.mainnet import VALIDATOR_REGISTRY_LIMIT # equal everywhere, fine to import -from eth2spec.test.context import is_post_altair, is_post_bellatrix +from eth2spec.test.helpers.phases import is_post_altair, is_post_bellatrix from eth2spec.test.helpers.state import ( next_epoch, ) diff --git a/tests/core/pyspec/eth2spec/test/helpers/state.py b/tests/core/pyspec/eth2spec/test/helpers/state.py index 6f1923e54..2190f1759 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/state.py +++ b/tests/core/pyspec/eth2spec/test/helpers/state.py @@ -1,5 +1,6 @@ -from eth2spec.test.context import expect_assertion_error, is_post_altair +from eth2spec.test.context import expect_assertion_error from eth2spec.test.helpers.block import apply_empty_block, sign_block, transition_unsigned_block +from eth2spec.test.helpers.phases import is_post_altair from eth2spec.test.helpers.voluntary_exits import get_unslashed_exited_validators diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_justification_and_finalization.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_justification_and_finalization.py index 1d3197ba6..c94d3e757 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_justification_and_finalization.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_justification_and_finalization.py @@ -1,8 +1,9 @@ from random import Random -from eth2spec.test.context import is_post_altair, spec_state_test, with_all_phases +from eth2spec.test.context import spec_state_test, with_all_phases from eth2spec.test.helpers.epoch_processing import ( run_epoch_processing_with, ) +from eth2spec.test.helpers.phases import is_post_altair from eth2spec.test.helpers.state import transition_to, next_epoch_via_block, next_slot from eth2spec.test.helpers.voluntary_exits import get_unslashed_exited_validators diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py index 2bb297498..443aa0c3c 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py @@ -5,6 +5,8 @@ from eth2spec.test.context import ( with_custom_state, zero_activation_threshold, misc_balances, low_single_balance, +) +from eth2spec.test.helpers.phases import ( is_post_altair, ) from eth2spec.test.helpers.state import ( diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_slashings.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_slashings.py index e7ae96ecb..7ad487fae 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_slashings.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_slashings.py @@ -1,9 +1,10 @@ from random import Random -from eth2spec.test.context import spec_state_test, with_all_phases, is_post_altair, is_post_bellatrix +from eth2spec.test.context import spec_state_test, with_all_phases from eth2spec.test.helpers.epoch_processing import ( run_epoch_processing_with, run_epoch_processing_to ) from eth2spec.test.helpers.random import randomize_state +from eth2spec.test.helpers.phases import is_post_altair, is_post_bellatrix from eth2spec.test.helpers.state import has_active_balance_differential from eth2spec.test.helpers.voluntary_exits import get_unslashed_exited_validators from eth2spec.test.helpers.state import next_epoch diff --git a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py index e8c2dc268..286bd06e5 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py +++ b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_get_head.py @@ -2,7 +2,6 @@ import random from eth_utils import encode_hex from eth2spec.test.context import ( - is_post_altair, spec_state_test, with_all_phases, with_presets, @@ -24,6 +23,9 @@ from eth2spec.test.helpers.fork_choice import ( tick_and_run_on_attestation, tick_and_add_block, ) +from eth2spec.test.helpers.phases import ( + is_post_altair, +) from eth2spec.test.helpers.state import ( next_slots, next_epoch, diff --git a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py index 53dd3760a..f311ec127 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py +++ b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py @@ -1,5 +1,4 @@ from eth2spec.test.context import ( - is_post_altair, single_phase, spec_test, with_presets, @@ -10,6 +9,9 @@ from eth2spec.test.helpers.deposits import ( prepare_full_genesis_deposits, prepare_random_genesis_deposits, ) +from eth2spec.test.helpers.phases import ( + is_post_altair, +) def get_post_altair_description(spec): diff --git a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_validity.py b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_validity.py index e17e72a4e..e0e64875d 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_validity.py +++ b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_validity.py @@ -1,5 +1,4 @@ from eth2spec.test.context import ( - is_post_altair, spec_test, single_phase, with_presets, @@ -9,6 +8,9 @@ from eth2spec.test.helpers.constants import MINIMAL from eth2spec.test.helpers.deposits import ( prepare_full_genesis_deposits, ) +from eth2spec.test.helpers.phases import ( + is_post_altair, +) def get_post_altair_description(spec): diff --git a/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py index a09c02b9a..57ae90093 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py @@ -30,6 +30,7 @@ from eth2spec.test.helpers.sync_committee import ( compute_sync_committee_participant_reward_and_penalty, ) from eth2spec.test.helpers.constants import PHASE0, MINIMAL +from eth2spec.test.helpers.phases import is_post_altair, is_post_bellatrix from eth2spec.test.context import ( spec_test, spec_state_test, dump_skipping_message, with_phases, with_all_phases, single_phase, @@ -37,8 +38,6 @@ from eth2spec.test.context import ( with_presets, with_custom_state, large_validator_set, - is_post_altair, - is_post_bellatrix, ) diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py index 7f0fd5d73..f33a2802b 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py @@ -1,9 +1,11 @@ from eth2spec.test.context import ( spec_state_test, with_all_phases, - is_post_altair, is_post_bellatrix, ) from eth2spec.test.helpers.constants import MAX_UINT_64 +from eth2spec.test.helpers.phases import ( + is_post_altair, is_post_bellatrix, +) def check_bound(value, lower_bound, upper_bound):