Ejemplo n.º 1
0
def test_process_attester_slashings(genesis_state, sample_beacon_block_params,
                                    sample_beacon_block_body_params, config,
                                    keymap, min_attestation_inclusion_delay,
                                    success):
    attesting_state = genesis_state.copy(
        slot=genesis_state.slot + config.SLOTS_PER_EPOCH,
        block_roots=tuple(
            i.to_bytes(32, "little")
            for i in range(config.SLOTS_PER_HISTORICAL_ROOT)))
    valid_attester_slashing = create_mock_attester_slashing_is_double_vote(
        attesting_state,
        config,
        keymap,
        attestation_epoch=0,
    )
    state = attesting_state.copy(slot=attesting_state.slot +
                                 min_attestation_inclusion_delay, )

    if success:
        block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy(
            attester_slashings=(valid_attester_slashing, ), )
        block = SerenityBeaconBlock(**sample_beacon_block_params).copy(
            slot=state.slot,
            body=block_body,
        )

        attester_index = valid_attester_slashing.attestation_1.custody_bit_0_indices[
            0]

        new_state = process_attester_slashings(
            state,
            block,
            config,
        )
        # Check if slashed
        assert not state.validators[attester_index].slashed
        assert new_state.validators[attester_index].slashed
    else:
        invalid_attester_slashing = valid_attester_slashing.copy(
            attestation_2=valid_attester_slashing.attestation_2.copy(
                data=valid_attester_slashing.attestation_1.data, ))
        block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy(
            attester_slashings=(invalid_attester_slashing, ), )
        block = SerenityBeaconBlock(**sample_beacon_block_params).copy(
            slot=state.slot,
            body=block_body,
        )

        with pytest.raises(ValidationError):
            process_attester_slashings(
                state,
                block,
                config,
            )
Ejemplo n.º 2
0
def test_process_attester_slashings(genesis_state, sample_beacon_block_params,
                                    sample_beacon_block_body_params, config,
                                    keymap, min_attestation_inclusion_delay,
                                    success):
    attesting_state = genesis_state.copy(slot=genesis_state.slot +
                                         config.SLOTS_PER_EPOCH, )
    valid_attester_slashing = create_mock_attester_slashing_is_double_vote(
        attesting_state,
        config,
        keymap,
        attestation_epoch=0,
    )
    state = attesting_state.copy(slot=attesting_state.slot +
                                 min_attestation_inclusion_delay, )

    if success:
        block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy(
            attester_slashings=(valid_attester_slashing, ), )
        block = SerenityBeaconBlock(**sample_beacon_block_params).copy(
            slot=state.slot,
            body=block_body,
        )

        attester_index = valid_attester_slashing.slashable_attestation_1.validator_indices[
            0]

        new_state = process_attester_slashings(
            state,
            block,
            config,
        )
        # Check if slashed
        assert (new_state.validator_balances[attester_index] <
                state.validator_balances[attester_index])
    else:
        invalid_attester_slashing = valid_attester_slashing.copy(
            slashable_attestation_2=valid_attester_slashing.
            slashable_attestation_2.copy(
                data=valid_attester_slashing.slashable_attestation_1.data, ))
        block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy(
            attester_slashings=(invalid_attester_slashing, ), )
        block = SerenityBeaconBlock(**sample_beacon_block_params).copy(
            slot=state.slot,
            body=block_body,
        )

        with pytest.raises(ValidationError):
            process_attester_slashings(
                state,
                block,
                config,
            )
Ejemplo n.º 3
0
def test_process_attester_slashings(
    genesis_state,
    sample_beacon_block_params,
    sample_beacon_block_body_params,
    config,
    keymap,
    min_attestation_inclusion_delay,
    success,
):
    attesting_state = genesis_state.mset(
        "slot",
        genesis_state.slot + config.SLOTS_PER_EPOCH,
        "block_roots",
        tuple(
            i.to_bytes(32, "little")
            for i in range(config.SLOTS_PER_HISTORICAL_ROOT)),
    )
    valid_attester_slashing = create_mock_attester_slashing_is_double_vote(
        attesting_state, config, keymap, attestation_epoch=0)
    state = attesting_state.set(
        "slot", attesting_state.slot + min_attestation_inclusion_delay)

    if success:
        block_body = BeaconBlockBody.create(
            **sample_beacon_block_body_params).set("attester_slashings",
                                                   (valid_attester_slashing, ))
        block = SerenityBeaconBlock.create(**sample_beacon_block_params).mset(
            "slot", state.slot, "body", block_body)

        attester_index = valid_attester_slashing.attestation_1.attesting_indices[
            0]

        new_state = process_attester_slashings(state, block, config)
        # Check if slashed
        assert not state.validators[attester_index].slashed
        assert new_state.validators[attester_index].slashed
    else:
        invalid_attester_slashing = valid_attester_slashing.transform(
            ["attestation_2", "data"],
            valid_attester_slashing.attestation_1.data)
        block_body = BeaconBlockBody.create(
            **sample_beacon_block_body_params).set(
                "attester_slashings", (invalid_attester_slashing, ))
        block = SerenityBeaconBlock.create(**sample_beacon_block_params).mset(
            "slot", state.slot, "body", block_body)

        with pytest.raises(ValidationError):
            process_attester_slashings(state, block, config)
Ejemplo n.º 4
0
def FuzzerRunOne(input_data: bytes) -> typing.Optional[bytes]:
    test_case = ssz.decode(input_data, AttesterSlashingTestCase)

    # NOTE Trinity doesn't implement a standalone process_attester_slashing
    # So we make a dummy block to pass to process_attester_slashings
    dummy_block = Dummy()
    dummy_block.body = Dummy()
    dummy_block.body.attester_slashings = [test_case.attester_slashing]

    try:
        post = process_attester_slashings(state=test_case.pre,
                                          block=dummy_block,
                                          config=SERENITY_CONFIG)
    except ValidationError as e:
        return None
    return ssz.encode(post)