From 1b66323806505a465236f3adb924ea3a777e02cb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 30 Jun 2019 01:44:17 +0800 Subject: [PATCH] head to 1229 --- scripts/build_spec.py | 6 ++-- specs/core/0_beacon-chain.md | 33 ++----------------- .../pyspec/eth2spec/test/helpers/deposits.py | 25 +++++++------- 3 files changed, 17 insertions(+), 47 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index e4e95c7ec..1f5fe1ee6 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -11,8 +11,7 @@ from typing import ( ) -PHASE0_IMPORTS = '''from math import log2 -from typing import ( +PHASE0_IMPORTS = '''from typing import ( Any, Callable, Dict, Set, Sequence, Tuple, ) @@ -37,8 +36,7 @@ from eth2spec.utils.bls import ( from eth2spec.utils.hash_function import hash ''' -PHASE1_IMPORTS = '''from math import log2 -from typing import ( +PHASE1_IMPORTS = '''from typing import ( Any, Callable, Dict, Optional, Set, Sequence, MutableSequence, Tuple, ) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index f9d529e0c..e51fbde3e 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -52,8 +52,6 @@ - [`hash`](#hash) - [`hash_tree_root`](#hash_tree_root) - [`signing_root`](#signing_root) - - [`calc_merkle_tree_from_leaves`](#calc_merkle_tree_from_leaves) - - [`get_merkle_root`](#get_merkle_root) - [`bls_domain`](#bls_domain) - [`slot_to_epoch`](#slot_to_epoch) - [`get_previous_epoch`](#get_previous_epoch) @@ -562,33 +560,6 @@ The `hash` function is SHA256. `def signing_root(object: Container) -> Hash` is a function for computing signing messages, as defined in the [SimpleSerialize spec](../simple-serialize.md#self-signed-containers). -### `calc_merkle_tree_from_leaves` - -```python -zerohashes = [ZERO_HASH] -for layer in range(1, 100): - zerohashes.append(hash(zerohashes[layer - 1] + zerohashes[layer - 1])) -def calc_merkle_tree_from_leaves(values: Sequence[Hash], layer_count: int=32) -> Sequence[Sequence[Hash]]: - values = list(values) - tree = [values[::]] - for h in range(layer_count): - if len(values) % 2 == 1: - values.append(zerohashes[h]) - values = [hash(values[i] + values[i + 1]) for i in range(0, len(values), 2)] - tree.append(values[::]) - return tree -``` - -### `get_merkle_root` - -```python -def get_merkle_root(values: Sequence[Hash], pad_to: int=1) -> Hash: - layer_count = int(log2(pad_to)) - if len(values) == 0: - return zerohashes[layer_count] - return calc_merkle_tree_from_leaves(values, layer_count)[-1][0] -``` - ### `bls_domain` ```python @@ -1146,7 +1117,7 @@ def is_genesis_trigger(deposits: List[Deposit, 2**DEPOSIT_CONTRACT_TREE_DEPTH], state = BeaconState() leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits)) for deposit_index, deposit in enumerate(deposits): - state.eth1_data.deposit_root = get_merkle_root(leaves[:deposit_index + 1], 2**DEPOSIT_CONTRACT_TREE_DEPTH) + state.eth1_data.deposit_root = hash_tree_root(leaves) state.eth1_data.deposit_count = deposit_index + 1 state.eth1_deposit_index = deposit_index process_deposit(state, deposit) @@ -1176,7 +1147,7 @@ def get_genesis_beacon_state(deposits: Sequence[Deposit], genesis_time: int, gen # Process genesis deposits leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits)) for deposit_index, deposit in enumerate(deposits): - state.eth1_data.deposit_root = get_merkle_root(leaves[:deposit_index + 1], 2**DEPOSIT_CONTRACT_TREE_DEPTH) + state.eth1_data.deposit_root = hash_tree_root(leaves) state.eth1_data.deposit_count = deposit_index + 1 state.eth1_deposit_index = deposit_index process_deposit(state, deposit) diff --git a/test_libs/pyspec/eth2spec/test/helpers/deposits.py b/test_libs/pyspec/eth2spec/test/helpers/deposits.py index ac842405e..a0d59b9a6 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/deposits.py +++ b/test_libs/pyspec/eth2spec/test/helpers/deposits.py @@ -47,10 +47,10 @@ def build_deposit(spec, spec, pubkey, privkey, amount, withdrawal_credentials, state=state, signed=signed, ) - deposit_data = build_deposit_data(spec, state, pubkey, privkey, amount, withdrawal_credentials, signed) + deposit_data = build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, state=state, signed=signed) deposit_data_list.append(deposit_data) index = len(deposit_data_list) - root = hash_tree_root(List[DepositData, 2**32](*deposit_data_list)) + root = hash_tree_root(List[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)) + [index.to_bytes(32, 'little')] leaf = deposit_data.hash_tree_root() @@ -61,7 +61,7 @@ def build_deposit(spec, def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False): - deposit_data_leaves = [] + deposit_data_list = [] genesis_deposits = [] for validator_index in range(genesis_validator_count): pubkey = pubkeys[validator_index] @@ -73,16 +73,17 @@ def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False withdrawal_credentials=withdrawal_credentials, amount=amount, ) - if signed: - sign_deposit_data(spec, deposit_data, privkey) # state=None - item = deposit_data.hash_tree_root() - deposit_data_leaves.append(item) - - tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves), spec.DEPOSIT_CONTRACT_TREE_DEPTH) - root = get_merkle_root((tuple(deposit_data_leaves)), 2**spec.DEPOSIT_CONTRACT_TREE_DEPTH) - genesis_deposits.append( - spec.Deposit(proof=list(get_merkle_proof(tree, item_index=validator_index)), data=deposit_data) + deposit, root, deposit_data_list = build_deposit( + spec, + None, + deposit_data_list, + pubkey, + privkey, + amount, + withdrawal_credentials, + signed, ) + genesis_deposits.append(deposit) return genesis_deposits, root