2019-10-18 01:10:41 +00:00
|
|
|
package precompute
|
|
|
|
|
|
|
|
import (
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-01-31 20:57:01 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-10-18 01:10:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-01-31 20:57:01 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2019-10-18 01:10:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/mathutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProcessSlashingsPrecompute processes the slashed validators during epoch processing.
|
|
|
|
// This is an optimized version by passing in precomputed total epoch balances.
|
2020-05-01 19:43:04 +00:00
|
|
|
func ProcessSlashingsPrecompute(state *stateTrie.BeaconState, pBal *Balance) error {
|
2019-10-18 01:10:41 +00:00
|
|
|
currentEpoch := helpers.CurrentEpoch(state)
|
|
|
|
exitLength := params.BeaconConfig().EpochsPerSlashingsVector
|
|
|
|
|
|
|
|
// Compute the sum of state slashings
|
2020-01-31 20:57:01 +00:00
|
|
|
slashings := state.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
|
|
|
|
2020-10-09 02:49:20 +00:00
|
|
|
minSlashing := mathutil.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.
|
2020-11-09 10:08:08 +00:00
|
|
|
err := state.ReadFromEveryValidator(func(idx int, val stateTrie.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
|
2021-02-23 00:14:50 +00:00
|
|
|
if err := helpers.DecreaseBalance(state, types.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
|
|
|
|
2020-05-01 02:48:46 +00:00
|
|
|
return state.ApplyToEveryValidator(validatorFunc)
|
2019-10-18 01:10:41 +00:00
|
|
|
}
|