From b78bd1fae036dcd74bd3c15246734a76751a2f6d Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 18 Nov 2022 23:14:57 +0800 Subject: [PATCH] Add other double-operations (attestations, deposits) tests --- .../test/capella/sanity/test_blocks.py | 2 +- .../test/phase0/sanity/test_blocks.py | 72 +++++++++++++++++-- 2 files changed, 69 insertions(+), 5 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 e42551f6f..0e910ed76 100644 --- a/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py @@ -79,7 +79,7 @@ def test_success_exit_and_bls_change(spec, state): @with_phases([CAPELLA]) @spec_state_test -def test_fail_double_bls_changes_in_same_block(spec, state): +def test_fail_double_bls_changes_same_block(spec, state): index = 0 signed_address_change = get_signed_address_change(spec, state, validator_index=index) yield 'pre', state diff --git a/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py index b94a01f7f..29757074d 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py @@ -433,7 +433,7 @@ def test_proposer_slashing(spec, state): @with_all_phases @spec_state_test -def test_double_same_proposer_slashings_same_block(spec, state): +def test_invalid_double_same_proposer_slashings_same_block(spec, state): proposer_slashing = get_valid_proposer_slashing(spec, state, signed_1=True, signed_2=True) slashed_index = proposer_slashing.signed_header_1.message.proposer_index assert not state.validators[slashed_index].slashed @@ -450,7 +450,7 @@ def test_double_same_proposer_slashings_same_block(spec, state): @with_all_phases @spec_state_test -def test_double_similar_proposer_slashings_same_block(spec, state): +def test_invalid_double_similar_proposer_slashings_same_block(spec, state): slashed_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] # Same validator, but different slashable offences in the same block @@ -549,7 +549,7 @@ def test_attester_slashing(spec, state): @with_all_phases @spec_state_test -def test_duplicate_attester_slashing(spec, state): +def test_invalid_double_same_attester_slashing_same_block(spec, state): if spec.MAX_ATTESTER_SLASHINGS < 2: return dump_skipping_message("Skip test if config cannot handle multiple AttesterSlashings per block") @@ -744,6 +744,27 @@ def test_deposit_in_block(spec, state): assert state.validators[validator_index].pubkey == pubkeys[validator_index] +@with_all_phases +@spec_state_test +def test_invalid_double_same_deposit_same_block(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + + yield 'pre', state + + block = build_empty_block_for_next_slot(spec, state) + + # The same deposit of the same validator + for _ in range(2): + block.body.deposits.append(deposit) + + signed_block = state_transition_and_sign_block(spec, state, block, expect_fail=True) + + yield 'blocks', [signed_block] + yield 'post', None + + @with_all_phases @spec_state_test def test_deposit_top_up(spec, state): @@ -831,6 +852,49 @@ def test_attestation(spec, state): assert spec.hash_tree_root(state.previous_epoch_participation) == pre_current_epoch_participation_root +@with_all_phases +@spec_state_test +def test_double_same_attestation_same_block(spec, state): + next_epoch(spec, state) + + yield 'pre', state + + attestation_block = build_empty_block(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) + + index = 0 + + attestation = get_valid_attestation(spec, state, index=index, signed=True) + + if not is_post_altair(spec): + pre_current_attestations_len = len(state.current_epoch_attestations) + + # Add to state via block transition + for _ in range(2): + attestation_block.body.attestations.append(attestation) + signed_attestation_block = state_transition_and_sign_block(spec, state, attestation_block) + + if not is_post_altair(spec): + assert len(state.current_epoch_attestations) == pre_current_attestations_len + 2 + # Epoch transition should move to previous_epoch_attestations + pre_current_attestations_root = spec.hash_tree_root(state.current_epoch_attestations) + else: + pre_current_epoch_participation_root = spec.hash_tree_root(state.current_epoch_participation) + + epoch_block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH) + signed_epoch_block = state_transition_and_sign_block(spec, state, epoch_block) + + yield 'blocks', [signed_attestation_block, signed_epoch_block] + yield 'post', state + + if not is_post_altair(spec): + assert len(state.current_epoch_attestations) == 0 + assert spec.hash_tree_root(state.previous_epoch_attestations) == pre_current_attestations_root + else: + for index in range(len(state.validators)): + assert state.current_epoch_participation[index] == spec.ParticipationFlags(0b0000_0000) + assert spec.hash_tree_root(state.previous_epoch_participation) == pre_current_epoch_participation_root + + # After SHARDING is enabled, a committee is computed for SHARD_COMMITTEE_PERIOD slots ago, # exceeding the minimal-config randao mixes memory size. # Applies to all voluntary-exit sanity block tests. @@ -866,7 +930,7 @@ def test_voluntary_exit(spec, state): @with_all_phases @spec_state_test -def test_double_validator_exit_same_block(spec, state): +def test_invalid_double_validator_exit_same_block(spec, state): validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[-1] # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit