Skip to content

Commit 49b6840

Browse files
authored
Merge pull request #3980 from mkalinin/more-op-tests
electra: Misc beacon chain operations tests
2 parents b4311fe + 08cb828 commit 49b6840

File tree

2 files changed

+108
-15
lines changed

2 files changed

+108
-15
lines changed

tests/core/pyspec/eth2spec/test/electra/block_processing/test_process_attestation.py

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
from eth2spec.test.helpers.constants import MINIMAL
12
from eth2spec.test.context import (
23
always_bls,
34
spec_state_test,
45
with_electra_and_later,
6+
with_presets,
57
)
68
from eth2spec.test.helpers.attestations import (
79
run_attestation_processing,
810
get_valid_attestation,
911
sign_attestation,
12+
build_attestation_data,
13+
get_valid_attestation_at_slot,
14+
get_empty_eip7549_aggregation_bits,
1015
)
1116
from eth2spec.test.helpers.state import (
1217
next_slots,
@@ -35,7 +40,7 @@ def test_invalid_attestation_data_index_not_zero(spec, state):
3540
@with_electra_and_later
3641
@spec_state_test
3742
@always_bls
38-
def test_invalid_committe_index(spec, state):
43+
def test_invalid_committee_index(spec, state):
3944
"""
4045
EIP-7549 test
4146
"""
@@ -53,7 +58,7 @@ def test_invalid_committe_index(spec, state):
5358

5459
@with_electra_and_later
5560
@spec_state_test
56-
def test_invalid_too_many_committe_bits(spec, state):
61+
def test_invalid_too_many_committee_bits(spec, state):
5762
"""
5863
EIP-7549 test
5964
"""
@@ -68,7 +73,7 @@ def test_invalid_too_many_committe_bits(spec, state):
6873

6974
@with_electra_and_later
7075
@spec_state_test
71-
def test_invalid_nonset_committe_bits(spec, state):
76+
def test_invalid_nonset_committee_bits(spec, state):
7277
"""
7378
EIP-7549 test
7479
"""
@@ -79,3 +84,67 @@ def test_invalid_nonset_committe_bits(spec, state):
7984
attestation.committee_bits[committee_index] = 0
8085

8186
yield from run_attestation_processing(spec, state, attestation, valid=False)
87+
88+
89+
@with_electra_and_later
90+
@spec_state_test
91+
def test_invalid_nonset_multiple_committee_bits(spec, state):
92+
"""
93+
EIP-7549 test
94+
"""
95+
attestation_data = build_attestation_data(spec, state, slot=state.slot, index=0)
96+
attestation = spec.Attestation(data=attestation_data)
97+
98+
# a single attestation with all committees of a slot, but with unset aggregation_bits
99+
committees_per_slot = spec.get_committee_count_per_slot(state, spec.get_current_epoch(state))
100+
for index in range(committees_per_slot):
101+
attestation.committee_bits[index] = True
102+
103+
attestation.aggregation_bits = get_empty_eip7549_aggregation_bits(
104+
spec, state, attestation.committee_bits, attestation.data.slot
105+
)
106+
107+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
108+
109+
yield from run_attestation_processing(spec, state, attestation, valid=False)
110+
111+
112+
@with_electra_and_later
113+
@spec_state_test
114+
@with_presets([MINIMAL], "need multiple committees per slot")
115+
@always_bls
116+
def test_multiple_committees(spec, state):
117+
"""
118+
EIP-7549 test
119+
"""
120+
attestation_data = build_attestation_data(spec, state, slot=state.slot, index=0)
121+
attestation = spec.Attestation(data=attestation_data)
122+
123+
# a single attestation with all committees of a slot
124+
attestation = get_valid_attestation_at_slot(state, spec, state.slot)
125+
126+
# check that all committees are presented in a single attestation
127+
attesting_indices = set()
128+
committees_per_slot = spec.get_committee_count_per_slot(state, spec.get_current_epoch(state))
129+
for index in range(committees_per_slot):
130+
attesting_indices.update(spec.get_beacon_committee(state, state.slot, index))
131+
assert spec.get_attesting_indices(state, attestation) == attesting_indices
132+
133+
# advance a slot
134+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
135+
136+
yield from run_attestation_processing(spec, state, attestation)
137+
138+
139+
@with_electra_and_later
140+
@spec_state_test
141+
@with_presets([MINIMAL], "need multiple committees per slot")
142+
@always_bls
143+
def test_one_committee_with_gap(spec, state):
144+
"""
145+
EIP-7549 test
146+
"""
147+
attestation = get_valid_attestation(spec, state, index=1, signed=True)
148+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
149+
150+
yield from run_attestation_processing(spec, state, attestation)

tests/core/pyspec/eth2spec/test/electra/block_processing/test_process_withdrawal_request.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_basic_withdrawal_request_with_full_partial_withdrawal_queue(spec, state
124124
)
125125

126126

127-
# Invalid tests
127+
# Tests that should fail
128128

129129

130130
@with_electra_and_later
@@ -237,6 +237,31 @@ def test_activation_epoch_less_than_shard_committee_period(spec, state):
237237
)
238238

239239

240+
@with_electra_and_later
241+
@spec_state_test
242+
def test_unknown_pubkey(spec, state):
243+
rng = random.Random(1344)
244+
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
245+
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
246+
247+
current_epoch = spec.get_current_epoch(state)
248+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
249+
address = b"\x22" * 20
250+
pubkey = spec.BLSPubkey(b"\x23" * 48)
251+
set_eth1_withdrawal_credential_with_balance(
252+
spec, state, validator_index, address=address
253+
)
254+
withdrawal_request = spec.WithdrawalRequest(
255+
source_address=address,
256+
validator_pubkey=pubkey,
257+
amount=spec.FULL_EXIT_REQUEST_AMOUNT,
258+
)
259+
260+
yield from run_withdrawal_request_processing(
261+
spec, state, withdrawal_request, success=False
262+
)
263+
264+
240265
# Partial withdrawals tests
241266

242267
@with_electra_and_later
@@ -896,10 +921,6 @@ def run_withdrawal_request_processing(
896921
If ``valid == False``, run expecting ``AssertionError``
897922
If ``success == False``, it doesn't initiate exit successfully
898923
"""
899-
validator_index = get_validator_index_by_pubkey(
900-
state, withdrawal_request.validator_pubkey
901-
)
902-
903924
yield "pre", state
904925
yield "withdrawal_request", withdrawal_request
905926

@@ -912,14 +933,7 @@ def run_withdrawal_request_processing(
912933
yield "post", None
913934
return
914935

915-
pre_exit_epoch = state.validators[validator_index].exit_epoch
916-
pre_pending_partial_withdrawals = state.pending_partial_withdrawals.copy()
917-
pre_balance = state.balances[validator_index]
918-
pre_effective_balance = state.validators[validator_index].effective_balance
919936
pre_state = state.copy()
920-
expected_amount_to_withdraw = compute_amount_to_withdraw(
921-
spec, state, validator_index, withdrawal_request.amount
922-
)
923937

924938
spec.process_withdrawal_request(
925939
state, withdrawal_request
@@ -931,6 +945,13 @@ def run_withdrawal_request_processing(
931945
# No-op
932946
assert pre_state == state
933947
else:
948+
validator_index = get_validator_index_by_pubkey(
949+
state, withdrawal_request.validator_pubkey
950+
)
951+
pre_exit_epoch = pre_state.validators[validator_index].exit_epoch
952+
pre_pending_partial_withdrawals = pre_state.pending_partial_withdrawals.copy()
953+
pre_balance = pre_state.balances[validator_index]
954+
pre_effective_balance = pre_state.validators[validator_index].effective_balance
934955
assert state.balances[validator_index] == pre_balance
935956
assert (
936957
state.validators[validator_index].effective_balance == pre_effective_balance
@@ -943,6 +964,9 @@ def run_withdrawal_request_processing(
943964
assert state.pending_partial_withdrawals == pre_pending_partial_withdrawals
944965
# Partial withdrawal request
945966
else:
967+
expected_amount_to_withdraw = compute_amount_to_withdraw(
968+
spec, pre_state, validator_index, withdrawal_request.amount
969+
)
946970
assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH
947971
expected_withdrawable_epoch = (
948972
state.earliest_exit_epoch

0 commit comments

Comments
 (0)