/
params.go
255 lines (218 loc) · 6.55 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
package types
import (
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
// Default parameter namespace
const (
DefaultParamspace string = ModuleName
DefaultCurationWindow time.Duration = time.Minute * 10
DefaultMaxNumVotes uint32 = 5
DefaultMaxVendors uint32 = 1
DefaultMaxPostBodyLength uint32 = 280
)
// Default vars
var (
DefaultVoteAmount = sdk.NewInt64Coin(DefaultVoteDenom, 1_000_000)
DefaultInitialRewardPool = sdk.NewInt64Coin(DefaultStakeDenom, 21_000_000_000_000)
DefaultRewardPoolAllocation = sdk.NewDecWithPrec(50, 2) // from inflation
DefaultRewardPoolCurationMaxAllocation = sdk.NewDecWithPrec(1, 3) // .001 (0.1%)
)
// Parameter store keys
var (
KeyCurationWindow = []byte("CurationWindow")
KeyVoteAmount = []byte("VoteAmount")
KeyMaxNumVotes = []byte("MaxNumVotes")
KeyMaxVendors = []byte("MaxVendors")
KeyMaxPostBodyLength = []byte("MaxPostBodyLength")
KeyRewardPoolAllocation = []byte("RewardPoolAllocation")
KeyRewardPoolCurationMaxAlloc = []byte("RewardPoolCurationMaxAlloc")
KeyInitialRewardPool = []byte("InitialRewardPool")
KeyStakeDenom = []byte("StakeDenom")
)
// NewParams creates a new Params object
func NewParams(
curationWindow time.Duration,
voteAmount,
initialRewardPool sdk.Coin,
maxNumVotes,
maxVendors uint32,
maxPostBodyLength uint32,
rewardPoolAllocation,
rewardPoolCurationMaxAllocation sdk.Dec,
stakeDenom string,
) Params {
return Params{
CurationWindow: curationWindow,
VoteAmount: voteAmount,
InitialRewardPool: initialRewardPool,
MaxNumVotes: maxNumVotes,
MaxVendors: maxVendors,
MaxPostBodyLength: maxPostBodyLength,
RewardPoolAllocation: rewardPoolAllocation,
RewardPoolCurationMaxAlloc: rewardPoolCurationMaxAllocation,
StakeDenom: stakeDenom,
}
}
// String implements the stringer interface for Params
func (p Params) String() string {
out, err := yaml.Marshal(p)
if err != nil {
return ""
}
return string(out)
}
// ParamSetPairs - Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyCurationWindow, &p.CurationWindow, validateCurationWindow),
paramtypes.NewParamSetPair(KeyVoteAmount, &p.VoteAmount, validateVoteAmount),
paramtypes.NewParamSetPair(KeyInitialRewardPool, &p.InitialRewardPool, validateRewardPoolAmount),
paramtypes.NewParamSetPair(KeyMaxNumVotes, &p.MaxNumVotes, validateMaxNumVotes),
paramtypes.NewParamSetPair(KeyMaxVendors, &p.MaxVendors, validateMaxVendors),
paramtypes.NewParamSetPair(KeyMaxPostBodyLength, &p.MaxPostBodyLength, validateMaxPostBodyLength),
paramtypes.NewParamSetPair(KeyRewardPoolAllocation, &p.RewardPoolAllocation, validateRewardPoolAlloc),
paramtypes.NewParamSetPair(KeyRewardPoolCurationMaxAlloc, &p.RewardPoolCurationMaxAlloc,
validateRewardPoolCurationMaxAllocation),
paramtypes.NewParamSetPair(KeyStakeDenom, &p.StakeDenom, validateDenom),
}
}
// DefaultParams defines the parameters for this module
func DefaultParams() Params {
return NewParams(
DefaultCurationWindow,
DefaultVoteAmount,
DefaultInitialRewardPool,
DefaultMaxNumVotes,
DefaultMaxVendors,
DefaultMaxPostBodyLength,
DefaultRewardPoolAllocation,
DefaultRewardPoolCurationMaxAllocation,
DefaultStakeDenom,
)
}
// Validate validates all params
func (p Params) Validate() error {
if err := validateCurationWindow(p.CurationWindow); err != nil {
return err
}
if err := validateVoteAmount(p.VoteAmount); err != nil {
return err
}
if err := validateRewardPoolAmount(p.InitialRewardPool); err != nil {
return err
}
if err := validateMaxNumVotes(p.MaxNumVotes); err != nil {
return err
}
if err := validateMaxVendors(p.MaxVendors); err != nil {
return err
}
if err := validateMaxPostBodyLength(p.MaxPostBodyLength); err != nil {
return err
}
if err := validateCurationWindow(p.CurationWindow); err != nil {
return err
}
if err := validateRewardPoolAlloc(p.RewardPoolAllocation); err != nil {
return err
}
if err := validateRewardPoolCurationMaxAllocation(p.RewardPoolCurationMaxAlloc); err != nil {
return err
}
return nil
}
func validateDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if strings.TrimSpace(v) == "" {
return fmt.Errorf("invalid denom: %s", v)
}
return nil
}
func validateVoteAmount(i interface{}) error {
v, ok := i.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !v.IsValid() {
return fmt.Errorf("invalid vote amount: %s", v)
}
return nil
}
func validateRewardPoolAmount(i interface{}) error {
v, ok := i.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !v.IsValid() {
return fmt.Errorf("invalid reward pool amount: %s", v)
}
return nil
}
func validateMaxNumVotes(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("max num votes must be greater than or equal to 1: %d", v)
}
return nil
}
func validateMaxVendors(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("max vendors must be greater than or equal to 1: %d", v)
}
return nil
}
func validateMaxPostBodyLength(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("max post body length must be greater than or equal to 1: %d", v)
}
return nil
}
func validateCurationWindow(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("curation window must be positive: %d", v)
}
return nil
}
func validateRewardPoolAlloc(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsZero() {
return fmt.Errorf("reward pool allocation can't be zero: %d", v)
}
return nil
}
func validateRewardPoolCurationMaxAllocation(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsZero() {
return fmt.Errorf("reward pool curation max allocation can't be zero: %d", v)
}
return nil
}