-
Notifications
You must be signed in to change notification settings - Fork 44
/
params.go
136 lines (109 loc) · 4.29 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const (
// Default params space for the params keeper
DefaultParamSpace = ModuleName
)
// Parameters store keys
var (
MaxPostMessageLengthKey = []byte("MaxPostMessageLength")
MaxAdditionalAttributesFieldsNumberKey = []byte("MaxAdditionalAttributesFieldsNumber")
MaxAdditionalAttributesFieldValueLengthKey = []byte("MaxAdditionalAttributesFieldValueLength")
MaxAdditionalAttributesFieldKeyLengthKey = []byte("MaxAdditionalAttributesFieldKeyLength")
)
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params obj
func NewParams(maxPostMLen, maxOpDataFieldNum, maxOpDataFieldValLen, maxOpDataFieldKeyLen sdk.Int) Params {
return Params{
MaxPostMessageLength: maxPostMLen,
MaxAdditionalAttributesFieldsNumber: maxOpDataFieldNum,
MaxAdditionalAttributesFieldValueLength: maxOpDataFieldValLen,
MaxAdditionalAttributesFieldKeyLength: maxOpDataFieldKeyLen,
}
}
// DefaultParams return default params object
func DefaultParams() Params {
return Params{
MaxPostMessageLength: sdk.NewInt(500),
MaxAdditionalAttributesFieldsNumber: sdk.NewInt(10),
MaxAdditionalAttributesFieldValueLength: sdk.NewInt(200),
MaxAdditionalAttributesFieldKeyLength: sdk.NewInt(10),
}
}
// ParamSetPairs implements the ParamSet interface and returns the key/value pairs
// of posts module's parameters.
func (params *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(MaxPostMessageLengthKey,
¶ms.MaxPostMessageLength, ValidateMaxPostMessageLengthParam),
paramstypes.NewParamSetPair(MaxAdditionalAttributesFieldsNumberKey,
¶ms.MaxAdditionalAttributesFieldsNumber, ValidateMaxAdditionalAttributesFieldNumberParam),
paramstypes.NewParamSetPair(MaxAdditionalAttributesFieldValueLengthKey,
¶ms.MaxAdditionalAttributesFieldValueLength, ValidateMaxAdditionalAttributesFieldValueLengthParam),
paramstypes.NewParamSetPair(MaxAdditionalAttributesFieldKeyLengthKey,
¶ms.MaxAdditionalAttributesFieldKeyLength, ValidateMaxAdditionalAttributesFieldKeyLengthParam),
}
}
// Validate perform basic checks on all parameters to ensure they are correct
func (params Params) Validate() error {
err := ValidateMaxPostMessageLengthParam(params.MaxPostMessageLength)
if err != nil {
return err
}
err = ValidateMaxAdditionalAttributesFieldNumberParam(params.MaxAdditionalAttributesFieldsNumber)
if err != nil {
return err
}
err = ValidateMaxAdditionalAttributesFieldValueLengthParam(params.MaxAdditionalAttributesFieldValueLength)
if err != nil {
return err
}
err = ValidateMaxAdditionalAttributesFieldKeyLengthParam(params.MaxAdditionalAttributesFieldKeyLength)
if err != nil {
return err
}
return nil
}
func ValidateMaxPostMessageLengthParam(i interface{}) error {
params, isCorrectParam := i.(sdk.Int)
if !isCorrectParam {
return fmt.Errorf("invalid parameters type: %s", i)
}
if params.IsZero() || params.IsNegative() {
return fmt.Errorf("invalid max post message length param: %s", params)
}
return nil
}
func ValidateMaxAdditionalAttributesFieldNumberParam(i interface{}) error {
params, isCorrectParam := i.(sdk.Int)
if !isCorrectParam {
return fmt.Errorf("invalid parameters type: %s", i)
}
if params.IsZero() || params.IsNegative() {
return fmt.Errorf("invalid max additional attributes fields number param: %s", params)
}
return nil
}
func validateAdditionalAttributesFieldLengthParam(i interface{}, paramName string) error {
params, isCorrectParam := i.(sdk.Int)
if !isCorrectParam {
return fmt.Errorf("invalid parameters type: %s", i)
}
if params.IsZero() || params.IsNegative() {
return fmt.Errorf("invalid max additional attributes fields %s length param: %s", paramName, params)
}
return nil
}
func ValidateMaxAdditionalAttributesFieldValueLengthParam(i interface{}) error {
return validateAdditionalAttributesFieldLengthParam(i, "value")
}
func ValidateMaxAdditionalAttributesFieldKeyLengthParam(i interface{}) error {
return validateAdditionalAttributesFieldLengthParam(i, "key")
}