diff --git a/docs/docs/new-feature.md b/docs/docs/new-feature.md index 50c737460..0b00ebb84 100644 --- a/docs/docs/new-feature.md +++ b/docs/docs/new-feature.md @@ -79,20 +79,6 @@ You can refer to the previous fork's `fork.md` file. We use `create_genesis_state` to create the default `state` in tests. -- Update `create_genesis_state` by adding `fork_version` setting: - -```python -def create_genesis_state(spec, validator_balances, activation_threshold): - ... - if spec.fork == ALTAIR: - current_version = spec.config.ALTAIR_FORK_VERSION - ... - elif spec.fork == EIP9999: - # Add the previous fork version of given fork - previous_version = spec.config. - current_version = spec.config.EIP9999_FORK_VERSION -``` - - If the given feature changes `BeaconState` fields, you have to set the initial values by adding: ```python diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index 0adc24b5c..d75a05447 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -12,7 +12,7 @@ from .helpers.constants import ( WHISK, MINIMAL, ALL_PHASES, - ALL_FORK_UPGRADES, + POST_FORK_OF, ALLOWED_TEST_RUNNER_FORKS, LIGHT_CLIENT_TESTING_FORKS, ) @@ -469,7 +469,7 @@ def with_phases(phases, other_phases=None): # When running test generator, it sets specific `phase` phase = kw['phase'] _phases = [phase] - _other_phases = [ALL_FORK_UPGRADES[phase]] + _other_phases = [POST_FORK_OF[phase]] ret = _run_test_case_with_phases(fn, _phases, _other_phases, kw, args, is_fork_transition=True) else: # When running pytest, go through `fork_metas` instead of using `phases` diff --git a/tests/core/pyspec/eth2spec/test/helpers/constants.py b/tests/core/pyspec/eth2spec/test/helpers/constants.py index 9dcaa6548..2d1b4a821 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/constants.py +++ b/tests/core/pyspec/eth2spec/test/helpers/constants.py @@ -45,7 +45,22 @@ TESTGEN_FORKS = (*MAINNET_FORKS, DENEB, EIP6110, WHISK) # Forks allowed in the test runner `--fork` flag, to fail fast in case of typos ALLOWED_TEST_RUNNER_FORKS = (*ALL_PHASES, WHISK) -ALL_FORK_UPGRADES = { +# NOTE: the same definition as in `pysetup/md_doc_paths.py` +PREVIOUS_FORK_OF = { + # post_fork_name: pre_fork_name + PHASE0: None, + ALTAIR: PHASE0, + BELLATRIX: ALTAIR, + CAPELLA: BELLATRIX, + DENEB: CAPELLA, + # Experimental patches + EIP6110: DENEB, + WHISK: CAPELLA, + EIP7002: CAPELLA, +} + +# For fork transition tests +POST_FORK_OF = { # pre_fork_name: post_fork_name PHASE0: ALTAIR, ALTAIR: BELLATRIX, @@ -53,15 +68,11 @@ ALL_FORK_UPGRADES = { CAPELLA: DENEB, DENEB: EIP6110, } -ALL_PRE_POST_FORKS = ALL_FORK_UPGRADES.items() -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() -AFTER_CAPELLA_UPGRADES = {key: value for key, value in ALL_FORK_UPGRADES.items() - if key not in [PHASE0, ALTAIR]} -AFTER_CAPELLA_PRE_POST_FORKS = AFTER_CAPELLA_UPGRADES.items() -AFTER_DENEB_UPGRADES = {key: value for key, value in ALL_FORK_UPGRADES.items() - if key not in [PHASE0, ALTAIR, BELLATRIX]} -AFTER_DENEB_PRE_POST_FORKS = AFTER_DENEB_UPGRADES.items() + +ALL_PRE_POST_FORKS = POST_FORK_OF.items() +DENEB_TRANSITION_UPGRADES_AND_AFTER = {key: value for key, value in POST_FORK_OF.items() + if key not in [PHASE0, ALTAIR, BELLATRIX]} +AFTER_DENEB_PRE_POST_FORKS = DENEB_TRANSITION_UPGRADES_AND_AFTER.items() # # Config and Preset diff --git a/tests/core/pyspec/eth2spec/test/helpers/forks.py b/tests/core/pyspec/eth2spec/test/helpers/forks.py index 9640f9585..99cab9a84 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/forks.py +++ b/tests/core/pyspec/eth2spec/test/helpers/forks.py @@ -1,27 +1,24 @@ from .constants import ( - PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, + ALTAIR, BELLATRIX, CAPELLA, DENEB, EIP6110, EIP7002, WHISK, + PREVIOUS_FORK_OF, ) -def is_post_fork(a, b): - if a == WHISK: - return b in [PHASE0, ALTAIR, BELLATRIX, CAPELLA, WHISK] - if a == EIP7002: - return b in [PHASE0, ALTAIR, BELLATRIX, CAPELLA, EIP7002] - if a == EIP6110: - return b in [PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, EIP6110] - if a == DENEB: - return b in [PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB] - 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] - raise ValueError("Unknown fork name %s" % a) +def is_post_fork(a, b) -> bool: + """ + Returns true if fork a is after b, or if a == b + """ + if a == b: + return True + + prev_fork = PREVIOUS_FORK_OF[a] + if prev_fork == b: + return True + elif prev_fork is None: + return False + else: + return is_post_fork(prev_fork, b) def is_post_altair(spec): diff --git a/tests/core/pyspec/eth2spec/test/helpers/genesis.py b/tests/core/pyspec/eth2spec/test/helpers/genesis.py index 597e7e2ac..ab8273f66 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/genesis.py +++ b/tests/core/pyspec/eth2spec/test/helpers/genesis.py @@ -1,5 +1,6 @@ from eth2spec.test.helpers.constants import ( - ALTAIR, BELLATRIX, CAPELLA, DENEB, EIP6110, EIP7002, WHISK, + PHASE0, + PREVIOUS_FORK_OF, ) from eth2spec.test.helpers.execution_payload import ( compute_el_header_block_hash, @@ -77,26 +78,13 @@ def create_genesis_state(spec, validator_balances, activation_threshold): previous_version = spec.config.GENESIS_FORK_VERSION current_version = spec.config.GENESIS_FORK_VERSION - if spec.fork == ALTAIR: - current_version = spec.config.ALTAIR_FORK_VERSION - elif spec.fork == BELLATRIX: - previous_version = spec.config.ALTAIR_FORK_VERSION - current_version = spec.config.BELLATRIX_FORK_VERSION - elif spec.fork == CAPELLA: - previous_version = spec.config.BELLATRIX_FORK_VERSION - current_version = spec.config.CAPELLA_FORK_VERSION - elif spec.fork == DENEB: - previous_version = spec.config.CAPELLA_FORK_VERSION - current_version = spec.config.DENEB_FORK_VERSION - elif spec.fork == EIP6110: - previous_version = spec.config.DENEB_FORK_VERSION - current_version = spec.config.EIP6110_FORK_VERSION - elif spec.fork == EIP7002: - previous_version = spec.config.CAPELLA_FORK_VERSION - current_version = spec.config.EIP7002_FORK_VERSION - elif spec.fork == WHISK: - previous_version = spec.config.CAPELLA_FORK_VERSION - current_version = spec.config.WHISK_FORK_VERSION + if spec.fork != PHASE0: + previous_fork = PREVIOUS_FORK_OF[spec.fork] + if previous_fork == PHASE0: + previous_version = spec.config.GENESIS_FORK_VERSION + else: + previous_version = getattr(spec.config, f"{previous_fork.upper()}_FORK_VERSION") + current_version = getattr(spec.config, f"{spec.fork.upper()}_FORK_VERSION") state = spec.BeaconState( genesis_time=0,