/
soft_opt_out.go
126 lines (110 loc) · 4.37 KB
/
soft_opt_out.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package keeper
import (
"encoding/binary"
"errors"
"fmt"
"sort"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/interchain-security/v5/x/ccv/consumer/types"
)
// BeginBlockSoftOptOut executes BeginBlock logic for the Soft Opt-Out sub-protocol
func (k Keeper) BeginBlockSoftOptOut(ctx sdk.Context) {
// Update smallest validator power that cannot opt out.
k.UpdateSmallestNonOptOutPower(ctx)
// Update the SigningInfo structs of the Slashing module
k.UpdateSlashingSigningInfo(ctx)
}
// SetSmallestNonOptOutPower sets the smallest validator power that cannot soft opt out.
func (k Keeper) SetSmallestNonOptOutPower(ctx sdk.Context, power uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.SmallestNonOptOutPowerKey(), sdk.Uint64ToBigEndian(power))
}
// UpdateSmallestNonOptOutPower updates the smallest validator power that cannot soft opt out.
// This is the smallest validator power such that the sum of the power of all validators with a lower power
// is less than [SoftOptOutThreshold] of the total power of all validators.
func (k Keeper) UpdateSmallestNonOptOutPower(ctx sdk.Context) {
// get soft opt-out threshold
optOutThreshold := math.LegacyMustNewDecFromStr(k.GetSoftOptOutThreshold(ctx))
if optOutThreshold.IsZero() {
// If the SoftOptOutThreshold is zero, then soft opt-out is disable.
// Setting the smallest non-opt-out power to zero, fixes the diff-testing
// when soft opt-out is disable.
k.SetSmallestNonOptOutPower(ctx, uint64(0))
return
}
// get all validators
valset := k.GetAllCCValidator(ctx)
// Valset should only be empty for hacky tests. Log error in case this ever happens in prod.
if len(valset) == 0 {
k.Logger(ctx).Error("UpdateSoftOptOutThresholdPower called with empty validator set")
return
}
// sort validators by power ascending
sort.SliceStable(valset, func(i, j int) bool {
return valset[i].Power < valset[j].Power
})
// get total power in set
totalPower := math.LegacyZeroDec()
for _, val := range valset {
totalPower = totalPower.Add(math.LegacyNewDecFromInt(math.NewInt(val.Power)))
}
// get power of the smallest validator that cannot soft opt out
powerSum := math.LegacyZeroDec()
for _, val := range valset {
powerSum = powerSum.Add(math.LegacyNewDecFromInt(math.NewInt(val.Power)))
// if powerSum / totalPower > SoftOptOutThreshold
if powerSum.Quo(totalPower).GT(optOutThreshold) {
// set smallest non opt out power
k.SetSmallestNonOptOutPower(ctx, uint64(val.Power))
k.Logger(ctx).Info("smallest non opt out power updated", "power", val.Power)
return
}
}
panic("UpdateSoftOptOutThresholdPower should not reach this point. Incorrect logic!")
}
// GetSmallestNonOptOutPower returns the smallest validator power that cannot soft opt out.
func (k Keeper) GetSmallestNonOptOutPower(ctx sdk.Context) int64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SmallestNonOptOutPowerKey())
if bz == nil {
return 0
}
return int64(binary.BigEndian.Uint64(bz))
}
func (k Keeper) UpdateSlashingSigningInfo(ctx sdk.Context) {
smallestNonOptOutPower := k.GetSmallestNonOptOutPower(ctx)
// Update SigningInfo for opted out validators
valset := k.GetAllCCValidator(ctx)
// Note that we don't need to sort the valset as GetAllCCValidator
// uses KVStorePrefixIterator that iterates over all the keys with
// a certain prefix in ascending order
for _, val := range valset {
consAddr := sdk.ConsAddress(val.Address)
signingInfo, err := k.slashingKeeper.GetValidatorSigningInfo(ctx, consAddr)
if errors.Is(err, slashingtypes.ErrNoSigningInfoFound) {
continue
} else if err != nil {
panic(fmt.Errorf("failed to get validator signing info for validator %s", consAddr))
}
if val.Power < smallestNonOptOutPower {
// validator CAN opt-out from validating on consumer chains
if !val.OptedOut {
// previously the validator couldn't opt-out
val.OptedOut = true
}
} else {
// validator CANNOT opt-out from validating on consumer chains
if val.OptedOut {
// previously the validator could opt-out
signingInfo.StartHeight = ctx.BlockHeight()
val.OptedOut = false
}
}
if err := k.slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo); err != nil {
panic(fmt.Errorf("failed to update validator signing info for validator %s", consAddr))
}
k.SetCCValidator(ctx, val)
}
}