forked from bandprotocol/chain
/
params.go
executable file
·222 lines (201 loc) · 8.38 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
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"time"
"gopkg.in/yaml.v2"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// nolint
const (
// Each value below is the default value for each parameter when generating the default
// genesis file. See comments in types.proto for explanation for each parameter.
DefaultMaxRawRequestCount = uint64(12)
DefaultMaxAskCount = uint64(16)
DefaultExpirationBlockCount = uint64(100)
DefaultBaseRequestGas = uint64(150000)
DefaultPerValidatorRequestGas = uint64(30000)
DefaultSamplingTryCount = uint64(3)
DefaultOracleRewardPercentage = uint64(70)
DefaultInactivePenaltyDuration = uint64(10 * time.Minute)
DefaultMaxDataSize = uint64(1 * 1024) // 1 KB
DefaultMaxCalldataSize = uint64(1 * 1024) // 1 KB
DefaultPrepareGas = uint64(40000)
DefaultExecuteGas = uint64(300000)
DefaultRewardThresholdBlocks = uint64(28820)
DefaultDataProviderRewardDenom = "minigeo"
DefaultDataRequesterFeeDenom = "loki"
)
var (
DefaultDataProviderRewardPerByte = sdk.NewCoins(sdk.NewInt64Coin(DefaultDataProviderRewardDenom, 1000000)) // 1 * 10^6
DefaultDataRequesterFeeDenoms = []string{DefaultDataRequesterFeeDenom}
DefaultFeeLimit = sdk.NewCoins()
DefaultRewardThresholdAmount = sdk.NewCoins(sdk.NewInt64Coin(DefaultDataProviderRewardDenom, 200000000000)) // 200000 * 10^6
DefaultRewardDecreasingFraction = sdk.NewDec(1).Quo(sdk.NewDec(20))
)
// nolint
var (
// Each value below is the key to store the respective oracle module parameter. See comments
// in types.proto for explanation for each parameter.
KeyMaxRawRequestCount = []byte("MaxRawRequestCount")
KeyMaxAskCount = []byte("MaxAskCount")
KeyExpirationBlockCount = []byte("ExpirationBlockCount")
KeyBaseOwasmGas = []byte("BaseOwasmGas")
KeyPerValidatorRequestGas = []byte("PerValidatorRequestGas")
KeySamplingTryCount = []byte("SamplingTryCount")
KeyOracleRewardPercentage = []byte("OracleRewardPercentage")
KeyInactivePenaltyDuration = []byte("InactivePenaltyDuration")
KeyMaxDataSize = []byte("MaxDataSize")
KeyMaxCalldataSize = []byte("MaxCalldataSize")
KeyDataProviderRewardPerByte = []byte("DataProviderRewardPerByte")
KeyRewardDecreasingFraction = []byte("RewardDecreasingFraction")
KeyDataProviderRewardThreshold = []byte("DataProviderRewardThreshold")
KeyDataRequesterFeeDenoms = []byte("DataRequesterFeeDenoms")
)
var _ paramtypes.ParamSet = (*Params)(nil)
// ParamKeyTable for oracle module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new parameter configuration for the oracle module
func NewParams(
maxRawRequestCount, maxAskCount, expirationBlockCount, baseRequestGas, perValidatorRequestGas,
samplingTryCount, oracleRewardPercentage, inactivePenaltyDuration, maxDataSize, maxCallDataSize uint64,
dataProviderRewardPerByte sdk.Coins, dataProviderRewardThreshold RewardThreshold, rewardDecreasingFraction sdk.Dec,
dataRequesterFeeDenoms []string,
) Params {
return Params{
MaxRawRequestCount: maxRawRequestCount,
MaxAskCount: maxAskCount,
ExpirationBlockCount: expirationBlockCount,
BaseOwasmGas: baseRequestGas,
PerValidatorRequestGas: perValidatorRequestGas,
SamplingTryCount: samplingTryCount,
OracleRewardPercentage: oracleRewardPercentage,
InactivePenaltyDuration: inactivePenaltyDuration,
MaxDataSize: maxDataSize,
MaxCalldataSize: maxCallDataSize,
DataProviderRewardPerByte: dataProviderRewardPerByte,
DataProviderRewardThreshold: dataProviderRewardThreshold,
RewardDecreasingFraction: rewardDecreasingFraction,
DataRequesterFeeDenoms: dataRequesterFeeDenoms,
}
}
func NewRewardThreshold(amount sdk.Coins, blocks uint64) RewardThreshold {
return RewardThreshold{
Amount: amount,
Blocks: blocks,
}
}
func DefaultRewardThreshold() RewardThreshold {
return RewardThreshold{
Amount: DefaultRewardThresholdAmount,
Blocks: DefaultRewardThresholdBlocks,
}
}
// ParamSetPairs implements the paramtypes.ParamSet interface for Params.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMaxRawRequestCount, &p.MaxRawRequestCount, validateUint64("max data source count", true)),
paramtypes.NewParamSetPair(KeyMaxAskCount, &p.MaxAskCount, validateUint64("max ask count", true)),
paramtypes.NewParamSetPair(KeyExpirationBlockCount, &p.ExpirationBlockCount, validateUint64("expiration block count", true)),
paramtypes.NewParamSetPair(KeyBaseOwasmGas, &p.BaseOwasmGas, validateUint64("base request gas", false)),
paramtypes.NewParamSetPair(KeyPerValidatorRequestGas, &p.PerValidatorRequestGas, validateUint64("per validator request gas", false)),
paramtypes.NewParamSetPair(KeySamplingTryCount, &p.SamplingTryCount, validateUint64("sampling try count", true)),
paramtypes.NewParamSetPair(KeyOracleRewardPercentage, &p.OracleRewardPercentage, validateUint64("oracle reward percentage", false)),
paramtypes.NewParamSetPair(KeyInactivePenaltyDuration, &p.InactivePenaltyDuration, validateUint64("inactive penalty duration", false)),
paramtypes.NewParamSetPair(KeyMaxDataSize, &p.MaxDataSize, validateUint64("max data size", true)),
paramtypes.NewParamSetPair(KeyMaxCalldataSize, &p.MaxCalldataSize, validateUint64("max calldata size", true)),
paramtypes.NewParamSetPair(KeyDataProviderRewardPerByte, &p.DataProviderRewardPerByte, validateDataProviderReward),
paramtypes.NewParamSetPair(KeyRewardDecreasingFraction, &p.RewardDecreasingFraction, validateRewardDecreasingFraction),
paramtypes.NewParamSetPair(KeyDataProviderRewardThreshold, &p.DataProviderRewardThreshold, validateRewardThreshold),
paramtypes.NewParamSetPair(KeyDataRequesterFeeDenoms, &p.DataRequesterFeeDenoms, validateDataRequesterFeeDenoms),
}
}
// DefaultParams defines the default parameters.
func DefaultParams() Params {
return NewParams(
DefaultMaxRawRequestCount,
DefaultMaxAskCount,
DefaultExpirationBlockCount,
DefaultBaseRequestGas,
DefaultPerValidatorRequestGas,
DefaultSamplingTryCount,
DefaultOracleRewardPercentage,
DefaultInactivePenaltyDuration,
DefaultMaxDataSize,
DefaultMaxCalldataSize,
DefaultDataProviderRewardPerByte,
DefaultRewardThreshold(),
DefaultRewardDecreasingFraction,
DefaultDataRequesterFeeDenoms,
)
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func validateUint64(name string, positiveOnly bool) func(interface{}) error {
return func(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 && positiveOnly {
return fmt.Errorf("%s must be positive: %d", name, v)
}
return nil
}
}
func validateDataProviderReward(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsAnyNegative() {
return fmt.Errorf("data provider reward must be positive: %v", v)
}
return nil
}
func validateRewardDecreasingFraction(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("reward decresing fraction must be positive: %v", v)
}
if v.GT(sdk.NewDec(1)) {
return fmt.Errorf("reward decresing fraction must be less or equal to 1 %v", v)
}
return nil
}
func validateRewardThreshold(i interface{}) error {
v, ok := i.(RewardThreshold)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.Amount.IsAnyNegative() {
return fmt.Errorf("reward threshold amount must be positive: %v", v.Amount)
}
if v.Amount.IsZero() {
return fmt.Errorf("reward threshold amount must be greater than zero: %v", v.Amount)
}
if v.Blocks <= 0 {
return fmt.Errorf("reward threshold blocks count must be greater than zero: %v", v.Blocks)
}
return nil
}
func validateDataRequesterFeeDenoms(i interface{}) error {
v, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for _, d := range v {
if err := sdk.ValidateDenom(d); err != nil {
return fmt.Errorf("denoms must be valid: %s, error: %w", d, err)
}
}
return nil
}