From 76d69263fc58c5dc3dd252f4f7c0266984712ea4 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 28 Sep 2020 18:01:13 -0600 Subject: [PATCH] use better rng practice for reproducibility --- tests/core/pyspec/eth2spec/test/helpers/deposits.py | 11 ++++++----- .../test/phase0/genesis/test_initialization.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/helpers/deposits.py b/tests/core/pyspec/eth2spec/test/helpers/deposits.py index 418ab1579..150de07db 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/deposits.py +++ b/tests/core/pyspec/eth2spec/test/helpers/deposits.py @@ -1,4 +1,4 @@ -import random +from random import Random from eth2spec.test.helpers.keys import pubkeys, privkeys from eth2spec.utils import bls @@ -98,7 +98,8 @@ def prepare_random_genesis_deposits(spec, min_pubkey_index=0, max_amount=None, min_amount=None, - deposit_data_list=None): + deposit_data_list=None, + rng=Random(3131)): if max_amount is None: max_amount = spec.MAX_EFFECTIVE_BALANCE if min_amount is None: @@ -107,11 +108,11 @@ def prepare_random_genesis_deposits(spec, deposit_data_list = [] deposits = [] for _ in range(num_deposits): - pubkey_index = random.randint(min_pubkey_index, max_pubkey_index) + pubkey_index = rng.randint(min_pubkey_index, max_pubkey_index) pubkey = pubkeys[pubkey_index] privkey = privkeys[pubkey_index] - amount = random.randint(min_amount, max_amount) - random_byte = bytes([random.randint(0, 255)]) + amount = rng.randint(min_amount, max_amount) + random_byte = bytes([rng.randint(0, 255)]) withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(random_byte)[1:] deposit, root, deposit_data_list = build_deposit( spec, diff --git a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py index 8cd66ae2d..73ee5528f 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py +++ b/tests/core/pyspec/eth2spec/test/phase0/genesis/test_initialization.py @@ -156,7 +156,7 @@ def test_initialize_beacon_state_random_valid_genesis(spec): # Make a bunch of random deposits random_deposits, _, deposit_data_list = prepare_random_genesis_deposits( spec, - num_deposits=30, + num_deposits=20, min_pubkey_index=spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT - 5, max_pubkey_index=spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT + 5, )