From f0a8e392434fa06e96b84249c4dd9533d8012cfb Mon Sep 17 00:00:00 2001 From: Justin Drake Date: Sat, 29 Jun 2019 17:29:21 +0100 Subject: [PATCH] WIP --- specs/core/0_beacon-chain.md | 4 +- .../pyspec/eth2spec/test/helpers/deposits.py | 40 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 6f28c0e3d..138d3fd2a 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -437,7 +437,7 @@ class Attestation(Container): ```python 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 ``` @@ -1682,7 +1682,7 @@ def process_deposit(state: BeaconState, deposit: Deposit) -> None: assert verify_merkle_branch( leaf=hash_tree_root(deposit.data), proof=deposit.proof, - depth=DEPOSIT_CONTRACT_TREE_DEPTH, + depth=DEPOSIT_CONTRACT_TREE_DEPTH + 1, index=state.eth1_deposit_index, root=state.eth1_data.deposit_root, ) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index 20ff7440f..5a0909185 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -1,11 +1,13 @@ from eth2spec.test.helpers.keys import pubkeys, privkeys 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.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): - deposit_data = spec.DepositData( + deposit_data = DepositData( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, @@ -29,27 +31,21 @@ def sign_deposit_data(spec, state, deposit_data, privkey): def build_deposit(spec, state, - deposit_data_leaves, + deposit_data_list, pubkey, privkey, amount, withdrawal_credentials, signed): deposit_data = build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed) - - item = deposit_data.hash_tree_root() - index = len(deposit_data_leaves) - deposit_data_leaves.append(item) - tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves)) - root = get_merkle_root((tuple(deposit_data_leaves))) - proof = list(get_merkle_proof(tree, item_index=index)) - assert spec.verify_merkle_branch(item, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH, index, root) - - deposit = spec.Deposit( - proof=list(proof), - index=index, - data=deposit_data, - ) + deposit_data_list.append(deposit_data) + index = len(deposit_data_list) + root = hash_tree_root(List[DepositData, 2**32](*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)) + [index.to_bytes(32, 'little')] + leaf = deposit_data.hash_tree_root() + assert spec.verify_merkle_branch(leaf, proof, spec.DEPOSIT_CONTRACT_TREE_DEPTH + 1, index, root) + deposit = spec.Deposit(proof, index, deposit_data) 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. """ - pre_validator_count = len(state.validators) - # fill previous deposits with zero-hash - deposit_data_leaves = [spec.ZERO_HASH] * pre_validator_count + deposit_data_list = [] pubkey = pubkeys[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: 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, state, - deposit_data_leaves, + deposit_data_list, pubkey, privkey, 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_count = len(deposit_data_leaves) + state.eth1_data.deposit_count = len(deposit_data_list) return deposit