Skip to content

Commit 1c081a0

Browse files
authored
Merge pull request #3731 from ethereum/rng
Fix rng of tests
2 parents 871c772 + c70bcba commit 1c081a0

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

tests/core/pyspec/eth2spec/test/bellatrix/block_processing/test_process_execution_payload.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def test_zero_length_transaction_regular_payload(spec, state):
324324
yield from run_zero_length_transaction_test(spec, state)
325325

326326

327-
def run_randomized_non_validated_execution_fields_test(spec, state, execution_valid=True, rng=Random(5555)):
327+
def run_randomized_non_validated_execution_fields_test(spec, state, rng, execution_valid=True):
328328
next_slot(spec, state)
329329
execution_payload = build_randomized_execution_payload(spec, state, rng)
330330

@@ -340,28 +340,28 @@ def run_randomized_non_validated_execution_fields_test(spec, state, execution_va
340340
def test_randomized_non_validated_execution_fields_first_payload__execution_valid(spec, state):
341341
rng = Random(1111)
342342
state = build_state_with_incomplete_transition(spec, state)
343-
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng=rng)
343+
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng)
344344

345345

346346
@with_bellatrix_and_later
347347
@spec_state_test
348348
def test_randomized_non_validated_execution_fields_regular_payload__execution_valid(spec, state):
349349
rng = Random(2222)
350350
state = build_state_with_complete_transition(spec, state)
351-
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng=rng)
351+
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng)
352352

353353

354354
@with_bellatrix_and_later
355355
@spec_state_test
356356
def test_invalid_randomized_non_validated_execution_fields_first_payload__execution_invalid(spec, state):
357357
rng = Random(3333)
358358
state = build_state_with_incomplete_transition(spec, state)
359-
yield from run_randomized_non_validated_execution_fields_test(spec, state, execution_valid=False, rng=rng)
359+
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng, execution_valid=False)
360360

361361

362362
@with_bellatrix_and_later
363363
@spec_state_test
364364
def test_invalid_randomized_non_validated_execution_fields_regular_payload__execution_invalid(spec, state):
365365
rng = Random(4444)
366366
state = build_state_with_complete_transition(spec, state)
367-
yield from run_randomized_non_validated_execution_fields_test(spec, state, execution_valid=False, rng=rng)
367+
yield from run_randomized_non_validated_execution_fields_test(spec, state, rng, execution_valid=False)

tests/core/pyspec/eth2spec/test/capella/block_processing/test_process_withdrawals.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_success_zero_expected_withdrawals(spec, state):
113113
@spec_state_test
114114
def test_success_one_full_withdrawal(spec, state):
115115
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
116-
spec, state, num_full_withdrawals=1)
116+
spec, state, rng=random.Random(42), num_full_withdrawals=1)
117117
assert len(fully_withdrawable_indices) == 1
118118
assert len(partial_withdrawals_indices) == 0
119119

@@ -130,7 +130,7 @@ def test_success_one_full_withdrawal(spec, state):
130130
@spec_state_test
131131
def test_success_one_partial_withdrawal(spec, state):
132132
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
133-
spec, state, num_partial_withdrawals=1)
133+
spec, state, rng=random.Random(42), num_partial_withdrawals=1)
134134
assert len(fully_withdrawable_indices) == 0
135135
assert len(partial_withdrawals_indices) == 1
136136
for index in partial_withdrawals_indices:
@@ -153,6 +153,7 @@ def test_success_mixed_fully_and_partial_withdrawable(spec, state):
153153
num_partial_withdrawals = spec.MAX_WITHDRAWALS_PER_PAYLOAD - num_full_withdrawals
154154
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
155155
spec, state,
156+
rng=random.Random(42),
156157
num_full_withdrawals=num_full_withdrawals,
157158
num_partial_withdrawals=num_partial_withdrawals,
158159
)
@@ -174,7 +175,7 @@ def test_success_all_fully_withdrawable_in_one_sweep(spec, state):
174175

175176
withdrawal_count = len(state.validators)
176177
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
177-
spec, state, num_full_withdrawals=withdrawal_count)
178+
spec, state, rng=random.Random(42), num_full_withdrawals=withdrawal_count)
178179

179180
next_slot(spec, state)
180181
execution_payload = build_empty_execution_payload(spec, state)
@@ -193,7 +194,7 @@ def test_success_all_fully_withdrawable(spec, state):
193194

194195
withdrawal_count = spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP
195196
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
196-
spec, state, num_full_withdrawals=withdrawal_count)
197+
spec, state, rng=random.Random(42), num_full_withdrawals=withdrawal_count)
197198

198199
next_slot(spec, state)
199200
execution_payload = build_empty_execution_payload(spec, state)
@@ -212,7 +213,7 @@ def test_success_all_partially_withdrawable_in_one_sweep(spec, state):
212213

213214
withdrawal_count = len(state.validators)
214215
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
215-
spec, state, num_partial_withdrawals=withdrawal_count)
216+
spec, state, rng=random.Random(42), num_partial_withdrawals=withdrawal_count)
216217

217218
next_slot(spec, state)
218219
execution_payload = build_empty_execution_payload(spec, state)
@@ -231,7 +232,7 @@ def test_success_all_partially_withdrawable(spec, state):
231232

232233
withdrawal_count = spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP
233234
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
234-
spec, state, num_partial_withdrawals=withdrawal_count)
235+
spec, state, rng=random.Random(42), num_partial_withdrawals=withdrawal_count)
235236

236237
next_slot(spec, state)
237238
execution_payload = build_empty_execution_payload(spec, state)
@@ -266,7 +267,7 @@ def test_invalid_non_withdrawable_non_empty_withdrawals(spec, state):
266267
@with_capella_and_later
267268
@spec_state_test
268269
def test_invalid_one_expected_full_withdrawal_and_none_in_withdrawals(spec, state):
269-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=1)
270+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_full_withdrawals=1)
270271

271272
next_slot(spec, state)
272273
execution_payload = build_empty_execution_payload(spec, state)
@@ -279,7 +280,7 @@ def test_invalid_one_expected_full_withdrawal_and_none_in_withdrawals(spec, stat
279280
@with_capella_and_later
280281
@spec_state_test
281282
def test_invalid_one_expected_partial_withdrawal_and_none_in_withdrawals(spec, state):
282-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=1)
283+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_partial_withdrawals=1)
283284

284285
next_slot(spec, state)
285286
execution_payload = build_empty_execution_payload(spec, state)
@@ -292,7 +293,7 @@ def test_invalid_one_expected_partial_withdrawal_and_none_in_withdrawals(spec, s
292293
@with_capella_and_later
293294
@spec_state_test
294295
def test_invalid_one_expected_full_withdrawal_and_duplicate_in_withdrawals(spec, state):
295-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=2)
296+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_full_withdrawals=2)
296297

297298
next_slot(spec, state)
298299
execution_payload = build_empty_execution_payload(spec, state)
@@ -305,7 +306,7 @@ def test_invalid_one_expected_full_withdrawal_and_duplicate_in_withdrawals(spec,
305306
@with_capella_and_later
306307
@spec_state_test
307308
def test_invalid_two_expected_partial_withdrawal_and_duplicate_in_withdrawals(spec, state):
308-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=2)
309+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_partial_withdrawals=2)
309310

310311
next_slot(spec, state)
311312
execution_payload = build_empty_execution_payload(spec, state)
@@ -318,7 +319,8 @@ def test_invalid_two_expected_partial_withdrawal_and_duplicate_in_withdrawals(sp
318319
@with_capella_and_later
319320
@spec_state_test
320321
def test_invalid_max_per_slot_full_withdrawals_and_one_less_in_withdrawals(spec, state):
321-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD)
322+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
323+
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD)
322324

323325
next_slot(spec, state)
324326
execution_payload = build_empty_execution_payload(spec, state)
@@ -331,7 +333,8 @@ def test_invalid_max_per_slot_full_withdrawals_and_one_less_in_withdrawals(spec,
331333
@with_capella_and_later
332334
@spec_state_test
333335
def test_invalid_max_per_slot_partial_withdrawals_and_one_less_in_withdrawals(spec, state):
334-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD)
336+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
337+
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD)
335338

336339
next_slot(spec, state)
337340
execution_payload = build_empty_execution_payload(spec, state)
@@ -344,7 +347,8 @@ def test_invalid_max_per_slot_partial_withdrawals_and_one_less_in_withdrawals(sp
344347
@with_capella_and_later
345348
@spec_state_test
346349
def test_invalid_a_lot_fully_withdrawable_too_few_in_withdrawals(spec, state):
347-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
350+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
351+
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
348352

349353
next_slot(spec, state)
350354
execution_payload = build_empty_execution_payload(spec, state)
@@ -357,7 +361,8 @@ def test_invalid_a_lot_fully_withdrawable_too_few_in_withdrawals(spec, state):
357361
@with_capella_and_later
358362
@spec_state_test
359363
def test_invalid_a_lot_partially_withdrawable_too_few_in_withdrawals(spec, state):
360-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
364+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
365+
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
361366

362367
next_slot(spec, state)
363368
execution_payload = build_empty_execution_payload(spec, state)
@@ -370,7 +375,8 @@ def test_invalid_a_lot_partially_withdrawable_too_few_in_withdrawals(spec, state
370375
@with_capella_and_later
371376
@spec_state_test
372377
def test_invalid_a_lot_mixed_withdrawable_in_queue_too_few_in_withdrawals(spec, state):
373-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD,
378+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
379+
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD,
374380
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD)
375381

376382
next_slot(spec, state)
@@ -388,7 +394,8 @@ def test_invalid_a_lot_mixed_withdrawable_in_queue_too_few_in_withdrawals(spec,
388394
@with_capella_and_later
389395
@spec_state_test
390396
def test_invalid_incorrect_withdrawal_index(spec, state):
391-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=1)
397+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
398+
num_full_withdrawals=1)
392399

393400
next_slot(spec, state)
394401
execution_payload = build_empty_execution_payload(spec, state)
@@ -401,7 +408,8 @@ def test_invalid_incorrect_withdrawal_index(spec, state):
401408
@with_capella_and_later
402409
@spec_state_test
403410
def test_invalid_incorrect_address_full(spec, state):
404-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=1)
411+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
412+
num_full_withdrawals=1)
405413

406414
next_slot(spec, state)
407415
execution_payload = build_empty_execution_payload(spec, state)
@@ -414,7 +422,8 @@ def test_invalid_incorrect_address_full(spec, state):
414422
@with_capella_and_later
415423
@spec_state_test
416424
def test_invalid_incorrect_address_partial(spec, state):
417-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=1)
425+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
426+
num_partial_withdrawals=1)
418427

419428
next_slot(spec, state)
420429
execution_payload = build_empty_execution_payload(spec, state)
@@ -427,7 +436,7 @@ def test_invalid_incorrect_address_partial(spec, state):
427436
@with_capella_and_later
428437
@spec_state_test
429438
def test_invalid_incorrect_amount_full(spec, state):
430-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=1)
439+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_full_withdrawals=1)
431440

432441
next_slot(spec, state)
433442
execution_payload = build_empty_execution_payload(spec, state)
@@ -440,7 +449,7 @@ def test_invalid_incorrect_amount_full(spec, state):
440449
@with_capella_and_later
441450
@spec_state_test
442451
def test_invalid_incorrect_amount_partial(spec, state):
443-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=1)
452+
prepare_expected_withdrawals(spec, state, rng=random.Random(42), num_full_withdrawals=1)
444453

445454
next_slot(spec, state)
446455
execution_payload = build_empty_execution_payload(spec, state)
@@ -453,7 +462,8 @@ def test_invalid_incorrect_amount_partial(spec, state):
453462
@with_capella_and_later
454463
@spec_state_test
455464
def test_invalid_one_of_many_incorrectly_full(spec, state):
456-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
465+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
466+
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
457467

458468
next_slot(spec, state)
459469
execution_payload = build_empty_execution_payload(spec, state)
@@ -472,7 +482,8 @@ def test_invalid_one_of_many_incorrectly_full(spec, state):
472482
@with_capella_and_later
473483
@spec_state_test
474484
def test_invalid_one_of_many_incorrectly_partial(spec, state):
475-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
485+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
486+
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
476487

477488
next_slot(spec, state)
478489
execution_payload = build_empty_execution_payload(spec, state)
@@ -491,7 +502,8 @@ def test_invalid_one_of_many_incorrectly_partial(spec, state):
491502
@with_capella_and_later
492503
@spec_state_test
493504
def test_invalid_many_incorrectly_full(spec, state):
494-
prepare_expected_withdrawals(spec, state, num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
505+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
506+
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
495507

496508
next_slot(spec, state)
497509
execution_payload = build_empty_execution_payload(spec, state)
@@ -510,7 +522,8 @@ def test_invalid_many_incorrectly_full(spec, state):
510522
@with_capella_and_later
511523
@spec_state_test
512524
def test_invalid_many_incorrectly_partial(spec, state):
513-
prepare_expected_withdrawals(spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
525+
prepare_expected_withdrawals(spec, state, rng=random.Random(42),
526+
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 4)
514527

515528
next_slot(spec, state)
516529
execution_payload = build_empty_execution_payload(spec, state)

tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from eth2spec.test.helpers.constants import MINIMAL
23
from eth2spec.test.helpers.forks import is_post_electra
34
from eth2spec.test.context import (
@@ -268,7 +269,8 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state):
268269

269270
def _perform_valid_withdrawal(spec, state):
270271
fully_withdrawable_indices, partial_withdrawals_indices = prepare_expected_withdrawals(
271-
spec, state, num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 2,
272+
spec, state, rng=random.Random(42),
273+
num_partial_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 2,
272274
num_full_withdrawals=spec.MAX_WITHDRAWALS_PER_PAYLOAD * 2)
273275

274276
next_slot(spec, state)

tests/core/pyspec/eth2spec/test/helpers/withdrawals.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import random
21
from eth2spec.test.helpers.forks import is_post_electra
32

43

@@ -48,8 +47,8 @@ def set_validator_partially_withdrawable(spec, state, index, excess_balance=1000
4847
assert spec.is_partially_withdrawable_validator(validator, state.balances[index])
4948

5049

51-
def prepare_expected_withdrawals(spec, state,
52-
num_full_withdrawals=0, num_partial_withdrawals=0, rng=random.Random(5566)):
50+
def prepare_expected_withdrawals(spec, state, rng,
51+
num_full_withdrawals=0, num_partial_withdrawals=0):
5352
bound = min(len(state.validators), spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP)
5453
assert num_full_withdrawals + num_partial_withdrawals <= bound
5554
eligible_validator_indices = list(range(bound))

0 commit comments

Comments
 (0)