fix decoder, also fix bug in pyssz, see PR 74

This commit is contained in:
protolambda 2019-06-14 20:41:08 +02:00
parent 01be8b7e65
commit 895ab67815
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 40 additions and 1 deletions

View File

@ -72,7 +72,7 @@ def translate_value(value, typ):
return value return value
elif spec_ssz.is_vector_type(typ): elif spec_ssz.is_vector_type(typ):
elem_typ = spec_ssz.read_elem_type(typ) elem_typ = spec_ssz.read_elem_type(typ)
return typ(translate_value(elem, elem_typ) for elem in value) return typ(*(translate_value(elem, elem_typ) for elem in value))
elif spec_ssz.is_bytesn_type(typ): elif spec_ssz.is_bytesn_type(typ):
return typ(value) return typ(value)
elif spec_ssz.is_bytes_type(typ): elif spec_ssz.is_bytes_type(typ):

View File

@ -0,0 +1,39 @@
from eth2spec.fuzzing.decoder import translate_typ, translate_value
from eth2spec.phase0 import spec
from preset_loader import loader
from eth2spec.utils.ssz import ssz_impl as spec_ssz_impl
from random import Random
from eth2spec.debug import random_value
def test_decoder():
configs_path = "../../../../configs/"
config_name = "minimal"
presets = loader.load_presets(configs_path, config_name)
spec.apply_constants_preset(presets)
rng = Random(123)
# check these types only, Block covers a lot of operation types already.
for typ in [spec.BeaconBlock, spec.BeaconState, spec.IndexedAttestation, spec.AttestationDataAndCustodyBit]:
# create a random pyspec value
original = random_value.get_random_ssz_object(rng, typ, 100, 10,
mode=random_value.RandomizationMode.mode_random,
chaos=True)
# serialize it, using pyspec
pyspec_data = spec_ssz_impl.serialize(original)
# get the py-ssz type for it
block_sedes = translate_typ(typ)
# try decoding using the py-ssz type
raw_value = block_sedes.deserialize(pyspec_data)
# serialize it using py-ssz
pyssz_data = block_sedes.serialize(raw_value)
# now check if the serialized form is equal. If so, we confirmed decoding and encoding to work.
assert pyspec_data == pyssz_data
# now translate the py-ssz value in a pyspec-value
block = translate_value(raw_value, typ)
# and see if the hash-tree-root of the original matches the hash-tree-root of the decoded & translated value.
assert spec_ssz_impl.hash_tree_root(original) == spec_ssz_impl.hash_tree_root(block)