update encoder and decoder for reading from parsed data

This commit is contained in:
protolambda
2019-05-27 21:46:14 +02:00
parent d63b553a2d
commit 87b3466eae
2 changed files with 40 additions and 30 deletions

View File

@@ -1,28 +1,35 @@
from eth2spec.utils.minimal_ssz import hash_tree_root
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import *
def decode(json, typ):
if isinstance(typ, str) and typ[:4] == 'uint':
return json
elif typ == 'bool':
assert json in (True, False)
return json
elif isinstance(typ, list):
return [decode(element, typ[0]) for element in json]
elif isinstance(typ, str) and typ[:4] == 'byte':
return bytes.fromhex(json[2:])
elif hasattr(typ, 'fields'):
def decode(data, typ):
if is_uint(typ):
return data
elif is_bool_type(typ):
assert data in (True, False)
return data
elif issubclass(typ, list):
elem_typ = read_list_elem_typ(typ)
return [decode(element, elem_typ) for element in data]
elif issubclass(typ, Vector):
elem_typ = read_vector_elem_typ(typ)
return Vector(decode(element, elem_typ) for element in data)
elif issubclass(typ, bytes):
return bytes.fromhex(data[2:])
elif issubclass(typ, BytesN):
return BytesN(bytes.fromhex(data[2:]))
elif is_container_typ(typ):
temp = {}
for field, subtype in typ.fields.items():
temp[field] = decode(json[field], subtype)
if field + "_hash_tree_root" in json:
assert(json[field + "_hash_tree_root"][2:] ==
for field, subtype in typ.get_fields():
temp[field] = decode(data[field], subtype)
if field + "_hash_tree_root" in data:
assert(data[field + "_hash_tree_root"][2:] ==
hash_tree_root(temp[field], subtype).hex())
ret = typ(**temp)
if "hash_tree_root" in json:
assert(json["hash_tree_root"][2:] ==
if "hash_tree_root" in data:
assert(data["hash_tree_root"][2:] ==
hash_tree_root(ret, typ).hex())
return ret
else:
print(json, typ)
print(data, typ)
raise Exception("Type not recognized")

View File

@@ -1,24 +1,27 @@
from eth2spec.utils.minimal_ssz import hash_tree_root
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import *
def encode(value, typ, include_hash_tree_roots=False):
if isinstance(typ, str) and typ[:4] == 'uint':
if typ[4:] == '128' or typ[4:] == '256':
if is_uint(typ):
if issubclass(typ, uint) and typ.byte_len > 8:
return str(value)
return value
elif typ == 'bool':
elif is_bool_type(typ):
assert value in (True, False)
return value
elif isinstance(typ, list):
return [encode(element, typ[0], include_hash_tree_roots) for element in value]
elif isinstance(typ, str) and typ[:4] == 'byte':
elif issubclass(typ, list) or issubclass(typ, Vector):
elem_typ = read_elem_typ(typ)
return [encode(element, elem_typ, include_hash_tree_roots) for element in value]
elif issubclass(typ, bytes):
return '0x' + value.hex()
elif hasattr(typ, 'fields'):
elif is_container_typ(typ):
ret = {}
for field, subtype in typ.fields.items():
ret[field] = encode(getattr(value, field), subtype, include_hash_tree_roots)
for field, subtype in typ.get_fields():
field_value = getattr(value, field)
ret[field] = encode(field_value, subtype, include_hash_tree_roots)
if include_hash_tree_roots:
ret[field + "_hash_tree_root"] = '0x' + hash_tree_root(getattr(value, field), subtype).hex()
ret[field + "_hash_tree_root"] = '0x' + hash_tree_root(field_value, subtype).hex()
if include_hash_tree_roots:
ret["hash_tree_root"] = '0x' + hash_tree_root(value, typ).hex()
return ret