From 12695425c9ba8ef705665d36788cf7751be15b39 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 8 Mar 2019 10:32:40 +0100 Subject: [PATCH 1/4] Use hash_tree_root everywhere And get rid of merkle_root. This is possible because of SSZ tuples. --- specs/core/0_beacon-chain.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index c51effc9a2..f79140379c 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -73,7 +73,6 @@ - [`get_active_index_root`](#get_active_index_root) - [`generate_seed`](#generate_seed) - [`get_beacon_proposer_index`](#get_beacon_proposer_index) - - [`merkle_root`](#merkle_root) - [`verify_merkle_branch`](#verify_merkle_branch) - [`get_attestation_participants`](#get_attestation_participants) - [`is_power_of_two`](#is_power_of_two) @@ -1005,20 +1004,6 @@ def get_beacon_proposer_index(state: BeaconState, return first_committee[slot % len(first_committee)] ``` -### `merkle_root` - -```python -def merkle_root(values: List[Bytes32]) -> Bytes32: - """ - Merkleize ``values`` (where ``len(values)`` is a power of two) and return the Merkle root. - Note that the leaves are not hashed. - """ - o = [0] * len(values) + values - for i in range(len(values) - 1, 0, -1): - o[i] = hash(o[i * 2] + o[i * 2 + 1]) - return o[1] -``` - ### `verify_merkle_branch` ```python @@ -2494,7 +2479,7 @@ def finish_epoch_update(state: BeaconState) -> None: state.latest_randao_mixes[next_epoch % LATEST_RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: - state.historical_roots.append(merkle_root(state.latest_block_roots + state.latest_state_roots)) + state.historical_roots.append(hash_tree_root(state.latest_block_roots + state.latest_state_roots)) # Rotate current/previous epoch attestations state.previous_epoch_attestations = state.current_epoch_attestations state.current_epoch_attestations = [] From 11414673495b0fdf5df7223f710ded05bfad9cd9 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 8 Mar 2019 17:28:00 +0100 Subject: [PATCH 2/4] Update 0_beacon-chain.md --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 971a521dc6..679b5a65b9 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -2191,7 +2191,7 @@ def finish_epoch_update(state: BeaconState) -> None: state.latest_randao_mixes[next_epoch % LATEST_RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: - state.historical_roots.append(merkle_root(state.latest_block_roots + state.latest_state_roots)) + state.historical_roots.append(hash_tree_root(state.latest_block_roots + state.latest_state_roots)) # Rotate current/previous epoch attestations state.previous_epoch_attestations = state.current_epoch_attestations state.current_epoch_attestations = [] From de60533d7238c4010439152176e9a10828d50fa2 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 8 Mar 2019 18:13:05 +0100 Subject: [PATCH 3/4] Update 0_beacon-chain.md --- specs/core/0_beacon-chain.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 679b5a65b9..cc7cc5b18f 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -34,6 +34,7 @@ - [`BeaconBlockHeader`](#beaconblockheader) - [`Validator`](#validator) - [`PendingAttestation`](#pendingattestation) + - [`HistoricalBatch`](#historicalbatch) - [Beacon transactions](#beacon-transactions) - [`ProposerSlashing`](#proposerslashing) - [`AttesterSlashing`](#attesterslashing) @@ -452,6 +453,17 @@ The types are defined topologically to aid in facilitating an executable version } ``` +#### `HistoricalBatch` + +```python +{ + // Block roots + 'block_roots': ['bytes32', SLOTS_PER_HISTORICAL_ROOT], + // State roots + 'state_roots': ['bytes32', SLOTS_PER_HISTORICAL_ROOT], +} +``` + ### Beacon transactions #### `ProposerSlashing` @@ -2191,7 +2203,11 @@ def finish_epoch_update(state: BeaconState) -> None: state.latest_randao_mixes[next_epoch % LATEST_RANDAO_MIXES_LENGTH] = get_randao_mix(state, current_epoch) # Set historical root accumulator if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: - state.historical_roots.append(hash_tree_root(state.latest_block_roots + state.latest_state_roots)) + historical_batch = HistoricalBatch( + block_roots=state.latest_block_roots, + state_roots=state.latest_state_roots, + ) + state.historical_roots.append(hash_tree_root(historical_batch)) # Rotate current/previous epoch attestations state.previous_epoch_attestations = state.current_epoch_attestations state.current_epoch_attestations = [] From f180eb5e9e2bcf33325c8aece7655adb76bf0561 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 8 Mar 2019 18:14:00 +0100 Subject: [PATCH 4/4] Update 0_beacon-chain.md --- specs/core/0_beacon-chain.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index cc7cc5b18f..8c9aafd385 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -457,9 +457,9 @@ The types are defined topologically to aid in facilitating an executable version ```python { - // Block roots + # Block roots 'block_roots': ['bytes32', SLOTS_PER_HISTORICAL_ROOT], - // State roots + # State roots 'state_roots': ['bytes32', SLOTS_PER_HISTORICAL_ROOT], } ```