ensure that pre-state slot is less than block for all block tests

This commit is contained in:
Danny Ryan 2020-05-13 17:25:39 -06:00
parent 1d147037eb
commit e7ae4be8a9
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
7 changed files with 53 additions and 51 deletions

View File

@ -53,6 +53,7 @@ def sign_block(spec, state, block, proposer_index=None):
def transition_unsigned_block(spec, state, block):
assert state.slot < block.slot # Preserve assertion from state transition to avoid strange pre-states from testing
if state.slot < block.slot:
spec.process_slots(state, block.slot)
assert state.latest_block_header.slot < block.slot # There may not already be a block in this slot or past it.
@ -60,11 +61,11 @@ def transition_unsigned_block(spec, state, block):
spec.process_block(state, block)
def apply_empty_block(spec, state):
def apply_empty_block(spec, state, slot=None):
"""
Transition via an empty block (on current slot, assuming no block has been applied yet).
"""
block = build_empty_block(spec, state)
block = build_empty_block(spec, state, slot)
transition_unsigned_block(spec, state, block)

View File

@ -1,5 +1,5 @@
from eth2spec.test.context import expect_assertion_error
from eth2spec.test.helpers.block import sign_block, transition_unsigned_block
from eth2spec.test.helpers.block import apply_empty_block, sign_block, transition_unsigned_block
def get_balance(state, index):
@ -31,6 +31,15 @@ def transition_to(spec, state, slot):
assert state.slot == slot
def transition_to_slot_via_block(spec, state, slot):
"""
Transition to ``slot`` via an empty block transition
"""
assert state.slot < slot
apply_empty_block(spec, state, slot)
assert state.slot == slot
def next_epoch(spec, state):
"""
Transition to the start slot of the next epoch
@ -40,6 +49,13 @@ def next_epoch(spec, state):
spec.process_slots(state, slot)
def next_epoch_via_block(spec, state):
"""
Transition to the start slot of the next epoch via a full block transition
"""
apply_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH)
def get_state_root(spec, state, slot) -> bytes:
"""
Return the state root at a recent ``slot``.

View File

@ -15,10 +15,9 @@ from eth2spec.test.helpers.attestations import (
from eth2spec.test.helpers.state import (
next_slot,
next_slots,
next_epoch,
transition_to,
next_epoch_via_block,
transition_to_slot_via_block,
)
from eth2spec.test.helpers.block import apply_empty_block
from eth2spec.utils.ssz.ssz_typing import Bitlist
@ -47,9 +46,7 @@ def test_success_multi_proposer_index_iterations(spec, state):
@spec_state_test
def test_success_previous_epoch(spec, state):
attestation = get_valid_attestation(spec, state, signed=True, on_time=False)
transition_to(spec, state, spec.SLOTS_PER_EPOCH - 1)
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
yield from run_attestation_processing(spec, state, attestation)
@ -79,8 +76,7 @@ def test_after_epoch_slots(spec, state):
attestation = get_valid_attestation(spec, state, signed=True, on_time=False)
# increment past latest inclusion slot
transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH + 1)
apply_empty_block(spec, state)
transition_to_slot_via_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH + 1)
yield from run_attestation_processing(spec, state, attestation, False)
@ -151,8 +147,8 @@ def test_invalid_index(spec, state):
@with_all_phases
@spec_state_test
def test_mismatched_target_and_slot(spec, state):
next_epoch(spec, state)
next_epoch(spec, state)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
attestation = get_valid_attestation(spec, state, on_time=False)
attestation.data.slot = attestation.data.slot - spec.SLOTS_PER_EPOCH

View File

@ -5,10 +5,9 @@ from eth2spec.test.context import (
from eth2spec.test.helpers.attestations import sign_indexed_attestation
from eth2spec.test.helpers.attester_slashings import get_valid_attester_slashing, \
get_indexed_attestation_participants, get_attestation_2_data, get_attestation_1_data
from eth2spec.test.helpers.block import apply_empty_block
from eth2spec.test.helpers.state import (
get_balance,
next_epoch,
next_epoch_via_block,
)
@ -91,8 +90,7 @@ def test_success_double(spec, state):
@with_all_phases
@spec_state_test
def test_success_surround(spec, state):
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
state.current_justified_checkpoint.epoch += 1
attester_slashing = get_valid_attester_slashing(spec, state, signed_1=False, signed_2=True)

View File

@ -1,6 +1,5 @@
from eth2spec.test.helpers.custody import get_valid_early_derived_secret_reveal
from eth2spec.test.helpers.block import apply_empty_block
from eth2spec.test.helpers.state import next_epoch, get_balance
from eth2spec.test.helpers.state import next_epoch_via_block, get_balance
from eth2spec.test.context import (
PHASE0,
with_all_phases_except,
@ -64,8 +63,7 @@ def test_reveal_from_current_epoch(spec, state):
@spec_state_test
@never_bls
def test_reveal_from_past_epoch(spec, state):
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state, spec.get_current_epoch(state) - 1)
yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False)

View File

@ -2,7 +2,10 @@ from copy import deepcopy
from eth2spec.utils import bls
from eth2spec.test.helpers.state import get_balance, state_transition_and_sign_block, next_slot, next_epoch
from eth2spec.test.helpers.state import (
get_balance, state_transition_and_sign_block,
next_slot, next_epoch, next_epoch_via_block,
)
from eth2spec.test.helpers.block import (
build_empty_block_for_next_slot, build_empty_block,
sign_block,
@ -48,10 +51,12 @@ def test_same_slot_block_transition(spec, state):
yield 'pre', state
signed_block = state_transition_and_sign_block(spec, state, block)
assert state.slot == block.slot
signed_block = state_transition_and_sign_block(spec, state, block, expect_fail=True)
yield 'blocks', [signed_block]
yield 'post', state
yield 'post', None
@with_all_phases
@ -357,22 +362,19 @@ def test_proposer_after_inactive_index(spec, state):
state.validators[inactive_index].exit_epoch = spec.get_current_epoch(state)
# skip forward, get brand new proposers
next_epoch(spec, state)
next_epoch(spec, state)
block = build_empty_block_for_next_slot(spec, state)
state_transition_and_sign_block(spec, state, block)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
while True:
next_slot(spec, state)
proposer_index = spec.get_beacon_proposer_index(state)
if proposer_index > inactive_index:
# found a proposer that has a higher index than a disabled validator
yield 'pre', state
# test if the proposer can be recognized correctly after the inactive validator
signed_block = state_transition_and_sign_block(spec, state, build_empty_block(spec, state))
signed_block = state_transition_and_sign_block(spec, state, build_empty_block_for_next_slot(spec, state))
yield 'blocks', [signed_block]
yield 'post', state
break
next_slot(spec, state)
@with_all_phases
@ -390,16 +392,16 @@ def test_high_proposer_index(spec, state):
active_count = len(spec.get_active_validator_indices(state, current_epoch))
while True:
next_slot(spec, state)
proposer_index = spec.get_beacon_proposer_index(state)
if proposer_index >= active_count:
# found a proposer that has a higher index than the active validator count
yield 'pre', state
# test if the proposer can be recognized correctly, even while it has a high index.
signed_block = state_transition_and_sign_block(spec, state, build_empty_block(spec, state))
signed_block = state_transition_and_sign_block(spec, state, build_empty_block_for_next_slot(spec, state))
yield 'blocks', [signed_block]
yield 'post', state
break
next_slot(spec, state)
@with_all_phases

View File

@ -1,7 +1,6 @@
from eth2spec.test.context import spec_state_test, never_bls, with_all_phases, with_phases
from eth2spec.test.helpers.state import next_epoch
from eth2spec.test.helpers.state import next_epoch_via_block
from eth2spec.test.helpers.attestations import next_epoch_with_attestations
from eth2spec.test.helpers.block import apply_empty_block
def check_finality(spec,
@ -58,10 +57,8 @@ def test_finality_no_updates_at_genesis(spec, state):
@never_bls
def test_finality_rule_4(spec, state):
# get past first two epochs that finality does not run on
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
yield 'pre', state
@ -86,10 +83,8 @@ def test_finality_rule_4(spec, state):
@never_bls
def test_finality_rule_1(spec, state):
# get past first two epochs that finality does not run on
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
yield 'pre', state
@ -116,10 +111,8 @@ def test_finality_rule_1(spec, state):
@never_bls
def test_finality_rule_2(spec, state):
# get past first two epochs that finality does not run on
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
yield 'pre', state
@ -152,10 +145,8 @@ def test_finality_rule_3(spec, state):
https://github.com/ethereum/eth2.0-specs/issues/611#issuecomment-463612892
"""
# get past first two epochs that finality does not run on
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch(spec, state)
apply_empty_block(spec, state)
next_epoch_via_block(spec, state)
next_epoch_via_block(spec, state)
yield 'pre', state