Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def pyspec_transition(pre_state, pre_block):
    yaml = YAML(typ='base')
    loaded = yaml.load(Path('min.config.bak'))
    config_data = dict()
    for k, v in loaded.items():
        if v.startswith("0x"):
            config_data[k] = bytes.fromhex(v[2:])
        else:
            config_data[k] = int(v)

    spec.apply_constants_preset(config_data)

    spec_pre_state = decoder.translate_value(pre_state, spec.BeaconState)
    spec_pre_block = decoder.translate_value(pre_block, spec.BeaconBlock)

    spec_post_block = spec.state_transition(spec_pre_state, spec_pre_block,
                                            False)
    return spec_post_block
Exemplo n.º 3
0
def convert_raw_to_ssz(raw, ssz_typ):
    # get the pyssz type
    # read raw with the pyszz from from pyssz
    # convert pyssz ob to ssz_type
    sedes = translate_typ(ssz_typ)
    pyssz_value = sedes.deserialize(raw)

    ssz_value = translate_value(pyssz_value, ssz_typ)

    return ssz_value
Exemplo n.º 4
0
def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(deposit_sedes.deserialize(input_data),
                                DepositTestCase)

    try:
        # modifies state in place
        spec.process_deposit(state=test_case.pre, deposit=test_case.deposit)
        return serialize(test_case.pre)
    except (AssertionError, IndexError):
        return None
Exemplo n.º 5
0
def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(block_header_sedes.deserialize(input_data),
                                BlockHeaderTestCase)

    try:
        # modifies state in place
        spec.process_block_header(state=test_case.pre, block=test_case.block)
        # NOTE - signature verification should do nothing with bls disabled
        return serialize(test_case.pre)
    except (AssertionError, IndexError):
        return None
Exemplo n.º 6
0
def FuzzerRunOne(FuzzerInput):
    state_block = translate_value(state_block_sedes.deserialize(FuzzerInput),
                                  StateBlock)
    prestate = copy.deepcopy(prestates[state_block.stateID])

    try:
        poststate = spec.state_transition(prestate, state_block.block, False)
        return serialize(poststate)
    except AssertionError as e:
        pass
    except IndexError:
        pass
Exemplo n.º 7
0
def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(
        proposer_slashing_sedes.deserialize(input_data),
        ProposerSlashingTestCase)

    try:
        # modifies state in place
        spec.process_proposer_slashing(test_case.pre,
                                       test_case.proposer_slashing)
        # NOTE - signature verification should do nothing with bls disabled
        return serialize(test_case.pre)
    except (AssertionError, IndexError):
        return None
Exemplo n.º 8
0
def FuzzerRunOne(fuzzer_input):
    state_block = translate_value(block_sedes.deserialize(fuzzer_input),
                                  BlockTestCase)

    try:
        # NOTE we don't validate state root here
        poststate = spec.state_transition(
            state=state_block.pre,
            block=state_block.block,
            validate_state_root=VALIDATE_STATE_ROOT,
        )
        return serialize(poststate)
    except (AssertionError, IndexError):
        return None
def get_operations(
    search_dir: pathlib.Path, op_details: OperationRegistryEntry
) -> typing.Iterable[SSZType]:
    """Returns unique operations in the search directory."""
    # TODO deduplication? Only an efficiency not correctness problem. SHA1 does dedup after this
    seen_ops: typing.Set[bytes] = set()
    for f in recursive_iterfiles(search_dir):
        if f.name.lower() in op_details.ssz_file_names:
            raw = f.read_bytes()
            raw_hash = hashlib.sha1(raw).digest()
            if raw_hash not in seen_ops:
                # TODO handle deserialization errors
                seen_ops.add(raw_hash)
                yield translate_value(
                    op_details.operation_sedes.deserialize(raw),
                    op_details.operation_type,
                )
Exemplo n.º 10
0
def load_prestates():
    prestates = []
    assert 'ETH2_FUZZER_STATE_CORPUS_PATH' in os.environ, "ETH2_FUZZER_STATE_CORPUS_PATH not set"

    ETH2_FUZZER_STATE_CORPUS_PATH = os.environ['ETH2_FUZZER_STATE_CORPUS_PATH']
    i = 0
    while True:
        try:
            with open(os.path.join(ETH2_FUZZER_STATE_CORPUS_PATH, str(i)),
                      'rb') as fp:
                raw_value = translate_typ(spec.BeaconState).deserialize(
                    fp.read())
                prestates += [translate_value(raw_value, spec.BeaconState)]
        except FileNotFoundError:
            break
        i += 1
    assert len(prestates) > 0, "Could not load any prestates"
    return prestates