make tests generic across forks

This commit is contained in:
djrtwo 2023-05-17 11:53:25 -06:00
parent a08cc48e9e
commit ee32e2a31f
2 changed files with 49 additions and 23 deletions

View File

@ -5,7 +5,7 @@ from typing import List
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
@ -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.

View File

@ -2,9 +2,6 @@ from eth2spec.test.context import (
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,
@ -15,6 +12,7 @@ from eth2spec.test.helpers.attestations import (
get_valid_attestation,
sign_aggregate_attestation,
sign_attestation,
compute_max_inclusion_slot,
)
from eth2spec.test.helpers.state import (
next_slots,
@ -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_invalid_after_epoch_slots(spec, state):
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, state.slot + spec.SLOTS_PER_EPOCH + 1)
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_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) + 1)
yield from run_attestation_processing(spec, state, attestation, valid=False)
@ -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
@ -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)
@ -433,11 +451,11 @@ 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)
@ -445,13 +463,13 @@ def test_incorrect_head_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_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)
@ -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
@ -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)