-
Notifications
You must be signed in to change notification settings - Fork 12
/
params.go
253 lines (223 loc) · 7.99 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package types
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const (
MaxOracleSize = 150
)
var (
// AttestationVotesPowerThreshold threshold of votes power to succeed
AttestationVotesPowerThreshold = sdk.NewInt(66)
// AttestationProposalOracleChangePowerThreshold
AttestationProposalOracleChangePowerThreshold = sdk.NewInt(30)
// ParamsStoreKeyGravityID stores the gravity id
ParamsStoreKeyGravityID = []byte("GravityID")
// ParamsStoreKeyAverageBlockTime stores the signed blocks window
ParamsStoreKeyAverageBlockTime = []byte("AverageBlockTime")
// ParamsStoreKeyExternalBatchTimeout stores the signed blocks window
ParamsStoreKeyExternalBatchTimeout = []byte("ExternalBatchTimeout")
// ParamsStoreKeyAverageExternalBlockTime stores the signed blocks window
ParamsStoreKeyAverageExternalBlockTime = []byte("AverageExternalBlockTime")
// ParamsStoreKeySignedWindow stores the signed blocks window
ParamsStoreKeySignedWindow = []byte("SignedWindow")
// ParamsStoreSlashFraction stores the slash fraction valset
ParamsStoreSlashFraction = []byte("SlashFraction")
// ParamStoreOracleSetUpdatePowerChangePercent valset update pwer change percent
ParamStoreOracleSetUpdatePowerChangePercent = []byte("OracleSetUpdatePowerChangePercent")
// ParamStoreIbcTransferTimeoutHeight gravity and ibc transfer timeout height
ParamStoreIbcTransferTimeoutHeight = []byte("IbcTransferTimeoutHeight")
// ParamStoreOracles module owner.
ParamStoreOracles = []byte("Oracles")
ParamOracleDepositThreshold = []byte("OracleDepositThreshold")
)
var (
// Ensure that params implements the proper interface
_ paramtypes.ParamSet = &Params{}
)
// ValidateBasic checks that the parameters have valid values.
func (m Params) ValidateBasic() error {
if err := validateGravityID(m.GravityId); err != nil {
return sdkerrors.Wrap(err, "gravity id")
}
if err := validateAverageBlockTime(m.AverageBlockTime); err != nil {
return sdkerrors.Wrap(err, "block time")
}
if err := validateExternalBatchTimeout(m.ExternalBatchTimeout); err != nil {
return sdkerrors.Wrap(err, "external batch timeout")
}
if err := validateAverageExternalBlockTime(m.AverageExternalBlockTime); err != nil {
return sdkerrors.Wrap(err, "average external block time")
}
if err := validateSignedWindow(m.SignedWindow); err != nil {
return sdkerrors.Wrap(err, "signed blocks window")
}
if err := validateSlashFraction(m.SlashFraction); err != nil {
return sdkerrors.Wrap(err, "slash fraction")
}
if err := validateIbcTransferTimeoutHeight(m.IbcTransferTimeoutHeight); err != nil {
return sdkerrors.Wrap(err, "ibc transfer timeout height")
}
if err := validateOracleSetUpdatePowerChangePercent(m.OracleSetUpdatePowerChangePercent); err != nil {
return sdkerrors.Wrap(err, "power changer update oracle set percent")
}
if err := validateOracles(m.Oracles); err != nil {
return sdkerrors.Wrap(err, "oracles")
}
if err := validateOracleDepositThreshold(m.DepositThreshold); err != nil {
return sdkerrors.Wrap(err, "oracle deposit threshold")
}
return nil
}
// ParamKeyTable for auth module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of auth module's parameters.
func (m *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamsStoreKeyGravityID, &m.GravityId, validateGravityID),
paramtypes.NewParamSetPair(ParamsStoreKeyAverageBlockTime, &m.AverageBlockTime, validateAverageBlockTime),
paramtypes.NewParamSetPair(ParamsStoreKeyExternalBatchTimeout, &m.ExternalBatchTimeout, validateExternalBatchTimeout),
paramtypes.NewParamSetPair(ParamsStoreKeyAverageExternalBlockTime, &m.AverageExternalBlockTime, validateAverageExternalBlockTime),
paramtypes.NewParamSetPair(ParamsStoreKeySignedWindow, &m.SignedWindow, validateSignedWindow),
paramtypes.NewParamSetPair(ParamsStoreSlashFraction, &m.SlashFraction, validateSlashFraction),
paramtypes.NewParamSetPair(ParamStoreOracleSetUpdatePowerChangePercent, &m.OracleSetUpdatePowerChangePercent, validateOracleSetUpdatePowerChangePercent),
paramtypes.NewParamSetPair(ParamStoreIbcTransferTimeoutHeight, &m.IbcTransferTimeoutHeight, validateIbcTransferTimeoutHeight),
paramtypes.NewParamSetPair(ParamStoreOracles, &m.Oracles, validateOracles),
paramtypes.NewParamSetPair(ParamOracleDepositThreshold, &m.DepositThreshold, validateOracleDepositThreshold),
}
}
// Equal returns a boolean determining if two Params types are identical.
func (m Params) Equal(p2 Params) bool {
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&m)
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p2)
return bytes.Equal(bz1, bz2)
}
func validateGravityID(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if len(v) == 0 {
return fmt.Errorf("gravityId cannpt be empty")
}
if _, err := StrToFixByteArray(v); err != nil {
return err
}
return nil
}
func validateExternalBatchTimeout(i interface{}) error {
timeout, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if timeout < 60000 {
return fmt.Errorf("invalid target batch timeout, less than 60 seconds is too short")
}
return nil
}
func validateAverageBlockTime(i interface{}) error {
time, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if time < 100 {
return fmt.Errorf("invalid average block time, too short for latency limitations")
}
return nil
}
func validateAverageExternalBlockTime(i interface{}) error {
val, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if val < 100 {
return fmt.Errorf("invalid average external block time, too short for latency limitations")
}
return nil
}
func validateSignedWindow(i interface{}) error {
window, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if window <= 1 {
return fmt.Errorf("invalid signed window too short")
}
return nil
}
func validateOracleDepositThreshold(i interface{}) error {
c, ok := i.(sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
} else if !c.IsValid() || !c.IsPositive() {
return fmt.Errorf("invalid deposit threshold")
}
return nil
}
func validateIbcTransferTimeoutHeight(i interface{}) error {
timeout, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if timeout <= 1 {
return fmt.Errorf("invalid ibc transfer timeout too short")
}
return nil
}
func validateOracleSetUpdatePowerChangePercent(i interface{}) error {
percent, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if percent.IsNegative() {
return fmt.Errorf("attempted to powet change percent with a negative: %v", percent)
}
return nil
}
func validateOracles(i interface{}) error {
if oracles, ok := i.([]string); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
} else {
if len(oracles) <= 0 {
return fmt.Errorf("oracles cannot be empty")
}
if len(oracles) > MaxOracleSize {
return fmt.Errorf("oracle length must be less than or equal : %d", MaxOracleSize)
}
oraclesMap := make(map[string]bool)
for _, addr := range oracles {
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return err
}
if oraclesMap[addr] {
return fmt.Errorf("duplicate oracle %s", addr)
}
oraclesMap[addr] = true
}
}
return nil
}
func validateSlashFraction(i interface{}) error {
slashFactor, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if slashFactor.IsNegative() {
return fmt.Errorf("attempted to slash with a negative slash factor: %v", slashFactor)
}
return nil
}
func StrToFixByteArray(s string) ([32]byte, error) {
var out [32]byte
if len([]byte(s)) > 32 {
return out, fmt.Errorf("string too long")
}
copy(out[:], s)
return out, nil
}