Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
- sanity check: deposit operation is independent of spec fork versions
- refactoring
- add comments
  • Loading branch information
hwwhww committed Oct 13, 2021
1 parent 53d4fa5 commit 91f63bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 becasue 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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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

Expand Down
20 changes: 4 additions & 16 deletions tests/core/pyspec/eth2spec/test/altair/transition/test_leaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
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,
)


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 3 additions & 11 deletions tests/core/pyspec/eth2spec/test/altair/transition/test_slashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
)
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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
only_at,
skip_slots,
state_transition_across_slots,
transition_to_next_epoch_and_append_blocks,
)


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/core/pyspec/eth2spec/test/helpers/fork_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

def _state_transition_and_sign_block_at_slot(spec,
state,
*,
operation_dict=None):
"""
Cribbed from ``transition_unsigned_block`` helper
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
])

0 comments on commit 91f63bb

Please sign in to comment.