/
dyncomm.go
175 lines (148 loc) · 4.9 KB
/
dyncomm.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package keeper
import (
types "github.com/classic-terra/core/v2/x/dyncomm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// GetVotingPower calculates the voting power of a validator in percent
func (k Keeper) CalculateVotingPower(ctx sdk.Context, validator stakingtypes.Validator) (ret sdk.Dec) {
totalPower := k.StakingKeeper.GetLastTotalPower(ctx).Int64()
validatorPower := sdk.TokensToConsensusPower(
validator.Tokens,
k.StakingKeeper.PowerReduction(ctx),
)
return sdk.NewDec(validatorPower).QuoInt64(totalPower).MulInt64(100)
}
// CalculateDynCommission calculates the min commission according
// to StrathColes formula
func (k Keeper) CalculateDynCommission(ctx sdk.Context, validator stakingtypes.Validator) (ret sdk.Dec) {
// The original parameters as defined
// by Strath
A := k.GetMaxZero(ctx)
B := k.GetSlopeBase(ctx)
C := k.GetSlopeVpImpact(ctx)
D := k.GetCap(ctx).MulInt64(100)
x := k.CalculateVotingPower(ctx, validator)
factorA := x.Sub(A)
quotient := x.Quo(C)
factorB := quotient.Add(B)
minComm := k.StakingKeeper.MinCommissionRate(ctx).MulInt64(100)
y := factorA.Mul(factorB)
if y.GT(D) {
y = D
}
if minComm.GT(y) {
y = minComm
}
return y.QuoInt64(100)
}
func (k Keeper) SetDynCommissionRate(ctx sdk.Context, validator string, rate sdk.Dec) {
var preSetRate types.ValidatorCommissionRate
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
targetRate := sdk.ZeroDec()
if bz != nil {
k.cdc.MustUnmarshal(bz, &preSetRate)
targetRate = *preSetRate.TargetCommissionRate
}
bz = k.cdc.MustMarshal(
&types.ValidatorCommissionRate{
ValidatorAddress: validator,
MinCommissionRate: &rate,
TargetCommissionRate: &targetRate,
},
)
store.Set(types.GetMinCommissionRatesKey(validator), bz)
}
func (k Keeper) SetTargetCommissionRate(ctx sdk.Context, validator string, rate sdk.Dec) {
var preSetRate types.ValidatorCommissionRate
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
minRate := sdk.ZeroDec()
if bz != nil {
k.cdc.MustUnmarshal(bz, &preSetRate)
minRate = *preSetRate.MinCommissionRate
}
bz = k.cdc.MustMarshal(
&types.ValidatorCommissionRate{
ValidatorAddress: validator,
MinCommissionRate: &minRate,
TargetCommissionRate: &rate,
},
)
store.Set(types.GetMinCommissionRatesKey(validator), bz)
}
func (k Keeper) GetDynCommissionRate(ctx sdk.Context, validator string) (rate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
if bz == nil {
return sdk.ZeroDec()
}
var validatorRate types.ValidatorCommissionRate
k.cdc.MustUnmarshal(bz, &validatorRate)
return *validatorRate.MinCommissionRate
}
func (k Keeper) GetTargetCommissionRate(ctx sdk.Context, validator string) (rate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMinCommissionRatesKey(validator))
if bz == nil {
return sdk.ZeroDec()
}
var validatorRate types.ValidatorCommissionRate
k.cdc.MustUnmarshal(bz, &validatorRate)
return *validatorRate.TargetCommissionRate
}
// IterateDynCommissionRates iterates over dyn commission rates in the store
func (k Keeper) IterateDynCommissionRates(ctx sdk.Context, cb func(types.ValidatorCommissionRate) bool) {
store := ctx.KVStore(k.storeKey)
it := store.Iterator(nil, nil)
defer it.Close()
for ; it.Valid(); it.Next() {
var entry types.ValidatorCommissionRate
if err := entry.Unmarshal(it.Value()); err != nil {
panic(err)
}
if cb(entry) {
break
}
}
}
func (k Keeper) UpdateValidatorMinRates(ctx sdk.Context, validator stakingtypes.Validator) {
var newRate sdk.Dec
minRate := k.CalculateDynCommission(ctx, validator)
newMaxRate := validator.Commission.MaxRate
targetRate := k.GetTargetCommissionRate(ctx, validator.OperatorAddress)
// assume the newRate will be the target rate ...
newRate = targetRate
// ... but enforce min rate
if newRate.LT(minRate) {
newRate = minRate
}
// new min rate pushes max rate
if newMaxRate.LT(minRate) {
newMaxRate = minRate
}
newValidator := validator
newValidator.Commission = stakingtypes.NewCommissionWithTime(
newRate,
newMaxRate,
validator.Commission.MaxChangeRate,
validator.Commission.UpdateTime,
)
k.StakingKeeper.SetValidator(ctx, newValidator)
k.SetDynCommissionRate(ctx, validator.OperatorAddress, minRate)
// Debug
targetRate = k.GetTargetCommissionRate(ctx, validator.OperatorAddress)
ctx.Logger().Debug("dyncomm:", "val", validator.OperatorAddress, "min_rate", minRate, "new target_rate", targetRate)
}
func (k Keeper) UpdateAllBondedValidatorRates(ctx sdk.Context) (err error) {
k.StakingKeeper.IterateValidators(ctx, func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
val := validator.(stakingtypes.Validator)
if !val.IsBonded() {
return false
}
k.UpdateValidatorMinRates(ctx, val)
return false
})
return nil
}