-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #874 from ethereum/prev-cur-crosslinks
add previous and current crosslinks
- Loading branch information
Showing
9 changed files
with
257 additions
and
141 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
test_libs/pyspec/tests/epoch_processing/test_process_crosslinks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from copy import deepcopy | ||
import pytest | ||
|
||
import eth2spec.phase0.spec as spec | ||
|
||
from eth2spec.phase0.state_transition import ( | ||
state_transition, | ||
) | ||
from eth2spec.phase0.spec import ( | ||
cache_state, | ||
get_crosslink_deltas, | ||
process_crosslinks, | ||
) | ||
from tests.helpers import ( | ||
add_attestation_to_state, | ||
build_empty_block_for_next_slot, | ||
fill_aggregate_attestation, | ||
get_crosslink_committee_for_attestation, | ||
get_valid_attestation, | ||
next_epoch, | ||
next_slot, | ||
set_bitfield_bit, | ||
) | ||
|
||
|
||
# mark entire file as 'crosslinks' | ||
pytestmark = pytest.mark.crosslinks | ||
|
||
|
||
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 = build_empty_block_for_next_slot(state) | ||
block.slot = slot | ||
state_transition(state, block) | ||
|
||
# cache state before epoch transition | ||
cache_state(state) | ||
|
||
post_state = deepcopy(state) | ||
process_crosslinks(post_state) | ||
|
||
return state, post_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] | ||
|
||
return pre_state, post_state | ||
|
||
|
||
def test_single_crosslink_update_from_current_epoch(state): | ||
next_epoch(state) | ||
|
||
attestation = get_valid_attestation(state) | ||
|
||
fill_aggregate_attestation(state, attestation) | ||
add_attestation_to_state(state, attestation, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) | ||
|
||
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] | ||
|
||
return pre_state, post_state | ||
|
||
|
||
def test_single_crosslink_update_from_previous_epoch(state): | ||
next_epoch(state) | ||
|
||
attestation = get_valid_attestation(state) | ||
|
||
fill_aggregate_attestation(state, attestation) | ||
add_attestation_to_state(state, attestation, state.slot + spec.SLOTS_PER_EPOCH) | ||
|
||
assert len(state.previous_epoch_attestations) == 1 | ||
|
||
pre_state, post_state = run_process_crosslinks(state) | ||
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] | ||
# ensure rewarded | ||
for index in get_crosslink_committee_for_attestation(state, attestation.data): | ||
assert crosslink_deltas[0][index] > 0 | ||
assert crosslink_deltas[1][index] == 0 | ||
|
||
return pre_state, post_state | ||
|
||
|
||
def test_double_late_crosslink(state): | ||
next_epoch(state) | ||
state.slot += 4 | ||
|
||
attestation_1 = get_valid_attestation(state) | ||
fill_aggregate_attestation(state, attestation_1) | ||
|
||
# add attestation_1 in the next epoch | ||
next_epoch(state) | ||
add_attestation_to_state(state, attestation_1, state.slot + 1) | ||
|
||
for slot in range(spec.SLOTS_PER_EPOCH): | ||
attestation_2 = get_valid_attestation(state) | ||
if attestation_2.data.shard == attestation_1.data.shard: | ||
break | ||
next_slot(state) | ||
fill_aggregate_attestation(state, attestation_2) | ||
|
||
# add attestation_2 in the next epoch after attestation_1 has | ||
# already updated the relevant crosslink | ||
next_epoch(state) | ||
add_attestation_to_state(state, attestation_2, state.slot + 1) | ||
|
||
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) | ||
|
||
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] | ||
# ensure no reward, only penalties for the failed crosslink | ||
for index in get_crosslink_committee_for_attestation(state, attestation_2.data): | ||
assert crosslink_deltas[0][index] == 0 | ||
assert crosslink_deltas[1][index] > 0 | ||
|
||
return pre_state, post_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.