Ejemplo n.º 1
0
def test_settle_penality_to_validator_and_whistleblower(
        monkeypatch, num_validators, committee, ten_validators_state,
        latest_penalized_exit_length, whistleblower_reward_quotient,
        epoch_length, max_deposit, target_committee_size, shard_count):
    from eth2.beacon import helpers

    def mock_get_crosslink_committees_at_slot(state, slot, epoch_length,
                                              target_committee_size,
                                              shard_count):
        return ((
            committee,
            1,
        ), )

    monkeypatch.setattr(helpers, 'get_crosslink_committees_at_slot',
                        mock_get_crosslink_committees_at_slot)

    state = ten_validators_state
    validator_index = 5
    whistleblower_index = get_beacon_proposer_index(
        state,
        state.slot,
        epoch_length,
        target_committee_size,
        shard_count,
    )
    effective_balance = max_deposit * GWEI_PER_ETH

    # Check the initial balance
    assert (state.validator_balances[validator_index] ==
            state.validator_balances[whistleblower_index] == effective_balance)

    state = _settle_penality_to_validator_and_whistleblower(
        state=state,
        validator_index=validator_index,
        latest_penalized_exit_length=latest_penalized_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        epoch_length=epoch_length,
        max_deposit=max_deposit,
        target_committee_size=target_committee_size,
        shard_count=shard_count,
    )

    # Check `state.latest_penalized_balances`
    latest_penalized_balances_list = list(state.latest_penalized_balances)
    last_penalized_epoch = (state.slot //
                            epoch_length) % latest_penalized_exit_length
    latest_penalized_balances_list[
        last_penalized_epoch] = max_deposit * GWEI_PER_ETH
    latest_penalized_balances = tuple(latest_penalized_balances_list)

    assert state.latest_penalized_balances == latest_penalized_balances

    # Check penality and reward
    whistleblower_reward = (effective_balance // whistleblower_reward_quotient)
    whistleblower_balance = state.validator_balances[whistleblower_index]
    validator_balance = state.validator_balances[validator_index]
    balance_difference = whistleblower_balance - validator_balance
    assert balance_difference == whistleblower_reward * 2
def test_penalize_validator(monkeypatch,
                            num_validators,
                            committee,
                            ten_validators_state,
                            epoch_length,
                            latest_penalized_exit_length,
                            whistleblower_reward_quotient,
                            entry_exit_delay,
                            max_deposit,
                            target_committee_size,
                            shard_count):
    from eth2.beacon import helpers

    def mock_get_crosslink_committees_at_slot(state,
                                              slot,
                                              epoch_length,
                                              target_committee_size,
                                              shard_count):
        return (
            (committee, 1,),
        )

    monkeypatch.setattr(
        helpers,
        'get_crosslink_committees_at_slot',
        mock_get_crosslink_committees_at_slot
    )

    state = ten_validators_state
    index = 1

    result_state = penalize_validator(
        state=state,
        index=index,
        epoch_length=epoch_length,
        latest_penalized_exit_length=latest_penalized_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        entry_exit_delay=entry_exit_delay,
        max_deposit=max_deposit,
        target_committee_size=target_committee_size,
        shard_count=shard_count,
    )

    # Just check if `prepare_validator_for_withdrawal` applied these two functions
    expected_state = exit_validator(state, index, epoch_length, entry_exit_delay)
    expected_state = _settle_penality_to_validator_and_whistleblower(
        state=expected_state,
        validator_index=index,
        latest_penalized_exit_length=latest_penalized_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        epoch_length=epoch_length,
        max_deposit=max_deposit,
        target_committee_size=target_committee_size,
        shard_count=shard_count,
    )

    assert result_state == expected_state
Ejemplo n.º 3
0
def test_settle_penality_to_validator_and_whistleblower(
        monkeypatch, num_validators, committee, n_validators_state,
        latest_slashed_exit_length, whistleblower_reward_quotient,
        max_deposit_amount, committee_config):
    from eth2.beacon import committee_helpers

    def mock_get_crosslink_committees_at_slot(state,
                                              slot,
                                              committee_config,
                                              registry_change=False):
        return ((
            committee,
            1,
        ), )

    monkeypatch.setattr(committee_helpers, 'get_crosslink_committees_at_slot',
                        mock_get_crosslink_committees_at_slot)

    state = n_validators_state
    validator_index = 5
    whistleblower_index = get_beacon_proposer_index(
        state,
        state.slot,
        committee_config,
    )
    effective_balance = max_deposit_amount

    # Check the initial balance
    assert (state.validator_balances[validator_index] ==
            state.validator_balances[whistleblower_index] == effective_balance)

    state = _settle_penality_to_validator_and_whistleblower(
        state=state,
        validator_index=validator_index,
        latest_slashed_exit_length=latest_slashed_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        max_deposit_amount=max_deposit_amount,
        committee_config=committee_config,
    )

    # Check `state.latest_slashed_balances`
    latest_slashed_balances_list = list(state.latest_slashed_balances)
    last_slashed_epoch = (
        state.current_epoch(committee_config.SLOTS_PER_EPOCH) %
        latest_slashed_exit_length)
    latest_slashed_balances_list[last_slashed_epoch] = max_deposit_amount
    latest_slashed_balances = tuple(latest_slashed_balances_list)

    assert state.latest_slashed_balances == latest_slashed_balances

    # Check penality and reward
    whistleblower_reward = (effective_balance // whistleblower_reward_quotient)
    whistleblower_balance = state.validator_balances[whistleblower_index]
    validator_balance = state.validator_balances[validator_index]
    balance_difference = whistleblower_balance - validator_balance
    assert balance_difference == whistleblower_reward * 2
Ejemplo n.º 4
0
def test_slash_validator(monkeypatch, num_validators, committee,
                         n_validators_state, genesis_epoch, slots_per_epoch,
                         latest_slashed_exit_length,
                         whistleblower_reward_quotient, activation_exit_delay,
                         max_deposit_amount, target_committee_size,
                         shard_count, committee_config):
    from eth2.beacon import committee_helpers

    def mock_get_crosslink_committees_at_slot(state,
                                              slot,
                                              committee_config,
                                              registry_change=False):
        return ((
            committee,
            1,
        ), )

    monkeypatch.setattr(committee_helpers, 'get_crosslink_committees_at_slot',
                        mock_get_crosslink_committees_at_slot)

    state = n_validators_state
    index = 1

    result_state = slash_validator(
        state=state,
        index=index,
        latest_slashed_exit_length=latest_slashed_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        max_deposit_amount=max_deposit_amount,
        committee_config=committee_config,
    )

    # Just check if `prepare_validator_for_withdrawal` applied these two functions
    expected_state = exit_validator(state, index, slots_per_epoch,
                                    activation_exit_delay)
    expected_state = _settle_penality_to_validator_and_whistleblower(
        state=expected_state,
        validator_index=index,
        latest_slashed_exit_length=latest_slashed_exit_length,
        whistleblower_reward_quotient=whistleblower_reward_quotient,
        max_deposit_amount=max_deposit_amount,
        committee_config=committee_config,
    )
    current_epoch = state.current_epoch(slots_per_epoch)
    validator = state.validator_registry[index].copy(
        slashed=False,
        withdrawable_epoch=current_epoch + latest_slashed_exit_length,
    )
    expected_state.update_validator_registry(index, validator)

    assert result_state == expected_state