2019-10-18 01:10:41 +00:00
|
|
|
package precompute
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
|
|
|
minSlashing := mathutil.Min(totalSlashing*3, pBal.ActiveCurrentEpoch)
|
2020-05-01 13:45:24 +00:00
|
|
|
epochToWithdraw := currentEpoch + exitLength/2
|
|
|
|
increment := params.BeaconConfig().EffectiveBalanceIncrement
|
2020-05-01 02:48:46 +00:00
|
|
|
validatorFunc := func(idx int, val *ethpb.Validator) (bool, 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
|
2020-01-31 20:57:01 +00:00
|
|
|
if err := helpers.DecreaseBalance(state, uint64(idx), penalty); err != nil {
|
2020-05-01 02:48:46 +00:00
|
|
|
return false, err
|
2020-01-31 20:57:01 +00:00
|
|
|
}
|
2020-05-01 02:48:46 +00:00
|
|
|
return true, nil
|
2019-10-18 01:10:41 +00:00
|
|
|
}
|
2020-05-01 02:48:46 +00:00
|
|
|
return false, 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
|
|
|
}
|