-
Notifications
You must be signed in to change notification settings - Fork 208
/
slash.go
55 lines (47 loc) · 1.7 KB
/
slash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/NibiruChain/collections"
)
// SlashAndResetMissCounters do slash any operator who over criteria & clear all operators miss counter to zero
func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) {
height := ctx.BlockHeight()
distributionHeight := height - sdk.ValidatorUpdateDelay - 1
// slash_window / vote_period
votePeriodsPerWindow := uint64(
sdk.NewDec(int64(k.SlashWindow(ctx))).
QuoInt64(int64(k.VotePeriod(ctx))).
TruncateInt64(),
)
minValidPerWindow := k.MinValidPerWindow(ctx)
slashFraction := k.SlashFraction(ctx)
powerReduction := k.StakingKeeper.PowerReduction(ctx)
for _, mc := range k.MissCounters.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() {
operator := mc.Key
missCounter := mc.Value
// Calculate valid vote rate; (SlashWindow - MissCounter)/SlashWindow
validVoteRate := sdk.NewDecFromInt(
sdk.NewInt(int64(votePeriodsPerWindow - missCounter))).
QuoInt64(int64(votePeriodsPerWindow))
// Penalize the validator whose the valid vote rate is smaller than min threshold
if validVoteRate.LT(minValidPerWindow) {
validator := k.StakingKeeper.Validator(ctx, operator)
if validator.IsBonded() && !validator.IsJailed() {
consAddr, err := validator.GetConsAddr()
if err != nil {
panic(err)
}
k.StakingKeeper.Slash(
ctx, consAddr,
distributionHeight, validator.GetConsensusPower(powerReduction), slashFraction,
)
k.Logger(ctx).Info("slash", "validator", consAddr.String(), "fraction", slashFraction.String())
k.StakingKeeper.Jail(ctx, consAddr)
}
}
err := k.MissCounters.Delete(ctx, operator)
if err != nil {
panic(err)
}
}
}