def test_validate_proposer_slashing_valid_double_vote(
        genesis_state, keymap, slots_per_epoch, max_indices_per_slashable_vote,
        config):
    attesting_state = genesis_state.copy(slot=genesis_state.slot +
                                         slots_per_epoch, )
    valid_attester_slashing = create_mock_attester_slashing_is_double_vote(
        attesting_state,
        config,
        keymap,
        attestation_epoch=0,
    )

    assert is_double_vote(
        valid_attester_slashing.slashable_attestation_1.data,
        valid_attester_slashing.slashable_attestation_2.data,
        slots_per_epoch,
    )
    assert not is_surround_vote(
        valid_attester_slashing.slashable_attestation_1.data,
        valid_attester_slashing.slashable_attestation_2.data,
        slots_per_epoch,
    )

    state = attesting_state.copy(slot=attesting_state.slot + 1, )
    validate_attester_slashing(
        state,
        valid_attester_slashing,
        max_indices_per_slashable_vote,
        slots_per_epoch,
    )
Beispiel #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,
        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,
            )
Beispiel #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.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,
            )
Beispiel #4
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)
def test_validate_attester_slashing_different_data(genesis_state, keymap,
                                                   slots_per_epoch, config):
    attesting_state = genesis_state.copy(slot=genesis_state.slot +
                                         slots_per_epoch, )
    valid_attester_slashing = create_mock_attester_slashing_is_double_vote(
        attesting_state,
        config,
        keymap,
        attestation_epoch=0,
    )

    with pytest.raises(ValidationError):
        validate_attester_slashing_different_data(
            valid_attester_slashing.slashable_attestation_1,
            valid_attester_slashing.
            slashable_attestation_1,  # Put the same SlashableAttestation
        )