remove Optional None from get_generalized_index. instead throw

This commit is contained in:
Danny Ryan 2019-08-23 14:31:26 -06:00
parent a4f86a8b24
commit 6923bdc46a
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
3 changed files with 15 additions and 13 deletions

View File

@ -129,7 +129,7 @@ def compute_historical_state_generalized_index(earlier: ShardSlot, later: ShardS
for i in range(63, -1, -1): for i in range(63, -1, -1):
if (later - 1) & 2**i > (earlier - 1) & 2**i: if (later - 1) & 2**i > (earlier - 1) & 2**i:
later = later - ((later - 1) % 2**i) - 1 later = later - ((later - 1) % 2**i) - 1
o = concat_generalized_indices(o, get_generalized_index(ShardState, ['history_acc', i])) o = concat_generalized_indices(o, GeneralizedIndex(get_generalized_index(ShardState, ['history_acc', i])))
return o return o
``` ```
@ -158,9 +158,9 @@ def process_shard_receipt_proof(state: BeaconState, receipt_proof: ShardReceiptP
first_slot_in_last_crosslink = state.current_crosslinks[receipt_proof.shard].start_epoch * SLOTS_PER_EPOCH first_slot_in_last_crosslink = state.current_crosslinks[receipt_proof.shard].start_epoch * SLOTS_PER_EPOCH
gindex = concat_generalized_indices( gindex = concat_generalized_indices(
get_generalized_index_of_crosslink_header(0), get_generalized_index_of_crosslink_header(0),
get_generalized_index(ShardBlockHeader, 'state_root'), GeneralizedIndex(get_generalized_index(ShardBlockHeader, 'state_root')),
compute_historical_state_generalized_index(receipt_slot, first_slot_in_last_crosslink), compute_historical_state_generalized_index(receipt_slot, first_slot_in_last_crosslink),
get_generalized_index(ShardState, 'receipt_root') GeneralizedIndex(get_generalized_index(ShardState, 'receipt_root'))
) )
assert verify_merkle_proof( assert verify_merkle_proof(
leaf=hash_tree_root(receipt_proof.receipt), leaf=hash_tree_root(receipt_proof.receipt),

View File

@ -152,7 +152,7 @@ def get_item_position(typ: SSZType, index_or_variable_name: Union[int, SSZVariab
``` ```
```python ```python
def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableName]]) -> Optional[GeneralizedIndex]: def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableName]]) -> GeneralizedIndex:
""" """
Converts a path (eg. `[7, "foo", 3]` for `x[7].foo[3]`, `[12, "bar", "__len__"]` for Converts a path (eg. `[7, "foo", 3]` for `x[7].foo[3]`, `[12, "bar", "__len__"]` for
`len(x[12].bar)`) into the generalized index representing its position in the Merkle tree. `len(x[12].bar)`) into the generalized index representing its position in the Merkle tree.
@ -162,10 +162,8 @@ def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableNam
assert not issubclass(typ, BasicValue) # If we descend to a basic type, the path cannot continue further assert not issubclass(typ, BasicValue) # If we descend to a basic type, the path cannot continue further
if p == '__len__': if p == '__len__':
typ = uint64 typ = uint64
if issubclass(typ, (List, Bytes)): assert issubclass(typ, (List, Bytes))
root = GeneralizedIndex(root * 2 + 1) root = GeneralizedIndex(root * 2 + 1)
else:
return None
else: else:
pos, _, _ = get_item_position(typ, p) pos, _, _ = get_item_position(typ, p)
base_index = (GeneralizedIndex(2) if issubclass(typ, (List, Bytes)) else GeneralizedIndex(1)) base_index = (GeneralizedIndex(2) if issubclass(typ, (List, Bytes)) else GeneralizedIndex(1))

View File

@ -1,10 +1,10 @@
import re import re
from eth_utils import ( from eth_utils import (
to_tuple, to_tuple,
) )
from eth2spec.test.context import ( from eth2spec.test.context import (
expect_assertion_error,
spec_state_test, spec_state_test,
with_all_phases_except, with_all_phases_except,
) )
@ -89,10 +89,14 @@ generalized_index_cases = [
@spec_state_test @spec_state_test
def test_get_generalized_index(spec, state): def test_get_generalized_index(spec, state):
for typ, path, generalized_index in generalized_index_cases: for typ, path, generalized_index in generalized_index_cases:
assert spec.get_generalized_index( if generalized_index is not None:
typ=typ, assert spec.get_generalized_index(
path=path, typ=typ,
) == generalized_index path=path,
) == generalized_index
else:
expect_assertion_error(lambda: spec.get_generalized_index(typ=typ, path=path))
yield 'typ', typ yield 'typ', typ
yield 'path', path yield 'path', path
yield 'generalized_index', generalized_index yield 'generalized_index', generalized_index