diff --git a/scripts/phase0/build_spec.py b/scripts/phase0/build_spec.py index c7a377297..0d5f64094 100644 --- a/scripts/phase0/build_spec.py +++ b/scripts/phase0/build_spec.py @@ -18,8 +18,8 @@ from eth2spec.utils.ssz.ssz_impl import ( signing_root, ) from eth2spec.utils.ssz.ssz_typing import ( - uint8, uint16, uint32, uint64, uint128, uint256, - Container, Vector, BytesN + # unused: uint8, uint16, uint32, uint128, uint256, + uint64, Container, Vector, BytesN ) from eth2spec.utils.hash_function import hash from eth2spec.utils.bls import ( diff --git a/scripts/phase0/function_puller.py b/scripts/phase0/function_puller.py index d7765a4a4..ea8688e86 100644 --- a/scripts/phase0/function_puller.py +++ b/scripts/phase0/function_puller.py @@ -68,7 +68,7 @@ def get_spec(file_name: str) -> List[str]: for type_line in ssz_type: if len(type_line) > 0: code_lines.append(' ' + type_line) - code_lines.append('\n') + code_lines.append('') for (ssz_type_name, _) in type_defs: code_lines.append(f' global_vars["{ssz_type_name}"] = {ssz_type_name}') code_lines.append(' global_vars["ssz_types"] = [') diff --git a/test_libs/pyspec/eth2spec/debug/decode.py b/test_libs/pyspec/eth2spec/debug/decode.py index c9657dc28..86716c5f4 100644 --- a/test_libs/pyspec/eth2spec/debug/decode.py +++ b/test_libs/pyspec/eth2spec/debug/decode.py @@ -1,5 +1,10 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root -from eth2spec.utils.ssz.ssz_typing import * +from eth2spec.utils.ssz.ssz_typing import ( + is_uint_type, is_bool_type, is_list_type, + is_vector_type, is_bytes_type, is_bytesn_type, is_container_type, + read_vector_elem_type, read_list_elem_type, + Vector, BytesN +) def decode(data, typ): diff --git a/test_libs/pyspec/eth2spec/debug/encode.py b/test_libs/pyspec/eth2spec/debug/encode.py index a3c3c1189..8be567f17 100644 --- a/test_libs/pyspec/eth2spec/debug/encode.py +++ b/test_libs/pyspec/eth2spec/debug/encode.py @@ -1,5 +1,9 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root -from eth2spec.utils.ssz.ssz_typing import * +from eth2spec.utils.ssz.ssz_typing import ( + is_uint_type, is_bool_type, is_list_type, is_vector_type, is_container_type, + read_elem_type, + uint +) def encode(value, typ, include_hash_tree_roots=False): diff --git a/test_libs/pyspec/eth2spec/debug/random_value.py b/test_libs/pyspec/eth2spec/debug/random_value.py index ab9c4c885..43c3de349 100644 --- a/test_libs/pyspec/eth2spec/debug/random_value.py +++ b/test_libs/pyspec/eth2spec/debug/random_value.py @@ -2,9 +2,15 @@ from random import Random from typing import Any from enum import Enum -from eth2spec.utils.ssz.ssz_typing import * from eth2spec.utils.ssz.ssz_impl import is_basic_type +from eth2spec.utils.ssz.ssz_typing import ( + is_uint_type, is_bool_type, is_list_type, + is_vector_type, is_bytes_type, is_bytesn_type, is_container_type, + read_vector_elem_type, read_list_elem_type, + uint_byte_size +) + # in bytes UINT_SIZES = [1, 2, 4, 8, 16, 32] diff --git a/test_libs/pyspec/eth2spec/utils/ssz/__init__.py b/test_libs/pyspec/eth2spec/utils/ssz/__init__.py index 6543bd9e5..e69de29bb 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/__init__.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/__init__.py @@ -1,2 +0,0 @@ -from .ssz_impl import * -from .ssz_partials import * diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py index 0ef89f97f..fac8d8051 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -1,5 +1,11 @@ from ..merkle_minimal import merkleize_chunks, hash -from .ssz_typing import * +from eth2spec.utils.ssz.ssz_typing import ( + is_uint_type, is_bool_type, is_container_type, + is_list_kind, is_vector_kind, + read_vector_elem_type, read_elem_type, + uint_byte_size, + infer_input_type +) # SSZ Serialization # ----------------------------- @@ -22,6 +28,7 @@ def serialize_basic(value, typ): else: raise Exception("Type not supported: {}".format(typ)) + def deserialize_basic(value, typ): if is_uint_type(typ): return typ(int.from_bytes(value, 'little')) @@ -148,4 +155,3 @@ def signing_root(obj, typ): # ignore last field leaves = [hash_tree_root(field_value, typ=field_typ) for field_value, field_typ in obj.get_typed_values()[:-1]] return merkleize_chunks(chunkify(b''.join(leaves))) - diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 47b4b5c9a..9424e9bd3 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -3,10 +3,6 @@ from typing import List, Iterable, TypeVar, Type, NewType from typing import Union from typing_inspect import get_origin -T = TypeVar('T') -L = TypeVar('L') - - # SSZ integers # ----------------------------- @@ -21,6 +17,7 @@ class uint(int): class uint8(uint): byte_len = 1 + def __new__(cls, value, *args, **kwargs): if value.bit_length() > 8: raise ValueError("value out of bounds for uint8") @@ -32,6 +29,7 @@ byte = NewType('byte', uint8) class uint16(uint): byte_len = 2 + def __new__(cls, value, *args, **kwargs): if value.bit_length() > 16: raise ValueError("value out of bounds for uint16") @@ -40,6 +38,7 @@ class uint16(uint): class uint32(uint): byte_len = 4 + def __new__(cls, value, *args, **kwargs): if value.bit_length() > 32: raise ValueError("value out of bounds for uint16") @@ -52,6 +51,7 @@ uint64 = NewType('uint64', int) class uint128(uint): byte_len = 16 + def __new__(cls, value, *args, **kwargs): if value.bit_length() > 128: raise ValueError("value out of bounds for uint128") @@ -60,6 +60,7 @@ class uint128(uint): class uint256(uint): byte_len = 32 + def __new__(cls, value, *args, **kwargs): if value.bit_length() > 256: raise ValueError("value out of bounds for uint256") @@ -397,12 +398,14 @@ def get_zero_value(typ): elif issubclass(typ, Container): result = typ(**{f: get_zero_value(t) for f, t in typ.get_fields()}) else: - return Exception("Type not supported: {}".format(typ)) + return Exception("Type not supported: {}".format(typ)) return result + # Type helpers # ----------------------------- + def infer_type(obj): if is_uint_type(obj.__class__): return obj.__class__