mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-27 19:05:00 +00:00
updat epoch processing tests
This commit is contained in:
parent
922a30a619
commit
1722d58067
@ -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))
|
|
@ -17,7 +17,7 @@ from tests.helpers import (
|
|||||||
next_slot,
|
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):
|
def run_attestation_processing(state, attestation, valid=True):
|
||||||
|
@ -11,10 +11,7 @@ from tests.helpers import (
|
|||||||
next_epoch,
|
next_epoch,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.utils import spectest
|
from tests.context import spec_state_test
|
||||||
from tests.context import with_state
|
|
||||||
|
|
||||||
from .block_test_helpers import spec_state_test
|
|
||||||
|
|
||||||
|
|
||||||
def run_attester_slashing_processing(state, attester_slashing, valid=True):
|
def run_attester_slashing_processing(state, attester_slashing, valid=True):
|
||||||
|
@ -13,7 +13,7 @@ from tests.helpers import (
|
|||||||
next_slot,
|
next_slot,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .block_test_helpers import spec_state_test
|
from tests.context import spec_state_test
|
||||||
|
|
||||||
|
|
||||||
def prepare_state_for_header_processing(state):
|
def prepare_state_for_header_processing(state):
|
||||||
|
@ -13,7 +13,7 @@ from tests.helpers import (
|
|||||||
pubkeys,
|
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):
|
def prepare_state_and_deposit(state, validator_index, amount):
|
||||||
|
@ -10,7 +10,7 @@ from tests.helpers import (
|
|||||||
get_valid_proposer_slashing,
|
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):
|
def run_proposer_slashing_processing(state, proposer_slashing, valid=True):
|
||||||
|
@ -13,7 +13,7 @@ from tests.helpers import (
|
|||||||
next_epoch,
|
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):
|
def run_transfer_processing(state, transfer, valid=True):
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
from copy import deepcopy
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import eth2spec.phase0.spec as spec
|
import eth2spec.phase0.spec as spec
|
||||||
@ -14,7 +13,7 @@ from tests.helpers import (
|
|||||||
pubkey_to_privkey,
|
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):
|
def run_voluntary_exit_processing(state, voluntary_exit, valid=True):
|
||||||
|
@ -6,5 +6,12 @@ from .helpers import (
|
|||||||
create_genesis_state,
|
create_genesis_state,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.utils import spectest
|
||||||
|
|
||||||
# Provides a genesis state as first argument to the function decorated with this
|
# 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())])
|
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))
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
import pytest
|
|
||||||
|
|
||||||
import eth2spec.phase0.spec as spec
|
import eth2spec.phase0.spec as spec
|
||||||
|
|
||||||
@ -19,15 +18,18 @@ from tests.helpers import (
|
|||||||
get_valid_attestation,
|
get_valid_attestation,
|
||||||
next_epoch,
|
next_epoch,
|
||||||
next_slot,
|
next_slot,
|
||||||
set_bitfield_bit,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.context import spec_state_test
|
||||||
# mark entire file as 'crosslinks'
|
|
||||||
pytestmark = pytest.mark.crosslinks
|
|
||||||
|
|
||||||
|
|
||||||
def run_process_crosslinks(state, valid=True):
|
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
|
# transition state to slot before state transition
|
||||||
slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) - 1
|
slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) - 1
|
||||||
block = build_empty_block_for_next_slot(state)
|
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 before epoch transition
|
||||||
cache_state(state)
|
cache_state(state)
|
||||||
|
|
||||||
post_state = deepcopy(state)
|
yield 'pre', state
|
||||||
process_crosslinks(post_state)
|
process_crosslinks(state)
|
||||||
|
yield 'post', state
|
||||||
return state, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_no_attestations(state):
|
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):
|
for shard in range(spec.SHARD_COUNT):
|
||||||
assert post_state.previous_crosslinks[shard] == post_state.current_crosslinks[shard]
|
assert state.previous_crosslinks[shard] == state.current_crosslinks[shard]
|
||||||
|
|
||||||
return pre_state, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_single_crosslink_update_from_current_epoch(state):
|
def test_single_crosslink_update_from_current_epoch(state):
|
||||||
next_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
|
assert len(state.current_epoch_attestations) == 1
|
||||||
|
|
||||||
pre_state, post_state = run_process_crosslinks(state)
|
|
||||||
|
|
||||||
shard = attestation.data.shard
|
shard = attestation.data.shard
|
||||||
assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard]
|
pre_crosslink = deepcopy(state.current_crosslinks[shard])
|
||||||
assert pre_state.current_crosslinks[shard] != post_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):
|
def test_single_crosslink_update_from_previous_epoch(state):
|
||||||
next_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
|
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)
|
crosslink_deltas = get_crosslink_deltas(state)
|
||||||
|
|
||||||
shard = attestation.data.shard
|
yield from run_process_crosslinks(state)
|
||||||
assert post_state.previous_crosslinks[shard] != post_state.current_crosslinks[shard]
|
|
||||||
assert pre_state.current_crosslinks[shard] != post_state.current_crosslinks[shard]
|
assert state.previous_crosslinks[shard] != state.current_crosslinks[shard]
|
||||||
|
assert pre_crosslink != state.current_crosslinks[shard]
|
||||||
|
|
||||||
# ensure rewarded
|
# ensure rewarded
|
||||||
for index in get_crosslink_committee(state, attestation.data.target_epoch, attestation.data.shard):
|
for index in get_crosslink_committee(state, attestation.data.target_epoch, attestation.data.shard):
|
||||||
assert crosslink_deltas[0][index] > 0
|
assert crosslink_deltas[0][index] > 0
|
||||||
assert crosslink_deltas[1][index] == 0
|
assert crosslink_deltas[1][index] == 0
|
||||||
|
|
||||||
return pre_state, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_double_late_crosslink(state):
|
def test_double_late_crosslink(state):
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
state.slot += 4
|
state.slot += 4
|
||||||
@ -121,16 +126,15 @@ def test_double_late_crosslink(state):
|
|||||||
assert len(state.previous_epoch_attestations) == 1
|
assert len(state.previous_epoch_attestations) == 1
|
||||||
assert len(state.current_epoch_attestations) == 0
|
assert len(state.current_epoch_attestations) == 0
|
||||||
|
|
||||||
pre_state, post_state = run_process_crosslinks(state)
|
|
||||||
crosslink_deltas = get_crosslink_deltas(state)
|
crosslink_deltas = get_crosslink_deltas(state)
|
||||||
|
|
||||||
|
yield from run_process_crosslinks(state)
|
||||||
|
|
||||||
shard = attestation_2.data.shard
|
shard = attestation_2.data.shard
|
||||||
|
|
||||||
# ensure that the current crosslinks were not updated by the second attestation
|
# 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
|
# 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):
|
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[0][index] == 0
|
||||||
assert crosslink_deltas[1][index] > 0
|
assert crosslink_deltas[1][index] > 0
|
||||||
|
|
||||||
return pre_state, post_state
|
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import eth2spec.phase0.spec as spec
|
import eth2spec.phase0.spec as spec
|
||||||
|
|
||||||
from eth2spec.phase0.spec import (
|
from eth2spec.phase0.spec import (
|
||||||
@ -12,10 +8,10 @@ from tests.helpers import (
|
|||||||
next_epoch,
|
next_epoch,
|
||||||
)
|
)
|
||||||
|
|
||||||
# mark entire file as 'state'
|
from tests.context import spec_state_test
|
||||||
pytestmark = pytest.mark.state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_activation(state):
|
def test_activation(state):
|
||||||
index = 0
|
index = 0
|
||||||
assert is_active_validator(state.validator_registry[index], get_current_epoch(state))
|
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
|
state.validator_registry[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE
|
||||||
assert not is_active_validator(state.validator_registry[index], get_current_epoch(state))
|
assert not is_active_validator(state.validator_registry[index], get_current_epoch(state))
|
||||||
|
|
||||||
pre_state = deepcopy(state)
|
yield 'pre', state
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
|
for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
|
||||||
block = next_epoch(state)
|
block = next_epoch(state)
|
||||||
blocks.append(block)
|
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_eligibility_epoch != spec.FAR_FUTURE_EPOCH
|
||||||
assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH
|
assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH
|
||||||
assert is_active_validator(
|
assert is_active_validator(
|
||||||
@ -40,9 +41,8 @@ def test_activation(state):
|
|||||||
get_current_epoch(state),
|
get_current_epoch(state),
|
||||||
)
|
)
|
||||||
|
|
||||||
return pre_state, blocks, state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_ejection(state):
|
def test_ejection(state):
|
||||||
index = 0
|
index = 0
|
||||||
assert is_active_validator(state.validator_registry[index], get_current_epoch(state))
|
assert is_active_validator(state.validator_registry[index], get_current_epoch(state))
|
||||||
@ -51,17 +51,20 @@ def test_ejection(state):
|
|||||||
# Mock an ejection
|
# Mock an ejection
|
||||||
state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE
|
state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE
|
||||||
|
|
||||||
pre_state = deepcopy(state)
|
yield 'pre', state
|
||||||
|
|
||||||
blocks = []
|
blocks = []
|
||||||
for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
|
for _ in range(spec.ACTIVATION_EXIT_DELAY + 1):
|
||||||
block = next_epoch(state)
|
block = next_epoch(state)
|
||||||
blocks.append(block)
|
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 state.validator_registry[index].exit_epoch != spec.FAR_FUTURE_EPOCH
|
||||||
assert not is_active_validator(
|
assert not is_active_validator(
|
||||||
state.validator_registry[index],
|
state.validator_registry[index],
|
||||||
get_current_epoch(state),
|
get_current_epoch(state),
|
||||||
)
|
)
|
||||||
|
|
||||||
return pre_state, blocks, state
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user