-
Notifications
You must be signed in to change notification settings - Fork 30
/
params.go
244 lines (206 loc) · 6.42 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
package types
import (
"fmt"
"time"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Service params default values
var (
DefaultMaxRequestTimeout = int64(100)
DefaultMinDepositMultiple = int64(200)
DefaultMinDeposit = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(6000))) // 6000stake
DefaultServiceFeeTax = sdk.NewDecWithPrec(1, 1) // 10%
DefaultSlashFraction = sdk.NewDecWithPrec(1, 3) // 0.1%
DefaultComplaintRetrospect = 15 * 24 * time.Hour // 15 days
DefaultArbitrationTimeLimit = 5 * 24 * time.Hour // 5 days
DefaultTxSizeLimit = uint64(4000)
DefaultBaseDenom = sdk.DefaultBondDenom
)
// Keys for parameter access
// nolint
var (
KeyMaxRequestTimeout = []byte("MaxRequestTimeout")
KeyMinDepositMultiple = []byte("MinDepositMultiple")
KeyMinDeposit = []byte("MinDeposit")
KeyServiceFeeTax = []byte("ServiceFeeTax")
KeySlashFraction = []byte("SlashFraction")
KeyComplaintRetrospect = []byte("ComplaintRetrospect")
KeyArbitrationTimeLimit = []byte("ArbitrationTimeLimit")
KeyTxSizeLimit = []byte("TxSizeLimit")
KeyBaseDenom = []byte("BaseDenom")
)
var _ paramstypes.ParamSet = (*Params)(nil)
// NewParams creates a new Params instance
func NewParams(
maxRequestTimeout,
minDepositMultiple int64,
minDeposit sdk.Coins,
serviceFeeTax,
slashFraction sdk.Dec,
complaintRetrospect,
arbitrationTimeLimit time.Duration,
txSizeLimit uint64,
baseDenom string,
) Params {
return Params{
MaxRequestTimeout: maxRequestTimeout,
MinDepositMultiple: minDepositMultiple,
MinDeposit: minDeposit,
ServiceFeeTax: serviceFeeTax,
SlashFraction: slashFraction,
ComplaintRetrospect: complaintRetrospect,
ArbitrationTimeLimit: arbitrationTimeLimit,
TxSizeLimit: txSizeLimit,
BaseDenom: baseDenom,
}
}
// ParamSetPairs implements paramstypes.ParamSet
func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyMaxRequestTimeout, &p.MaxRequestTimeout, validateMaxRequestTimeout),
paramstypes.NewParamSetPair(KeyMinDepositMultiple, &p.MinDepositMultiple, validateMinDepositMultiple),
paramstypes.NewParamSetPair(KeyMinDeposit, &p.MinDeposit, validateMinDeposit),
paramstypes.NewParamSetPair(KeyServiceFeeTax, &p.ServiceFeeTax, validateServiceFeeTax),
paramstypes.NewParamSetPair(KeySlashFraction, &p.SlashFraction, validateSlashFraction),
paramstypes.NewParamSetPair(KeyComplaintRetrospect, &p.ComplaintRetrospect, validateComplaintRetrospect),
paramstypes.NewParamSetPair(KeyArbitrationTimeLimit, &p.ArbitrationTimeLimit, validateArbitrationTimeLimit),
paramstypes.NewParamSetPair(KeyTxSizeLimit, &p.TxSizeLimit, validateTxSizeLimit),
paramstypes.NewParamSetPair(KeyBaseDenom, &p.BaseDenom, validateTxBaseDenom),
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return NewParams(
DefaultMaxRequestTimeout,
DefaultMinDepositMultiple,
DefaultMinDeposit,
DefaultServiceFeeTax,
DefaultSlashFraction,
DefaultComplaintRetrospect,
DefaultArbitrationTimeLimit,
DefaultTxSizeLimit,
DefaultBaseDenom,
)
}
// String implements stringer
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Validate validates a set of params
func (p Params) Validate() error {
if err := validateMaxRequestTimeout(p.MaxRequestTimeout); err != nil {
return err
}
if err := validateMinDepositMultiple(p.MinDepositMultiple); err != nil {
return err
}
if err := validateMinDeposit(p.MinDeposit); err != nil {
return err
}
if err := validateSlashFraction(p.SlashFraction); err != nil {
return err
}
if err := validateServiceFeeTax(p.ServiceFeeTax); err != nil {
return err
}
if err := validateComplaintRetrospect(p.ComplaintRetrospect); err != nil {
return err
}
if err := validateArbitrationTimeLimit(p.ArbitrationTimeLimit); err != nil {
return err
}
if err := sdk.ValidateDenom(p.BaseDenom); err != nil {
return err
}
return validateTxSizeLimit(p.TxSizeLimit)
}
func validateMaxRequestTimeout(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("maximum request timeout must be positive: %d", v)
}
return nil
}
func validateMinDepositMultiple(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("minimum deposit multiple must be positive: %d", v)
}
return nil
}
func validateMinDeposit(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !v.IsValid() {
return fmt.Errorf("invalid minimum deposit: %s", v)
}
return nil
}
func validateSlashFraction(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.LT(sdk.ZeroDec()) || v.GT(sdk.OneDec()) {
return fmt.Errorf("slashing fraction must be between [0, 1]: %s", v)
}
return nil
}
func validateServiceFeeTax(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.LT(sdk.ZeroDec()) || v.GTE(sdk.OneDec()) {
return fmt.Errorf("service fee tax must be between [0, 1): %s", v)
}
return nil
}
func validateComplaintRetrospect(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("complaint retrospect must be positive: %d", v)
}
return nil
}
func validateArbitrationTimeLimit(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("arbitration time limit must be positive: %d", v)
}
return nil
}
func validateTxSizeLimit(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("tx size limit must be positive: %d", v)
}
return nil
}
func validateTxBaseDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return sdk.ValidateDenom(v)
}