def load_builtin_registry() -> OperationRegistry:
    registry_entries = [
        OperationRegistryEntry(
            name="block",
            operation_type=spec.BeaconBlock,
            operation_sedes=translate_typ(spec.BeaconBlock),
            test_type=BlockTestCase,
            test_type_factory=lambda i, o: BlockTestCase(state_id=i, block=o),
            test_sedes=translate_typ(BlockTestCase),
            ssz_file_names=("block.ssz"),
        ),
        OperationRegistryEntry(
            name="block_header",
            operation_type=spec.BeaconBlock,
            operation_sedes=translate_typ(spec.BeaconBlock),
            test_type=BlockHeaderTestCase,
            test_type_factory=lambda i, o: BlockHeaderTestCase(state_id=i, block=o),
            test_sedes=translate_typ(BlockHeaderTestCase),
            ssz_file_names=("block.ssz"),
        ),
        OperationRegistryEntry(
            name="attestation",
            operation_type=spec.Attestation,
            operation_sedes=translate_typ(spec.Attestation),
            test_type=AttestationTestCase,
            test_type_factory=lambda i, o: AttestationTestCase(
                state_id=i, attestation=o
            ),
            test_sedes=translate_typ(AttestationTestCase),
            ssz_file_names=("attestation.ssz"),
        ),
        OperationRegistryEntry(
            name="attester_slashing",
            operation_type=spec.AttesterSlashing,
            operation_sedes=translate_typ(spec.AttesterSlashing),
            test_type=AttesterSlashingTestCase,
            test_type_factory=lambda i, o: AttesterSlashingTestCase(
                state_id=i, attester_slashing=o
            ),
            test_sedes=translate_typ(AttesterSlashingTestCase),
            ssz_file_names=("attester_slashing.ssz"),
        ),
        OperationRegistryEntry(
            name="proposer_slashing",
            operation_type=spec.ProposerSlashing,
            operation_sedes=translate_typ(spec.ProposerSlashing),
            test_type=ProposerSlashingTestCase,
            test_type_factory=lambda i, o: ProposerSlashingTestCase(
                state_id=i, proposer_slashing=o
            ),
            test_sedes=translate_typ(ProposerSlashingTestCase),
            ssz_file_names=("proposer_slashing.ssz"),
        ),
    ]
    return OperationRegistry({r.name: r for r in registry_entries})
Esempio n. 2
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)
Esempio 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
Esempio n. 4
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
Esempio n. 5
0
# TODO(gnattishness) fix config path difficult to do unless we assume the eth2spec
# module is at a fixed position relative to the configs
# (i.e. it is inside a cloned eth2.0-specs repo)
configs_path = "/eth2/eth2.0-specs/configs"
# TODO allow this to be adjusted?
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)


class BlockHeaderTestCase(spec.Container):
    pre: spec.BeaconState
    block: spec.BeaconBlock


block_header_sedes = translate_typ(BlockHeaderTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


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
Esempio n. 6
0
from eth2spec.phase0 import spec as spec
from preset_loader import loader
from ssz.exceptions import DeserializationError
presets = loader.load_presets(
    '/home/jhg/eth-2019/x/eth2.0-fuzzing/files/fuzzers/block/eth2.0-specs/configs',
    'minimal')
spec.apply_constants_preset(presets)

from eth2spec.fuzzing.decoder import translate_typ, translate_value

block_sedes = translate_typ(spec.BeaconBlock)


def FuzzerRunOne(FuzzerInput):
    try:
        obj = block_sedes.deserialize(FuzzerInput)
        serialized = block_sedes.serialize(obj)
        if serialized != FuzzerInput:
            print("original: " + str([FuzzerInput]))
            print("serialized: " + str([serialized]))
            raise Exception("")
    except DeserializationError:
        pass
Esempio n. 7
0
# TODO fix up so not hard-coded
configs_path = "/eth2/eth2.0-specs/configs"
# Apply 'mainnet' template
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)

VALIDATE_STATE_ROOT = True


class BlockTestCase(spec.Container):
    pre: spec.BeaconState
    block: spec.BeaconBlock


block_sedes = translate_typ(BlockTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


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,
Esempio n. 8
0
# TODO(gnattishness) fix config path difficult to do unless we assume the eth2spec
# module is at a fixed position relative to the configs
# (i.e. it is inside a cloned eth2.0-specs repo)
configs_path = "/eth2/eth2.0-specs/configs"
# TODO allow this to be adjusted?
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)


class AttestationTestCase(spec.Container):
    pre: spec.BeaconState
    attestation: spec.Attestation


attestation_sedes = translate_typ(AttestationTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(attestation_sedes.deserialize(input_data),
                                AttestationTestCase)

    try:
        # modifies state in place
        spec.process_attestation(test_case.pre, test_case.attestation)
        # NOTE - signature verification should do nothing with bls disabled
Esempio n. 9
0
import copy
from eth2spec.fuzzing.decoder import translate_typ, translate_value
from eth2spec.utils.ssz.ssz_typing import uint32, uint8
from eth2spec.utils.ssz.ssz_impl import serialize

from eth2spec.utils import bls

bls.bls_active = False


class StateBlock(spec.Container):
    stateID: uint32
    block: spec.BeaconBlock


state_block_sedes = translate_typ(StateBlock)


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)]
Esempio n. 10
0
# TODO(gnattishness) fix config path difficult to do unless we assume the eth2spec
# module is at a fixed position relative to the configs
# (i.e. it is inside a cloned eth2.0-specs repo)
configs_path = "/eth2/eth2.0-specs/configs"
# TODO allow this to be adjusted?
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)


class DepositTestCase(spec.Container):
    pre: spec.BeaconState
    deposit: spec.Deposit


deposit_sedes = translate_typ(DepositTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


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)
Esempio n. 11
0
# TODO(gnattishness) fix config path difficult to do unless we assume the eth2spec
# module is at a fixed position relative to the configs
# (i.e. it is inside a cloned eth2.0-specs repo)
configs_path = "/eth2/eth2.0-specs/configs"
# TODO allow this to be adjusted?
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)


class VoluntaryExitTestCase(spec.Container):
    pre: spec.BeaconState
    exit: spec.VoluntaryExit


voluntary_exit_sedes = translate_typ(VoluntaryExitTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = translate_value(voluntary_exit_sedes.deserialize(input_data),
                                VoluntaryExitTestCase)

    try:
        # modifies state in place
        spec.process_voluntary_exit(state=test_case.pre, exit=test_case.exit)
        return serialize(test_case.pre)
Esempio n. 12
0
# TODO(gnattishness) fix config path difficult to do unless we assume the eth2spec
# module is at a fixed position relative to the configs
# (i.e. it is inside a cloned eth2.0-specs repo)
configs_path = "/eth2/eth2.0-specs/configs"
# TODO allow this to be adjusted?
presets = loader.load_presets(configs_path, "mainnet")
spec.apply_constants_preset(presets)


class ProposerSlashingTestCase(spec.Container):
    pre: spec.BeaconState
    proposer_slashing: spec.ProposerSlashing


proposer_slashing_sedes = translate_typ(ProposerSlashingTestCase)


def FuzzerInit(bls_disabled: bool) -> None:
    if bls_disabled:
        bls.bls_active = False


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,