port deposit test from #1133

This commit is contained in:
Danny Ryan 2019-05-31 13:54:58 -06:00
parent a0fd345a77
commit 8b65b95bd4
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
2 changed files with 28 additions and 6 deletions

View File

@ -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)

View File

@ -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