eth2.0-specs/tests/core/pyspec/eth2spec/debug/decode.py

32 lines
1.2 KiB
Python
Raw Normal View History

2019-06-19 00:37:22 +00:00
from typing import Any
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import (
uint, Container, List, boolean,
Vector, ByteVector, ByteList
)
def decode(data: Any, typ):
if issubclass(typ, (uint, boolean)):
2019-06-19 00:37:22 +00:00
return typ(data)
elif issubclass(typ, (List, Vector)):
return typ(decode(element, typ.element_cls()) for element in data)
elif issubclass(typ, ByteVector):
return typ(bytes.fromhex(data[2:]))
elif issubclass(typ, ByteList):
2019-06-19 00:37:22 +00:00
return typ(bytes.fromhex(data[2:]))
elif issubclass(typ, Container):
temp = {}
for field_name, field_type in typ.fields().items():
2019-06-19 00:37:22 +00:00
temp[field_name] = decode(data[field_name], field_type)
if field_name + "_hash_tree_root" in data:
assert (data[field_name + "_hash_tree_root"][2:] ==
hash_tree_root(temp[field_name]).hex())
ret = typ(**temp)
if "hash_tree_root" in data:
2019-06-19 00:37:22 +00:00
assert (data["hash_tree_root"][2:] ==
hash_tree_root(ret).hex())
return ret
else:
2019-06-04 09:42:21 +00:00
raise Exception(f"Type not recognized: data={data}, typ={typ}")