Moves spec to global variable

This commit is contained in:
Carl Beekhuizen 2019-05-20 17:08:03 +02:00
parent fac9f6c91d
commit b68c471a54
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
14 changed files with 163 additions and 176 deletions

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.attestations
def run_attestation_processing(spec, helpers, state, attestation, valid=True):
def run_attestation_processing(state, attestation, valid=True):
"""
Run ``spec.process_attestation`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -29,81 +29,81 @@ def run_attestation_processing(spec, helpers, state, attestation, valid=True):
return state, post_state
def test_success(spec, helpers, state):
def test_success(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation)
pre_state, post_state = run_attestation_processing(state, attestation)
return pre_state, attestation, post_state
def test_success_prevous_epoch(spec, helpers, state):
def test_success_prevous_epoch(state):
attestation = helpers.get_valid_attestation(state)
block = helpers.build_empty_block_for_next_slot(state)
block.slot = state.slot + spec.SLOTS_PER_EPOCH
spec.state_transition(state, block)
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation)
pre_state, post_state = run_attestation_processing(state, attestation)
return pre_state, attestation, post_state
def test_before_inclusion_delay(spec, helpers, state):
def test_before_inclusion_delay(state):
attestation = helpers.get_valid_attestation(state)
# do not increment slot to allow for inclusion delay
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_after_epoch_slots(spec, helpers, state):
def test_after_epoch_slots(state):
attestation = helpers.get_valid_attestation(state)
block = helpers.build_empty_block_for_next_slot(state)
# increment past latest inclusion slot
block.slot = state.slot + spec.SLOTS_PER_EPOCH + 1
spec.state_transition(state, block)
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_bad_source_epoch(spec, helpers, state):
def test_bad_source_epoch(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.data.source_epoch += 10
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_bad_source_root(spec, helpers, state):
def test_bad_source_root(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.data.source_root = b'\x42' * 32
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_non_zero_crosslink_data_root(spec, helpers, state):
def test_non_zero_crosslink_data_root(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.data.crosslink.data_root = b'\x42' * 32
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_bad_previous_crosslink(spec, helpers, state):
def test_bad_previous_crosslink(state):
helpers.next_epoch(state)
attestation = helpers.get_valid_attestation(state)
for _ in range(spec.MIN_ATTESTATION_INCLUSION_DELAY):
@ -111,28 +111,28 @@ def test_bad_previous_crosslink(spec, helpers, state):
state.current_crosslinks[attestation.data.crosslink.shard].epoch += 10
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_non_empty_custody_bitfield(spec, helpers, state):
def test_non_empty_custody_bitfield(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.custody_bitfield = deepcopy(attestation.aggregation_bitfield)
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation, False)
pre_state, post_state = run_attestation_processing(state, attestation, False)
return pre_state, attestation, post_state
def test_empty_aggregation_bitfield(spec, helpers, state):
def test_empty_aggregation_bitfield(state):
attestation = helpers.get_valid_attestation(state)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
attestation.aggregation_bitfield = b'\x00' * len(attestation.aggregation_bitfield)
pre_state, post_state = run_attestation_processing(spec, helpers, state, attestation)
pre_state, post_state = run_attestation_processing(state, attestation)
return pre_state, attestation, post_state

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.attester_slashings
def run_attester_slashing_processing(spec, helpers, state, attester_slashing, valid=True):
def run_attester_slashing_processing(state, attester_slashing, valid=True):
"""
Run ``spec.process_attester_slashing`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -40,15 +40,15 @@ def run_attester_slashing_processing(spec, helpers, state, attester_slashing, va
return state, post_state
def test_success_double(spec, helpers, state):
def test_success_double(state):
attester_slashing = helpers.get_valid_attester_slashing(state)
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing)
return pre_state, attester_slashing, post_state
def test_success_surround(spec, helpers, state):
def test_success_surround(state):
helpers.next_epoch(state)
state.current_justified_epoch += 1
attester_slashing = helpers.get_valid_attester_slashing(state)
@ -57,32 +57,32 @@ def test_success_surround(spec, helpers, state):
attester_slashing.attestation_1.data.source_epoch = attester_slashing.attestation_2.data.source_epoch - 1
attester_slashing.attestation_1.data.target_epoch = attester_slashing.attestation_2.data.target_epoch + 1
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing)
return pre_state, attester_slashing, post_state
def test_same_data(spec, helpers, state):
def test_same_data(state):
attester_slashing = helpers.get_valid_attester_slashing(state)
attester_slashing.attestation_1.data = attester_slashing.attestation_2.data
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing, False)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
return pre_state, attester_slashing, post_state
def test_no_double_or_surround(spec, helpers, state):
def test_no_double_or_surround(state):
attester_slashing = helpers.get_valid_attester_slashing(state)
attester_slashing.attestation_1.data.target_epoch += 1
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing, False)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
return pre_state, attester_slashing, post_state
def test_participants_already_slashed(spec, helpers, state):
def test_participants_already_slashed(state):
attester_slashing = helpers.get_valid_attester_slashing(state)
# set all indices to slashed
@ -91,17 +91,17 @@ def test_participants_already_slashed(spec, helpers, state):
for index in validator_indices:
state.validator_registry[index].slashed = True
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing, False)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
return pre_state, attester_slashing, post_state
def test_custody_bit_0_and_1(spec, helpers, state):
def test_custody_bit_0_and_1(state):
attester_slashing = helpers.get_valid_attester_slashing(state)
attester_slashing.attestation_1.custody_bit_1_indices = (
attester_slashing.attestation_1.custody_bit_0_indices
)
pre_state, post_state = run_attester_slashing_processing(spec, helpers, state, attester_slashing, False)
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
return pre_state, attester_slashing, post_state

View File

@ -6,17 +6,17 @@ import pytest
pytestmark = pytest.mark.header
def prepare_state_for_header_processing(spec, helpers, state):
def prepare_state_for_header_processing(state):
spec.process_slot(state)
helpers.advance_slot(state)
def run_block_header_processing(spec, helpers, state, block, valid=True):
def run_block_header_processing(state, block, valid=True):
"""
Run ``spec.process_block_header`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
"""
prepare_state_for_header_processing(spec, helpers, state)
prepare_state_for_header_processing(state)
post_state = deepcopy(state)
if not valid:
@ -28,29 +28,29 @@ def run_block_header_processing(spec, helpers, state, block, valid=True):
return state, post_state
def test_success(spec, helpers, state):
def test_success(state):
block = helpers.build_empty_block_for_next_slot(state)
pre_state, post_state = run_block_header_processing(spec, helpers, state, block)
pre_state, post_state = run_block_header_processing(state, block)
return state, block, post_state
def test_invalid_slot(spec, helpers, state):
def test_invalid_slot(state):
block = helpers.build_empty_block_for_next_slot(state)
block.slot = state.slot + 2 # invalid slot
pre_state, post_state = run_block_header_processing(spec, helpers, state, block, valid=False)
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None
def test_invalid_parent_block_root(spec, helpers, state):
def test_invalid_parent_block_root(state):
block = helpers.build_empty_block_for_next_slot(state)
block.parent_root = b'\12' * 32 # invalid prev root
pre_state, post_state = run_block_header_processing(spec, helpers, state, block, valid=False)
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None
def test_proposer_slashed(spec, helpers, state):
def test_proposer_slashed(state):
# use stub state to get proposer index of next slot
stub_state = deepcopy(state)
helpers.next_slot(stub_state)
@ -61,5 +61,5 @@ def test_proposer_slashed(spec, helpers, state):
block = helpers.build_empty_block_for_next_slot(state)
pre_state, post_state = run_block_header_processing(spec, helpers, state, block, valid=False)
pre_state, post_state = run_block_header_processing(state, block, valid=False)
return pre_state, block, None

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.deposits
def test_success(spec, helpers, state):
def test_success(state):
pre_state = deepcopy(state)
# fill previous deposits with zero-hash
deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)
@ -38,7 +38,7 @@ def test_success(spec, helpers, state):
return pre_state, deposit, post_state
def test_success_top_up(spec, helpers, state):
def test_success_top_up(state):
pre_state = deepcopy(state)
deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)
@ -70,7 +70,7 @@ def test_success_top_up(spec, helpers, state):
return pre_state, deposit, post_state
def test_wrong_index(spec, helpers, state):
def test_wrong_index(state):
pre_state = deepcopy(state)
deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)
@ -99,7 +99,7 @@ def test_wrong_index(spec, helpers, state):
return pre_state, deposit, None
def test_bad_merkle_proof(spec, helpers, state):
def test_bad_merkle_proof(state):
pre_state = deepcopy(state)
deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.proposer_slashings
def run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, valid=True):
def run_proposer_slashing_processing(state, proposer_slashing, valid=True):
"""
Run ``spec.process_proposer_slashing`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -33,48 +33,48 @@ def run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, va
return state, post_state
def test_success(spec, helpers, state):
def test_success(state):
proposer_slashing = helpers.get_valid_proposer_slashing(state)
pre_state, post_state = run_proposer_slashing_processing(spec, helpers, state, proposer_slashing)
pre_state, post_state = run_proposer_slashing_processing(state, proposer_slashing)
return pre_state, proposer_slashing, post_state
def test_epochs_are_different(spec, helpers, state):
def test_epochs_are_different(state):
proposer_slashing = helpers.get_valid_proposer_slashing(state)
# set slots to be in different epochs
proposer_slashing.header_2.slot += spec.SLOTS_PER_EPOCH
pre_state, post_state = run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, False)
pre_state, post_state = run_proposer_slashing_processing(state, proposer_slashing, False)
return pre_state, proposer_slashing, post_state
def test_headers_are_same(spec, helpers, state):
def test_headers_are_same(state):
proposer_slashing = helpers.get_valid_proposer_slashing(state)
# set headers to be the same
proposer_slashing.header_2 = proposer_slashing.header_1
pre_state, post_state = run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, False)
pre_state, post_state = run_proposer_slashing_processing(state, proposer_slashing, False)
return pre_state, proposer_slashing, post_state
def test_proposer_is_slashed(spec, helpers, state):
def test_proposer_is_slashed(state):
proposer_slashing = helpers.get_valid_proposer_slashing(state)
# set proposer to slashed
state.validator_registry[proposer_slashing.proposer_index].slashed = True
pre_state, post_state = run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, False)
pre_state, post_state = run_proposer_slashing_processing(state, proposer_slashing, False)
return pre_state, proposer_slashing, post_state
def test_proposer_is_withdrawn(spec, helpers, state):
def test_proposer_is_withdrawn(state):
proposer_slashing = helpers.get_valid_proposer_slashing(state)
# set proposer withdrawable_epoch in past
@ -82,6 +82,6 @@ def test_proposer_is_withdrawn(spec, helpers, state):
proposer_index = proposer_slashing.proposer_index
state.validator_registry[proposer_index].withdrawable_epoch = current_epoch - 1
pre_state, post_state = run_proposer_slashing_processing(spec, helpers, state, proposer_slashing, False)
pre_state, post_state = run_proposer_slashing_processing(state, proposer_slashing, False)
return pre_state, proposer_slashing, post_state

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.transfers
def run_transfer_processing(spec, helpers, state, transfer, valid=True):
def run_transfer_processing(state, transfer, valid=True):
"""
Run ``spec.process_transfer`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -34,17 +34,17 @@ def run_transfer_processing(spec, helpers, state, transfer, valid=True):
return state, post_state
def test_success_non_activated(spec, helpers, state):
def test_success_non_activated(state):
transfer = helpers.get_valid_transfer(state)
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer)
pre_state, post_state = run_transfer_processing(state, transfer)
return pre_state, transfer, post_state
def test_success_withdrawable(spec, helpers, state):
def test_success_withdrawable(state):
helpers.next_epoch(state)
transfer = helpers.get_valid_transfer(state)
@ -52,44 +52,44 @@ def test_success_withdrawable(spec, helpers, state):
# withdrawable_epoch in past so can transfer
state.validator_registry[transfer.sender].withdrawable_epoch = spec.get_current_epoch(state) - 1
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer)
pre_state, post_state = run_transfer_processing(state, transfer)
return pre_state, transfer, post_state
def test_success_active_above_max_effective(spec, helpers, state):
def test_success_active_above_max_effective(state):
sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1]
amount = spec.MAX_EFFECTIVE_BALANCE // 32
state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE + amount
transfer = helpers.get_valid_transfer(state, sender_index=sender_index, amount=amount, fee=0)
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer)
pre_state, post_state = run_transfer_processing(state, transfer)
return pre_state, transfer, post_state
def test_active_but_transfer_past_effective_balance(spec, helpers, state):
def test_active_but_transfer_past_effective_balance(state):
sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1]
amount = spec.MAX_EFFECTIVE_BALANCE // 32
state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE
transfer = helpers.get_valid_transfer(state, sender_index=sender_index, amount=amount, fee=0)
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer, False)
pre_state, post_state = run_transfer_processing(state, transfer, False)
return pre_state, transfer, post_state
def test_incorrect_slot(spec, helpers, state):
def test_incorrect_slot(state):
transfer = helpers.get_valid_transfer(state, slot=state.slot+1)
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer, False)
pre_state, post_state = run_transfer_processing(state, transfer, False)
return pre_state, transfer, post_state
def test_insufficient_balance(spec, helpers, state):
def test_insufficient_balance(state):
sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1]
amount = spec.MAX_EFFECTIVE_BALANCE
state.balances[sender_index] = spec.MAX_EFFECTIVE_BALANCE
@ -98,12 +98,12 @@ def test_insufficient_balance(spec, helpers, state):
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer, False)
pre_state, post_state = run_transfer_processing(state, transfer, False)
return pre_state, transfer, post_state
def test_no_dust(spec, helpers, state):
def test_no_dust(state):
sender_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1]
balance = state.balances[sender_index]
transfer = helpers.get_valid_transfer(state, sender_index=sender_index, amount=balance - spec.MIN_DEPOSIT_AMOUNT + 1, fee=0)
@ -111,18 +111,18 @@ def test_no_dust(spec, helpers, state):
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer, False)
pre_state, post_state = run_transfer_processing(state, transfer, False)
return pre_state, transfer, post_state
def test_invalid_pubkey(spec, helpers, state):
def test_invalid_pubkey(state):
transfer = helpers.get_valid_transfer(state)
state.validator_registry[transfer.sender].withdrawal_credentials = spec.ZERO_HASH
# un-activate so validator can transfer
state.validator_registry[transfer.sender].activation_epoch = spec.FAR_FUTURE_EPOCH
pre_state, post_state = run_transfer_processing(spec, helpers, state, transfer, False)
pre_state, post_state = run_transfer_processing(state, transfer, False)
return pre_state, transfer, post_state

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.voluntary_exits
def run_voluntary_exit_processing(spec, helpers, state, voluntary_exit, valid=True):
def run_voluntary_exit_processing(state, voluntary_exit, valid=True):
"""
Run ``spec.process_voluntary_exit`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -27,7 +27,7 @@ def run_voluntary_exit_processing(spec, helpers, state, voluntary_exit, valid=Tr
return state, post_state
def test_success(spec, helpers, state):
def test_success(state):
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
@ -42,11 +42,11 @@ def test_success(spec, helpers, state):
privkey,
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, state, voluntary_exit)
pre_state, post_state = run_voluntary_exit_processing(state, voluntary_exit)
return pre_state, voluntary_exit, post_state
def test_success_exit_queue(spec, helpers, state):
def test_success_exit_queue(state):
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
@ -64,7 +64,7 @@ def test_success_exit_queue(spec, helpers, state):
privkey,
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, post_state, voluntary_exit)
pre_state, post_state = run_voluntary_exit_processing(post_state, voluntary_exit)
# exit an additional validator
validator_index = spec.get_active_validator_indices(state, current_epoch)[-1]
@ -76,7 +76,7 @@ def test_success_exit_queue(spec, helpers, state):
privkey,
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, post_state, voluntary_exit)
pre_state, post_state = run_voluntary_exit_processing(post_state, voluntary_exit)
assert (
post_state.validator_registry[validator_index].exit_epoch ==
@ -86,7 +86,7 @@ def test_success_exit_queue(spec, helpers, state):
return pre_state, voluntary_exit, post_state
def test_validator_not_active(spec, helpers, state):
def test_validator_not_active(state):
current_epoch = spec.get_current_epoch(state)
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
privkey = helpers.pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
@ -103,11 +103,11 @@ def test_validator_not_active(spec, helpers, state):
privkey,
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, state, voluntary_exit, False)
pre_state, post_state = run_voluntary_exit_processing(state, voluntary_exit, False)
return pre_state, voluntary_exit, post_state
def test_validator_already_exited(spec, helpers, state):
def test_validator_already_exited(state):
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow validator able to exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
@ -125,11 +125,11 @@ def test_validator_already_exited(spec, helpers, state):
privkey,
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, state, voluntary_exit, False)
pre_state, post_state = run_voluntary_exit_processing(state, voluntary_exit, False)
return pre_state, voluntary_exit, post_state
def test_validator_not_active_long_enough(spec, helpers, state):
def test_validator_not_active_long_enough(state):
current_epoch = spec.get_current_epoch(state)
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
privkey = helpers.pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
@ -146,5 +146,5 @@ def test_validator_not_active_long_enough(spec, helpers, state):
spec.PERSISTENT_COMMITTEE_PERIOD
)
pre_state, post_state = run_voluntary_exit_processing(spec, helpers, state, voluntary_exit, False)
pre_state, post_state = run_voluntary_exit_processing(state, voluntary_exit, False)
return pre_state, voluntary_exit, post_state

View File

@ -1,10 +1,10 @@
import pytest
from eth2spec.phase0 import spec as _spec
from eth2spec.phase0 import spec
from preset_loader import loader
from tests.phase0 import helpers as _helpers
from tests.phase0 import helpers
from tests.phase0 import test_finality
def pytest_addoption(parser):
parser.addoption(
@ -13,13 +13,15 @@ def pytest_addoption(parser):
@pytest.fixture(autouse=True)
def config(request):
request.function.__globals__['spec'] = spec
request.function.__globals__['helpers'] = helpers
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
return spec.SLOTS_PER_EPOCH * 8
@pytest.fixture
def deposit_data_leaves():
@ -27,13 +29,4 @@ def deposit_data_leaves():
@pytest.fixture
def 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
return helpers.create_genesis_state(num_validators, deposit_data_leaves)

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.crosslinks
def run_process_crosslinks(spec, helpers, state, valid=True):
def run_process_crosslinks(state, valid=True):
# transition state to slot before state transition
slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) - 1
block = helpers.build_empty_block_for_next_slot(state)
@ -22,8 +22,8 @@ def run_process_crosslinks(spec, helpers, state, valid=True):
return state, post_state
def test_no_attestations(spec, helpers, state):
pre_state, post_state = run_process_crosslinks(spec, helpers, state)
def test_no_attestations(state):
pre_state, post_state = run_process_crosslinks(state)
for shard in range(spec.SHARD_COUNT):
assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard]
@ -31,7 +31,7 @@ def test_no_attestations(spec, helpers, state):
return pre_state, post_state
def test_single_crosslink_update_from_current_epoch(spec, helpers, state):
def test_single_crosslink_update_from_current_epoch(state):
helpers.next_epoch(state)
attestation = helpers.get_valid_attestation(state)
@ -41,7 +41,7 @@ def test_single_crosslink_update_from_current_epoch(spec, helpers, state):
assert len(state.current_epoch_attestations) == 1
pre_state, post_state = run_process_crosslinks(spec, helpers, state)
pre_state, post_state = run_process_crosslinks(state)
shard = attestation.data.crosslink.shard
assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard]
@ -50,7 +50,7 @@ def test_single_crosslink_update_from_current_epoch(spec, helpers, state):
return pre_state, post_state
def test_single_crosslink_update_from_previous_epoch(spec, helpers, state):
def test_single_crosslink_update_from_previous_epoch(state):
helpers.next_epoch(state)
attestation = helpers.get_valid_attestation(state)
@ -60,7 +60,7 @@ def test_single_crosslink_update_from_previous_epoch(spec, helpers, state):
assert len(state.previous_epoch_attestations) == 1
pre_state, post_state = run_process_crosslinks(spec, helpers, state)
pre_state, post_state = run_process_crosslinks(state)
crosslink_deltas = spec.get_crosslink_deltas(state)
shard = attestation.data.crosslink.shard
@ -74,7 +74,7 @@ def test_single_crosslink_update_from_previous_epoch(spec, helpers, state):
return pre_state, post_state
def test_double_late_crosslink(spec, helpers, state):
def test_double_late_crosslink(state):
helpers.next_epoch(state)
state.slot += 4
@ -100,7 +100,7 @@ def test_double_late_crosslink(spec, helpers, state):
assert len(state.previous_epoch_attestations) == 1
assert len(state.current_epoch_attestations) == 0
pre_state, post_state = run_process_crosslinks(spec, helpers, state)
pre_state, post_state = run_process_crosslinks(state)
crosslink_deltas = spec.get_crosslink_deltas(state)
shard = attestation_2.data.crosslink.shard

View File

@ -6,7 +6,7 @@ import pytest
pytestmark = pytest.mark.state
def test_activation(spec, helpers, state):
def test_activation(state):
index = 0
assert spec.is_active_validator(state.validator_registry[index], spec.get_current_epoch(state))
@ -33,7 +33,7 @@ def test_activation(spec, helpers, state):
return pre_state, blocks, state
def test_ejection(spec, helpers, state):
def test_ejection(state):
index = 0
assert spec.is_active_validator(state.validator_registry[index], spec.get_current_epoch(state))
assert state.validator_registry[index].exit_epoch == spec.FAR_FUTURE_EPOCH

View File

@ -33,7 +33,7 @@ def check_finality(state,
assert state.finalized_root == prev_state.finalized_root
def next_epoch_with_attestations(spec, helpers, state,
def next_epoch_with_attestations(state,
fill_cur_epoch,
fill_prev_epoch):
post_state = deepcopy(state)
@ -59,12 +59,12 @@ def next_epoch_with_attestations(spec, helpers, state,
return state, blocks, post_state
def test_finality_rule_4(spec, helpers, state):
def test_finality_rule_4(state):
test_state = deepcopy(state)
blocks = []
for epoch in range(4):
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, True, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
blocks += new_blocks
# justification/finalization skipped at GENESIS_EPOCH
@ -84,7 +84,7 @@ def test_finality_rule_4(spec, helpers, state):
return state, blocks, test_state
def test_finality_rule_1(spec, helpers, state):
def test_finality_rule_1(state):
# get past first two epochs that finality does not run on
helpers.next_epoch(state)
helpers.next_epoch(state)
@ -94,7 +94,7 @@ def test_finality_rule_1(spec, helpers, state):
blocks = []
for epoch in range(3):
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, False, True)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
blocks += new_blocks
if epoch == 0:
@ -110,7 +110,7 @@ def test_finality_rule_1(spec, helpers, state):
return pre_state, blocks, test_state
def test_finality_rule_2(spec, helpers, state):
def test_finality_rule_2(state):
# get past first two epochs that finality does not run on
helpers.next_epoch(state)
helpers.next_epoch(state)
@ -121,13 +121,13 @@ def test_finality_rule_2(spec, helpers, state):
blocks = []
for epoch in range(3):
if epoch == 0:
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, True, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
check_finality(test_state, prev_state, True, False, False)
elif epoch == 1:
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, False, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, False)
check_finality(test_state, prev_state, False, True, False)
elif epoch == 2:
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, False, True)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
# finalized by rule 2
check_finality(test_state, prev_state, True, False, True)
assert test_state.finalized_epoch == prev_state.previous_justified_epoch
@ -138,7 +138,7 @@ def test_finality_rule_2(spec, helpers, state):
return pre_state, blocks, test_state
def test_finality_rule_3(spec, helpers, state):
def test_finality_rule_3(state):
"""
Test scenario described here
https://github.com/ethereum/eth2.0-specs/issues/611#issuecomment-463612892
@ -152,29 +152,29 @@ def test_finality_rule_3(spec, helpers, state):
test_state = deepcopy(state)
blocks = []
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, True, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
blocks += new_blocks
check_finality(test_state, prev_state, True, False, False)
# In epoch N, JE is set to N, prev JE is set to N-1
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, True, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, False)
blocks += new_blocks
check_finality(test_state, prev_state, True, True, True)
# In epoch N+1, JE is N, prev JE is N-1, and not enough messages get in to do anything
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, False, False)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, False)
blocks += new_blocks
check_finality(test_state, prev_state, False, True, False)
# In epoch N+2, JE is N, prev JE is N, and enough messages from the previous epoch get in to justify N+1.
# N+1 now becomes the JE. Not enough messages from epoch N+2 itself get in to justify N+2
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, False, True)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, False, True)
blocks += new_blocks
# rule 2
check_finality(test_state, prev_state, True, False, True)
# In epoch N+3, LJE is N+1, prev LJE is N, and enough messages get in to justify epochs N+2 and N+3.
prev_state, new_blocks, test_state = next_epoch_with_attestations(spec, helpers, test_state, True, True)
prev_state, new_blocks, test_state = next_epoch_with_attestations(test_state, True, True)
blocks += new_blocks
# rule 3
check_finality(test_state, prev_state, True, True, True)

View File

@ -17,7 +17,7 @@ from eth2spec.utils.merkle_minimal import (
pytestmark = pytest.mark.sanity
def test_slot_transition(spec, helpers, state):
def test_slot_transition(state):
test_state = deepcopy(state)
spec.process_slot(test_state)
helpers.advance_slot(test_state)
@ -26,7 +26,7 @@ def test_slot_transition(spec, helpers, state):
return test_state
def test_empty_block_transition(spec, helpers, state):
def test_empty_block_transition(state):
test_state = deepcopy(state)
block = helpers.build_empty_block_for_next_slot(test_state)
@ -38,7 +38,7 @@ def test_empty_block_transition(spec, helpers, state):
return state, [block], test_state
def test_skipped_slots(spec, helpers, state):
def test_skipped_slots(state):
test_state = deepcopy(state)
block = helpers.build_empty_block_for_next_slot(test_state)
block.slot += 3
@ -52,7 +52,7 @@ def test_skipped_slots(spec, helpers, state):
return state, [block], test_state
def test_empty_epoch_transition(spec, helpers, state):
def test_empty_epoch_transition(state):
test_state = deepcopy(state)
block = helpers.build_empty_block_for_next_slot(test_state)
block.slot += spec.SLOTS_PER_EPOCH
@ -66,7 +66,7 @@ def test_empty_epoch_transition(spec, helpers, state):
return state, [block], test_state
def test_empty_epoch_transition_not_finalizing(spec, helpers, state):
def test_empty_epoch_transition_not_finalizing(state):
test_state = deepcopy(state)
block = helpers.build_empty_block_for_next_slot(test_state)
block.slot += spec.SLOTS_PER_EPOCH * 5
@ -81,7 +81,7 @@ def test_empty_epoch_transition_not_finalizing(spec, helpers, state):
return state, [block], test_state
def test_proposer_slashing(spec, helpers, state):
def test_proposer_slashing(state):
test_state = deepcopy(state)
proposer_slashing = helpers.get_valid_proposer_slashing(state)
validator_index = proposer_slashing.proposer_index
@ -105,7 +105,7 @@ def test_proposer_slashing(spec, helpers, state):
return state, [block], test_state
def test_attester_slashing(spec, helpers, state):
def test_attester_slashing(state):
test_state = deepcopy(state)
attester_slashing = helpers.get_valid_attester_slashing(state)
validator_index = attester_slashing.attestation_1.custody_bit_0_indices[0]
@ -136,7 +136,7 @@ def test_attester_slashing(spec, helpers, state):
return state, [block], test_state
def test_deposit_in_block(spec, helpers, state):
def test_deposit_in_block(state):
pre_state = deepcopy(state)
test_deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)
@ -173,7 +173,7 @@ def test_deposit_in_block(spec, helpers, state):
return pre_state, [block], post_state
def test_deposit_top_up(spec, helpers, state):
def test_deposit_top_up(state):
pre_state = deepcopy(state)
test_deposit_data_leaves = [spec.ZERO_HASH] * len(pre_state.validator_registry)
@ -212,7 +212,7 @@ def test_deposit_top_up(spec, helpers, state):
return pre_state, [block], post_state
def test_attestation(spec, helpers, state):
def test_attestation(state):
state.slot = spec.SLOTS_PER_EPOCH
test_state = deepcopy(state)
attestation = helpers.get_valid_attestation(state)
@ -243,7 +243,7 @@ def test_attestation(spec, helpers, state):
return state, [attestation_block, epoch_block], test_state
def test_voluntary_exit(spec, helpers, state):
def test_voluntary_exit(state):
pre_state = deepcopy(state)
validator_index = spec.get_active_validator_indices(
pre_state,
@ -289,7 +289,7 @@ def test_voluntary_exit(spec, helpers, state):
return pre_state, [initiate_exit_block, exit_block], post_state
def test_transfer(spec, helpers, state):
def test_transfer(state):
# overwrite default 0 to test
spec.apply_constants_preset({"MAX_TRANSFERS": 1})
@ -341,7 +341,7 @@ def test_transfer(spec, helpers, state):
return pre_state, [block], post_state
def test_balance_driven_status_transitions(spec, helpers, state):
def test_balance_driven_status_transitions(state):
current_epoch = spec.get_current_epoch(state)
validator_index = spec.get_active_validator_indices(state, current_epoch)[-1]
@ -363,7 +363,7 @@ def test_balance_driven_status_transitions(spec, helpers, state):
return state, [block], post_state
def test_historical_batch(spec, helpers, state):
def test_historical_batch(state):
state.slot += spec.SLOTS_PER_HISTORICAL_ROOT - (state.slot % spec.SLOTS_PER_HISTORICAL_ROOT) - 1
post_state = deepcopy(state)
@ -379,7 +379,7 @@ def test_historical_batch(spec, helpers, state):
return state, [block], post_state
def test_eth1_data_votes(spec, helpers, state):
def test_eth1_data_votes(state):
post_state = deepcopy(state)
expected_votes = 0

View File

@ -4,7 +4,7 @@ import pytest
#mark entire file as 'randao_key_reveals'
pytestmark = pytest.mark.randao_key_reveals
def terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, valid=True):
def terun_early_derived_secret_reveal_processing(state, randao_key_reveal, valid=True):
"""
Run ``process_randao_key_reveal`` returning the pre and post state.
If ``valid == False``, run expecting ``AssertionError``
@ -34,63 +34,63 @@ def terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_ke
return state, post_state
def test_success(spec, helpers, state):
def test_success(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state)
pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal)
return pre_state, randao_key_reveal, post_state
def test_reveal_from_current_epoch(spec, helpers, state):
def test_reveal_from_current_epoch(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state))
pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False)
pre_state, post_state = terun_early_derived_secret_reveal_processing(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(spec, helpers, state):
def test_reveal_from_past_epoch(state):
randao_key_reveal = helpers.get_valid_early_derived_secret_reveal(state, spec.get_current_epoch(state) - 1)
pre_state, post_state = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding(spec, helpers, state):
def test_reveal_with_custody_padding(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)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
return pre_state, randao_key_reveal, post_state
def test_reveal_with_custody_padding_minus_one(spec, helpers, state):
def test_reveal_with_custody_padding_minus_one(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)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, True)
return pre_state, randao_key_reveal, post_state
def test_double_reveal(spec, helpers, state):
def test_double_reveal(state):
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)
pre_state, intermediate_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal1)
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)
_, post_state = terun_early_derived_secret_reveal_processing(intermediate_state, randao_key_reveal2, False)
return pre_state, [randao_key_reveal1, randao_key_reveal2], post_state
def test_revealer_is_slashed(spec, helpers, state):
def test_revealer_is_slashed(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 = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state
def test_far_future_epoch(spec, helpers, state):
def test_far_future_epoch(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 = terun_early_derived_secret_reveal_processing(spec, helpers, state, randao_key_reveal, False)
pre_state, post_state = terun_early_derived_secret_reveal_processing(state, randao_key_reveal, False)
return pre_state, randao_key_reveal, post_state

View File

@ -1,9 +1,9 @@
import pytest
from eth2spec.phase1 import spec as _spec
from eth2spec.phase1 import spec
from preset_loader import loader
from tests.phase1 import helpers as _helpers
from tests.phase1 import helpers as helpers
from tests.phase0.conftest import (
pytest_addoption,
@ -13,23 +13,17 @@ from tests.phase0.conftest import (
# This is redfined so that the constants are re-applied
@pytest.fixture(autouse=True)
def config(request):
request.function.__globals__['spec'] = spec
request.function.__globals__['helpers'] = helpers
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
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 _helpers.create_genesis_state(num_validators, deposit_data_leaves)
@pytest.fixture
def spec():
return _spec
@pytest.fixture
def helpers():
return _helpers
return helpers.create_genesis_state(num_validators, deposit_data_leaves)