From 60954286f9e41f2870721889628ec91494964ae9 Mon Sep 17 00:00:00 2001 From: protolambda Date: Mon, 6 Jan 2020 16:16:38 +0100 Subject: [PATCH] make tests correctly sign for general genesis-domain --- .../pyspec/eth2spec/test/helpers/deposits.py | 22 +++++-------------- .../block_processing/test_process_deposit.py | 15 +++++++++++-- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index 125a9e73c..071e177fd 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -5,27 +5,18 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_typing import List -def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, state=None, signed=False): +def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, signed=False): deposit_data = spec.DepositData( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, ) if signed: - sign_deposit_data(spec, deposit_data, privkey, state) + sign_deposit_data(spec, deposit_data, privkey) return deposit_data -def sign_deposit_data(spec, deposit_data, privkey, state=None): - if state is None: - # Genesis - domain = spec.compute_domain(spec.DOMAIN_DEPOSIT) - else: - domain = spec.get_domain( - state, - spec.DOMAIN_DEPOSIT, - ) - +def sign_deposit_data(spec, deposit_data, privkey): deposit_message = spec.DepositMessage( pubkey=deposit_data.pubkey, withdrawal_credentials=deposit_data.withdrawal_credentials, @@ -33,20 +24,19 @@ def sign_deposit_data(spec, deposit_data, privkey, state=None): signature = bls_sign( message_hash=hash_tree_root(deposit_message), privkey=privkey, - domain=domain, + domain=spec.compute_domain(spec.DOMAIN_DEPOSIT), ) deposit_data.signature = signature def build_deposit(spec, - state, deposit_data_list, pubkey, privkey, amount, withdrawal_credentials, signed): - deposit_data = build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, state=state, signed=signed) + deposit_data = build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, signed=signed) index = len(deposit_data_list) deposit_data_list.append(deposit_data) return deposit_from_context(spec, deposit_data_list, index) @@ -75,7 +65,6 @@ def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] deposit, root, deposit_data_list = build_deposit( spec, - None, deposit_data_list, pubkey, privkey, @@ -103,7 +92,6 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c deposit, root, deposit_data_list = build_deposit( spec, - state, deposit_data_list, pubkey, privkey, diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py index 71d23dcba..25222664d 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_deposit.py @@ -105,6 +105,7 @@ def test_invalid_sig_other_version(spec, state): privkey = privkeys[validator_index] withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + # Go through the effort of manually signing, not something normally done. This sig domain will be invalid. deposit_data = spec.DepositData( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, signature=bls_sign( @@ -123,6 +124,18 @@ def test_invalid_sig_other_version(spec, state): yield from run_deposit_processing(spec, state, deposit, validator_index, valid=True, effective=False) +@with_all_phases +@spec_state_test +@always_bls +def test_valid_sig_but_forked_state(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + # deposits will always be valid, regardless of the current fork + state.fork.current_version = spec.Version('0x1234abcd') + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing(spec, state, deposit, validator_index, valid=True, effective=True) + + @with_all_phases @spec_state_test @always_bls @@ -185,7 +198,6 @@ def test_wrong_deposit_for_deposit_count(spec, state): privkey_1 = privkeys[index_1] _, _, deposit_data_leaves = build_deposit( spec, - state, deposit_data_leaves, pubkey_1, privkey_1, @@ -201,7 +213,6 @@ def test_wrong_deposit_for_deposit_count(spec, state): privkey_2 = privkeys[index_2] deposit_2, root_2, deposit_data_leaves = build_deposit( spec, - state, deposit_data_leaves, pubkey_2, privkey_2,