-
Notifications
You must be signed in to change notification settings - Fork 122
/
params.go
223 lines (203 loc) · 8.03 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
package types
import (
fmt "fmt"
time "time"
sdktypes "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
const (
// about 2 hr at 7.6 seconds per blocks
DefaultBlocksPerDistributionTransmission = 1000
// Default transfer timeout period is 1 hour, less than the default blocks
// per dist transmission * average block time.
// Since IBC token transfers do not have to be in order, it could be easier
// to reason about the distribution protocol if the previous reward times out
// before sending the next one. Note that on timeout, the transferred funds are
// added back to the pool, so the next transfer will include them as well.
DefaultTransferTimeoutPeriod = time.Hour
// The default fraction of tokens allocated to the consumer redistribution address
// during distribution events. The fraction is a string representing a
// decimal number. For example "0.75" would represent 75%.
DefaultConsumerRedistributeFrac = "0.75"
// Default number of historical info entries to persist in store.
// We use the same default as the staking module, but use a signed integer
// so that negative values can be caught during parameter validation in a readable way,
// (and for consistency with other protobuf schemas defined for ccv).
DefaultHistoricalEntries = int64(stakingtypes.DefaultHistoricalEntries)
// In general, the default unbonding period on the consumer is one week less
// than the default unbonding period on the provider, where the provider uses
// the staking module default.
DefaultConsumerUnbondingPeriod = stakingtypes.DefaultUnbondingTime - 7*24*time.Hour
// By default, the bottom 5% of the validator set can opt out of validating consumer chains
DefaultSoftOptOutThreshold = "0.05"
)
// Reflection based keys for params subspace
var (
KeyEnabled = []byte("Enabled")
KeyBlocksPerDistributionTransmission = []byte("BlocksPerDistributionTransmission")
KeyDistributionTransmissionChannel = []byte("DistributionTransmissionChannel")
KeyProviderFeePoolAddrStr = []byte("ProviderFeePoolAddrStr")
KeyTransferTimeoutPeriod = []byte("TransferTimeoutPeriod")
KeyConsumerRedistributionFrac = []byte("ConsumerRedistributionFraction")
KeyHistoricalEntries = []byte("HistoricalEntries")
KeyConsumerUnbondingPeriod = []byte("UnbondingPeriod")
KeySoftOptOutThreshold = []byte("SoftOptOutThreshold")
KeyRewardDenoms = []byte("RewardDenoms")
KeyProviderRewardDenoms = []byte("ProviderRewardDenoms")
)
// ParamKeyTable type declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&ConsumerParams{})
}
// NewParams creates new consumer parameters with provided arguments
func NewParams(enabled bool, blocksPerDistributionTransmission int64,
distributionTransmissionChannel, providerFeePoolAddrStr string,
ccvTimeoutPeriod, transferTimeoutPeriod time.Duration,
consumerRedistributionFraction string, historicalEntries int64,
consumerUnbondingPeriod time.Duration, softOptOutThreshold string, rewardDenoms, providerRewardDenoms []string,
) ConsumerParams {
return ConsumerParams{
Enabled: enabled,
BlocksPerDistributionTransmission: blocksPerDistributionTransmission,
DistributionTransmissionChannel: distributionTransmissionChannel,
ProviderFeePoolAddrStr: providerFeePoolAddrStr,
CcvTimeoutPeriod: ccvTimeoutPeriod,
TransferTimeoutPeriod: transferTimeoutPeriod,
ConsumerRedistributionFraction: consumerRedistributionFraction,
HistoricalEntries: historicalEntries,
UnbondingPeriod: consumerUnbondingPeriod,
SoftOptOutThreshold: softOptOutThreshold,
RewardDenoms: rewardDenoms,
ProviderRewardDenoms: providerRewardDenoms,
}
}
// DefaultParams is the default params for the consumer module
func DefaultParams() ConsumerParams {
var rewardDenoms []string
var provideRewardDenoms []string
return NewParams(
false,
DefaultBlocksPerDistributionTransmission,
"",
"",
DefaultCCVTimeoutPeriod,
DefaultTransferTimeoutPeriod,
DefaultConsumerRedistributeFrac,
DefaultHistoricalEntries,
DefaultConsumerUnbondingPeriod,
DefaultSoftOptOutThreshold,
rewardDenoms,
provideRewardDenoms,
)
}
// Validate all ccv-consumer module parameters
func (p ConsumerParams) Validate() error {
if err := ValidateBool(p.Enabled); err != nil {
return err
}
if err := ValidatePositiveInt64(p.BlocksPerDistributionTransmission); err != nil {
return err
}
if err := ValidateDistributionTransmissionChannel(p.DistributionTransmissionChannel); err != nil {
return err
}
if err := ValidateProviderFeePoolAddrStr(p.ProviderFeePoolAddrStr); err != nil {
return err
}
if err := ValidateDuration(p.CcvTimeoutPeriod); err != nil {
return err
}
if err := ValidateDuration(p.TransferTimeoutPeriod); err != nil {
return err
}
if err := ValidateStringFraction(p.ConsumerRedistributionFraction); err != nil {
return err
}
if err := ValidatePositiveInt64(p.HistoricalEntries); err != nil {
return err
}
if err := ValidateDuration(p.UnbondingPeriod); err != nil {
return err
}
if err := ValidateSoftOptOutThreshold(p.SoftOptOutThreshold); err != nil {
return err
}
if err := ValidateDenoms(p.RewardDenoms); err != nil {
return err
}
if err := ValidateDenoms(p.ProviderRewardDenoms); err != nil {
return err
}
return nil
}
// ParamSetPairs implements params.ParamSet
func (p *ConsumerParams) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyEnabled, p.Enabled, ValidateBool),
paramtypes.NewParamSetPair(KeyBlocksPerDistributionTransmission,
p.BlocksPerDistributionTransmission, ValidatePositiveInt64),
paramtypes.NewParamSetPair(KeyDistributionTransmissionChannel,
p.DistributionTransmissionChannel, ValidateDistributionTransmissionChannel),
paramtypes.NewParamSetPair(KeyProviderFeePoolAddrStr,
p.ProviderFeePoolAddrStr, ValidateProviderFeePoolAddrStr),
paramtypes.NewParamSetPair(KeyCCVTimeoutPeriod,
p.CcvTimeoutPeriod, ValidateDuration),
paramtypes.NewParamSetPair(KeyTransferTimeoutPeriod,
p.TransferTimeoutPeriod, ValidateDuration),
paramtypes.NewParamSetPair(KeyConsumerRedistributionFrac,
p.ConsumerRedistributionFraction, ValidateStringFraction),
paramtypes.NewParamSetPair(KeyHistoricalEntries,
p.HistoricalEntries, ValidatePositiveInt64),
paramtypes.NewParamSetPair(KeyConsumerUnbondingPeriod,
p.UnbondingPeriod, ValidateDuration),
paramtypes.NewParamSetPair(KeySoftOptOutThreshold,
p.SoftOptOutThreshold, ValidateSoftOptOutThreshold),
paramtypes.NewParamSetPair(KeyRewardDenoms,
p.RewardDenoms, ValidateDenoms),
paramtypes.NewParamSetPair(KeyProviderRewardDenoms,
p.ProviderRewardDenoms, ValidateDenoms),
}
}
func ValidateProviderFeePoolAddrStr(i interface{}) error {
// Accept empty string as valid, since this will be the default value on genesis
if i == "" {
return nil
}
// Cannot validate provider chain address on the consumer chain
return nil
}
func ValidateSoftOptOutThreshold(i interface{}) error {
str, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
dec, err := sdktypes.NewDecFromStr(str)
if err != nil {
return err
}
if dec.IsNegative() {
return fmt.Errorf("soft opt out threshold cannot be negative, got %s", str)
}
if !dec.Sub(sdktypes.MustNewDecFromStr("0.2")).IsNegative() {
return fmt.Errorf("soft opt out threshold cannot be greater than 0.2, got %s", str)
}
return nil
}
func ValidateDenoms(i interface{}) error {
v, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// iterate over the denoms, turning them into coins and validating them
for _, denom := range v {
coin := sdktypes.Coin{
Denom: denom,
Amount: sdktypes.NewInt(0),
}
if err := coin.Validate(); err != nil {
return err
}
}
return nil
}