-
Notifications
You must be signed in to change notification settings - Fork 1
/
slash.go
executable file
·64 lines (55 loc) · 2.42 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
58
59
60
61
62
63
64
package keeper
import (
"strconv"
cosmostelemetry "github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fibonacci-chain/core/x/oracle/types"
)
// SlashAndResetCounters do slash any operator who over criteria & clear all operators miss counter to zero
func (k Keeper) SlashAndResetCounters(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)
k.IterateVotePenaltyCounters(ctx, func(operator sdk.ValAddress, votePenaltyCounter types.VotePenaltyCounter) bool {
// Calculate valid vote rate; (SlashWindow - (MissCounter + AbstainCounter))/SlashWindow
validVoteRate := sdk.NewDecFromInt(
sdk.NewInt(int64(votePeriodsPerWindow - (votePenaltyCounter.MissCount + votePenaltyCounter.AbstainCount)))).
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.StakingKeeper.Jail(ctx, consAddr)
cosmostelemetry.IncrValidatorSlashedCounter(consAddr.String(), "oracle")
}
}
winCount := votePeriodsPerWindow - (votePenaltyCounter.MissCount + votePenaltyCounter.AbstainCount)
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeEndSlashWindow,
sdk.NewAttribute(types.AttributeKeyOperator, operator.String()),
sdk.NewAttribute(types.AttributeKeyMissCount, strconv.FormatUint(votePenaltyCounter.MissCount, 10)),
sdk.NewAttribute(types.AttributeKeyAbstainCount, strconv.FormatUint(votePenaltyCounter.AbstainCount, 10)),
sdk.NewAttribute(types.AttributeKeySuccessCount, strconv.FormatUint(votePenaltyCounter.SuccessCount, 10)),
sdk.NewAttribute(types.AttributeKeyWinCount, strconv.FormatUint(winCount, 10)),
),
)
k.DeleteVotePenaltyCounter(ctx, operator)
return false
})
}