From 67da1ba2bf24f0cec1a233a2bad50b189b65cec5 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Wed, 13 Oct 2021 02:35:47 +0800 Subject: [PATCH] Minor refactoring - sanity check: deposit operation is independent of spec fork versions - refactoring - add comments --- .../test/altair/transition/test_activation.py | 43 ++++++------------- .../test/altair/transition/test_leaking.py | 20 ++------- .../test/altair/transition/test_slashing.py | 14 ++---- .../test/altair/transition/test_transition.py | 19 ++------ .../eth2spec/test/helpers/fork_transition.py | 14 ++++-- 5 files changed, 34 insertions(+), 76 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_activation.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_activation.py index 91888b0c3..df8ac6934 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_activation.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_activation.py @@ -8,6 +8,7 @@ from eth2spec.test.helpers.fork_transition import ( set_validators_exit_epoch, state_transition_across_slots, transition_until_fork, + transition_to_next_epoch_and_append_blocks, ) from eth2spec.test.helpers.random import set_some_new_deposits from eth2spec.test.helpers.state import ( @@ -52,11 +53,7 @@ def test_transition_with_one_fourth_exiting_validators_exit_post_fork( assert any(set(exited_pubkeys).intersection(list(state.current_sync_committee.pubkeys))) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) # check state for index in exited_indices: @@ -108,11 +105,7 @@ def test_transition_with_one_fourth_exiting_validators_exit_at_fork( assert not any(set(exited_pubkeys).intersection(list(state.current_sync_committee.pubkeys))) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -121,7 +114,8 @@ def test_transition_with_one_fourth_exiting_validators_exit_at_fork( @fork_transition_test(PHASE0, ALTAIR, fork_epoch=260) def test_transition_with_voluntary_exit_at_fork(state, fork_epoch, spec, post_spec, pre_tag, post_tag): """ - Create an attester slashing at the transition + Create an attester slashing at the transition. + fork_epoch=260 because mainnet `SHARD_COMMITTEE_PERIOD` is 256 epochs. """ transition_to(spec, state, spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH) transition_until_fork(spec, state, fork_epoch) @@ -141,11 +135,7 @@ def test_transition_with_voluntary_exit_at_fork(state, fork_epoch, spec, post_sp assert validator.exit_epoch < post_spec.FAR_FUTURE_EPOCH # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -178,11 +168,7 @@ def test_transition_with_non_empty_activation_queue(state, fork_epoch, spec, pos blocks.append(post_tag(block)) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -201,6 +187,9 @@ def test_transition_with_deposit_at_fork(state, fork_epoch, spec, post_spec, pre validator_index = len(state.validators) amount = post_spec.MAX_EFFECTIVE_BALANCE deposit = prepare_state_and_deposit(post_spec, state, validator_index, amount, signed=True) + deposit_old = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + # sanity check: deposit operation is independent of spec fork versions + assert deposit_old == deposit operation_dict = {'deposits': [deposit]} # irregular state transition to handle fork: state, block = do_altair_fork(state, spec, post_spec, fork_epoch, operation_dict=operation_dict) @@ -210,11 +199,7 @@ def test_transition_with_deposit_at_fork(state, fork_epoch, spec, post_spec, pre assert not post_spec.is_active_validator(state.validators[validator_index], post_spec.get_current_epoch(state)) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) # finalize activation_eligibility_epoch _, blocks_in_epoch, state = next_slots_with_attestations( @@ -228,11 +213,7 @@ def test_transition_with_deposit_at_fork(state, fork_epoch, spec, post_spec, pre assert state.finalized_checkpoint.epoch == state.validators[validator_index].activation_eligibility_epoch # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) assert state.validators[validator_index].activation_epoch < post_spec.FAR_FUTURE_EPOCH diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_leaking.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_leaking.py index 37c0f80d1..086e43a93 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_leaking.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_leaking.py @@ -2,8 +2,8 @@ from eth2spec.test.context import fork_transition_test from eth2spec.test.helpers.constants import PHASE0, ALTAIR from eth2spec.test.helpers.fork_transition import ( do_altair_fork, - state_transition_across_slots, transition_until_fork, + transition_to_next_epoch_and_append_blocks, ) @@ -29,11 +29,7 @@ def test_transition_with_leaking_pre_fork(state, fork_epoch, spec, post_spec, pr assert spec.is_in_inactivity_leak(state) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -61,11 +57,7 @@ def test_transition_with_leaking_at_fork(state, fork_epoch, spec, post_spec, pre assert spec.is_in_inactivity_leak(state) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -93,11 +85,7 @@ def test_transition_with_leaking_post_fork(state, fork_epoch, spec, post_spec, p assert not spec.is_in_inactivity_leak(state) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) # check state again assert spec.is_in_inactivity_leak(state) diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_slashing.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_slashing.py index 018ce6781..cc22f5c22 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_slashing.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_slashing.py @@ -12,9 +12,9 @@ from eth2spec.test.helpers.proposer_slashings import ( ) from eth2spec.test.helpers.fork_transition import ( do_altair_fork, - state_transition_across_slots, state_transition_across_slots_with_ignoring_proposers, transition_until_fork, + transition_to_next_epoch_and_append_blocks, ) from eth2spec.test.helpers.inactivity_scores import ( slash_some_validators_for_inactivity_scores_test, @@ -98,11 +98,7 @@ def test_transition_with_attester_slashing_at_fork(state, fork_epoch, spec, post assert state.validators[validator_index].slashed # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state @@ -131,11 +127,7 @@ def test_transition_with_proposer_slashing_at_fork(state, fork_epoch, spec, post assert slashed_proposer.slashed # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) yield "blocks", blocks yield "post", state diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py index c6dec8d3e..fe6248c5f 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py @@ -11,6 +11,7 @@ from eth2spec.test.helpers.fork_transition import ( only_at, skip_slots, state_transition_across_slots, + transition_to_next_epoch_and_append_blocks, ) @@ -37,11 +38,7 @@ def test_normal_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag blocks.append(post_tag(block)) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) assert state.slot % post_spec.SLOTS_PER_EPOCH == 0 assert post_spec.get_current_epoch(state) == fork_epoch + 1 @@ -77,11 +74,7 @@ def test_transition_missing_first_post_block(state, fork_epoch, spec, post_spec, state, _ = do_altair_fork(state, spec, post_spec, fork_epoch, with_block=False) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) assert state.slot % post_spec.SLOTS_PER_EPOCH == 0 assert post_spec.get_current_epoch(state) == fork_epoch + 1 @@ -120,11 +113,7 @@ def test_transition_missing_last_pre_fork_block(state, fork_epoch, spec, post_sp blocks.append(post_tag(block)) # continue regular state transition with new spec into next epoch - to_slot = post_spec.SLOTS_PER_EPOCH + state.slot - blocks.extend([ - post_tag(block) for block in - state_transition_across_slots(post_spec, state, to_slot) - ]) + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks) assert state.slot % post_spec.SLOTS_PER_EPOCH == 0 assert post_spec.get_current_epoch(state) == fork_epoch + 1 diff --git a/tests/core/pyspec/eth2spec/test/helpers/fork_transition.py b/tests/core/pyspec/eth2spec/test/helpers/fork_transition.py index e0e7d028b..ea5419d0d 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/fork_transition.py +++ b/tests/core/pyspec/eth2spec/test/helpers/fork_transition.py @@ -14,7 +14,6 @@ from eth2spec.test.helpers.block import ( def _state_transition_and_sign_block_at_slot(spec, state, - *, operation_dict=None): """ Cribbed from ``transition_unsigned_block`` helper @@ -24,7 +23,8 @@ def _state_transition_and_sign_block_at_slot(spec, Used to produce a block during an irregular state transition. """ block = build_empty_block(spec, state) - # we can't just pass `body` because randao_reveal and eth1_data was set in `build_empty_block` + # we can't just pass `body` and assign it because randao_reveal and eth1_data was set in `build_empty_block` + # thus use dict to pass operations. if operation_dict is not None: for key, value in operation_dict.items(): setattr(block.body, key, value) @@ -117,7 +117,7 @@ def do_altair_fork(state, spec, post_spec, fork_epoch, with_block=True, operatio def set_validators_exit_epoch(spec, state, exit_epoch, rng=random.Random(40404040), fraction=0.25): """ - Set some valdiators' exit_epoch. + Set some valdiators' `exit_epoch` and `withdrawable_epoch`. """ selected_count = int(len(state.validators) * fraction) selected_indices = rng.sample(range(len(state.validators)), selected_count) @@ -132,3 +132,11 @@ def set_validators_exit_epoch(spec, state, exit_epoch, rng=random.Random(4040404 def transition_until_fork(spec, state, fork_epoch): to_slot = fork_epoch * spec.SLOTS_PER_EPOCH - 1 transition_to(spec, state, to_slot) + + +def transition_to_next_epoch_and_append_blocks(spec, state, post_tag, blocks): + to_slot = spec.SLOTS_PER_EPOCH + state.slot + blocks.extend([ + post_tag(block) for block in + state_transition_across_slots(spec, state, to_slot) + ])