From b1711b4ff1399461d0832ae7206ecf06b1312160 Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Thu, 16 May 2019 12:10:08 +0200 Subject: [PATCH] Implements parameterised phase1 tests --- .gitignore | 1 + Makefile | 3 +- .../test_phase0_block_processing.py | 7 ++ ...est_process_early_derived_secret_reveal.py | 72 ++++++++----------- test_libs/pyspec/tests/phase1/conftest.py | 23 ++++-- .../phase1/epoch_processing/test_phase0.py | 2 + test_libs/pyspec/tests/phase1/helpers.py | 16 +---- .../pyspec/tests/phase1/helpers_phase1.py | 52 -------------- .../pyspec/tests/phase1/test_finality.py | 25 +------ test_libs/pyspec/tests/phase1/test_sanity.py | 66 +---------------- 10 files changed, 60 insertions(+), 207 deletions(-) create mode 100644 test_libs/pyspec/tests/phase1/block_processing_phase1/test_phase0_block_processing.py create mode 100644 test_libs/pyspec/tests/phase1/epoch_processing/test_phase0.py delete mode 100644 test_libs/pyspec/tests/phase1/helpers_phase1.py diff --git a/.gitignore b/.gitignore index 3dd86fc80..c6b39955f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ eth2.0-spec-tests/ # Dynamically built from Markdown spec test_libs/pyspec/eth2spec/phase0/spec.py +test_libs/pyspec/eth2spec/phase1/spec.py diff --git a/Makefile b/Makefile index 49460bdf1..c3ec21280 100644 --- a/Makefile +++ b/Makefile @@ -38,8 +38,7 @@ gen_yaml_tests: $(PY_SPEC_ALL_TARGETS) $(YAML_TEST_TARGETS) install_test: cd $(PY_SPEC_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements-testing.txt; -test: $(PY_SPEC_ALL_TARGETS) - cd $(PY_SPEC_DIR); . venv/bin/activate; python -m pytest . +test: test_phase0 test_phase1 test_phase0: $(PY_SPEC_ALL_TARGETS) cd $(PY_SPEC_DIR); . venv/bin/activate; python -m pytest tests/phase0 diff --git a/test_libs/pyspec/tests/phase1/block_processing_phase1/test_phase0_block_processing.py b/test_libs/pyspec/tests/phase1/block_processing_phase1/test_phase0_block_processing.py new file mode 100644 index 000000000..246ba163c --- /dev/null +++ b/test_libs/pyspec/tests/phase1/block_processing_phase1/test_phase0_block_processing.py @@ -0,0 +1,7 @@ +from tests.phase0.block_processing.test_process_attestation import * +from tests.phase0.block_processing.test_process_attester_slashing import * +from tests.phase0.block_processing.test_process_block_header import * +from tests.phase0.block_processing.test_process_deposit import * +from tests.phase0.block_processing.test_process_proposer_slashing import * +from tests.phase0.block_processing.test_process_transfer import * +from tests.phase0.block_processing.test_voluntary_exit import * diff --git a/test_libs/pyspec/tests/phase1/block_processing_phase1/test_process_early_derived_secret_reveal.py b/test_libs/pyspec/tests/phase1/block_processing_phase1/test_process_early_derived_secret_reveal.py index 6661e883f..e3cf30ef3 100644 --- a/test_libs/pyspec/tests/phase1/block_processing_phase1/test_process_early_derived_secret_reveal.py +++ b/test_libs/pyspec/tests/phase1/block_processing_phase1/test_process_early_derived_secret_reveal.py @@ -1,22 +1,10 @@ from copy import deepcopy import pytest -import eth2spec.phase1.spec as spec -from eth2spec.phase1.spec import ( - get_current_epoch, - process_early_derived_secret_reveal, - RANDAO_PENALTY_EPOCHS, - CUSTODY_PERIOD_TO_RANDAO_PADDING, - EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS, -) -from tests.phase1.helpers import ( - get_valid_early_derived_secret_reveal, -) - #mark entire file as 'randao_key_reveals' pytestmark = pytest.mark.randao_key_reveals -def run_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=True): +def terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, valid=True): """ Run ``process_randao_key_reveal`` returning the pre and post state. If ``valid == False``, run expecting ``AssertionError`` @@ -25,14 +13,14 @@ def run_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=T if not valid: with pytest.raises(AssertionError): - process_early_derived_secret_reveal(post_state, randao_key_reveal) + spec.process_early_derived_secret_reveal(post_state, randao_key_reveal) return state, None - process_early_derived_secret_reveal(post_state, randao_key_reveal) + spec.process_early_derived_secret_reveal(post_state, randao_key_reveal) slashed_validator = post_state.validator_registry[randao_key_reveal.revealed_index] - if randao_key_reveal.epoch >= get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING: + if randao_key_reveal.epoch >= spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING: assert slashed_validator.slashed assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH @@ -46,63 +34,63 @@ def run_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=T return state, post_state -def test_success(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state) +def test_success(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal) return pre_state, randao_key_reveal, post_state -def test_reveal_from_current_epoch(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state)) +def test_reveal_from_current_epoch(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state)) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False) return pre_state, randao_key_reveal, post_state @pytest.mark.skip(reason="Not currently possible as we are testing at epoch 0") -def test_reveal_from_past_epoch(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) - 1) +def test_reveal_from_past_epoch(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) - 1) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False) return pre_state, randao_key_reveal, post_state -def test_reveal_with_custody_padding(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True) +def test_reveal_with_custody_padding(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, True) return pre_state, randao_key_reveal, post_state -def test_reveal_with_custody_padding_minus_one(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING - 1) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True) +def test_reveal_with_custody_padding_minus_one(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING - 1) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, True) return pre_state, randao_key_reveal, post_state -def test_double_reveal(state): +def test_double_reveal(spec, helpers, state): - randao_key_reveal1 = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + RANDAO_PENALTY_EPOCHS + 1) - pre_state, intermediate_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal1) + randao_key_reveal1 = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.RANDAO_PENALTY_EPOCHS + 1) + pre_state, intermediate_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal1) - randao_key_reveal2 = get_valid_early_derived_secret_reveal(intermediate_state, get_current_epoch(pre_state) + RANDAO_PENALTY_EPOCHS + 1) - _, post_state = run_early_derived_secret_reveal_processing(intermediate_state, randao_key_reveal2, False) + randao_key_reveal2 = helpers.get_valid_early_derived_secret_reveal(intermediate_state, spec.get_current_epoch(pre_state) + spec.RANDAO_PENALTY_EPOCHS + 1) + _, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, intermediate_state, randao_key_reveal2, False) return pre_state, [randao_key_reveal1, randao_key_reveal2], post_state -def test_revealer_is_slashed(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state)) +def test_revealer_is_slashed(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state)) state.validator_registry[randao_key_reveal.revealed_index].slashed = True - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False) return pre_state, randao_key_reveal, post_state -def test_far_future_epoch(state): - randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS) +def test_far_future_epoch(spec, helpers, state): + randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) + spec.EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS) - pre_state, post_state = run_early_derived_secret_reveal_processing(state, randao_key_reveal, False) + pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False) return pre_state, randao_key_reveal, post_state diff --git a/test_libs/pyspec/tests/phase1/conftest.py b/test_libs/pyspec/tests/phase1/conftest.py index c73f25c1b..4af3451a5 100644 --- a/test_libs/pyspec/tests/phase1/conftest.py +++ b/test_libs/pyspec/tests/phase1/conftest.py @@ -1,15 +1,12 @@ import pytest -from eth2spec.phase1 import spec +from eth2spec.phase1 import spec as _spec from preset_loader import loader -from tests.phase0.helpers import ( - create_genesis_state, -) +from tests.phase1 import helpers as _helpers from tests.phase0.conftest import ( pytest_addoption, - num_validators, deposit_data_leaves, ) @@ -18,9 +15,21 @@ from tests.phase0.conftest import ( def config(request): config_name = request.config.getoption("--config") presets = loader.load_presets('../../configs/', config_name) - spec.apply_constants_preset(presets) + _spec.apply_constants_preset(presets) + +@pytest.fixture +def num_validators(config): + return _spec.SLOTS_PER_EPOCH * 8 #This is redefined so that the BeaconState is the new SSZ Object @pytest.fixture def state(num_validators, deposit_data_leaves): - return create_genesis_state(num_validators, deposit_data_leaves) + return _helpers.create_genesis_state(num_validators, deposit_data_leaves) + +@pytest.fixture +def spec(): + return _spec + +@pytest.fixture +def helpers(): + return _helpers diff --git a/test_libs/pyspec/tests/phase1/epoch_processing/test_phase0.py b/test_libs/pyspec/tests/phase1/epoch_processing/test_phase0.py new file mode 100644 index 000000000..ec3012560 --- /dev/null +++ b/test_libs/pyspec/tests/phase1/epoch_processing/test_phase0.py @@ -0,0 +1,2 @@ +from tests.phase0.epoch_processing.test_process_crosslinks import * +from tests.phase0.epoch_processing.test_process_registry_updates import * diff --git a/test_libs/pyspec/tests/phase1/helpers.py b/test_libs/pyspec/tests/phase1/helpers.py index 8520ce9d7..b56266617 100644 --- a/test_libs/pyspec/tests/phase1/helpers.py +++ b/test_libs/pyspec/tests/phase1/helpers.py @@ -3,7 +3,6 @@ from py_ecc import bls import eth2spec.phase1.spec as spec from eth2spec.phase1.spec import ( # constants - ZERO_HASH, CUSTODY_PERIOD_TO_RANDAO_PADDING, # SSZ EarlyDerivedSecretReveal, @@ -14,20 +13,7 @@ from eth2spec.phase1.spec import ( hash_tree_root, ) -from tests.phase0.helpers import ( - advance_slot, - get_balance, - build_deposit_data, - build_empty_block_for_next_slot, - fill_aggregate_attestation, - get_state_root, - get_valid_attestation, - get_valid_attester_slashing, - get_valid_proposer_slashing, - next_slot, - privkeys, - pubkeys, -) +from tests.phase0.helpers import * def get_valid_early_derived_secret_reveal(state, epoch=None): current_epoch = get_current_epoch(state) diff --git a/test_libs/pyspec/tests/phase1/helpers_phase1.py b/test_libs/pyspec/tests/phase1/helpers_phase1.py deleted file mode 100644 index 5b67e9aa6..000000000 --- a/test_libs/pyspec/tests/phase1/helpers_phase1.py +++ /dev/null @@ -1,52 +0,0 @@ -from py_ecc import bls - -import eth2spec.phase1.spec as spec -from eth2spec.phase1.spec import ( - # constants - ZERO_HASH, - CUSTODY_PERIOD_TO_RANDAO_PADDING, - # SSZ - EarlyDerivedSecretReveal, - # functions - get_active_validator_indices, - get_current_epoch, - get_domain, - hash_tree_root, -) - -from .helpers import privkeys - -def get_valid_early_derived_secret_reveal(state, epoch=None): - current_epoch = get_current_epoch(state) - revealed_index = get_active_validator_indices(state, current_epoch)[-1] - masker_index = get_active_validator_indices(state, current_epoch)[0] - - if epoch is None: - epoch = current_epoch + CUSTODY_PERIOD_TO_RANDAO_PADDING - - reveal = bls.sign( - message_hash=hash_tree_root(epoch), - privkey=privkeys[revealed_index], - domain=get_domain( - state=state, - domain_type=spec.DOMAIN_RANDAO, - message_epoch=epoch, - ), - ) - mask = bls.sign( - message_hash=hash_tree_root(epoch), - privkey=privkeys[masker_index], - domain=get_domain( - state=state, - domain_type=spec.DOMAIN_RANDAO, - message_epoch=epoch, - ), - ) - - return EarlyDerivedSecretReveal( - revealed_index=revealed_index, - epoch=epoch, - reveal=reveal, - masker_index=masker_index, - mask=mask, - ) diff --git a/test_libs/pyspec/tests/phase1/test_finality.py b/test_libs/pyspec/tests/phase1/test_finality.py index 4b57c9493..1e4e0374a 100644 --- a/test_libs/pyspec/tests/phase1/test_finality.py +++ b/test_libs/pyspec/tests/phase1/test_finality.py @@ -1,27 +1,4 @@ -from copy import deepcopy - import pytest -import eth2spec.phase1.spec as spec -from eth2spec.phase1.spec import ( - state_transition, -) - -from tests.phase0.helpers import ( - build_empty_block_for_next_slot, - fill_aggregate_attestation, - get_current_epoch, - get_epoch_start_slot, - get_valid_attestation, - next_epoch, -) - -from tests.phase0.test_finality import ( - pytestmark, - check_finality, - test_finality_rule_1, - test_finality_rule_2, - test_finality_rule_3, - test_finality_rule_4, -) +from tests.phase0.test_finality import * diff --git a/test_libs/pyspec/tests/phase1/test_sanity.py b/test_libs/pyspec/tests/phase1/test_sanity.py index 45a08bd0e..a9b4d7190 100644 --- a/test_libs/pyspec/tests/phase1/test_sanity.py +++ b/test_libs/pyspec/tests/phase1/test_sanity.py @@ -1,68 +1,4 @@ -from copy import deepcopy - import pytest -from py_ecc import bls -import eth2spec.phase1.spec as spec -from eth2spec.utils.minimal_ssz import signing_root -from eth2spec.phase1.spec import ( - # constants - ZERO_HASH, - SLOTS_PER_HISTORICAL_ROOT, - # SSZ - Deposit, - Transfer, - VoluntaryExit, - # functions - get_active_validator_indices, - get_beacon_proposer_index, - get_block_root_at_slot, - get_current_epoch, - get_domain, - process_slot, - verify_merkle_branch, - state_transition, - hash, -) -from eth2spec.utils.merkle_minimal import ( - calc_merkle_tree_from_leaves, - get_merkle_proof, - get_merkle_root, -) -from .helpers import ( - advance_slot, - get_balance, - build_deposit_data, - build_empty_block_for_next_slot, - fill_aggregate_attestation, - get_state_root, - get_valid_attestation, - get_valid_attester_slashing, - get_valid_proposer_slashing, - next_slot, - privkeys, - pubkeys, -) - - -# mark entire file as 'sanity' -pytestmark = pytest.mark.sanity - -from tests.phase0.test_sanity import ( - test_slot_transition, - test_empty_block_transition, - test_skipped_slots, - test_empty_epoch_transition, - test_empty_epoch_transition_not_finalizing, - test_proposer_slashing, - test_attester_slashing, - test_deposit_in_block, - test_deposit_top_up, - test_attestation, - test_voluntary_exit, - test_transfer, - test_balance_driven_status_transitions, - test_historical_batch, - test_eth1_data_votes, -) +from tests.phase0.test_sanity import *