Skip to content

Commit daf1002

Browse files
authored
Merge pull request #3892 from ensi321/dev
Randomize validator index in partial withdrawal test
2 parents a5990f9 + 725f963 commit daf1002

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

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

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from eth2spec.test.context import (
23
spec_state_test,
34
expect_assertion_error,
@@ -19,6 +20,31 @@
1920
@with_electra_and_later
2021
@spec_state_test
2122
def test_basic_withdrawal_request(spec, state):
23+
rng = random.Random(1337)
24+
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
25+
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
26+
27+
current_epoch = spec.get_current_epoch(state)
28+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
29+
validator_pubkey = state.validators[validator_index].pubkey
30+
address = b"\x22" * 20
31+
set_eth1_withdrawal_credential_with_balance(
32+
spec, state, validator_index, address=address
33+
)
34+
withdrawal_request = spec.WithdrawalRequest(
35+
source_address=address,
36+
validator_pubkey=validator_pubkey,
37+
amount=spec.FULL_EXIT_REQUEST_AMOUNT,
38+
)
39+
40+
yield from run_withdrawal_request_processing(
41+
spec, state, withdrawal_request
42+
)
43+
44+
45+
@with_electra_and_later
46+
@spec_state_test
47+
def test_basic_withdrawal_request_with_first_validator(spec, state):
2248
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
2349
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
2450

@@ -43,11 +69,12 @@ def test_basic_withdrawal_request(spec, state):
4369
@with_electra_and_later
4470
@spec_state_test
4571
def test_basic_withdrawal_request_with_compounding_credentials(spec, state):
72+
rng = random.Random(1338)
4673
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
4774
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
4875

4976
current_epoch = spec.get_current_epoch(state)
50-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
77+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
5178
validator_pubkey = state.validators[validator_index].pubkey
5279
address = b"\x22" * 20
5380
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@@ -66,9 +93,10 @@ def test_basic_withdrawal_request_with_compounding_credentials(spec, state):
6693
@spec_state_test
6794
@with_presets([MINIMAL], "need full partial withdrawal queue")
6895
def test_basic_withdrawal_request_with_full_partial_withdrawal_queue(spec, state):
96+
rng = random.Random(1339)
6997
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
7098
current_epoch = spec.get_current_epoch(state)
71-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
99+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
72100
validator_pubkey = state.validators[validator_index].pubkey
73101
address = b"\x22" * 20
74102
set_eth1_withdrawal_credential_with_balance(
@@ -102,11 +130,12 @@ def test_basic_withdrawal_request_with_full_partial_withdrawal_queue(spec, state
102130
@with_electra_and_later
103131
@spec_state_test
104132
def test_incorrect_source_address(spec, state):
133+
rng = random.Random(1340)
105134
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
106135
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
107136

108137
current_epoch = spec.get_current_epoch(state)
109-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
138+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
110139
validator_pubkey = state.validators[validator_index].pubkey
111140
address = b"\x22" * 20
112141
incorrect_address = b"\x33" * 20
@@ -127,11 +156,12 @@ def test_incorrect_source_address(spec, state):
127156
@with_electra_and_later
128157
@spec_state_test
129158
def test_incorrect_withdrawal_credential_prefix(spec, state):
159+
rng = random.Random(1341)
130160
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
131161
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
132162

133163
current_epoch = spec.get_current_epoch(state)
134-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
164+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
135165
validator_pubkey = state.validators[validator_index].pubkey
136166
address = b"\x22" * 20
137167
set_eth1_withdrawal_credential_with_balance(
@@ -156,11 +186,12 @@ def test_incorrect_withdrawal_credential_prefix(spec, state):
156186
@with_electra_and_later
157187
@spec_state_test
158188
def test_on_withdrawal_request_initiated_validator(spec, state):
189+
rng = random.Random(1342)
159190
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
160191
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
161192

162193
current_epoch = spec.get_current_epoch(state)
163-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
194+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
164195
validator_pubkey = state.validators[validator_index].pubkey
165196
address = b"\x22" * 20
166197
set_eth1_withdrawal_credential_with_balance(
@@ -182,8 +213,9 @@ def test_on_withdrawal_request_initiated_validator(spec, state):
182213
@with_electra_and_later
183214
@spec_state_test
184215
def test_activation_epoch_less_than_shard_committee_period(spec, state):
216+
rng = random.Random(1343)
185217
current_epoch = spec.get_current_epoch(state)
186-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
218+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
187219
validator_pubkey = state.validators[validator_index].pubkey
188220
address = b"\x22" * 20
189221
set_eth1_withdrawal_credential_with_balance(
@@ -211,9 +243,10 @@ def test_activation_epoch_less_than_shard_committee_period(spec, state):
211243
@spec_state_test
212244
@with_presets([MINIMAL])
213245
def test_basic_partial_withdrawal_request(spec, state):
246+
rng = random.Random(1344)
214247
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
215248
current_epoch = spec.get_current_epoch(state)
216-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
249+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
217250
validator_pubkey = state.validators[validator_index].pubkey
218251
address = b"\x22" * 20
219252
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -243,9 +276,10 @@ def test_basic_partial_withdrawal_request(spec, state):
243276
@spec_state_test
244277
@with_presets([MINIMAL])
245278
def test_basic_partial_withdrawal_request_higher_excess_balance(spec, state):
279+
rng = random.Random(1345)
246280
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
247281
current_epoch = spec.get_current_epoch(state)
248-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
282+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
249283
validator_pubkey = state.validators[validator_index].pubkey
250284
address = b"\x22" * 20
251285
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -275,9 +309,10 @@ def test_basic_partial_withdrawal_request_higher_excess_balance(spec, state):
275309
@spec_state_test
276310
@with_presets([MINIMAL])
277311
def test_basic_partial_withdrawal_request_lower_than_excess_balance(spec, state):
312+
rng = random.Random(1346)
278313
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
279314
current_epoch = spec.get_current_epoch(state)
280-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
315+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
281316
validator_pubkey = state.validators[validator_index].pubkey
282317
address = b"\x22" * 20
283318
excess_balance = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -308,9 +343,10 @@ def test_basic_partial_withdrawal_request_lower_than_excess_balance(spec, state)
308343
@spec_state_test
309344
@with_presets([MINIMAL])
310345
def test_partial_withdrawal_request_with_pending_withdrawals(spec, state):
346+
rng = random.Random(1347)
311347
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
312348
current_epoch = spec.get_current_epoch(state)
313-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
349+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
314350
validator_pubkey = state.validators[validator_index].pubkey
315351
address = b"\x22" * 20
316352
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -349,9 +385,10 @@ def test_partial_withdrawal_request_with_pending_withdrawals(spec, state):
349385
def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(
350386
spec, state
351387
):
388+
rng = random.Random(1348)
352389
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
353390
current_epoch = spec.get_current_epoch(state)
354-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
391+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
355392
validator_pubkey = state.validators[validator_index].pubkey
356393
address = b"\x22" * 20
357394
amount = spec.UINT64_MAX
@@ -387,9 +424,10 @@ def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(
387424
@spec_state_test
388425
@with_presets([MINIMAL])
389426
def test_partial_withdrawal_request_with_high_balance(spec, state):
427+
rng = random.Random(1349)
390428
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
391429
current_epoch = spec.get_current_epoch(state)
392-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
430+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
393431
validator_pubkey = state.validators[validator_index].pubkey
394432
address = b"\x22" * 20
395433
amount = spec.MAX_EFFECTIVE_BALANCE_ELECTRA
@@ -424,9 +462,10 @@ def test_partial_withdrawal_request_with_high_balance(spec, state):
424462
@spec_state_test
425463
@with_presets([MINIMAL])
426464
def test_partial_withdrawal_request_with_high_amount(spec, state):
465+
rng = random.Random(1350)
427466
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
428467
current_epoch = spec.get_current_epoch(state)
429-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
468+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
430469
validator_pubkey = state.validators[validator_index].pubkey
431470
address = b"\x22" * 20
432471
# Set high amount requested to withdraw
@@ -457,9 +496,10 @@ def test_partial_withdrawal_request_with_high_amount(spec, state):
457496
@spec_state_test
458497
@with_presets([MINIMAL])
459498
def test_partial_withdrawal_request_with_low_amount(spec, state):
499+
rng = random.Random(1351)
460500
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
461501
current_epoch = spec.get_current_epoch(state)
462-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
502+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
463503
validator_pubkey = state.validators[validator_index].pubkey
464504
address = b"\x22" * 20
465505
amount = 1
@@ -492,9 +532,10 @@ def test_partial_withdrawal_request_with_low_amount(spec, state):
492532
@spec_state_test
493533
@with_presets([MINIMAL], "need full partial withdrawal queue")
494534
def test_partial_withdrawal_queue_full(spec, state):
535+
rng = random.Random(1352)
495536
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
496537
current_epoch = spec.get_current_epoch(state)
497-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
538+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
498539
validator_pubkey = state.validators[validator_index].pubkey
499540
address = b"\x22" * 20
500541
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -522,9 +563,10 @@ def test_partial_withdrawal_queue_full(spec, state):
522563
@with_electra_and_later
523564
@spec_state_test
524565
def test_no_compounding_credentials(spec, state):
566+
rng = random.Random(1353)
525567
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
526568
current_epoch = spec.get_current_epoch(state)
527-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
569+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
528570
validator_pubkey = state.validators[validator_index].pubkey
529571
address = b"\x22" * 20
530572
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -551,9 +593,10 @@ def test_no_compounding_credentials(spec, state):
551593
@with_electra_and_later
552594
@spec_state_test
553595
def test_no_excess_balance(spec, state):
596+
rng = random.Random(1354)
554597
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
555598
current_epoch = spec.get_current_epoch(state)
556-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
599+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
557600
validator_pubkey = state.validators[validator_index].pubkey
558601
address = b"\x22" * 20
559602
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -573,9 +616,10 @@ def test_no_excess_balance(spec, state):
573616
@with_electra_and_later
574617
@spec_state_test
575618
def test_pending_withdrawals_consume_all_excess_balance(spec, state):
619+
rng = random.Random(1355)
576620
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
577621
current_epoch = spec.get_current_epoch(state)
578-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
622+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
579623
validator_pubkey = state.validators[validator_index].pubkey
580624
address = b"\x22" * 20
581625
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -603,9 +647,10 @@ def test_pending_withdrawals_consume_all_excess_balance(spec, state):
603647
@with_electra_and_later
604648
@spec_state_test
605649
def test_insufficient_effective_balance(spec, state):
650+
rng = random.Random(1356)
606651
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
607652
current_epoch = spec.get_current_epoch(state)
608-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
653+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
609654
validator_pubkey = state.validators[validator_index].pubkey
610655
address = b"\x22" * 20
611656
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -632,11 +677,12 @@ def test_insufficient_effective_balance(spec, state):
632677
@with_electra_and_later
633678
@spec_state_test
634679
def test_partial_withdrawal_incorrect_source_address(spec, state):
680+
rng = random.Random(1357)
635681
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
636682
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
637683

638684
current_epoch = spec.get_current_epoch(state)
639-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
685+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
640686
validator_pubkey = state.validators[validator_index].pubkey
641687
address = b"\x22" * 20
642688
incorrect_address = b"\x33" * 20
@@ -658,11 +704,12 @@ def test_partial_withdrawal_incorrect_source_address(spec, state):
658704
@with_electra_and_later
659705
@spec_state_test
660706
def test_partial_withdrawal_incorrect_withdrawal_credential_prefix(spec, state):
707+
rng = random.Random(1358)
661708
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
662709
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
663710

664711
current_epoch = spec.get_current_epoch(state)
665-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
712+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
666713
validator_pubkey = state.validators[validator_index].pubkey
667714
address = b"\x22" * 20
668715
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -687,11 +734,12 @@ def test_partial_withdrawal_incorrect_withdrawal_credential_prefix(spec, state):
687734
@with_electra_and_later
688735
@spec_state_test
689736
def test_partial_withdrawal_on_exit_initiated_validator(spec, state):
737+
rng = random.Random(1359)
690738
# move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit
691739
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
692740

693741
current_epoch = spec.get_current_epoch(state)
694-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
742+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
695743
validator_pubkey = state.validators[validator_index].pubkey
696744
address = b"\x22" * 20
697745
amount = spec.EFFECTIVE_BALANCE_INCREMENT
@@ -715,8 +763,9 @@ def test_partial_withdrawal_on_exit_initiated_validator(spec, state):
715763
def test_partial_withdrawal_activation_epoch_less_than_shard_committee_period(
716764
spec, state
717765
):
766+
rng = random.Random(1360)
718767
current_epoch = spec.get_current_epoch(state)
719-
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
768+
validator_index = rng.choice(spec.get_active_validator_indices(state, current_epoch))
720769
validator_pubkey = state.validators[validator_index].pubkey
721770
address = b"\x22" * 20
722771
amount = spec.EFFECTIVE_BALANCE_INCREMENT

0 commit comments

Comments
 (0)