fix encoder to also encode bytes nicely

This commit is contained in:
protolambda 2019-06-30 15:36:54 +02:00
parent b38802ced0
commit bf618f8d28
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 7 additions and 7 deletions

View File

@ -1,10 +1,10 @@
from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import ( from eth2spec.utils.ssz.ssz_typing import (
SSZValue, uint, Container, boolean uint, Container, boolean
) )
def encode(value: SSZValue, include_hash_tree_roots=False): def encode(value, include_hash_tree_roots=False):
if isinstance(value, uint): if isinstance(value, uint):
# Larger uints are boxed and the class declares their byte length # Larger uints are boxed and the class declares their byte length
if value.type().byte_len > 8: if value.type().byte_len > 8:

View File

@ -29,12 +29,12 @@ def spectest(description: str = None):
(key, value, typ) = data (key, value, typ) = data
out[key] = encode(value, typ) out[key] = encode(value, typ)
else: else:
# Otherwise, try to infer the type, but keep it as-is if it's not a SSZ container. # Otherwise, try to infer the type, but keep it as-is if it's not a SSZ type or bytes.
(key, value) = data (key, value) = data
if isinstance(value, SSZValue): if isinstance(value, (SSZValue, bytes)):
out[key] = encode(value, value.__class__) out[key] = encode(value)
elif isinstance(value, list) and all([isinstance(el, SSZValue) for el in value]): elif isinstance(value, list) and all([isinstance(el, (SSZValue, bytes)) for el in value]):
out[key] = [encode(el, el.__class__) for el in value] out[key] = [encode(el) for el in value]
else: else:
# not a ssz value. # not a ssz value.
# It could be vector or bytes still, but it is a rare case, # It could be vector or bytes still, but it is a rare case,