Skip to content

Commit

Permalink
Merge pull request #1558 from ethereum/other_deposit_fork_version
Browse files Browse the repository at this point in the history
deposit with other fork version
  • Loading branch information
djrtwo committed Jan 6, 2020
2 parents a295451 + 8391d8e commit 83abd3e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
27 changes: 10 additions & 17 deletions test_libs/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,45 @@
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,
amount=deposit_data.amount)
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)


def deposit_from_context(spec, deposit_data_list, index):
deposit_data = deposit_data_list[index]
root = hash_tree_root(List[spec.DepositData, 2**spec.DEPOSIT_CONTRACT_TREE_DEPTH](*deposit_data_list))
tree = calc_merkle_tree_from_leaves(tuple([d.hash_tree_root() for d in deposit_data_list]))
proof = list(get_merkle_proof(tree, item_index=index, tree_len=32)) + [(index + 1).to_bytes(32, 'little')]
Expand All @@ -70,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,
Expand Down Expand Up @@ -98,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
build_deposit,
prepare_state_and_deposit,
sign_deposit_data,
)
deposit_from_context)
from eth2spec.test.helpers.state import get_balance
from eth2spec.test.helpers.keys import privkeys, pubkeys
from eth2spec.utils.bls import bls_sign


def run_deposit_processing(spec, state, deposit, validator_index, valid=True, effective=True):
Expand Down Expand Up @@ -93,6 +94,48 @@ def test_new_deposit_over_max(spec, state):
yield from run_deposit_processing(spec, state, deposit, validator_index)


@with_all_phases
@spec_state_test
@always_bls
def test_invalid_sig_other_version(spec, state):
validator_index = len(state.validators)
amount = spec.MAX_EFFECTIVE_BALANCE

pubkey = pubkeys[validator_index]
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(
message_hash=spec.hash_tree_root(
spec.DepositMessage(pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount)),
privkey=privkey,
domain=spec.compute_domain(domain_type=spec.DOMAIN_DEPOSIT, fork_version=spec.Version('0xaabbccdd')),
)
)
deposit, root, _ = deposit_from_context(spec, [deposit_data], 0)

state.eth1_deposit_index = 0
state.eth1_data.deposit_root = root
state.eth1_data.deposit_count = 1

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
Expand Down Expand Up @@ -155,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,
Expand All @@ -171,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,
Expand All @@ -197,6 +238,6 @@ def test_bad_merkle_proof(spec, state):
# mess up merkle branch
deposit.proof[5] = spec.Bytes32()

sign_deposit_data(spec, deposit.data, privkeys[validator_index], state=state)
sign_deposit_data(spec, deposit.data, privkeys[validator_index])

yield from run_deposit_processing(spec, state, deposit, validator_index, valid=False)

1 comment on commit 83abd3e

@maicoco
Copy link

@maicoco maicoco commented on 83abd3e Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.