make tests correctly sign for general genesis-domain

This commit is contained in:
protolambda 2020-01-06 16:16:38 +01:00
parent 71206c9a26
commit 60954286f9
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 18 additions and 19 deletions

View File

@ -5,27 +5,18 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import List 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( deposit_data = spec.DepositData(
pubkey=pubkey, pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials, withdrawal_credentials=withdrawal_credentials,
amount=amount, amount=amount,
) )
if signed: if signed:
sign_deposit_data(spec, deposit_data, privkey, state) sign_deposit_data(spec, deposit_data, privkey)
return deposit_data return deposit_data
def sign_deposit_data(spec, deposit_data, privkey, state=None): def sign_deposit_data(spec, deposit_data, privkey):
if state is None:
# Genesis
domain = spec.compute_domain(spec.DOMAIN_DEPOSIT)
else:
domain = spec.get_domain(
state,
spec.DOMAIN_DEPOSIT,
)
deposit_message = spec.DepositMessage( deposit_message = spec.DepositMessage(
pubkey=deposit_data.pubkey, pubkey=deposit_data.pubkey,
withdrawal_credentials=deposit_data.withdrawal_credentials, withdrawal_credentials=deposit_data.withdrawal_credentials,
@ -33,20 +24,19 @@ def sign_deposit_data(spec, deposit_data, privkey, state=None):
signature = bls_sign( signature = bls_sign(
message_hash=hash_tree_root(deposit_message), message_hash=hash_tree_root(deposit_message),
privkey=privkey, privkey=privkey,
domain=domain, domain=spec.compute_domain(spec.DOMAIN_DEPOSIT),
) )
deposit_data.signature = signature deposit_data.signature = signature
def build_deposit(spec, def build_deposit(spec,
state,
deposit_data_list, deposit_data_list,
pubkey, pubkey,
privkey, privkey,
amount, amount,
withdrawal_credentials, withdrawal_credentials,
signed): 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) index = len(deposit_data_list)
deposit_data_list.append(deposit_data) deposit_data_list.append(deposit_data)
return deposit_from_context(spec, deposit_data_list, index) 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:] withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:]
deposit, root, deposit_data_list = build_deposit( deposit, root, deposit_data_list = build_deposit(
spec, spec,
None,
deposit_data_list, deposit_data_list,
pubkey, pubkey,
privkey, privkey,
@ -103,7 +92,6 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c
deposit, root, deposit_data_list = build_deposit( deposit, root, deposit_data_list = build_deposit(
spec, spec,
state,
deposit_data_list, deposit_data_list,
pubkey, pubkey,
privkey, privkey,

View File

@ -105,6 +105,7 @@ def test_invalid_sig_other_version(spec, state):
privkey = privkeys[validator_index] privkey = privkeys[validator_index]
withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] 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( deposit_data = spec.DepositData(
pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount,
signature=bls_sign( 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) 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 @with_all_phases
@spec_state_test @spec_state_test
@always_bls @always_bls
@ -185,7 +198,6 @@ def test_wrong_deposit_for_deposit_count(spec, state):
privkey_1 = privkeys[index_1] privkey_1 = privkeys[index_1]
_, _, deposit_data_leaves = build_deposit( _, _, deposit_data_leaves = build_deposit(
spec, spec,
state,
deposit_data_leaves, deposit_data_leaves,
pubkey_1, pubkey_1,
privkey_1, privkey_1,
@ -201,7 +213,6 @@ def test_wrong_deposit_for_deposit_count(spec, state):
privkey_2 = privkeys[index_2] privkey_2 = privkeys[index_2]
deposit_2, root_2, deposit_data_leaves = build_deposit( deposit_2, root_2, deposit_data_leaves = build_deposit(
spec, spec,
state,
deposit_data_leaves, deposit_data_leaves,
pubkey_2, pubkey_2,
privkey_2, privkey_2,