/
params.go
161 lines (141 loc) · 4.24 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
package types
import (
"fmt"
"regexp"
yaml "gopkg.in/yaml.v2"
"cosmossdk.io/math"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const (
// DefaultEnableGovernance (true) indicates that governance proposals are allowed for managing markers
DefaultEnableGovernance = true
// DefaultMaxTotalSupply is deprecated.
DefaultMaxTotalSupply = uint64(100000000000)
// DefaultMaxSupply is the upper bound to enforce on supply for markers.
DefaultMaxSupply = "100000000000000000000"
// DefaultUnrestrictedDenomRegex is a regex that denoms created by normal requests must pass.
DefaultUnrestrictedDenomRegex = `[a-zA-Z][a-zA-Z0-9\-\.]{2,83}`
)
var (
// ParamStoreKeyEnableGovernance indicates if governance proposal management of markers is enabled
ParamStoreKeyEnableGovernance = []byte("EnableGovernance")
// ParamStoreKeyMaxTotalSupply is deprecated.
ParamStoreKeyMaxTotalSupply = []byte("MaxTotalSupply")
// ParamStoreKeyMaxSupply is maximum supply to allow a marker to create
ParamStoreKeyMaxSupply = []byte("MaxSupply")
// ParamStoreKeyUnrestrictedDenomRegex is the validation regex for validating denoms supplied by users.
ParamStoreKeyUnrestrictedDenomRegex = []byte("UnrestrictedDenomRegex")
)
// ParamKeyTable for marker module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new parameter object
func NewParams(
maxTotalSupply uint64,
enableGovernance bool,
unrestrictedDenomRegex string,
maxSupply math.Int,
) Params {
return Params{
EnableGovernance: enableGovernance,
MaxTotalSupply: maxTotalSupply,
UnrestrictedDenomRegex: unrestrictedDenomRegex,
MaxSupply: maxSupply,
}
}
// ParamSetPairs - Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyEnableGovernance, &p.EnableGovernance, validateEnableGovernance),
paramtypes.NewParamSetPair(ParamStoreKeyMaxTotalSupply, &p.MaxTotalSupply, validateIntParam),
paramtypes.NewParamSetPair(ParamStoreKeyUnrestrictedDenomRegex, &p.UnrestrictedDenomRegex, validateRegexParam),
paramtypes.NewParamSetPair(ParamStoreKeyMaxSupply, &p.MaxSupply, validateBigIntParam),
}
}
// DefaultParams is the default parameter configuration for the bank module
func DefaultParams() Params {
return NewParams(
DefaultMaxTotalSupply,
DefaultEnableGovernance,
DefaultUnrestrictedDenomRegex,
StringToBigInt(DefaultMaxSupply),
)
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Equal returns true if the given value is equivalent to the current instance of params
func (p *Params) Equal(that interface{}) bool {
if that == nil {
return p == nil
}
that1, ok := that.(*Params)
if !ok {
that2, ok := that.(Params)
if ok {
that1 = &that2
} else {
return false
}
}
if that1 == nil {
return p == nil
} else if p == nil {
return false
}
if p.MaxTotalSupply != that1.MaxTotalSupply {
return false
}
if !p.MaxSupply.Equal(that1.MaxSupply) {
return false
}
if p.EnableGovernance != that1.EnableGovernance {
return false
}
if p.UnrestrictedDenomRegex != that1.UnrestrictedDenomRegex {
return false
}
return true
}
func validateIntParam(i interface{}) error {
_, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateBigIntParam(i interface{}) error {
_, ok := i.(math.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateEnableGovernance(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateRegexParam(i interface{}) error {
exp, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if len(exp) > 0 && (exp[0:1] == "^" || exp[len(exp)-1:] == "$") {
return fmt.Errorf("invalid parameter, validation regex must not contain anchors ^,$")
}
_, err := regexp.Compile(fmt.Sprintf(`^%s$`, exp))
return err
}
func StringToBigInt(val string) math.Int {
res, ok := math.NewIntFromString(val)
if !ok {
panic(fmt.Errorf("unable to create math.Int from string: %s", val))
}
return res
}