From d63b553a2da3f5293d7202183c18cfdf28816b7f Mon Sep 17 00:00:00 2001 From: protolambda Date: Mon, 27 May 2019 21:45:47 +0200 Subject: [PATCH] efficiency bugfix in bytes encoding, improve typing doc + bugfix --- test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py | 4 ++-- test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py index 655cd9da4..46d842e16 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -14,7 +14,7 @@ def is_basic_type(typ): def serialize_basic(value, typ): if is_uint(typ): return value.to_bytes(uint_byte_size(typ), 'little') - if issubclass(typ, bool): + if is_bool_type(typ): if value: return b'\x01' else: @@ -39,7 +39,7 @@ def serialize(obj, typ): if is_basic_type(typ): return serialize_basic(obj, typ) elif is_list_type(typ) or is_vector_type(typ): - return encode_series(list(obj), [read_elem_typ(typ)]*len(obj)) + return encode_series(obj, [read_elem_typ(typ)]*len(obj)) elif is_container_typ(typ): return encode_series(obj.get_field_values(), typ.get_field_types()) else: diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 1d8aed0de..93a272c8c 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -376,12 +376,20 @@ def infer_input_type(fn): return fn(obj, typ) return infer_helper +def is_bool_type(typ): + return issubclass(typ, bool) def is_list_type(typ): + """ + Checks if the given type is a kind of list. Can be bytes. + """ return (hasattr(typ, '_name') and typ._name == 'List') or typ == bytes def is_vector_type(typ): - return issubclass(typ, Vector) + """ + Checks if the given type is a kind of vector. Can be BytesN. + """ + return issubclass(typ, Vector) or issubclass(typ, BytesN) def is_container_typ(typ): return issubclass(typ, Container)