Search in sources :

Example 1 with ValidatorStatus

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus in project teku by ConsenSys.

the class EpochProcessorAltair method processInactivityUpdates.

@Override
public void processInactivityUpdates(final MutableBeaconState baseState, final ValidatorStatuses validatorStatuses) {
    if (beaconStateAccessors.getCurrentEpoch(baseState).equals(SpecConfig.GENESIS_EPOCH)) {
        return;
    }
    final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(baseState);
    final SszMutableUInt64List inactivityScores = state.getInactivityScores();
    final List<ValidatorStatus> statuses = validatorStatuses.getStatuses();
    final boolean isInInactivityLeak = beaconStateAccessors.isInactivityLeak(state);
    for (int i = 0; i < statuses.size(); i++) {
        final ValidatorStatus validatorStatus = statuses.get(i);
        if (!validatorStatus.isEligibleValidator()) {
            continue;
        }
        // Increase inactivity score of inactive validators
        final UInt64 currentScore = inactivityScores.getElement(i);
        UInt64 newScore;
        if (validatorStatus.isNotSlashed() && validatorStatus.isPreviousEpochTargetAttester()) {
            newScore = currentScore.minusMinZero(1);
        } else {
            newScore = currentScore.plus(specConfigAltair.getInactivityScoreBias());
        }
        // Decrease the score of all validators for forgiveness when not during a leak
        if (!isInInactivityLeak) {
            newScore = newScore.minusMinZero(specConfigAltair.getInactivityScoreRecoveryRate());
        }
        if (!currentScore.equals(newScore)) {
            inactivityScores.setElement(i, newScore);
        }
    }
}
Also used : SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MutableBeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Example 2 with ValidatorStatus

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus in project teku by ConsenSys.

the class ValidatorStatusFactoryAltair method processParticipation.

@Override
protected void processParticipation(final List<ValidatorStatus> statuses, final BeaconState genericState, final UInt64 previousEpoch, final UInt64 currentEpoch) {
    final BeaconStateAltair state = BeaconStateAltair.required(genericState);
    final SszList<SszByte> previousParticipation = state.getPreviousEpochParticipation();
    final SszList<SszByte> currentParticipation = state.getCurrentEpochParticipation();
    for (int i = 0; i < statuses.size(); i++) {
        final ValidatorStatus status = statuses.get(i);
        if (status.isActiveInPreviousEpoch()) {
            final byte previousParticipationFlags = previousParticipation.get(i).get();
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_SOURCE_FLAG_INDEX)) {
                status.updatePreviousEpochSourceAttester(true);
            }
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_TARGET_FLAG_INDEX)) {
                status.updatePreviousEpochTargetAttester(true);
            }
            if (miscHelpersAltair.hasFlag(previousParticipationFlags, ParticipationFlags.TIMELY_HEAD_FLAG_INDEX)) {
                status.updatePreviousEpochHeadAttester(true);
            }
        }
        if (status.isActiveInCurrentEpoch()) {
            final byte currentParticipationFlags = currentParticipation.get(i).get();
            if (miscHelpersAltair.hasFlag(currentParticipationFlags, ParticipationFlags.TIMELY_SOURCE_FLAG_INDEX)) {
                status.updateCurrentEpochSourceAttester(true);
            }
            if (miscHelpersAltair.hasFlag(currentParticipationFlags, ParticipationFlags.TIMELY_TARGET_FLAG_INDEX)) {
                status.updateCurrentEpochTargetAttester(true);
            }
        }
    }
}
Also used : SszByte(tech.pegasys.teku.infrastructure.ssz.primitive.SszByte) BeaconStateAltair(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Example 3 with ValidatorStatus

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus in project teku by ConsenSys.

the class RewardsAndPenaltiesCalculatorPhase0 method getDeltas.

public RewardAndPenaltyDeltas getDeltas(final Step step) throws IllegalArgumentException {
    final RewardAndPenaltyDeltas deltas = new RewardAndPenaltyDeltas(validatorStatuses.getValidatorCount());
    final TotalBalances totalBalances = validatorStatuses.getTotalBalances();
    final List<ValidatorStatus> statuses = validatorStatuses.getStatuses();
    final UInt64 finalityDelay = getFinalityDelay();
    final UInt64 totalActiveBalanceSquareRoot = squareRootOrZero(totalBalances.getCurrentEpochActiveValidators());
    for (int index = 0; index < statuses.size(); index++) {
        final ValidatorStatus validator = statuses.get(index);
        if (!validator.isEligibleValidator()) {
            continue;
        }
        final UInt64 baseReward = getBaseReward(validator, totalActiveBalanceSquareRoot);
        final RewardAndPenalty delta = deltas.getDelta(index);
        step.apply(deltas, totalBalances, finalityDelay, validator, baseReward, delta);
    }
    return deltas;
}
Also used : TotalBalances(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances) RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus) RewardAndPenaltyDeltas(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas)

Example 4 with ValidatorStatus

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus in project teku by ConsenSys.

the class RewardsAndPenaltiesCalculatorAltair method processFlagIndexDeltas.

/**
 * Corresponds to altair beacon chain accessor get_flag_index_deltas
 *
 * @see <a
 *     href="https://github.com/ethereum/eth2.0-specs/blob/master/specs/altair/beacon-chain.md#beacon-state-accessors">Altair
 *     beacon-chain.md</a>
 * @param deltas The deltas accumulator (holding deltas for all validators) to be updated
 * @param flagIndex The flag index to process
 */
public void processFlagIndexDeltas(final RewardAndPenaltyDeltas deltas, final int flagIndex) {
    final List<ValidatorStatus> statusList = validatorStatuses.getStatuses();
    final TotalBalances totalBalances = validatorStatuses.getTotalBalances();
    final UInt64 effectiveBalanceIncrement = specConfigAltair.getEffectiveBalanceIncrement();
    final UInt64 unslashedParticipatingIncrements = getPrevEpochTotalParticipatingBalance(flagIndex).dividedBy(effectiveBalanceIncrement);
    final UInt64 weight = PARTICIPATION_FLAG_WEIGHTS.get(flagIndex);
    final UInt64 activeIncrements = totalBalances.getCurrentEpochActiveValidators().dividedBy(effectiveBalanceIncrement);
    for (int i = 0; i < statusList.size(); i++) {
        final ValidatorStatus validator = statusList.get(i);
        if (!validator.isEligibleValidator()) {
            continue;
        }
        final RewardAndPenalty validatorDeltas = deltas.getDelta(i);
        final UInt64 baseReward = getBaseReward(i);
        if (isUnslashedPrevEpochParticipatingIndex(validator, flagIndex)) {
            if (!isInactivityLeak()) {
                final UInt64 rewardNumerator = baseReward.times(weight).times(unslashedParticipatingIncrements);
                validatorDeltas.reward(rewardNumerator.dividedBy(activeIncrements.times(WEIGHT_DENOMINATOR)));
            }
        } else if (flagIndex != TIMELY_HEAD_FLAG_INDEX) {
            validatorDeltas.penalize(baseReward.times(weight).dividedBy(WEIGHT_DENOMINATOR));
        }
    }
}
Also used : TotalBalances(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances) RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Example 5 with ValidatorStatus

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus in project teku by ConsenSys.

the class RewardsAndPenaltiesCalculatorAltair method processInactivityPenaltyDeltas.

/**
 * Corresponds to altair beacon chain accessor get_inactivity_penalty_deltas
 *
 * @see <a
 *     href="https://github.com/ethereum/eth2.0-specs/blob/master/specs/altair/beacon-chain.md#beacon-state-accessors">Altair
 *     beacon-chain.md</a>
 * @param deltas The deltas accumulator (holding deltas for all validators) to be updated
 */
public void processInactivityPenaltyDeltas(final RewardAndPenaltyDeltas deltas) {
    final List<ValidatorStatus> statusList = validatorStatuses.getStatuses();
    for (int i = 0; i < statusList.size(); i++) {
        final ValidatorStatus validator = statusList.get(i);
        if (!validator.isEligibleValidator()) {
            continue;
        }
        if (validator.isPreviousEpochTargetAttester() && !validator.isSlashed()) {
            continue;
        }
        final UInt64 penaltyNumerator = validator.getCurrentEpochEffectiveBalance().times(stateAltair.getInactivityScores().get(i).get());
        final UInt64 penaltyDenominator = specConfigAltair.getInactivityScoreBias().times(getInactivityPenaltyQuotient());
        final UInt64 penalty = penaltyNumerator.dividedBy(penaltyDenominator);
        deltas.getDelta(i).penalize(penalty);
    }
}
Also used : UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidatorStatus(tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)

Aggregations

ValidatorStatus (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)5 RewardAndPenalty (tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty)3 TotalBalances (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances)3 SszMutableUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)1 SszMutableList (tech.pegasys.teku.infrastructure.ssz.SszMutableList)1 SszBitvector (tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector)1 SszUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszUInt64List)1 SszByte (tech.pegasys.teku.infrastructure.ssz.primitive.SszByte)1 SpecConfig (tech.pegasys.teku.spec.config.SpecConfig)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 HistoricalBatch (tech.pegasys.teku.spec.datastructures.state.HistoricalBatch)1 Validator (tech.pegasys.teku.spec.datastructures.state.Validator)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 MutableBeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState)1 BeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)1