Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Electra penalty computation. EIP7125 #8612

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.logic.versions.electra.statetransition.epoch;

import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO;
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;

import java.util.ArrayList;
Expand All @@ -34,6 +35,7 @@
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators;
import tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatus;
import tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatusFactory;
import tech.pegasys.teku.spec.logic.common.statetransition.epoch.status.ValidatorStatuses;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException;
import tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil;
import tech.pegasys.teku.spec.logic.common.util.ValidatorsUtil;
Expand Down Expand Up @@ -297,4 +299,40 @@ public void processPendingConsolidations(final MutableBeaconState state) {
schemaDefinitionsElectra.getPendingConsolidationsSchema().createFromElements(newList));
}
}

/** Processes slashings */
@Override
public void processSlashings(
final MutableBeaconState state, final ValidatorStatuses validatorStatuses) {
final UInt64 totalBalance =
validatorStatuses.getTotalBalances().getCurrentEpochActiveValidators();
final UInt64 epoch = beaconStateAccessors.getCurrentEpoch(state);
final UInt64 adjustedTotalSlashingBalance =
state
.getSlashings()
.streamUnboxed()
.reduce(ZERO, UInt64::plus)
.times(getProportionalSlashingMultiplier())
.min(totalBalance);

final List<ValidatorStatus> validatorStatusList = validatorStatuses.getStatuses();
final int halfEpochsPerSlashingsVector = specConfig.getEpochsPerSlashingsVector() / 2;

final UInt64 increment = specConfig.getEffectiveBalanceIncrement();
final UInt64 penaltyPerEffectiveBalanceIncrement =
adjustedTotalSlashingBalance.dividedBy(totalBalance.dividedBy(increment));
for (int index = 0; index < validatorStatusList.size(); index++) {
final ValidatorStatus status = validatorStatusList.get(index);
if (status.isSlashed()
&& epoch.plus(halfEpochsPerSlashingsVector).equals(status.getWithdrawableEpoch())) {

// EIP7251
david-ry4n marked this conversation as resolved.
Show resolved Hide resolved
final UInt64 effectiveBalanceIncrements =
status.getCurrentEpochEffectiveBalance().dividedBy(increment);
final UInt64 penalty =
penaltyPerEffectiveBalanceIncrement.times(effectiveBalanceIncrements);
beaconStateMutators.decreaseBalance(state, index, penalty);
}
}
}
}