updat epoch processing tests

This commit is contained in:
protolambda 2019-05-06 17:10:43 +02:00
parent 922a30a619
commit 1722d58067
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
11 changed files with 61 additions and 59 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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