2019-06-13 23:56:36 +00:00
|
|
|
from eth2spec.utils.ssz import ssz_typing as spec_ssz
|
|
|
|
import ssz
|
|
|
|
|
|
|
|
|
|
|
|
def translate_typ(typ) -> ssz.BaseSedes:
|
|
|
|
"""
|
|
|
|
Translates a spec type to a Py-SSZ type description (sedes).
|
|
|
|
:param typ: The spec type, a class.
|
|
|
|
:return: The Py-SSZ equivalent.
|
|
|
|
"""
|
2019-06-20 19:07:23 +00:00
|
|
|
if issubclass(typ, spec_ssz.Container):
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.Container(
|
2019-06-20 19:07:23 +00:00
|
|
|
[translate_typ(field_typ) for field_name, field_typ in typ.get_fields().items()])
|
2019-11-15 13:50:58 +00:00
|
|
|
elif issubclass(typ, spec_ssz.ByteVector):
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.ByteVector(typ.length)
|
2019-11-15 13:50:58 +00:00
|
|
|
elif issubclass(typ, spec_ssz.ByteList):
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.ByteList()
|
2019-06-20 19:07:23 +00:00
|
|
|
elif issubclass(typ, spec_ssz.Vector):
|
|
|
|
return ssz.Vector(translate_typ(typ.elem_type), typ.length)
|
|
|
|
elif issubclass(typ, spec_ssz.List):
|
2019-07-11 12:51:34 +00:00
|
|
|
return ssz.List(translate_typ(typ.elem_type), typ.length)
|
2019-06-27 10:45:35 +00:00
|
|
|
elif issubclass(typ, spec_ssz.Bitlist):
|
2019-07-11 12:51:34 +00:00
|
|
|
return ssz.Bitlist(typ.length)
|
2019-06-27 10:45:35 +00:00
|
|
|
elif issubclass(typ, spec_ssz.Bitvector):
|
2019-07-11 12:51:34 +00:00
|
|
|
return ssz.Bitvector(typ.length)
|
2019-06-27 08:51:06 +00:00
|
|
|
elif issubclass(typ, spec_ssz.boolean):
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.boolean
|
2019-06-20 19:07:23 +00:00
|
|
|
elif issubclass(typ, spec_ssz.uint):
|
|
|
|
if typ.byte_len == 1:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint8
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 2:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint16
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 4:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint32
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 8:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint64
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 16:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint128
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 32:
|
2019-06-13 23:56:36 +00:00
|
|
|
return ssz.uint256
|
|
|
|
else:
|
|
|
|
raise TypeError("invalid uint size")
|
|
|
|
else:
|
2019-06-14 17:09:49 +00:00
|
|
|
raise TypeError("Type not supported: {}".format(typ))
|
2019-06-13 23:56:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def translate_value(value, typ):
|
|
|
|
"""
|
|
|
|
Translate a value output from Py-SSZ deserialization into the given spec type.
|
|
|
|
:param value: The PySSZ value
|
|
|
|
:param typ: The type from the spec to translate into
|
|
|
|
:return: the translated value
|
|
|
|
"""
|
2019-06-20 19:07:23 +00:00
|
|
|
if issubclass(typ, spec_ssz.uint):
|
|
|
|
if typ.byte_len == 1:
|
2019-06-13 23:56:36 +00:00
|
|
|
return spec_ssz.uint8(value)
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 2:
|
2019-06-13 23:56:36 +00:00
|
|
|
return spec_ssz.uint16(value)
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 4:
|
2019-06-13 23:56:36 +00:00
|
|
|
return spec_ssz.uint32(value)
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 8:
|
|
|
|
return spec_ssz.uint64(value)
|
|
|
|
elif typ.byte_len == 16:
|
2019-06-13 23:56:36 +00:00
|
|
|
return spec_ssz.uint128(value)
|
2019-06-20 19:07:23 +00:00
|
|
|
elif typ.byte_len == 32:
|
2019-06-13 23:56:36 +00:00
|
|
|
return spec_ssz.uint256(value)
|
|
|
|
else:
|
|
|
|
raise TypeError("invalid uint size")
|
2019-06-20 19:07:23 +00:00
|
|
|
elif issubclass(typ, spec_ssz.List):
|
|
|
|
return [translate_value(elem, typ.elem_type) for elem in value]
|
2019-06-27 08:51:06 +00:00
|
|
|
elif issubclass(typ, spec_ssz.boolean):
|
2019-06-14 17:16:15 +00:00
|
|
|
return value
|
2019-06-20 19:07:23 +00:00
|
|
|
elif issubclass(typ, spec_ssz.Vector):
|
|
|
|
return typ(*(translate_value(elem, typ.elem_type) for elem in value))
|
2019-06-27 10:45:35 +00:00
|
|
|
elif issubclass(typ, spec_ssz.Bitlist):
|
|
|
|
return typ(value)
|
|
|
|
elif issubclass(typ, spec_ssz.Bitvector):
|
|
|
|
return typ(value)
|
2019-11-15 13:50:58 +00:00
|
|
|
elif issubclass(typ, spec_ssz.ByteVector):
|
2019-06-13 23:56:36 +00:00
|
|
|
return typ(value)
|
2019-11-15 13:50:58 +00:00
|
|
|
elif issubclass(typ, spec_ssz.ByteList):
|
2019-06-13 23:56:36 +00:00
|
|
|
return value
|
2019-06-20 19:07:23 +00:00
|
|
|
if issubclass(typ, spec_ssz.Container):
|
|
|
|
return typ(**{f_name: translate_value(f_val, f_typ) for (f_val, (f_name, f_typ))
|
|
|
|
in zip(value, typ.get_fields().items())})
|
2019-06-13 23:56:36 +00:00
|
|
|
else:
|
2019-06-14 17:09:49 +00:00
|
|
|
raise TypeError("Type not supported: {}".format(typ))
|