update ssz-pyssz decoder for fuzzing

This commit is contained in:
protolambda 2019-06-20 21:07:23 +02:00
parent 224c98a094
commit 8c6d2b42d8
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
1 changed files with 34 additions and 39 deletions

View File

@ -8,32 +8,31 @@ def translate_typ(typ) -> ssz.BaseSedes:
:param typ: The spec type, a class. :param typ: The spec type, a class.
:return: The Py-SSZ equivalent. :return: The Py-SSZ equivalent.
""" """
if spec_ssz.is_container_type(typ): if issubclass(typ, spec_ssz.Container):
return ssz.Container( return ssz.Container(
[translate_typ(field_typ) for (field_name, field_typ) in typ.get_fields()]) [translate_typ(field_typ) for field_name, field_typ in typ.get_fields().items()])
elif spec_ssz.is_bytesn_type(typ): elif issubclass(typ, spec_ssz.BytesN):
return ssz.ByteVector(typ.length) return ssz.ByteVector(typ.length)
elif spec_ssz.is_bytes_type(typ): elif issubclass(typ, spec_ssz.Bytes):
return ssz.ByteList() return ssz.ByteList()
elif spec_ssz.is_vector_type(typ): elif issubclass(typ, spec_ssz.Vector):
return ssz.Vector(translate_typ(spec_ssz.read_vector_elem_type(typ)), typ.length) return ssz.Vector(translate_typ(typ.elem_type), typ.length)
elif spec_ssz.is_list_type(typ): elif issubclass(typ, spec_ssz.List):
return ssz.List(translate_typ(spec_ssz.read_list_elem_type(typ))) return ssz.List(translate_typ(typ.elem_type))
elif spec_ssz.is_bool_type(typ): elif issubclass(typ, spec_ssz.Bit):
return ssz.boolean return ssz.boolean
elif spec_ssz.is_uint_type(typ): elif issubclass(typ, spec_ssz.uint):
size = spec_ssz.uint_byte_size(typ) if typ.byte_len == 1:
if size == 1:
return ssz.uint8 return ssz.uint8
elif size == 2: elif typ.byte_len == 2:
return ssz.uint16 return ssz.uint16
elif size == 4: elif typ.byte_len == 4:
return ssz.uint32 return ssz.uint32
elif size == 8: elif typ.byte_len == 8:
return ssz.uint64 return ssz.uint64
elif size == 16: elif typ.byte_len == 16:
return ssz.uint128 return ssz.uint128
elif size == 32: elif typ.byte_len == 32:
return ssz.uint256 return ssz.uint256
else: else:
raise TypeError("invalid uint size") raise TypeError("invalid uint size")
@ -48,37 +47,33 @@ def translate_value(value, typ):
:param typ: The type from the spec to translate into :param typ: The type from the spec to translate into
:return: the translated value :return: the translated value
""" """
if spec_ssz.is_uint_type(typ): if issubclass(typ, spec_ssz.uint):
size = spec_ssz.uint_byte_size(typ) if typ.byte_len == 1:
if size == 1:
return spec_ssz.uint8(value) return spec_ssz.uint8(value)
elif size == 2: elif typ.byte_len == 2:
return spec_ssz.uint16(value) return spec_ssz.uint16(value)
elif size == 4: elif typ.byte_len == 4:
return spec_ssz.uint32(value) return spec_ssz.uint32(value)
elif size == 8: elif typ.byte_len == 8:
# uint64 is default (TODO this is changing soon) return spec_ssz.uint64(value)
return value elif typ.byte_len == 16:
elif size == 16:
return spec_ssz.uint128(value) return spec_ssz.uint128(value)
elif size == 32: elif typ.byte_len == 32:
return spec_ssz.uint256(value) return spec_ssz.uint256(value)
else: else:
raise TypeError("invalid uint size") raise TypeError("invalid uint size")
elif spec_ssz.is_list_type(typ): elif issubclass(typ, spec_ssz.List):
elem_typ = spec_ssz.read_elem_type(typ) return [translate_value(elem, typ.elem_type) for elem in value]
return [translate_value(elem, elem_typ) for elem in value] elif issubclass(typ, spec_ssz.Bit):
elif spec_ssz.is_bool_type(typ):
return value return value
elif spec_ssz.is_vector_type(typ): elif issubclass(typ, spec_ssz.Vector):
elem_typ = spec_ssz.read_elem_type(typ) return typ(*(translate_value(elem, typ.elem_type) for elem in value))
return typ(*(translate_value(elem, elem_typ) for elem in value)) elif issubclass(typ, spec_ssz.BytesN):
elif spec_ssz.is_bytesn_type(typ):
return typ(value) return typ(value)
elif spec_ssz.is_bytes_type(typ): elif issubclass(typ, spec_ssz.Bytes):
return value return value
elif spec_ssz.is_container_type(typ): if issubclass(typ, spec_ssz.Container):
return typ(**{f_name: translate_value(f_val, f_typ) for (f_name, f_val, f_typ) return typ(**{f_name: translate_value(f_val, f_typ) for (f_val, (f_name, f_typ))
in zip(typ.get_field_names(), value, typ.get_field_types())}) in zip(value, typ.get_fields().items())})
else: else:
raise TypeError("Type not supported: {}".format(typ)) raise TypeError("Type not supported: {}".format(typ))