From aac1af7db0342d385886b380d70200b1289c8afb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 18 Nov 2022 17:27:59 +0800 Subject: [PATCH] Add `test_fail_double_bls_changes_in_same_block` --- .../test/capella/sanity/test_blocks.py | 87 ++++++++++++------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py index 3c406e524..e42551f6f 100644 --- a/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py @@ -21,9 +21,13 @@ from eth2spec.test.helpers.withdrawals import ( from eth2spec.test.helpers.voluntary_exits import prepare_signed_exits +# +# BLSToExecutionChange +# + @with_phases([CAPELLA]) @spec_state_test -def test_successful_bls_change(spec, state): +def test_success_bls_change(spec, state): index = 0 signed_address_change = get_signed_address_change(spec, state, validator_index=index) pre_credentials = state.validators[index].withdrawal_credentials @@ -44,6 +48,58 @@ def test_successful_bls_change(spec, state): assert post_credentials[12:] == signed_address_change.message.to_execution_address +@with_phases([CAPELLA]) +@spec_state_test +def test_success_exit_and_bls_change(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + index = 0 + signed_address_change = get_signed_address_change(spec, state, validator_index=index) + signed_exit = prepare_signed_exits(spec, state, [index])[0] + + yield 'pre', state + + block = build_empty_block_for_next_slot(spec, state) + block.body.voluntary_exits.append(signed_exit) + block.body.bls_to_execution_changes.append(signed_address_change) + + signed_block = state_transition_and_sign_block(spec, state, block) + + yield 'blocks', [signed_block] + yield 'post', state + + validator = state.validators[index] + balance = state.balances[index] + current_epoch = spec.get_current_epoch(state) + assert not spec.is_fully_withdrawable_validator(validator, balance, current_epoch) + assert validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH + assert spec.is_fully_withdrawable_validator(validator, balance, validator.withdrawable_epoch) + + +@with_phases([CAPELLA]) +@spec_state_test +def test_fail_double_bls_changes_in_same_block(spec, state): + index = 0 + signed_address_change = get_signed_address_change(spec, state, validator_index=index) + yield 'pre', state + + block = build_empty_block_for_next_slot(spec, state) + + # Double BLSToExecutionChange of the same validator + for _ in range(2): + block.body.bls_to_execution_changes.append(signed_address_change) + + signed_block = state_transition_and_sign_block(spec, state, block, expect_fail=True) + + yield 'blocks', [signed_block] + yield 'post', None + + +# +# Withdrawals +# + @with_phases([CAPELLA]) @spec_state_test def test_full_withdrawal_in_epoch_transition(spec, state): @@ -112,35 +168,6 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state): assert len(spec.get_expected_withdrawals(state)) == 1 -@with_phases([CAPELLA]) -@spec_state_test -def test_exit_and_bls_change(spec, state): - # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit - state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH - - index = 0 - signed_address_change = get_signed_address_change(spec, state, validator_index=index) - signed_exit = prepare_signed_exits(spec, state, [index])[0] - - yield 'pre', state - - block = build_empty_block_for_next_slot(spec, state) - block.body.voluntary_exits.append(signed_exit) - block.body.bls_to_execution_changes.append(signed_address_change) - - signed_block = state_transition_and_sign_block(spec, state, block) - - yield 'blocks', [signed_block] - yield 'post', state - - validator = state.validators[index] - balance = state.balances[index] - current_epoch = spec.get_current_epoch(state) - assert not spec.is_fully_withdrawable_validator(validator, balance, current_epoch) - assert validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH - assert spec.is_fully_withdrawable_validator(validator, balance, validator.withdrawable_epoch) - - def _perform_valid_withdrawal(spec, state): fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals( spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4,