eth2.0-specs/test_libs/pyspec/eth2spec/debug/encode.py

33 lines
1.4 KiB
Python
Raw Normal View History

from eth2spec.utils.ssz.ssz_impl import hash_tree_root, serialize
from eth2spec.utils.ssz.ssz_typing import (
uint, boolean,
Bitlist, Bitvector, Container
)
def encode(value, include_hash_tree_roots=False):
2019-06-19 00:37:22 +00:00
if isinstance(value, uint):
# Larger uints are boxed and the class declares their byte length
2019-06-19 00:37:22 +00:00
if value.type().byte_len > 8:
return str(int(value))
return int(value)
elif isinstance(value, boolean):
return value == 1
elif isinstance(value, (Bitlist, Bitvector)):
return '0x' + serialize(value).hex()
elif isinstance(value, list): # normal python lists, ssz-List, Vector
2019-06-19 00:37:22 +00:00
return [encode(element, include_hash_tree_roots) for element in value]
elif isinstance(value, bytes): # both bytes and ByteVector
return '0x' + value.hex()
2019-06-19 00:37:22 +00:00
elif isinstance(value, Container):
ret = {}
2019-06-19 00:37:22 +00:00
for field_value, field_name in zip(value, value.get_fields().keys()):
ret[field_name] = encode(field_value, include_hash_tree_roots)
if include_hash_tree_roots:
2019-06-19 00:37:22 +00:00
ret[field_name + "_hash_tree_root"] = '0x' + hash_tree_root(field_value).hex()
if include_hash_tree_roots:
2019-06-19 00:37:22 +00:00
ret["hash_tree_root"] = '0x' + hash_tree_root(value).hex()
return ret
else:
raise Exception(f"Type not recognized: value={value}, typ={type(value)}")