2019-10-18 01:10:41 +00:00
|
|
|
package precompute
|
|
|
|
|
|
|
|
import (
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/time"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/math"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
2019-10-18 01:10:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProcessSlashingsPrecompute processes the slashed validators during epoch processing.
|
|
|
|
// This is an optimized version by passing in precomputed total epoch balances.
|
2021-07-23 16:11:21 +00:00
|
|
|
func ProcessSlashingsPrecompute(s state.BeaconState, pBal *Balance) error {
|
2021-09-30 19:00:14 +00:00
|
|
|
currentEpoch := time.CurrentEpoch(s)
|
2019-10-18 01:10:41 +00:00
|
|
|
exitLength := params.BeaconConfig().EpochsPerSlashingsVector
|
|
|
|
|
|
|
|
// Compute the sum of state slashings
|
2021-07-23 16:11:21 +00:00
|
|
|
slashings := s.Slashings()
|
2019-10-18 01:10:41 +00:00
|
|
|
totalSlashing := uint64(0)
|
2020-01-31 20:57:01 +00:00
|
|
|
for _, slashing := range slashings {
|
2019-10-18 01:10:41 +00:00
|
|
|
totalSlashing += slashing
|
|
|
|
}
|
2020-05-01 19:43:04 +00:00
|
|
|
|
2021-09-17 21:55:24 +00:00
|
|
|
minSlashing := math.Min(totalSlashing*params.BeaconConfig().ProportionalSlashingMultiplier, pBal.ActiveCurrentEpoch)
|
2020-05-01 13:45:24 +00:00
|
|
|
epochToWithdraw := currentEpoch + exitLength/2
|
2020-10-21 19:12:39 +00:00
|
|
|
|
|
|
|
var hasSlashing bool
|
|
|
|
// Iterate through validator list in state, stop until a validator satisfies slashing condition of current epoch.
|
2021-07-23 16:11:21 +00:00
|
|
|
err := s.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
|
2020-11-02 18:04:15 +00:00
|
|
|
correctEpoch := epochToWithdraw == val.WithdrawableEpoch()
|
|
|
|
if val.Slashed() && correctEpoch {
|
2020-10-21 19:12:39 +00:00
|
|
|
hasSlashing = true
|
|
|
|
}
|
2020-11-02 18:04:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-21 19:12:39 +00:00
|
|
|
}
|
|
|
|
// Exit early if there's no meaningful slashing to process.
|
|
|
|
if !hasSlashing {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 13:45:24 +00:00
|
|
|
increment := params.BeaconConfig().EffectiveBalanceIncrement
|
2021-02-10 20:52:45 +00:00
|
|
|
validatorFunc := func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error) {
|
2020-05-01 13:45:24 +00:00
|
|
|
correctEpoch := epochToWithdraw == val.WithdrawableEpoch
|
2020-01-31 20:57:01 +00:00
|
|
|
if val.Slashed && correctEpoch {
|
|
|
|
penaltyNumerator := val.EffectiveBalance / increment * minSlashing
|
2020-05-01 19:43:04 +00:00
|
|
|
penalty := penaltyNumerator / pBal.ActiveCurrentEpoch * increment
|
2023-01-26 14:40:12 +00:00
|
|
|
if err := helpers.DecreaseBalance(s, primitives.ValidatorIndex(idx), penalty); err != nil {
|
2021-02-10 20:52:45 +00:00
|
|
|
return false, val, err
|
2020-01-31 20:57:01 +00:00
|
|
|
}
|
2021-02-10 20:52:45 +00:00
|
|
|
return true, val, nil
|
2019-10-18 01:10:41 +00:00
|
|
|
}
|
2021-02-10 20:52:45 +00:00
|
|
|
return false, val, nil
|
2019-10-18 01:10:41 +00:00
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
return s.ApplyToEveryValidator(validatorFunc)
|
2019-10-18 01:10:41 +00:00
|
|
|
}
|