-
Notifications
You must be signed in to change notification settings - Fork 169
/
slash.go
57 lines (48 loc) · 1.65 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
56
57
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// SlashAndResetMissCounters iterates over all the current missed counters and
// calculates the "valid vote rate" as:
// (votePeriodsPerWindow - missCounter)/votePeriodsPerWindow.
//
// If the valid vote rate is below the minValidPerWindow, the validator will be
// slashed and jailed.
func (k Keeper) SlashAndResetMissCounters(ctx sdk.Context) {
height := ctx.BlockHeight()
distributionHeight := height - sdk.ValidatorUpdateDelay - 1
var (
slashWindow = int64(k.SlashWindow(ctx))
votePeriod = int64(k.VotePeriod(ctx))
votePeriodsPerWindow = sdk.NewDec(slashWindow).QuoInt64(votePeriod).TruncateInt64()
)
var (
minValidPerWindow = k.MinValidPerWindow(ctx)
slashFraction = k.SlashFraction(ctx)
powerReduction = k.StakingKeeper.PowerReduction(ctx)
)
k.IterateMissCounters(ctx, func(operator sdk.ValAddress, missCounter uint64) bool {
diff := sdk.NewInt(votePeriodsPerWindow - int64(missCounter))
validVoteRate := sdk.NewDecFromInt(diff).QuoInt64(votePeriodsPerWindow)
// Slash and jail the validator if their valid vote rate is smaller than the
// minimum 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.StakingKeeper.Jail(ctx, consAddr)
}
}
k.DeleteMissCounter(ctx, operator)
return false
})
}