head to 1229

This commit is contained in:
Hsiao-Wei Wang 2019-06-30 01:44:17 +08:00
parent d4755653ff
commit 1b66323806
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
3 changed files with 17 additions and 47 deletions

View File

@ -11,8 +11,7 @@ from typing import (
) )
PHASE0_IMPORTS = '''from math import log2 PHASE0_IMPORTS = '''from typing import (
from typing import (
Any, Callable, Dict, Set, Sequence, Tuple, Any, Callable, Dict, Set, Sequence, Tuple,
) )
@ -37,8 +36,7 @@ from eth2spec.utils.bls import (
from eth2spec.utils.hash_function import hash from eth2spec.utils.hash_function import hash
''' '''
PHASE1_IMPORTS = '''from math import log2 PHASE1_IMPORTS = '''from typing import (
from typing import (
Any, Callable, Dict, Optional, Set, Sequence, MutableSequence, Tuple, Any, Callable, Dict, Optional, Set, Sequence, MutableSequence, Tuple,
) )

View File

@ -52,8 +52,6 @@
- [`hash`](#hash) - [`hash`](#hash)
- [`hash_tree_root`](#hash_tree_root) - [`hash_tree_root`](#hash_tree_root)
- [`signing_root`](#signing_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) - [`bls_domain`](#bls_domain)
- [`slot_to_epoch`](#slot_to_epoch) - [`slot_to_epoch`](#slot_to_epoch)
- [`get_previous_epoch`](#get_previous_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). `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` ### `bls_domain`
```python ```python
@ -1146,7 +1117,7 @@ def is_genesis_trigger(deposits: List[Deposit, 2**DEPOSIT_CONTRACT_TREE_DEPTH],
state = BeaconState() state = BeaconState()
leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits)) leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits))
for deposit_index, deposit in enumerate(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_data.deposit_count = deposit_index + 1
state.eth1_deposit_index = deposit_index state.eth1_deposit_index = deposit_index
process_deposit(state, deposit) process_deposit(state, deposit)
@ -1176,7 +1147,7 @@ def get_genesis_beacon_state(deposits: Sequence[Deposit], genesis_time: int, gen
# Process genesis deposits # Process genesis deposits
leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits)) leaves = list(map(lambda deposit: hash_tree_root(deposit.data), deposits))
for deposit_index, deposit in enumerate(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_data.deposit_count = deposit_index + 1
state.eth1_deposit_index = deposit_index state.eth1_deposit_index = deposit_index
process_deposit(state, deposit) process_deposit(state, deposit)

View File

@ -47,10 +47,10 @@ def build_deposit(spec,
spec, pubkey, privkey, amount, withdrawal_credentials, state=state, signed=signed, 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) deposit_data_list.append(deposit_data)
index = len(deposit_data_list) 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])) 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')] proof = list(get_merkle_proof(tree, item_index=index)) + [index.to_bytes(32, 'little')]
leaf = deposit_data.hash_tree_root() 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): def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False):
deposit_data_leaves = [] deposit_data_list = []
genesis_deposits = [] genesis_deposits = []
for validator_index in range(genesis_validator_count): for validator_index in range(genesis_validator_count):
pubkey = pubkeys[validator_index] pubkey = pubkeys[validator_index]
@ -73,16 +73,17 @@ def prepare_genesis_deposits(spec, genesis_validator_count, amount, signed=False
withdrawal_credentials=withdrawal_credentials, withdrawal_credentials=withdrawal_credentials,
amount=amount, amount=amount,
) )
if signed: deposit, root, deposit_data_list = build_deposit(
sign_deposit_data(spec, deposit_data, privkey) # state=None spec,
item = deposit_data.hash_tree_root() None,
deposit_data_leaves.append(item) deposit_data_list,
pubkey,
tree = calc_merkle_tree_from_leaves(tuple(deposit_data_leaves), spec.DEPOSIT_CONTRACT_TREE_DEPTH) privkey,
root = get_merkle_root((tuple(deposit_data_leaves)), 2**spec.DEPOSIT_CONTRACT_TREE_DEPTH) amount,
genesis_deposits.append( withdrawal_credentials,
spec.Deposit(proof=list(get_merkle_proof(tree, item_index=validator_index)), data=deposit_data) signed,
) )
genesis_deposits.append(deposit)
return genesis_deposits, root return genesis_deposits, root