From c108d1a3563e97e8b5f28f5645f66c4ddc3fdb6e Mon Sep 17 00:00:00 2001 From: Denis Bogdanas Date: Sun, 6 Oct 2019 14:59:13 +0300 Subject: [PATCH] test for initialize_beacon_state_from_eth1, case when some small deposits don't contribute to active balance. --- .../test/genesis/test_initialization.py | 39 ++++++++++++++++++- .../eth2spec/test/genesis/test_validity.py | 6 +-- .../pyspec/eth2spec/test/helpers/deposits.py | 7 ++-- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/genesis/test_initialization.py b/test_libs/pyspec/eth2spec/test/genesis/test_initialization.py index 2ff57be74..6c9a46ed3 100644 --- a/test_libs/pyspec/eth2spec/test/genesis/test_initialization.py +++ b/test_libs/pyspec/eth2spec/test/genesis/test_initialization.py @@ -8,7 +8,7 @@ from eth2spec.test.helpers.deposits import ( @spec_test def test_initialize_beacon_state_from_eth1(spec): deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT - deposits, deposit_root = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) + deposits, deposit_root, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) eth1_block_hash = b'\x12' * 32 eth1_timestamp = spec.MIN_GENESIS_TIME @@ -25,6 +25,43 @@ def test_initialize_beacon_state_from_eth1(spec): assert state.eth1_data.deposit_root == deposit_root assert state.eth1_data.deposit_count == deposit_count assert state.eth1_data.block_hash == eth1_block_hash + assert spec.get_total_active_balance(state) == deposit_count * spec.MAX_EFFECTIVE_BALANCE # yield state yield 'state', state + + +@with_phases(['phase0']) +@spec_test +def test_initialize_beacon_state_some_small_balances(spec): + main_deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT + main_deposits, _, deposit_data_list = prepare_genesis_deposits(spec, main_deposit_count, + spec.MAX_EFFECTIVE_BALANCE, signed=True) + # For deposits above, and for another deposit_count, add a balance of EFFECTIVE_BALANCE_INCREMENT + small_deposit_count = main_deposit_count * 2 + small_deposits, deposit_root, _ = prepare_genesis_deposits(spec, small_deposit_count, + spec.MIN_DEPOSIT_AMOUNT, + signed=True, + deposit_data_list=deposit_data_list) + deposits = main_deposits + small_deposits + + eth1_block_hash = b'\x12' * 32 + eth1_timestamp = spec.MIN_GENESIS_TIME + + yield 'eth1_block_hash', eth1_block_hash + yield 'eth1_timestamp', eth1_timestamp + yield 'deposits', deposits + + # initialize beacon_state + state = spec.initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits) + + assert state.genesis_time == eth1_timestamp - eth1_timestamp % spec.SECONDS_PER_DAY + 2 * spec.SECONDS_PER_DAY + assert len(state.validators) == small_deposit_count + assert state.eth1_data.deposit_root == deposit_root + assert state.eth1_data.deposit_count == len(deposits) + assert state.eth1_data.block_hash == eth1_block_hash + # only main deposits participate to the active balance + assert spec.get_total_active_balance(state) == main_deposit_count * spec.MAX_EFFECTIVE_BALANCE + + # yield state + yield 'state', state \ No newline at end of file diff --git a/test_libs/pyspec/eth2spec/test/genesis/test_validity.py b/test_libs/pyspec/eth2spec/test/genesis/test_validity.py index 07ad3a73c..a003938e7 100644 --- a/test_libs/pyspec/eth2spec/test/genesis/test_validity.py +++ b/test_libs/pyspec/eth2spec/test/genesis/test_validity.py @@ -6,7 +6,7 @@ from eth2spec.test.helpers.deposits import ( def create_valid_beacon_state(spec): deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT - deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) + deposits, _, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) eth1_block_hash = b'\x12' * 32 eth1_timestamp = spec.MIN_GENESIS_TIME @@ -65,7 +65,7 @@ def test_is_valid_genesis_state_true_more_balance(spec): @spec_test def test_is_valid_genesis_state_true_one_more_validator(spec): deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT + 1 - deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) + deposits, _, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) eth1_block_hash = b'\x12' * 32 eth1_timestamp = spec.MIN_GENESIS_TIME @@ -78,7 +78,7 @@ def test_is_valid_genesis_state_true_one_more_validator(spec): @spec_test def test_is_valid_genesis_state_false_not_enough_validator(spec): deposit_count = spec.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT - 1 - deposits, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) + deposits, _, _ = prepare_genesis_deposits(spec, deposit_count, spec.MAX_EFFECTIVE_BALANCE, signed=True) eth1_block_hash = b'\x12' * 32 eth1_timestamp = spec.MIN_GENESIS_TIME diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index 8dc6b3b58..b3d230eaf 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -55,8 +55,9 @@ def build_deposit(spec, return deposit, root, deposit_data_list -def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False): - deposit_data_list = [] +def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False, deposit_data_list=None): + if deposit_data_list is None: + deposit_data_list = [] genesis_deposits = [] for validator_index in range(genesis_validator_count): pubkey = pubkeys[validator_index] @@ -75,7 +76,7 @@ def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False ) genesis_deposits.append(deposit) - return genesis_deposits, root + return genesis_deposits, root, deposit_data_list def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_credentials=None, signed=False):