From dfb92fa4f198da30d6eeec09a31bfa86ec153341 Mon Sep 17 00:00:00 2001 From: David Ryan Date: Wed, 18 Sep 2024 15:22:43 +1000 Subject: [PATCH 1/2] Update Electra penalty computation. --- .../epoch/EpochProcessorElectra.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java index 0c6259a8922..f6b03ee3789 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java @@ -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; @@ -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; @@ -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 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 + final UInt64 effectiveBalanceIncrements = + status.getCurrentEpochEffectiveBalance().dividedBy(increment); + final UInt64 penalty = + penaltyPerEffectiveBalanceIncrement.times(effectiveBalanceIncrements); + beaconStateMutators.decreaseBalance(state, index, penalty); + } + } + } } From 3f90ab393a93411e6f6ef6d6a51dd27af431c267 Mon Sep 17 00:00:00 2001 From: David Ryan Date: Fri, 20 Sep 2024 13:21:35 +1000 Subject: [PATCH 2/2] Fixing a nit. :) --- .../electra/statetransition/epoch/EpochProcessorElectra.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java index f6b03ee3789..ee7c0b570c9 100644 --- a/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java +++ b/ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/electra/statetransition/epoch/EpochProcessorElectra.java @@ -326,7 +326,7 @@ public void processSlashings( if (status.isSlashed() && epoch.plus(halfEpochsPerSlashingsVector).equals(status.getWithdrawableEpoch())) { - // EIP7251 + // EIP-7251 final UInt64 effectiveBalanceIncrements = status.getCurrentEpochEffectiveBalance().dividedBy(increment); final UInt64 penalty =