-
Notifications
You must be signed in to change notification settings - Fork 0
/
slash.go
50 lines (42 loc) · 1.51 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// SlashAndResetMissCounters slashes any operator who over criteria and clears all operators' miss counter to zero.
func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) {
stakingKeeper := k.StakingKeeper()
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 := stakingKeeper.PowerReduction(ctx)
k.IterateMissCounters(ctx, func(operator sdk.ValAddress, missCounter uint64) bool {
// Calculate valid vote rate: (SlashWindow - MissCounter) / SlashWindow
validVoteRate := sdk.NewDecFromInt(
sdk.NewInt(int64(votePeriodsPerWindow - missCounter))).
QuoInt64(int64(votePeriodsPerWindow))
// Penalize the validator whose valid vote rate is smaller than min threshold
if validVoteRate.LT(minValidPerWindow) {
validator := stakingKeeper.Validator(ctx, operator)
if validator.IsBonded() && !validator.IsJailed() {
consAddr, err := validator.GetConsAddr()
if err != nil {
panic(err)
}
stakingKeeper.Slash(
ctx, consAddr,
distributionHeight, validator.GetConsensusPower(powerReduction), slashFraction,
)
stakingKeeper.Jail(ctx, consAddr)
}
}
k.DeleteMissCounter(ctx, operator)
return false
})
}