From bbaa238742a93e2aa0524baaf6fd9049d2943d59 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 23 Aug 2019 20:16:46 +0800 Subject: [PATCH] Fix the definition of `GeneralizedIndex` --- scripts/build_spec.py | 3 ++- specs/light_client/merkle_proofs.md | 16 +++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 0a5171e8f..83f9a2145 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -37,7 +37,7 @@ from eth2spec.utils.bls import ( from eth2spec.utils.hash_function import hash ''' PHASE1_IMPORTS = '''from typing import ( - Any, Dict, Optional, Set, Sequence, MutableSequence, Tuple, Union, + Any, Dict, Optional, Set, Sequence, MutableSequence, NewType, Tuple, Union, ) from math import ( log2, @@ -70,6 +70,7 @@ from eth2spec.utils.hash_function import hash SSZVariableName = str +GeneralizedIndex = NewType('GeneralizedIndex', int) ''' SUNDRY_CONSTANTS_FUNCTIONS = ''' def ceillog2(x: uint64) -> int: diff --git a/specs/light_client/merkle_proofs.md b/specs/light_client/merkle_proofs.md index d7f0ab382..ce7dc647c 100644 --- a/specs/light_client/merkle_proofs.md +++ b/specs/light_client/merkle_proofs.md @@ -7,7 +7,6 @@ - [Merkle proof formats](#merkle-proof-formats) - [Table of contents](#table-of-contents) - - [Custom types](#custom-types) - [Helper functions](#helper-functions) - [Generalized Merkle tree index](#generalized-merkle-tree-index) - [SSZ object to index](#ssz-object-to-index) @@ -22,13 +21,6 @@ -## Custom types - -We define the following Python custom types for type hinting and readability: - -| - | - | - | -| `GeneralizedIndex` | `uint64` | the index of a node in a binary Merkle tree | - ## Helper functions ```python @@ -75,6 +67,8 @@ def merkle_tree(leaves: Sequence[Hash]) -> Sequence[Hash]: return o ``` +We define a custom type `GeneralizedIndex` as a Python integer type in this document. It can be represented as a Bitvector/Bitlist object as well. + We will define Merkle proofs in terms of generalized indices. ## SSZ object to index @@ -175,7 +169,7 @@ def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableNam else: pos, _, _ = get_item_position(typ, p) base_index = (GeneralizedIndex(2) if issubclass(typ, (List, Bytes)) else GeneralizedIndex(1)) - root = root * base_index * get_next_power_of_two(chunk_count(typ)) + pos + root = GeneralizedIndex(root * base_index * get_next_power_of_two(chunk_count(typ)) + pos) typ = get_elem_type(typ, p) return root ``` @@ -280,8 +274,8 @@ def get_helper_indices(indices: Sequence[GeneralizedIndex]) -> Sequence[Generali return sorted([ x for x in all_indices if ( not ( - generalized_index_child(x, GeneralizedIndex(0)) in all_indices and - generalized_index_child(x, GeneralizedIndex(1)) in all_indices + generalized_index_child(x, False) in all_indices and + generalized_index_child(x, True) in all_indices ) and not (x in indices) ) ], reverse=True)