def container_case_fn(rng: Random, mode: RandomizationMode, typ: SSZType): return get_random_ssz_object(rng, typ, max_bytes_length=2000, max_list_length=2000, mode=mode, chaos=False)
def uint_case_fn(rng: Random, mode: RandomizationMode, typ: Type[BasicView]): return get_random_ssz_object(rng, typ, max_bytes_length=typ.type_byte_length(), max_list_length=1, mode=mode, chaos=False)
def test_decoder(): rng = Random(123) # check these types only, Block covers a lot of operation types already. for typ in [spec.AttestationDataAndCustodyBit, spec.BeaconState, spec.BeaconBlock]: # 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. original_hash_tree_root = spec_ssz_impl.hash_tree_root(original) assert original_hash_tree_root == spec_ssz_impl.hash_tree_root(block) assert original_hash_tree_root == block_sedes.get_hash_tree_root(raw_value)
def bitlist_case_fn(rng: Random, mode: RandomizationMode, limit: int): return get_random_ssz_object(rng, Bitlist[limit], max_bytes_length=(limit // 8) + 1, max_list_length=limit, mode=mode, chaos=False)
def create_test_case(rng: Random, typ, mode: random_value.RandomizationMode, chaos: bool) -> Iterable[gen_typing.TestCasePart]: value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos) yield "value", "data", encode.encode(value) yield "serialized", "ssz", serialize(value) roots_data = { "root": '0x' + hash_tree_root(value).hex() } yield "roots", "data", roots_data
def basic_vector_case_fn(rng: Random, mode: RandomizationMode, elem_type: Type[BasicView], length: int): return get_random_ssz_object(rng, Vector[elem_type, length], max_bytes_length=length * 8, max_list_length=length, mode=mode, chaos=False)
def create_test_case(rng: Random, name: str, mode: random_value.RandomizationMode, chaos: bool): typ = spec.get_ssz_type_by_name(name) value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos) yield "type_name", name yield "value", encode.encode(value, typ) yield "serialized", '0x' + serialize(value).hex() yield "root", '0x' + hash_tree_root(value).hex() if hasattr(value, "signature"): yield "signing_root", '0x' + signing_root(value).hex()
def create_test_case(rng: Random, typ, mode: random_value.RandomizationMode, chaos: bool) -> Iterable[gen_typing.TestCasePart]: value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos) yield "value", "data", encode.encode(value) yield "serialized", "ssz", serialize(value) roots_data = {"root": '0x' + hash_tree_root(value).hex()} if isinstance(value, Container) and hasattr(value, "signature"): roots_data["signing_root"] = '0x' + signing_root(value).hex() yield "roots", "data", roots_data
def bitvector_case_fn(rng: Random, mode: RandomizationMode, size: int, invalid_making_pos: int = None): bits = get_random_ssz_object(rng, Bitvector[size], max_bytes_length=(size + 7) // 8, max_list_length=size, mode=mode, chaos=False) if invalid_making_pos is not None and invalid_making_pos <= size: already_invalid = False for i in range(invalid_making_pos, size): if bits[i]: already_invalid = True if not already_invalid: bits[invalid_making_pos] = True return bits
def create_test_case(rng: Random, name: str, typ, mode: random_value.RandomizationMode, chaos: bool): value = random_value.get_random_ssz_object(rng, typ, MAX_BYTES_LENGTH, MAX_LIST_LENGTH, mode, chaos) yield name, create_test_case_contents(value, typ)