This commit is contained in:
Justin Drake 2019-06-29 17:29:21 +01:00
parent b162a8ff8a
commit f0a8e39243
2 changed files with 19 additions and 25 deletions

View File

@ -437,7 +437,7 @@ class Attestation(Container):
```python ```python
class Deposit(Container): class Deposit(Container):
proof: Vector[Hash, DEPOSIT_CONTRACT_TREE_DEPTH] # Merkle path to deposit root proof: Vector[Hash, DEPOSIT_CONTRACT_TREE_DEPTH + 1] # Merkle path to deposit root
data: DepositData data: DepositData
``` ```
@ -1682,7 +1682,7 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None:
assert verify_merkle_branch( assert verify_merkle_branch(
leaf=hash_tree_root(deposit.data), leaf=hash_tree_root(deposit.data),
proof=deposit.proof, proof=deposit.proof,
depth=DEPOSIT_CONTRACT_TREE_DEPTH, depth=DEPOSIT_CONTRACT_TREE_DEPTH + 1,
index=state.eth1_deposit_index, index=state.eth1_deposit_index,
root=state.eth1_data.deposit_root, root=state.eth1_data.deposit_root,
) )

View File

@ -1,11 +1,13 @@
from eth2spec.test.helpers.keys import pubkeys, privkeys from eth2spec.test.helpers.keys import pubkeys, privkeys
from eth2spec.utils.bls import bls_sign from eth2spec.utils.bls import bls_sign
from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merkle_root, get_merkle_proof from eth2spec.utils.merkle_minimal import calc_merkle_tree_from_leaves, get_merkle_root, get_merkle_proof
from eth2spec.utils.ssz.ssz_impl import signing_root from eth2spec.utils.ssz.ssz_impl import signing_root, hash_tree_root
from eth2spec.utils.ssz.ssz_typing import List
from eth2spec.phase0.spec import DepositData
def build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed=False): def build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed=False):
deposit_data = spec.DepositData( deposit_data = DepositData(
pubkey=pubkey, pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials, withdrawal_credentials=withdrawal_credentials,
amount=amount, amount=amount,
@ -29,27 +31,21 @@ def sign_deposit_data(spec, state, deposit_data, privkey):
def build_deposit(spec, def build_deposit(spec,
state, state,
deposit_data_leaves, deposit_data_list,
pubkey, pubkey,
privkey, privkey,
amount, amount,
withdrawal_credentials, withdrawal_credentials,
signed): signed):
deposit_data = build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed) deposit_data = build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed)
deposit_data_list.append(deposit_data)
item = deposit_data.hash_tree_root() index = len(deposit_data_list)
index = len(deposit_data_leaves) root = hash_tree_root(List[DepositData, 2**32](*deposit_data_list))
deposit_data_leaves.append(item) tree = calc_merkle_tree_from_leaves(tuple([d.hash_tree_root() for d in deposit_data_list]))
tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves)) proof = list(get_merkle_proof(tree, item_index=index)) + [index.to_bytes(32, 'little')]
root = get_merkle_root((tuple(deposit_data_leaves))) leaf = deposit_data.hash_tree_root()
proof = list(get_merkle_proof(tree, item_index=index)) assert spec.verify_merkle_branch(leaf, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH + 1, index, root)
assert spec.verify_merkle_branch(item, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH, index, root) deposit = spec.Deposit(proof, index, deposit_data)
deposit = spec.Deposit(
proof=list(proof),
index=index,
data=deposit_data,
)
return deposit, root, deposit_data_leaves return deposit, root, deposit_data_leaves
@ -58,9 +54,7 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c
""" """
Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount. Prepare the state for the deposit, and create a deposit for the given validator, depositing the given amount.
""" """
pre_validator_count = len(state.validators) deposit_data_list = []
# fill previous deposits with zero-hash
deposit_data_leaves = [spec.ZERO_HASH] * pre_validator_count
pubkey = pubkeys[validator_index] pubkey = pubkeys[validator_index]
privkey = privkeys[validator_index] privkey = privkeys[validator_index]
@ -69,10 +63,10 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c
if withdrawal_credentials is None: if withdrawal_credentials is None:
withdrawal_credentials = spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(pubkey)[1:] withdrawal_credentials = spec.int_to_bytes(spec.BLS_WITHDRAWAL_PREFIX, length=1) + spec.hash(pubkey)[1:]
deposit, root, deposit_data_leaves = build_deposit( deposit, root, deposit_data_list = build_deposit(
spec, spec,
state, state,
deposit_data_leaves, deposit_data_list,
pubkey, pubkey,
privkey, privkey,
amount, amount,
@ -81,5 +75,5 @@ def prepare_state_and_deposit(spec, state, validator_index, amount, withdrawal_c
) )
state.eth1_data.deposit_root = root state.eth1_data.deposit_root = root
state.eth1_data.deposit_count = len(deposit_data_leaves) state.eth1_data.deposit_count = len(deposit_data_list)
return deposit return deposit