Search in sources :

Example 1 with RewardAndPenalty

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty in project teku by ConsenSys.

the class EpochTransitionBenchmark method applyDeltas.

@Benchmark
public void applyDeltas(Blackhole bh) {
    final SszMutableUInt64List balances = preEpochTransitionMutableState.getBalances();
    int validatorsSize = preEpochTransitionMutableState.getValidators().size();
    for (int i = 0; i < validatorsSize; i++) {
        final RewardAndPenalty delta = attestationDeltas.getDelta(i);
        balances.setElement(i, balances.getElement(i).plus(delta.getReward()).minusMinZero(delta.getPenalty()));
    }
}
Also used : RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 2 with RewardAndPenalty

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty 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 3 with RewardAndPenalty

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty 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 4 with RewardAndPenalty

use of tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty in project teku by ConsenSys.

the class AbstractEpochProcessor method applyDeltas.

protected void applyDeltas(final MutableBeaconState state, final RewardAndPenaltyDeltas attestationDeltas) {
    final SszMutableUInt64List balances = state.getBalances();
    // To optimize performance, calculate validator size once outside of the loop
    int validatorsCount = state.getValidators().size();
    for (int i = 0; i < validatorsCount; i++) {
        final RewardAndPenalty delta = attestationDeltas.getDelta(i);
        balances.setElement(i, balances.getElement(i).plus(delta.getReward()).minusMinZero(delta.getPenalty()));
    }
}
Also used : RewardAndPenalty(tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty) SszMutableUInt64List(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint)

Aggregations

RewardAndPenalty (tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas.RewardAndPenalty)4 SszMutableUInt64List (tech.pegasys.teku.infrastructure.ssz.collections.SszMutableUInt64List)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 TotalBalances (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.TotalBalances)2 ValidatorStatus (tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)1 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)1 RewardAndPenaltyDeltas (tech.pegasys.teku.spec.logic.common.statetransition.epoch.RewardAndPenaltyDeltas)1