diff --git a/scripts/build_spec.py b/scripts/build_spec.py index ca648a754..20e90ee33 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -20,8 +20,8 @@ from eth2spec.utils.ssz.ssz_impl import ( signing_root, ) from eth2spec.utils.ssz.ssz_typing import ( - # unused: uint8, uint16, uint32, uint128, uint256, - Bit, Container, List, Vector, Bytes, BytesN, uint64 + Bit, Container, List, Vector, Bytes, uint64, + Bytes4, Bytes32, Bytes48, Bytes96, ) from eth2spec.utils.bls import ( bls_aggregate_pubkeys, @@ -45,7 +45,7 @@ from eth2spec.utils.ssz.ssz_impl import ( is_empty, ) from eth2spec.utils.ssz.ssz_typing import ( - Bit, Container, List, Vector, Bytes, BytesN, uint64, + Bit, Container, List, Vector, Bytes, uint64, Bytes4, Bytes32, Bytes48, Bytes96, ) from eth2spec.utils.bls import ( diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py index 1b59b276b..fd17e29f9 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -1,7 +1,7 @@ from ..merkle_minimal import merkleize_chunks from ..hash_function import hash from .ssz_typing import ( - SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, Elements, Bit, Container, List, Vector, Bytes, BytesN, uint + SSZValue, SSZType, BasicValue, BasicType, Series, Elements, Bit, Container, List, Bytes, BytesN, uint ) # SSZ Serialization diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 5aadfae32..082e3ed30 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -123,7 +123,7 @@ class Container(Series, metaclass=SSZType): value = coerce_type_maybe(kwargs[f], t) if not isinstance(value, t): raise ValueError(f"Bad input for class {self.__class__}:" - f" field: {f} type: {t} value: {value} value type: {type(value)}") + f" field: {f} type: {t} value: {value} value type: {type(value)}") setattr(self, f, value) def serialize(self): @@ -145,7 +145,7 @@ class Container(Series, metaclass=SSZType): value = coerce_type_maybe(value, field_typ) if not isinstance(value, field_typ): raise ValueError(f"Cannot set field of {self.__class__}:" - f" field: {name} type: {field_typ} value: {value} value type: {type(value)}") + f" field: {name} type: {field_typ} value: {value} value type: {type(value)}") super().__setattr__(name, value) def get_field_values(self) -> Tuple[SSZValue, ...]: @@ -301,7 +301,7 @@ class Elements(ParamsBase, metaclass=ElementsType): v = coerce_type_maybe(v, typ) if not isinstance(v, typ): raise ValueError(f"Cannot set item in type {self.__class__}," - f" mismatched element type: {v} of {type(v)}, expected {typ}") + f" mismatched element type: {v} of {type(v)}, expected {typ}") self.items[k] = v def __len__(self): @@ -400,3 +400,10 @@ class BytesN(BytesLike): @classmethod def is_fixed_size(cls): return True + + +# Helpers for common BytesN types. +Bytes4 = BytesN[4] +Bytes32 = BytesN[32] +Bytes48 = BytesN[48] +Bytes96 = BytesN[96] diff --git a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py index 8dd04a86d..ae0849098 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py @@ -1,7 +1,7 @@ -from .ssz_impl import serialize, serialize_basic, encode_series, signing_root, hash_tree_root +from .ssz_impl import serialize, hash_tree_root from .ssz_typing import ( - SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, Bit, Container, List, Vector, Bytes, BytesN, - uint, uint8, uint16, uint32, uint64, uint128, uint256, byte + Bit, Container, List, Vector, Bytes, BytesN, + uint8, uint16, uint32, uint64, byte ) import pytest diff --git a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py index 0f4a06c5f..f604b6468 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py @@ -1,5 +1,6 @@ from .ssz_typing import ( - SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, Elements, Bit, Container, List, Vector, Bytes, BytesN, + SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, + Elements, Bit, Container, List, Vector, Bytes, BytesN, uint, uint8, uint16, uint32, uint64, uint128, uint256 ) @@ -41,13 +42,13 @@ def test_basic_instances(): def test_basic_value_bounds(): max = { - Bit: 2**1, - uint8: 2**(8 * 1), - uint16: 2**(8 * 2), - uint32: 2**(8 * 4), - uint64: 2**(8 * 8), - uint128: 2**(8 * 16), - uint256: 2**(8 * 32), + Bit: 2 ** 1, + uint8: 2 ** (8 * 1), + uint16: 2 ** (8 * 2), + uint32: 2 ** (8 * 4), + uint64: 2 ** (8 * 8), + uint128: 2 ** (8 * 16), + uint256: 2 ** (8 * 32), } for k, v in max.items(): # this should work @@ -161,12 +162,12 @@ def test_list(): foo[127] = 222 assert sum(foo) == 999 try: - foo[3] = 2**32 # out of bounds + foo[3] = 2 ** 32 # out of bounds except ValueError: pass try: - foo[3] = uint64(2**32 - 1) # within bounds, wrong type + foo[3] = uint64(2 ** 32 - 1) # within bounds, wrong type assert False except ValueError: pass