2019-03-26 17:27:07 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
|
|
|
2019-04-03 03:18:17 +00:00
|
|
|
import eth2spec.phase0.spec as spec
|
2019-03-26 17:27:07 +00:00
|
|
|
|
2019-04-03 03:18:17 +00:00
|
|
|
from eth2spec.phase0.state_transition import (
|
2019-03-26 17:27:07 +00:00
|
|
|
state_transition,
|
|
|
|
)
|
2019-04-03 03:18:17 +00:00
|
|
|
from eth2spec.phase0.spec import (
|
2019-03-26 17:27:07 +00:00
|
|
|
get_current_epoch,
|
2019-05-05 22:31:57 +00:00
|
|
|
process_attestation
|
2019-03-26 17:27:07 +00:00
|
|
|
)
|
2019-04-17 07:47:56 +00:00
|
|
|
from tests.helpers import (
|
2019-03-26 17:27:07 +00:00
|
|
|
build_empty_block_for_next_slot,
|
|
|
|
get_valid_attestation,
|
2019-04-18 02:27:25 +00:00
|
|
|
next_epoch,
|
|
|
|
next_slot,
|
2019-03-26 17:27:07 +00:00
|
|
|
)
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
from .block_test_helpers import spec_state_test
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_attestation_processing(state, attestation, valid=True):
|
|
|
|
"""
|
2019-05-06 14:23:15 +00:00
|
|
|
Run ``process_attestation``, yielding:
|
|
|
|
- pre-state ('pre')
|
|
|
|
- attestation ('attestation')
|
|
|
|
- post-state ('post').
|
2019-03-26 17:27:07 +00:00
|
|
|
If ``valid == False``, run expecting ``AssertionError``
|
|
|
|
"""
|
2019-05-05 22:31:57 +00:00
|
|
|
# yield pre-state
|
|
|
|
yield 'pre', state
|
|
|
|
|
|
|
|
yield 'attestation', attestation
|
2019-03-26 17:27:07 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
# If the attestation is invalid, processing is aborted, and there is no post-state.
|
2019-03-26 17:27:07 +00:00
|
|
|
if not valid:
|
|
|
|
with pytest.raises(AssertionError):
|
2019-05-05 22:31:57 +00:00
|
|
|
process_attestation(state, attestation)
|
|
|
|
yield 'post', None
|
|
|
|
return
|
2019-03-26 17:27:07 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
current_epoch_count = len(state.current_epoch_attestations)
|
|
|
|
previous_epoch_count = len(state.previous_epoch_attestations)
|
2019-03-26 17:27:07 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
# process attestation
|
|
|
|
process_attestation(state, attestation)
|
|
|
|
|
|
|
|
# Make sure the attestation has been processed
|
|
|
|
if attestation.data.target_epoch == get_current_epoch(state):
|
|
|
|
assert len(state.current_epoch_attestations) == current_epoch_count + 1
|
2019-03-26 17:27:07 +00:00
|
|
|
else:
|
2019-05-05 22:31:57 +00:00
|
|
|
assert len(state.previous_epoch_attestations) == previous_epoch_count + 1
|
|
|
|
|
|
|
|
# yield post-state
|
|
|
|
yield 'post', state
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:27:07 +00:00
|
|
|
def test_success(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation)
|
2019-03-26 17:27:07 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:27:07 +00:00
|
|
|
def test_success_prevous_epoch(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
block = build_empty_block_for_next_slot(state)
|
|
|
|
block.slot = state.slot + spec.SLOTS_PER_EPOCH
|
|
|
|
state_transition(state, block)
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_before_inclusion_delay(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
# do not increment slot to allow for inclusion delay
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_after_epoch_slots(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
block = build_empty_block_for_next_slot(state)
|
|
|
|
# increment past latest inclusion slot
|
|
|
|
block.slot = state.slot + spec.SLOTS_PER_EPOCH + 1
|
|
|
|
state_transition(state, block)
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_bad_source_epoch(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
|
|
|
attestation.data.source_epoch += 10
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_bad_source_root(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
2019-04-06 15:09:07 +00:00
|
|
|
attestation.data.source_root = b'\x42' * 32
|
2019-03-26 17:41:15 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_non_zero_crosslink_data_root(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
2019-04-06 15:09:07 +00:00
|
|
|
attestation.data.crosslink_data_root = b'\x42' * 32
|
2019-03-26 17:41:15 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_bad_previous_crosslink(state):
|
2019-04-18 02:27:25 +00:00
|
|
|
next_epoch(state)
|
2019-03-26 17:41:15 +00:00
|
|
|
attestation = get_valid_attestation(state)
|
2019-04-18 02:27:25 +00:00
|
|
|
for _ in range(spec.MIN_ATTESTATION_INCLUSION_DELAY):
|
|
|
|
next_slot(state)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
2019-04-02 05:00:36 +00:00
|
|
|
state.current_crosslinks[attestation.data.shard].epoch += 10
|
2019-03-26 17:41:15 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_non_empty_custody_bitfield(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
2019-03-27 14:31:56 +00:00
|
|
|
attestation.custody_bitfield = deepcopy(attestation.aggregation_bitfield)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|
2019-03-26 17:41:15 +00:00
|
|
|
|
|
|
|
|
2019-05-06 14:23:15 +00:00
|
|
|
@spec_state_test
|
2019-03-26 17:41:15 +00:00
|
|
|
def test_empty_aggregation_bitfield(state):
|
|
|
|
attestation = get_valid_attestation(state)
|
|
|
|
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
|
|
|
|
|
|
|
attestation.aggregation_bitfield = b'\x00' * len(attestation.aggregation_bitfield)
|
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
yield from run_attestation_processing(state, attestation, False)
|