Skip to content

Commit 3b52edf

Browse files
committed
Add EIP-7549 tests and Electra random tests
1 parent 88f5dde commit 3b52edf

File tree

8 files changed

+556
-2
lines changed

8 files changed

+556
-2
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from eth2spec.test.context import (
2+
always_bls,
3+
spec_state_test,
4+
with_electra_and_later,
5+
)
6+
from eth2spec.test.helpers.attestations import (
7+
run_attestation_processing,
8+
get_valid_attestation,
9+
sign_attestation,
10+
)
11+
from eth2spec.test.helpers.state import (
12+
next_slots,
13+
)
14+
15+
16+
@with_electra_and_later
17+
@spec_state_test
18+
def test_invalid_attestation_data_index_not_zero(spec, state):
19+
"""
20+
EIP-7549 test
21+
"""
22+
committee_index = 1
23+
attestation = get_valid_attestation(spec, state, index=committee_index)
24+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
25+
26+
# flip the attestations index to make it non-zero and invalid
27+
assert committee_index == spec.get_committee_indices(attestation.committee_bits)[0]
28+
attestation.data.index = committee_index
29+
30+
sign_attestation(spec, state, attestation)
31+
32+
yield from run_attestation_processing(spec, state, attestation, valid=False)
33+
34+
35+
@with_electra_and_later
36+
@spec_state_test
37+
@always_bls
38+
def test_invalid_committe_index(spec, state):
39+
"""
40+
EIP-7549 test
41+
"""
42+
committee_index = 0
43+
attestation = get_valid_attestation(spec, state, index=committee_index, signed=True)
44+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
45+
46+
# flip the bits of the attestation to make it invalid
47+
assert attestation.committee_bits[committee_index] == 1
48+
attestation.committee_bits[committee_index] = 0
49+
attestation.committee_bits[committee_index + 1] = 1
50+
51+
yield from run_attestation_processing(spec, state, attestation, valid=False)
52+
53+
54+
@with_electra_and_later
55+
@spec_state_test
56+
def test_invalid_too_many_committe_bits(spec, state):
57+
"""
58+
EIP-7549 test
59+
"""
60+
committee_index = 0
61+
attestation = get_valid_attestation(spec, state, index=committee_index, signed=True)
62+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
63+
64+
attestation.committee_bits[committee_index + 1] = 1
65+
66+
yield from run_attestation_processing(spec, state, attestation, valid=False)
67+
68+
69+
@with_electra_and_later
70+
@spec_state_test
71+
def test_invalid_nonset_committe_bits(spec, state):
72+
"""
73+
EIP-7549 test
74+
"""
75+
committee_index = 0
76+
attestation = get_valid_attestation(spec, state, index=committee_index, signed=True)
77+
next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY)
78+
79+
attestation.committee_bits[committee_index] = 0
80+
81+
yield from run_attestation_processing(spec, state, attestation, valid=False)

tests/core/pyspec/eth2spec/test/electra/random/__init__.py

Whitespace-only changes.

tests/core/pyspec/eth2spec/test/electra/random/test_random.py

Lines changed: 438 additions & 0 deletions
Large diffs are not rendered by default.

tests/core/pyspec/eth2spec/test/utils/randomized_block_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ def randomize_state_deneb(spec, state, stats, exit_fraction=0.1, slash_fraction=
9999
return scenario_state
100100

101101

102+
def randomize_state_electra(spec, state, stats, exit_fraction=0.1, slash_fraction=0.1):
103+
scenario_state = randomize_state_deneb(
104+
spec,
105+
state,
106+
stats,
107+
exit_fraction=exit_fraction,
108+
slash_fraction=slash_fraction,
109+
)
110+
return scenario_state
111+
112+
102113
# epochs
103114

104115
def epochs_until_leak(spec):
@@ -248,6 +259,12 @@ def random_block_deneb(spec, state, signed_blocks, scenario_state, rng=Random(34
248259
return block
249260

250261

262+
def random_block_electra(spec, state, signed_blocks, scenario_state, rng=Random(3456)):
263+
block = random_block_deneb(spec, state, signed_blocks, scenario_state, rng=rng)
264+
265+
return block
266+
267+
251268
# validations
252269

253270
def no_op_validation(_spec, _state):

tests/generators/operations/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
deneb_mods = combine_mods(_new_deneb_mods, capella_mods)
4545

4646
_new_electra_mods = {key: 'eth2spec.test.electra.block_processing.test_process_' + key for key in [
47+
'attestation',
4748
'deposit_receipt',
4849
'execution_layer_exit',
4950
]}

tests/generators/random/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ all:
77
rm -f ../../core/pyspec/eth2spec/test/bellatrix/random/test_random.py
88
rm -f ../../core/pyspec/eth2spec/test/capella/random/test_random.py
99
rm -f ../../core/pyspec/eth2spec/test/deneb/random/test_random.py
10+
rm -f ../../core/pyspec/eth2spec/test/electra/random/test_random.py
1011
python3 generate.py phase0 > ../../core/pyspec/eth2spec/test/phase0/random/test_random.py
1112
python3 generate.py altair > ../../core/pyspec/eth2spec/test/altair/random/test_random.py
1213
python3 generate.py bellatrix > ../../core/pyspec/eth2spec/test/bellatrix/random/test_random.py
1314
python3 generate.py capella > ../../core/pyspec/eth2spec/test/capella/random/test_random.py
1415
python3 generate.py deneb > ../../core/pyspec/eth2spec/test/deneb/random/test_random.py
16+
python3 generate.py electra > ../../core/pyspec/eth2spec/test/electra/random/test_random.py

tests/generators/random/generate.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
randomize_state_bellatrix,
2323
randomize_state_capella,
2424
randomize_state_deneb,
25+
randomize_state_electra,
2526
random_block,
2627
random_block_altair_with_cycling_sync_committee_participation,
2728
random_block_bellatrix,
2829
random_block_capella,
2930
random_block_deneb,
31+
random_block_electra,
3032
last_slot_in_epoch,
3133
random_slot_in_epoch,
3234
penultimate_slot_in_epoch,
@@ -36,7 +38,7 @@
3638
transition_to_leaking,
3739
transition_without_leak,
3840
)
39-
from eth2spec.test.helpers.constants import PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB
41+
from eth2spec.test.helpers.constants import PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, ELECTRA
4042

4143

4244
# Ensure this many blocks are present in *each* randomized scenario
@@ -281,5 +283,12 @@ def run_generate_tests_to_std_out(phase, state_randomizer, block_randomizer):
281283
state_randomizer=randomize_state_deneb,
282284
block_randomizer=random_block_deneb,
283285
)
286+
if ELECTRA in sys.argv:
287+
did_generate = True
288+
run_generate_tests_to_std_out(
289+
ELECTRA,
290+
state_randomizer=randomize_state_electra,
291+
block_randomizer=random_block_electra,
292+
)
284293
if not did_generate:
285294
warnings.warn("no phase given for test generation")

tests/generators/random/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from eth2spec.test.helpers.constants import PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB
1+
from eth2spec.test.helpers.constants import (
2+
PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, ELECTRA,
3+
)
24
from eth2spec.gen_helpers.gen_from_tests.gen import run_state_test_generators
35

46

@@ -18,13 +20,17 @@
1820
deneb_mods = {key: 'eth2spec.test.deneb.random.test_' + key for key in [
1921
'random',
2022
]}
23+
electra_mods = {key: 'eth2spec.test.electra.random.test_' + key for key in [
24+
'random',
25+
]}
2126

2227
all_mods = {
2328
PHASE0: phase_0_mods,
2429
ALTAIR: altair_mods,
2530
BELLATRIX: bellatrix_mods,
2631
CAPELLA: capella_mods,
2732
DENEB: deneb_mods,
33+
ELECTRA: electra_mods,
2834
}
2935

3036
run_state_test_generators(runner_name="random", all_mods=all_mods)

0 commit comments

Comments
 (0)