Skip to content

Commit

Permalink
make tests generic across forks
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed May 17, 2023
1 parent a08cc48 commit ee32e2a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
10 changes: 9 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/attestations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from eth2spec.test.context import expect_assertion_error
from eth2spec.test.helpers.state import state_transition_and_sign_block, next_epoch, next_slot
from eth2spec.test.helpers.block import build_empty_block_for_next_slot
from eth2spec.test.helpers.forks import is_post_altair
from eth2spec.test.helpers.forks import is_post_altair, is_post_attslotrange
from eth2spec.test.helpers.keys import privkeys
from eth2spec.utils import bls
from eth2spec.utils.ssz.ssz_typing import Bitlist
Expand Down Expand Up @@ -158,6 +158,14 @@ def get_attestation_signature(spec, state, attestation_data, privkey):
return bls.Sign(privkey, signing_root)


def compute_max_inclusion_slot(spec, attestation):
if is_post_attslotrange(spec):
next_epoch = spec.compute_epoch_at_slot(attestation.data.slot) + 1
end_of_next_epoch = spec.compute_start_slot_at_epoch(next_epoch + 1) - 1
return end_of_next_epoch
return attestation.data.slot + spec.SLOTS_PER_EPOCH


def fill_aggregate_attestation(spec, state, attestation, signed=False, filter_participant_set=None):
"""
`signed`: Signing is optional.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
spec_state_test,
always_bls, never_bls,
with_all_phases,
# Note, if attslotrange gets included, this will need to be
# 'with all phases up until attslotrange'
with_all_phases_except_attslotrange,
spec_test,
low_balances,
with_custom_state,
Expand All @@ -15,6 +12,7 @@
get_valid_attestation,
sign_aggregate_attestation,
sign_attestation,
compute_max_inclusion_slot,
)
from eth2spec.test.helpers.state import (
next_slots,
Expand Down Expand Up @@ -96,13 +94,24 @@ def test_invalid_before_inclusion_delay(spec, state):
yield from run_attestation_processing(spec, state, attestation, valid=False)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_at_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=True)

# increment past latest inclusion slot
transition_to_slot_via_block(spec, state, compute_max_inclusion_slot(spec, attestation))

yield from run_attestation_processing(spec, state, attestation)


@with_all_phases
@spec_state_test
def test_invalid_after_epoch_slots(spec, state):
def test_invalid_after_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=True)

# increment past latest inclusion slot
transition_to_slot_via_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH + 1)
transition_to_slot_via_block(spec, state, compute_max_inclusion_slot(spec, attestation) + 1)

yield from run_attestation_processing(spec, state, attestation, valid=False)

Expand Down Expand Up @@ -364,7 +373,7 @@ def test_invalid_too_few_aggregation_bits(spec, state):


#
# Full correct atttestation contents at different slot inclusions
# Full correct attestation contents at different slot inclusions
#

@with_all_phases
Expand Down Expand Up @@ -394,13 +403,22 @@ def test_correct_attestation_included_at_one_epoch_delay(spec, state):
yield from run_attestation_processing(spec, state, attestation)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_invalid_correct_attestation_included_after_epoch_delay(spec, state):
def test_correct_attestation_included_at_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=True)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation))

yield from run_attestation_processing(spec, state, attestation)


@with_all_phases
@spec_state_test
def test_invalid_correct_attestation_included_after_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=True)

# increment past latest inclusion slot
next_slots(spec, state, spec.SLOTS_PER_EPOCH + 1)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation) + 1)

yield from run_attestation_processing(spec, state, attestation, valid=False)

Expand Down Expand Up @@ -433,25 +451,25 @@ def test_incorrect_head_included_at_sqrt_epoch_delay(spec, state):
yield from run_attestation_processing(spec, state, attestation)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_incorrect_head_included_at_epoch_delay(spec, state):
def test_incorrect_head_included_at_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=False)
next_slots(spec, state, spec.SLOTS_PER_EPOCH)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation))

attestation.data.beacon_block_root = b'\x42' * 32
sign_attestation(spec, state, attestation)

yield from run_attestation_processing(spec, state, attestation)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_invalid_incorrect_head_included_after_epoch_delay(spec, state):
def test_invalid_incorrect_head_included_after_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=False)

# increment past latest inclusion slot
next_slots(spec, state, spec.SLOTS_PER_EPOCH + 1)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation) + 1)

attestation.data.beacon_block_root = b'\x42' * 32
sign_attestation(spec, state, attestation)
Expand Down Expand Up @@ -502,12 +520,12 @@ def test_incorrect_head_and_target_included_at_epoch_delay(spec, state):
yield from run_attestation_processing(spec, state, attestation)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_invalid_incorrect_head_and_target_included_after_epoch_delay(spec, state):
def test_invalid_incorrect_head_and_target_included_after_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=False)
# increment past latest inclusion slot
next_slots(spec, state, spec.SLOTS_PER_EPOCH + 1)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation) + 1)

attestation.data.beacon_block_root = b'\x42' * 32
attestation.data.target.root = b'\x42' * 32
Expand Down Expand Up @@ -556,12 +574,12 @@ def test_incorrect_target_included_at_epoch_delay(spec, state):
yield from run_attestation_processing(spec, state, attestation)


@with_all_phases_except_attslotrange
@with_all_phases
@spec_state_test
def test_invalid_incorrect_target_included_after_epoch_delay(spec, state):
def test_invalid_incorrect_target_included_after_max_inclusion_slot(spec, state):
attestation = get_valid_attestation(spec, state, signed=False)
# increment past latest inclusion slot
next_slots(spec, state, spec.SLOTS_PER_EPOCH + 1)
next_slots(spec, state, compute_max_inclusion_slot(spec, attestation) + 1)

attestation.data.target.root = b'\x42' * 32
sign_attestation(spec, state, attestation)
Expand Down

0 comments on commit ee32e2a

Please sign in to comment.