-
Notifications
You must be signed in to change notification settings - Fork 44
/
params.go
291 lines (238 loc) · 8.01 KB
/
params.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package types
import (
"errors"
"fmt"
"math/big"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
var _ paramtypes.ParamSet = (*Params)(nil)
var (
KeyChallengeCountPerBlock = []byte("ChallengeCountPerBlock")
DefaultChallengeCountPerBlock uint64 = 1
)
var (
KeySlashCoolingOffPeriod = []byte("SlashCoolingOffPeriod")
DefaultSlashCoolingOffPeriod uint64 = 300
)
var (
KeySlashAmountSizeRate = []byte("SlashAmountSizeRate")
DefaultSlashAmountSizeRate = sdk.NewDecWithPrec(85, 4)
)
var (
KeySlashAmountMin = []byte("SlashAmountMin")
DefaultSlashAmountMin = math.NewIntFromBigInt(big.NewInt(1e16))
)
var (
KeySlashAmountMax = []byte("SlashAmountMax")
DefaultSlashAmountMax = math.NewIntFromBigInt(big.NewInt(1e18))
)
var (
KeyRewardValidatorRatio = []byte("RewardValidatorRatio")
DefaultRewardValidatorRatio = sdk.NewDecWithPrec(9, 1)
)
var (
KeyRewardSubmitterRatio = []byte("RewardSubmitterRatio")
DefaultRewardSubmitterRatio = sdk.NewDecWithPrec(1, 3)
)
var (
KeyRewardSubmitterThreshold = []byte("RewardSubmitterThreshold")
DefaultRewardSubmitterThreshold = math.NewIntFromBigInt(big.NewInt(1e15))
)
var (
KeyHeartbeatInterval = []byte("HeartbeatInterval")
DefaultHeartbeatInterval uint64 = 1000
)
// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params instance
func NewParams(
challengeCountPerBlock uint64,
slashCoolingOffPeriod uint64,
slashAmountSizeRate sdk.Dec,
slashAmountMin math.Int,
slashAmountMax math.Int,
rewardValidatorRatio sdk.Dec,
rewardSubmitterRatio sdk.Dec,
rewardSubmitterThreshold math.Int,
heartbeatInterval uint64,
) Params {
return Params{
ChallengeCountPerBlock: challengeCountPerBlock,
SlashCoolingOffPeriod: slashCoolingOffPeriod,
SlashAmountSizeRate: slashAmountSizeRate,
SlashAmountMin: slashAmountMin,
SlashAmountMax: slashAmountMax,
RewardValidatorRatio: rewardValidatorRatio,
RewardSubmitterRatio: rewardSubmitterRatio,
RewardSubmitterThreshold: rewardSubmitterThreshold,
HeartbeatInterval: heartbeatInterval,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
DefaultChallengeCountPerBlock,
DefaultSlashCoolingOffPeriod,
DefaultSlashAmountSizeRate,
DefaultSlashAmountMin,
DefaultSlashAmountMax,
DefaultRewardValidatorRatio,
DefaultRewardSubmitterRatio,
DefaultRewardSubmitterThreshold,
DefaultHeartbeatInterval,
)
}
// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyChallengeCountPerBlock, &p.ChallengeCountPerBlock, validateChallengeCountPerBlock),
paramtypes.NewParamSetPair(KeySlashCoolingOffPeriod, &p.SlashCoolingOffPeriod, validateSlashCoolingOffPeriod),
paramtypes.NewParamSetPair(KeySlashAmountSizeRate, &p.SlashAmountSizeRate, validateSlashAmountSizeRate),
paramtypes.NewParamSetPair(KeySlashAmountMin, &p.SlashAmountMin, validateSlashAmountMin),
paramtypes.NewParamSetPair(KeySlashAmountMax, &p.SlashAmountMax, validateSlashAmountMax),
paramtypes.NewParamSetPair(KeyRewardValidatorRatio, &p.RewardValidatorRatio, validateRewardValidatorRatio),
paramtypes.NewParamSetPair(KeyRewardSubmitterRatio, &p.RewardSubmitterRatio, validateRewardSubmitterRatio),
paramtypes.NewParamSetPair(KeyRewardSubmitterThreshold, &p.RewardSubmitterThreshold, validateRewardSubmitterThreshold),
paramtypes.NewParamSetPair(KeyHeartbeatInterval, &p.HeartbeatInterval, validateHeartbeatInterval),
}
}
// Validate validates the set of params
func (p Params) Validate() error {
if err := validateChallengeCountPerBlock(p.ChallengeCountPerBlock); err != nil {
return err
}
if err := validateSlashCoolingOffPeriod(p.SlashCoolingOffPeriod); err != nil {
return err
}
if err := validateSlashAmountSizeRate(p.SlashAmountSizeRate); err != nil {
return err
}
if err := validateSlashAmountMin(p.SlashAmountMin); err != nil {
return err
}
if err := validateSlashAmountMax(p.SlashAmountMax); err != nil {
return err
}
if err := validateRewardValidatorRatio(p.RewardValidatorRatio); err != nil {
return err
}
if err := validateRewardSubmitterRatio(p.RewardSubmitterRatio); err != nil {
return err
}
if err := validateRewardSubmitterThreshold(p.RewardSubmitterThreshold); err != nil {
return err
}
if p.SlashAmountMax.LTE(p.SlashAmountMin) {
return errors.New("max slash amount should be bigger than min slash amount")
}
if p.RewardValidatorRatio.Add(p.RewardSubmitterRatio).GT(sdk.NewDec(1)) {
return errors.New("the sum of validator and challenger reward ratio should be equal to or less than one")
}
if err := validateHeartbeatInterval(p.HeartbeatInterval); err != nil {
return err
}
return nil
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// validateChallengeCountPerBlock validates the ChallengeCountPerBlock param
func validateChallengeCountPerBlock(v interface{}) error {
_, ok := v.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
return nil
}
// validateSlashCoolingOffPeriod validates the SlashCoolingOffPeriod param
func validateSlashCoolingOffPeriod(v interface{}) error {
_, ok := v.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
return nil
}
// validateSlashAmountSizeRate validates the SlashAmountPerSizeRate param
func validateSlashAmountSizeRate(v interface{}) error {
slashAmountSizeRate, ok := v.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if slashAmountSizeRate.LT(sdk.ZeroDec()) {
return errors.New("slash amount size rate cannot be lower than zero")
}
return nil
}
// validateSlashAmountMin validates the SlashAmountMin param
func validateSlashAmountMin(v interface{}) error {
slashAmountMin, ok := v.(math.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if slashAmountMin.LT(sdk.ZeroInt()) {
return errors.New("min slash amount cannot be lower than zero")
}
return nil
}
// validateSlashAmountMax validates the SlashAmountMax param
func validateSlashAmountMax(v interface{}) error {
slashAmountMax, ok := v.(math.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if slashAmountMax.LT(sdk.ZeroInt()) {
return errors.New("max slash amount cannot be lower than zero")
}
return nil
}
// validateRewardValidatorRatio validates the RewardValidatorRatio param
func validateRewardValidatorRatio(v interface{}) error {
rewardValidatorRatio, ok := v.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if rewardValidatorRatio.LT(sdk.ZeroDec()) {
return errors.New("validator reward ratio cannot be lower than zero")
}
return nil
}
// validateRewardSubmitterRatio validates the RewardSubmitterRatio param
func validateRewardSubmitterRatio(v interface{}) error {
rewardSubmitterRatio, ok := v.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if rewardSubmitterRatio.LT(sdk.ZeroDec()) {
return errors.New("submitter reward ratio cannot be lower than zero")
}
return nil
}
// validateRewardSubmitterThreshold validates the RewardSubmitterThreshold param
func validateRewardSubmitterThreshold(v interface{}) error {
rewardSubmitterThreshold, ok := v.(math.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if rewardSubmitterThreshold.LT(sdk.ZeroInt()) {
return errors.New("submitter reward threshold cannot be lower than zero")
}
return nil
}
// validateHeartbeatInterval validates the HeartbeatInterval param
func validateHeartbeatInterval(v interface{}) error {
heartbeatInterval, ok := v.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if heartbeatInterval == 0 {
return errors.New("heartbeat interval cannot be zero")
}
return nil
}