-
Notifications
You must be signed in to change notification settings - Fork 111
/
params.go
207 lines (183 loc) · 8.14 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v4/modules/core/23-commitment/types"
ibctmtypes "github.com/cosmos/ibc-go/v4/modules/light-clients/07-tendermint/types"
consumertypes "github.com/cosmos/interchain-security/v2/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v2/x/ccv/types"
)
const (
// DefaultMaxClockDrift defines how much new (untrusted) header's Time can drift into the future.
// This default is only used in the default template client param.
DefaultMaxClockDrift = 10 * time.Second
// DefaultTrustingPeriodFraction is the default fraction used to compute TrustingPeriod
// as UnbondingPeriod * TrustingPeriodFraction
DefaultTrustingPeriodFraction = "0.66"
// DefaultInitTimeoutPeriod defines the init timeout period
DefaultInitTimeoutPeriod = 7 * 24 * time.Hour
// DefaultVscTimeoutPeriod defines the VSC timeout period
DefaultVscTimeoutPeriod = 5 * 7 * 24 * time.Hour
// DefaultSlashMeterReplenishPeriod defines the default period for which the slash gas meter is replenished
DefaultSlashMeterReplenishPeriod = time.Hour
// DefaultSlashMeterReplenishFraction defines the default fraction of total voting power
// that is replenished to the slash meter every replenish period. This param also serves as a maximum
// fraction of total voting power that the slash meter can hold.
DefaultSlashMeterReplenishFraction = "0.05"
// DefaultMaxThrottledPackets defines the default amount of throttled slash or vsc matured packets
// that can be queued for a single consumer before the provider chain halts.
DefaultMaxThrottledPackets = 100000
)
// Reflection based keys for params subspace
var (
KeyTemplateClient = []byte("TemplateClient")
KeyTrustingPeriodFraction = []byte("TrustingPeriodFraction")
KeyInitTimeoutPeriod = []byte("InitTimeoutPeriod")
KeyVscTimeoutPeriod = []byte("VscTimeoutPeriod")
KeySlashMeterReplenishPeriod = []byte("SlashMeterReplenishPeriod")
KeySlashMeterReplenishFraction = []byte("SlashMeterReplenishFraction")
KeyMaxThrottledPackets = []byte("MaxThrottledPackets")
KeyConsumerRewardDenomRegistrationFee = []byte("ConsumerRewardDenomRegistrationFee")
)
// ParamKeyTable returns a key table with the necessary registered provider params
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates new provider parameters with provided arguments
func NewParams(
cs *ibctmtypes.ClientState,
trustingPeriodFraction string,
ccvTimeoutPeriod time.Duration,
initTimeoutPeriod time.Duration,
vscTimeoutPeriod time.Duration,
slashMeterReplenishPeriod time.Duration,
slashMeterReplenishFraction string,
maxThrottledPackets int64,
consumerRewardDenomRegistrationFee sdk.Coin,
) Params {
return Params{
TemplateClient: cs,
TrustingPeriodFraction: trustingPeriodFraction,
CcvTimeoutPeriod: ccvTimeoutPeriod,
InitTimeoutPeriod: initTimeoutPeriod,
VscTimeoutPeriod: vscTimeoutPeriod,
SlashMeterReplenishPeriod: slashMeterReplenishPeriod,
SlashMeterReplenishFraction: slashMeterReplenishFraction,
MaxThrottledPackets: maxThrottledPackets,
ConsumerRewardDenomRegistrationFee: consumerRewardDenomRegistrationFee,
}
}
// DefaultParams is the default params for the provider module
func DefaultParams() Params {
// create default client state with chainID, trusting period, unbonding period, and initial height zeroed out.
// these fields will be populated during proposal handler.
return NewParams(
ibctmtypes.NewClientState(
"", // chainID
ibctmtypes.DefaultTrustLevel,
0, // trusting period
0, // unbonding period
DefaultMaxClockDrift,
clienttypes.Height{}, // latest(initial) height
commitmenttypes.GetSDKSpecs(),
[]string{"upgrade", "upgradedIBCState"},
true,
true,
),
DefaultTrustingPeriodFraction,
ccvtypes.DefaultCCVTimeoutPeriod,
DefaultInitTimeoutPeriod,
DefaultVscTimeoutPeriod,
DefaultSlashMeterReplenishPeriod,
DefaultSlashMeterReplenishFraction,
DefaultMaxThrottledPackets,
// Defining this inline because it's not possible to define a constant of type sdk.Coin.
// Following the pattern from cosmos-sdk/staking/types/params.go
sdk.Coin{
Denom: sdk.DefaultBondDenom,
Amount: sdk.NewInt(10000000),
},
)
}
// Validate all ccv-provider module parameters
func (p Params) Validate() error {
if p.TemplateClient == nil {
return fmt.Errorf("template client is nil")
}
if err := ValidateTemplateClient(*p.TemplateClient); err != nil {
return err
}
if err := ccvtypes.ValidateStringFraction(p.TrustingPeriodFraction); err != nil {
return fmt.Errorf("trusting period fraction is invalid: %s", err)
}
if err := ccvtypes.ValidateDuration(p.CcvTimeoutPeriod); err != nil {
return fmt.Errorf("ccv timeout period is invalid: %s", err)
}
if err := ccvtypes.ValidateDuration(p.InitTimeoutPeriod); err != nil {
return fmt.Errorf("init timeout period is invalid: %s", err)
}
if err := ccvtypes.ValidateDuration(p.VscTimeoutPeriod); err != nil {
return fmt.Errorf("vsc timeout period is invalid: %s", err)
}
if err := ccvtypes.ValidateDuration(p.SlashMeterReplenishPeriod); err != nil {
return fmt.Errorf("slash meter replenish period is invalid: %s", err)
}
if err := ccvtypes.ValidateStringFraction(p.SlashMeterReplenishFraction); err != nil {
return fmt.Errorf("slash meter replenish fraction is invalid: %s", err)
}
if err := ccvtypes.ValidatePositiveInt64(p.MaxThrottledPackets); err != nil {
return fmt.Errorf("max throttled packets is invalid: %s", err)
}
if err := ValidateCoin(p.ConsumerRewardDenomRegistrationFee); err != nil {
return fmt.Errorf("consumer reward denom registration fee is invalid: %s", err)
}
return nil
}
// ParamSetPairs implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyTemplateClient, p.TemplateClient, ValidateTemplateClient),
paramtypes.NewParamSetPair(KeyTrustingPeriodFraction, p.TrustingPeriodFraction, ccvtypes.ValidateStringFraction),
paramtypes.NewParamSetPair(ccvtypes.KeyCCVTimeoutPeriod, p.CcvTimeoutPeriod, ccvtypes.ValidateDuration),
paramtypes.NewParamSetPair(KeyInitTimeoutPeriod, p.InitTimeoutPeriod, ccvtypes.ValidateDuration),
paramtypes.NewParamSetPair(KeyVscTimeoutPeriod, p.VscTimeoutPeriod, ccvtypes.ValidateDuration),
paramtypes.NewParamSetPair(KeySlashMeterReplenishPeriod, p.SlashMeterReplenishPeriod, ccvtypes.ValidateDuration),
paramtypes.NewParamSetPair(KeySlashMeterReplenishFraction, p.SlashMeterReplenishFraction, ccvtypes.ValidateStringFraction),
paramtypes.NewParamSetPair(KeyMaxThrottledPackets, p.MaxThrottledPackets, ccvtypes.ValidatePositiveInt64),
paramtypes.NewParamSetPair(KeyConsumerRewardDenomRegistrationFee, p.ConsumerRewardDenomRegistrationFee, ValidateCoin),
}
}
func ValidateTemplateClient(i interface{}) error {
cs, ok := i.(ibctmtypes.ClientState)
if !ok {
return fmt.Errorf("invalid parameter type: %T, expected: %T", i, ibctmtypes.ClientState{})
}
// copy clientstate to prevent changing original pointer
copiedClient := cs
// populate zeroed fields with valid fields
copiedClient.ChainId = "chainid"
trustPeriod, err := ccvtypes.CalculateTrustPeriod(consumertypes.DefaultConsumerUnbondingPeriod, DefaultTrustingPeriodFraction)
if err != nil {
return fmt.Errorf("invalid TrustPeriodFraction: %T", err)
}
copiedClient.TrustingPeriod = trustPeriod
copiedClient.UnbondingPeriod = consumertypes.DefaultConsumerUnbondingPeriod
copiedClient.LatestHeight = clienttypes.NewHeight(0, 1)
if err := copiedClient.Validate(); err != nil {
return err
}
return nil
}
func ValidateCoin(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 consumer reward denom registration fee: %s", v)
}
return nil
}