From 8b65b95bd44e58012acb0e9905644d743f452371 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Fri, 31 May 2019 13:54:58 -0600 Subject: [PATCH] port deposit test from #1133 --- .../block_processing/test_process_deposit.py | 16 ++++++++++++++++ .../pyspec/eth2spec/test/helpers/deposits.py | 18 ++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py b/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py index 336af3bf7..95b13c779 100644 --- a/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py +++ b/test_libs/pyspec/eth2spec/test/block_processing/test_process_deposit.py @@ -93,6 +93,22 @@ def test_invalid_sig_top_up(state): yield from run_deposit_processing(state, deposit, validator_index, valid=True, effective=True) +@spec_state_test +def test_invalid_withdrawal_credentials_top_up(state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(b"junk")[1:] + deposit = prepare_state_and_deposit( + state, + validator_index, + amount, + withdrawal_credentials=withdrawal_credentials, + ) + + # inconsistent withdrawal credentials, in top-ups, are allowed! + yield from run_deposit_processing(state, deposit, validator_index, valid=True, effective=True) + + @spec_state_test def test_wrong_index(state): validator_index = len(state.validator_registry) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index c5deb124e..d28d0bcb8 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -8,11 +8,10 @@ from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merk from eth2spec.utils.minimal_ssz import signing_root -def build_deposit_data(state, pubkey, privkey, amount, signed=False): +def build_deposit_data(state, pubkey, privkey, amount, withdrawal_credentials, signed=False): deposit_data = DepositData( pubkey=pubkey, - # insecurely use pubkey as withdrawal key as well - withdrawal_credentials=spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:], + withdrawal_credentials=withdrawal_credentials, amount=amount, ) if signed: @@ -37,8 +36,9 @@ def build_deposit(state, pubkey, privkey, amount, + withdrawal_credentials, signed): - deposit_data = build_deposit_data(state, pubkey, privkey, amount, signed) + deposit_data = build_deposit_data(state, pubkey, privkey, amount, withdrawal_credentials, signed) item = deposit_data.hash_tree_root() index = len(deposit_data_leaves) @@ -57,7 +57,7 @@ def build_deposit(state, return deposit, root, deposit_data_leaves -def prepare_state_and_deposit(state, validator_index, amount, signed=False): +def prepare_state_and_deposit(state, validator_index, amount, withdrawal_credentials=None, signed=False): """ Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount. """ @@ -67,13 +67,19 @@ def prepare_state_and_deposit(state, validator_index, amount, signed=False): pubkey = pubkeys[validator_index] privkey = privkeys[validator_index] + + # insecurely use pubkey as withdrawal key if no credentials provided + if withdrawal_credentials is None: + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX_BYTE + spec.hash(pubkey)[1:] + deposit, root, deposit_data_leaves = build_deposit( state, deposit_data_leaves, pubkey, privkey, amount, - signed + withdrawal_credentials, + signed, ) state.latest_eth1_data.deposit_root = root