add "normal" transition test

This commit is contained in:
Alex Stokes 2021-04-30 11:46:46 -07:00
parent d34b2a08d5
commit 0e71496eb5
No known key found for this signature in database
GPG Key ID: 99B3D88FD6C55A69
1 changed files with 46 additions and 31 deletions

View File

@ -1,24 +1,44 @@
from eth2spec.test.context import (
fork_transition_test,
single_phase,
with_custom_state,
default_activation_threshold,
low_balances,
)
from eth2spec.test.context import fork_transition_test
from eth2spec.test.helpers.constants import PHASE0, ALTAIR
from eth2spec.test.helpers.state import state_transition_and_sign_block
from eth2spec.test.helpers.block import build_empty_block_for_next_slot
from eth2spec.test.helpers.block import build_empty_block_for_next_slot, build_empty_block, sign_block
def _state_transition_and_sign_block_at_slot(spec, state):
"""
Cribbed from `transition_unsigned_block` helper
where the early parts of the state transition have already
been applied to `state`.
Used to produce a block during an irregular state transition.
"""
block = build_empty_block(spec, state)
assert state.latest_block_header.slot < block.slot
assert state.slot == block.slot
spec.process_block(state, block)
block.state_root = state.hash_tree_root()
return sign_block(spec, state, block)
@fork_transition_test(PHASE0, ALTAIR, fork_epoch=2)
def test_normal_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag):
yield "pre", state
assert spec.get_current_epoch(state) < fork_epoch
blocks = []
for slot in range(state.slot, fork_epoch * spec.SLOTS_PER_EPOCH):
# regular state transition until fork:
for _ in range(state.slot, fork_epoch * spec.SLOTS_PER_EPOCH - 1):
block = build_empty_block_for_next_slot(spec, state)
state_transition_and_sign_block(spec, state, block)
blocks.append(pre_tag(block))
signed_block = state_transition_and_sign_block(spec, state, block)
blocks.append(pre_tag(signed_block))
# irregular state transition to handle fork:
spec.process_slots(state, state.slot + 1)
assert state.slot % spec.SLOTS_PER_EPOCH == 0
assert spec.compute_epoch_at_slot(state.slot) == fork_epoch
state = post_spec.upgrade_to_altair(state)
@ -26,26 +46,21 @@ def test_normal_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag
assert state.fork.previous_version == post_spec.GENESIS_FORK_VERSION
assert state.fork.current_version == post_spec.ALTAIR_FORK_VERSION
block = build_empty_block_for_next_slot(post_spec, state)
state_transition_and_sign_block(post_spec, state, block)
blocks.append(post_tag(block))
signed_block = _state_transition_and_sign_block_at_slot(post_spec, state)
blocks.append(post_tag(signed_block))
# continue regular state transition with new spec into next epoch
for _ in range(post_spec.SLOTS_PER_EPOCH):
block = build_empty_block_for_next_slot(post_spec, state)
signed_block = state_transition_and_sign_block(post_spec, state, block)
blocks.append(post_tag(signed_block))
assert state.slot % post_spec.SLOTS_PER_EPOCH == 0
assert post_spec.compute_epoch_at_slot(state.slot) == fork_epoch + 1
slots_with_blocks = [block.message.slot for block in blocks]
assert len(set(slots_with_blocks)) == len(slots_with_blocks)
assert set(range(1, state.slot + 1)) == set(slots_with_blocks)
yield "blocks", blocks
yield "post", state
@fork_transition_test(PHASE0, ALTAIR)
def test_normal_transition_with_manual_fork_epoch(state, spec, post_spec, pre_tag, post_tag):
fork_epoch = 2
yield "fork_epoch", "meta", fork_epoch
# run test with computed fork_epoch...
@fork_transition_test(PHASE0, ALTAIR, fork_epoch=2)
@with_custom_state(low_balances, default_activation_threshold)
@single_phase
def test_normal_transition_with_low_balances(state, fork_epoch, spec, post_spec, pre_tag, post_tag):
yield "pre", state
# run test with custom state...