Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move is_post_xxx functions to new module #3072

Merged
merged 4 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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])
Expand Down
40 changes: 17 additions & 23 deletions tests/core/pyspec/eth2spec/test/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
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 (
Expand Down Expand Up @@ -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
Expand All @@ -417,6 +427,12 @@ def decorator(fn):
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:
Expand Down Expand Up @@ -587,28 +603,6 @@ def wrapper(*args, spec: Spec, **kw):
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):
Expand Down
3 changes: 2 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/attestations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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

Expand Down
2 changes: 1 addition & 1 deletion tests/core/pyspec/eth2spec/test/helpers/block.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions tests/core/pyspec/eth2spec/test/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
# 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
Expand All @@ -37,7 +34,7 @@
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()

#
Expand Down
6 changes: 2 additions & 4 deletions tests/core/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
10 changes: 6 additions & 4 deletions tests/core/pyspec/eth2spec/test/helpers/genesis.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -88,21 +90,21 @@ 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))

# 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,
Expand Down
33 changes: 33 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/phases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from .constants import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we had Phase0, now I wish we could retire "phases" someday since developments are in-parallel.

What do you think about protocols.py or forks.py?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will go with forks.py then. It is consistent with the fork=... naming when running make citest.
I will not touch the decorators such as @with_all_phases for now, as it is out of scope of this PR.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert False # Fork is missing
raise ValueError("Wrong fork name %s" % a)



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)
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/core/pyspec/eth2spec/test/helpers/random.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/core/pyspec/eth2spec/test/helpers/rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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,
)
Expand Down
3 changes: 2 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/state.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from eth_utils import encode_hex

from eth2spec.test.context import (
is_post_altair,
spec_state_test,
with_all_phases,
with_presets,
Expand All @@ -24,6 +23,9 @@
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from eth2spec.test.context import (
is_post_altair,
single_phase,
spec_test,
with_presets,
Expand All @@ -10,6 +9,9 @@
prepare_full_genesis_deposits,
prepare_random_genesis_deposits,
)
from eth2spec.test.helpers.phases import (
is_post_altair,
)


def get_post_altair_description(spec):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from eth2spec.test.context import (
is_post_altair,
spec_test,
single_phase,
with_presets,
Expand All @@ -9,6 +8,9 @@
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):
Expand Down
3 changes: 1 addition & 2 deletions tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
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,
expect_assertion_error, always_bls,
with_presets,
with_custom_state,
large_validator_set,
is_post_altair,
is_post_bellatrix,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down