Implements parameterised phase1 tests

This commit is contained in:
Carl Beekhuizen 2019-05-16 12:10:08 +02:00
parent 1b2e7c1b6e
commit b1711b4ff1
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
10 changed files with 60 additions and 207 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ eth2.0-spec-tests/
# Dynamically built from Markdown spec # Dynamically built from Markdown spec
test_libs/pyspec/eth2spec/phase0/spec.py test_libs/pyspec/eth2spec/phase0/spec.py
test_libs/pyspec/eth2spec/phase1/spec.py

View File

@ -38,8 +38,7 @@ gen_yaml_tests: $(PY_SPEC_ALL_TARGETS) $(YAML_TEST_TARGETS)
install_test: install_test:
cd $(PY_SPEC_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements-testing.txt; cd $(PY_SPEC_DIR); python3 -m venv venv; . venv/bin/activate; pip3 install -r requirements-testing.txt;
test: $(PY_SPEC_ALL_TARGETS) test: test_phase0 test_phase1
cd $(PY_SPEC_DIR); . venv/bin/activate; python -m pytest .
test_phase0: $(PY_SPEC_ALL_TARGETS) test_phase0: $(PY_SPEC_ALL_TARGETS)
cd $(PY_SPEC_DIR); . venv/bin/activate; python -m pytest tests/phase0 cd $(PY_SPEC_DIR); . venv/bin/activate; python -m pytest tests/phase0

View File

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

View File

@ -1,22 +1,10 @@
from copy import deepcopy from copy import deepcopy
import pytest 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' #mark entire file as 'randao_key_reveals'
pytestmark = pytest.mark.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. Run ``process_randao_key_reveal`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError`` 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: if not valid:
with pytest.raises(AssertionError): 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 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] 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.slashed
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
assert slashed_validator.withdrawable_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 return state, post_state
def test_success(state): def test_success(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(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 return pre_state, randao_key_reveal, post_state
def test_reveal_from_current_epoch(state): def test_reveal_from_current_epoch(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(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 return pre_state, randao_key_reveal, post_state
@pytest.mark.skip(reason="Not currently possible as we are testing at epoch 0") @pytest.mark.skip(reason="Not currently possible as we are testing at epoch 0")
def test_reveal_from_past_epoch(state): def test_reveal_from_past_epoch(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) - 1) 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 return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding(state): def test_reveal_with_custody_padding(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING) 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 = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True) 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 return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding_minus_one(state): def test_reveal_with_custody_padding_minus_one(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + CUSTODY_PERIOD_TO_RANDAO_PADDING - 1) 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 = run_early_derived_secret_reveal_processing(state, randao_key_reveal, True) 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 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) 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 = run_early_derived_secret_reveal_processing(state, randao_key_reveal1) 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) 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 = run_early_derived_secret_reveal_processing(intermediate_state, randao_key_reveal2, False) _, 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 return pre_state, [randao_key_reveal1, randao_key_reveal2], post_state
def test_revealer_is_slashed(state): def test_revealer_is_slashed(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(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 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 return pre_state, randao_key_reveal, post_state
def test_far_future_epoch(state): def test_far_future_epoch(spec, helpers, state):
randao_key_reveal = get_valid_early_derived_secret_reveal(state, get_current_epoch(state) + EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS) 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 return pre_state, randao_key_reveal, post_state

View File

@ -1,15 +1,12 @@
import pytest import pytest
from eth2spec.phase1 import spec from eth2spec.phase1 import spec as _spec
from preset_loader import loader from preset_loader import loader
from tests.phase0.helpers import ( from tests.phase1 import helpers as _helpers
create_genesis_state,
)
from tests.phase0.conftest import ( from tests.phase0.conftest import (
pytest_addoption, pytest_addoption,
num_validators,
deposit_data_leaves, deposit_data_leaves,
) )
@ -18,9 +15,21 @@ from tests.phase0.conftest import (
def config(request): def config(request):
config_name = request.config.getoption("--config") config_name = request.config.getoption("--config")
presets = loader.load_presets('../../configs/', config_name) 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 #This is redefined so that the BeaconState is the new SSZ Object
@pytest.fixture @pytest.fixture
def state(num_validators, deposit_data_leaves): 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

View File

@ -0,0 +1,2 @@
from tests.phase0.epoch_processing.test_process_crosslinks import *
from tests.phase0.epoch_processing.test_process_registry_updates import *

View File

@ -3,7 +3,6 @@ from py_ecc import bls
import eth2spec.phase1.spec as spec import eth2spec.phase1.spec as spec
from eth2spec.phase1.spec import ( from eth2spec.phase1.spec import (
# constants # constants
ZERO_HASH,
CUSTODY_PERIOD_TO_RANDAO_PADDING, CUSTODY_PERIOD_TO_RANDAO_PADDING,
# SSZ # SSZ
EarlyDerivedSecretReveal, EarlyDerivedSecretReveal,
@ -14,20 +13,7 @@ from eth2spec.phase1.spec import (
hash_tree_root, hash_tree_root,
) )
from tests.phase0.helpers import ( 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,
)
def get_valid_early_derived_secret_reveal(state, epoch=None): def get_valid_early_derived_secret_reveal(state, epoch=None):
current_epoch = get_current_epoch(state) current_epoch = get_current_epoch(state)

View File

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

View File

@ -1,27 +1,4 @@
from copy import deepcopy
import pytest import pytest
import eth2spec.phase1.spec as spec
from eth2spec.phase1.spec import ( from tests.phase0.test_finality 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,
)

View File

@ -1,68 +1,4 @@
from copy import deepcopy
import pytest import pytest
from py_ecc import bls
import eth2spec.phase1.spec as spec
from eth2spec.utils.minimal_ssz import signing_root from tests.phase0.test_sanity import *
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,
)