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.
This commit is contained in:
Etan Kissling 2022-10-30 12:50:56 +01:00
parent bab01d6e99
commit 332ad4cb57
No known key found for this signature in database
GPG Key ID: B21DA824C5A3D03D
22 changed files with 91 additions and 53 deletions

View File

@ -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])

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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()
#

View File

@ -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

View File

@ -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):

View File

@ -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):

View File

@ -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,

View File

@ -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)

View File

@ -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,

View File

@ -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

View File

@ -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,
)

View File

@ -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

View File

@ -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

View File

@ -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 (

View File

@ -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

View File

@ -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,

View File

@ -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):

View File

@ -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):

View File

@ -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,
)

View File

@ -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):