From 1722d5806763ccd286b640250cc57de4b417e0de Mon Sep 17 00:00:00 2001 From: protolambda Date: Mon, 6 May 2019 17:10:43 +0200 Subject: [PATCH] updat epoch processing tests --- .../block_processing/block_test_helpers.py | 8 --- .../test_process_attestation.py | 2 +- .../test_process_attester_slashing.py | 5 +- .../test_process_block_header.py | 2 +- .../block_processing/test_process_deposit.py | 2 +- .../test_process_proposer_slashing.py | 2 +- .../block_processing/test_process_transfer.py | 2 +- .../block_processing/test_voluntary_exit.py | 3 +- test_libs/pyspec/tests/context.py | 7 +++ .../test_process_crosslinks.py | 60 ++++++++++--------- .../test_process_registry_updates.py | 27 +++++---- 11 files changed, 61 insertions(+), 59 deletions(-) delete mode 100644 test_libs/pyspec/tests/block_processing/block_test_helpers.py diff --git a/test_libs/pyspec/tests/block_processing/block_test_helpers.py b/test_libs/pyspec/tests/block_processing/block_test_helpers.py deleted file mode 100644 index 71b9fc250..000000000 --- a/test_libs/pyspec/tests/block_processing/block_test_helpers.py +++ /dev/null @@ -1,8 +0,0 @@ - -from tests.utils import spectest -from tests.context import with_state - - -# shorthand for decorating @with_state @spectest() -def spec_state_test(fn): - return with_state(spectest()(fn)) diff --git a/test_libs/pyspec/tests/block_processing/test_process_attestation.py b/test_libs/pyspec/tests/block_processing/test_process_attestation.py index 41e9419b3..8b43416a6 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_attestation.py +++ b/test_libs/pyspec/tests/block_processing/test_process_attestation.py @@ -17,7 +17,7 @@ from tests.helpers import ( next_slot, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def run_attestation_processing(state, attestation, valid=True): diff --git a/test_libs/pyspec/tests/block_processing/test_process_attester_slashing.py b/test_libs/pyspec/tests/block_processing/test_process_attester_slashing.py index 631484b12..4a19fccb3 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_attester_slashing.py +++ b/test_libs/pyspec/tests/block_processing/test_process_attester_slashing.py @@ -11,10 +11,7 @@ from tests.helpers import ( next_epoch, ) -from tests.utils import spectest -from tests.context import with_state - -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def run_attester_slashing_processing(state, attester_slashing, valid=True): diff --git a/test_libs/pyspec/tests/block_processing/test_process_block_header.py b/test_libs/pyspec/tests/block_processing/test_process_block_header.py index 27058d28c..199f2bda4 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_block_header.py +++ b/test_libs/pyspec/tests/block_processing/test_process_block_header.py @@ -13,7 +13,7 @@ from tests.helpers import ( next_slot, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def prepare_state_for_header_processing(state): diff --git a/test_libs/pyspec/tests/block_processing/test_process_deposit.py b/test_libs/pyspec/tests/block_processing/test_process_deposit.py index c45191020..db1738acc 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/tests/block_processing/test_process_deposit.py @@ -13,7 +13,7 @@ from tests.helpers import ( pubkeys, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def prepare_state_and_deposit(state, validator_index, amount): diff --git a/test_libs/pyspec/tests/block_processing/test_process_proposer_slashing.py b/test_libs/pyspec/tests/block_processing/test_process_proposer_slashing.py index fbf4a1959..5a16c7d6c 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_proposer_slashing.py +++ b/test_libs/pyspec/tests/block_processing/test_process_proposer_slashing.py @@ -10,7 +10,7 @@ from tests.helpers import ( get_valid_proposer_slashing, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def run_proposer_slashing_processing(state, proposer_slashing, valid=True): diff --git a/test_libs/pyspec/tests/block_processing/test_process_transfer.py b/test_libs/pyspec/tests/block_processing/test_process_transfer.py index 920c26f59..8fc4d1fc4 100644 --- a/test_libs/pyspec/tests/block_processing/test_process_transfer.py +++ b/test_libs/pyspec/tests/block_processing/test_process_transfer.py @@ -13,7 +13,7 @@ from tests.helpers import ( next_epoch, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def run_transfer_processing(state, transfer, valid=True): diff --git a/test_libs/pyspec/tests/block_processing/test_voluntary_exit.py b/test_libs/pyspec/tests/block_processing/test_voluntary_exit.py index 3e1ae4f7f..566ebf028 100644 --- a/test_libs/pyspec/tests/block_processing/test_voluntary_exit.py +++ b/test_libs/pyspec/tests/block_processing/test_voluntary_exit.py @@ -1,4 +1,3 @@ -from copy import deepcopy import pytest import eth2spec.phase0.spec as spec @@ -14,7 +13,7 @@ from tests.helpers import ( pubkey_to_privkey, ) -from .block_test_helpers import spec_state_test +from tests.context import spec_state_test def run_voluntary_exit_processing(state, voluntary_exit, valid=True): diff --git a/test_libs/pyspec/tests/context.py b/test_libs/pyspec/tests/context.py index 27a91a031..d78acdc0b 100644 --- a/test_libs/pyspec/tests/context.py +++ b/test_libs/pyspec/tests/context.py @@ -6,5 +6,12 @@ from .helpers import ( create_genesis_state, ) +from tests.utils import spectest + # Provides a genesis state as first argument to the function decorated with this with_state = with_args(lambda: [create_genesis_state(spec.SLOTS_PER_EPOCH * 8, list())]) + + +# shorthand for decorating @with_state @spectest() +def spec_state_test(fn): + return with_state(spectest()(fn)) diff --git a/test_libs/pyspec/tests/epoch_processing/test_process_crosslinks.py b/test_libs/pyspec/tests/epoch_processing/test_process_crosslinks.py index d6765e3a7..10de064ed 100644 --- a/test_libs/pyspec/tests/epoch_processing/test_process_crosslinks.py +++ b/test_libs/pyspec/tests/epoch_processing/test_process_crosslinks.py @@ -1,5 +1,4 @@ from copy import deepcopy -import pytest import eth2spec.phase0.spec as spec @@ -19,15 +18,18 @@ from tests.helpers import ( get_valid_attestation, next_epoch, next_slot, - set_bitfield_bit, ) - -# mark entire file as 'crosslinks' -pytestmark = pytest.mark.crosslinks +from tests.context import spec_state_test def run_process_crosslinks(state, valid=True): + """ + Run ``process_crosslinks``, yielding: + - pre-state ('pre') + - post-state ('post'). + If ``valid == False``, run expecting ``AssertionError`` + """ # transition state to slot before state transition slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) - 1 block = build_empty_block_for_next_slot(state) @@ -37,21 +39,20 @@ def run_process_crosslinks(state, valid=True): # cache state before epoch transition cache_state(state) - post_state = deepcopy(state) - process_crosslinks(post_state) - - return state, post_state + yield 'pre', state + process_crosslinks(state) + yield 'post', state +@spec_state_test def test_no_attestations(state): - pre_state, post_state = run_process_crosslinks(state) + yield from run_process_crosslinks(state) for shard in range(spec.SHARD_COUNT): - assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard] - - return pre_state, post_state + assert state.previous_crosslinks[shard] == state.current_crosslinks[shard] +@spec_state_test def test_single_crosslink_update_from_current_epoch(state): next_epoch(state) @@ -62,15 +63,16 @@ def test_single_crosslink_update_from_current_epoch(state): assert len(state.current_epoch_attestations) == 1 - pre_state, post_state = run_process_crosslinks(state) - shard = attestation.data.shard - assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard] - assert pre_state.current_crosslinks[shard] != post_state.current_crosslinks[shard] + pre_crosslink = deepcopy(state.current_crosslinks[shard]) - return pre_state, post_state + yield from run_process_crosslinks(state) + + assert state.previous_crosslinks[shard] != state.current_crosslinks[shard] + assert pre_crosslink != state.current_crosslinks[shard] +@spec_state_test def test_single_crosslink_update_from_previous_epoch(state): next_epoch(state) @@ -81,20 +83,23 @@ def test_single_crosslink_update_from_previous_epoch(state): assert len(state.previous_epoch_attestations) == 1 - pre_state, post_state = run_process_crosslinks(state) + shard = attestation.data.shard + pre_crosslink = deepcopy(state.current_crosslinks[shard]) + crosslink_deltas = get_crosslink_deltas(state) - shard = attestation.data.shard - assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard] - assert pre_state.current_crosslinks[shard] != post_state.current_crosslinks[shard] + yield from run_process_crosslinks(state) + + assert state.previous_crosslinks[shard] != state.current_crosslinks[shard] + assert pre_crosslink != state.current_crosslinks[shard] + # ensure rewarded for index in get_crosslink_committee(state, attestation.data.target_epoch, attestation.data.shard): assert crosslink_deltas[0][index] > 0 assert crosslink_deltas[1][index] == 0 - return pre_state, post_state - +@spec_state_test def test_double_late_crosslink(state): next_epoch(state) state.slot += 4 @@ -121,16 +126,15 @@ def test_double_late_crosslink(state): assert len(state.previous_epoch_attestations) == 1 assert len(state.current_epoch_attestations) == 0 - pre_state, post_state = run_process_crosslinks(state) crosslink_deltas = get_crosslink_deltas(state) + + yield from run_process_crosslinks(state) shard = attestation_2.data.shard # ensure that the current crosslinks were not updated by the second attestation - assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard] + assert state.previous_crosslinks[shard] == state.current_crosslinks[shard] # ensure no reward, only penalties for the failed crosslink for index in get_crosslink_committee(state, attestation_2.data.target_epoch, attestation_2.data.shard): assert crosslink_deltas[0][index] == 0 assert crosslink_deltas[1][index] > 0 - - return pre_state, post_state diff --git a/test_libs/pyspec/tests/epoch_processing/test_process_registry_updates.py b/test_libs/pyspec/tests/epoch_processing/test_process_registry_updates.py index 11f5de2ad..85d0d07f4 100644 --- a/test_libs/pyspec/tests/epoch_processing/test_process_registry_updates.py +++ b/test_libs/pyspec/tests/epoch_processing/test_process_registry_updates.py @@ -1,7 +1,3 @@ -from copy import deepcopy - -import pytest - import eth2spec.phase0.spec as spec from eth2spec.phase0.spec import ( @@ -12,10 +8,10 @@ from tests.helpers import ( next_epoch, ) -# mark entire file as 'state' -pytestmark = pytest.mark.state +from tests.context import spec_state_test +@spec_state_test def test_activation(state): index = 0 assert is_active_validator(state.validator_registry[index], get_current_epoch(state)) @@ -26,13 +22,18 @@ def test_activation(state): state.validator_registry[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE assert not is_active_validator(state.validator_registry[index], get_current_epoch(state)) - pre_state = deepcopy(state) + yield 'pre', state blocks = [] for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): block = next_epoch(state) blocks.append(block) + # provide extra type hinting here, since it is wrapped in a list. + yield 'blocks', blocks, [spec.BeaconBlock] + + yield 'post', state + assert state.validator_registry[index].activation_eligibility_epoch != spec.FAR_FUTURE_EPOCH assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH assert is_active_validator( @@ -40,9 +41,8 @@ def test_activation(state): get_current_epoch(state), ) - return pre_state, blocks, state - +@spec_state_test def test_ejection(state): index = 0 assert is_active_validator(state.validator_registry[index], get_current_epoch(state)) @@ -51,17 +51,20 @@ def test_ejection(state): # Mock an ejection state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE - pre_state = deepcopy(state) + yield 'pre', state blocks = [] for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): block = next_epoch(state) blocks.append(block) + # provide extra type hinting here, since it is wrapped in a list. + yield 'blocks', blocks, [spec.BeaconBlock] + + yield 'post', state + assert state.validator_registry[index].exit_epoch != spec.FAR_FUTURE_EPOCH assert not is_active_validator( state.validator_registry[index], get_current_epoch(state), ) - - return pre_state, blocks, state