Skip to content

Commit

Permalink
Fix the definition of GeneralizedIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Aug 23, 2019
1 parent 7409b5a commit bbaa238
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 2 additions & 1 deletion scripts/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
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,
Expand Down Expand Up @@ -70,6 +70,7 @@
SSZVariableName = str
GeneralizedIndex = NewType('GeneralizedIndex', int)
'''
SUNDRY_CONSTANTS_FUNCTIONS = '''
def ceillog2(x: uint64) -> int:
Expand Down
16 changes: 5 additions & 11 deletions specs/light_client/merkle_proofs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,13 +21,6 @@

<!-- /TOC -->

## 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bbaa238

Please sign in to comment.