-
Notifications
You must be signed in to change notification settings - Fork 113
/
params.go
250 lines (212 loc) · 9.08 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
package types
import (
"fmt"
params "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/x/tss/exported"
)
// DefaultParamspace - default parameter namespace
const (
DefaultParamspace = ModuleName
)
// Parameter keys
var (
KeyKeyRequirements = []byte("keyRequirements")
KeySuspendDurationInBlocks = []byte("SuspendDurationInBlocks")
KeyHeartbeatPeriodInBlocks = []byte("HeartbeatPeriodInBlocks")
KeyMaxMissedBlocksPerWindow = []byte("MaxMissedBlocksPerWindow")
KeyUnbondingLockingKeyRotationCount = []byte("UnbondingLockingKeyRotationCount")
KeyExternalMultisigThreshold = []byte("externalMultisigThreshold")
KeyMaxSignQueueSize = []byte("MaxSignQueueSize")
MaxSimultaneousSignShares = []byte("MaxSimultaneousSignShares")
)
// KeyTable returns a subspace.KeyTable that has registered all parameter types in this module's parameter set
func KeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns the module's parameter set initialized with default values
func DefaultParams() Params {
return Params{
KeyRequirements: []exported.KeyRequirement{
{
KeyRole: exported.MasterKey,
KeyType: exported.Threshold,
MinKeygenThreshold: utils.Threshold{Numerator: 5, Denominator: 6},
SafetyThreshold: utils.Threshold{Numerator: 2, Denominator: 3},
KeyShareDistributionPolicy: exported.WeightedByStake,
MaxTotalShareCount: 50,
MinTotalShareCount: 4,
KeygenVotingThreshold: utils.Threshold{Numerator: 5, Denominator: 6},
SignVotingThreshold: utils.Threshold{Numerator: 2, Denominator: 3},
KeygenTimeout: 250,
SignTimeout: 250,
},
{
KeyRole: exported.SecondaryKey,
KeyType: exported.Threshold,
MinKeygenThreshold: utils.Threshold{Numerator: 15, Denominator: 20},
SafetyThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeyShareDistributionPolicy: exported.OnePerValidator,
MaxTotalShareCount: 20,
MinTotalShareCount: 4,
KeygenVotingThreshold: utils.Threshold{Numerator: 15, Denominator: 20},
SignVotingThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeygenTimeout: 150,
SignTimeout: 150,
},
{
KeyRole: exported.MasterKey,
KeyType: exported.Multisig,
MinKeygenThreshold: utils.Threshold{Numerator: 31, Denominator: 40},
SafetyThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeyShareDistributionPolicy: exported.WeightedByStake,
MaxTotalShareCount: 50,
MinTotalShareCount: 4,
KeygenVotingThreshold: utils.Threshold{Numerator: 31, Denominator: 40},
SignVotingThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeygenTimeout: 50,
SignTimeout: 50,
},
{
KeyRole: exported.SecondaryKey,
KeyType: exported.Multisig,
MinKeygenThreshold: utils.Threshold{Numerator: 15, Denominator: 20},
SafetyThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeyShareDistributionPolicy: exported.OnePerValidator,
MaxTotalShareCount: 20,
MinTotalShareCount: 4,
KeygenVotingThreshold: utils.Threshold{Numerator: 15, Denominator: 20},
SignVotingThreshold: utils.Threshold{Numerator: 11, Denominator: 20},
KeygenTimeout: 50,
SignTimeout: 50,
},
},
SuspendDurationInBlocks: 8646,
HeartbeatPeriodInBlocks: 50,
MaxMissedBlocksPerWindow: utils.Threshold{Numerator: 5, Denominator: 100},
UnbondingLockingKeyRotationCount: 8,
ExternalMultisigThreshold: utils.Threshold{Numerator: 3, Denominator: 6},
MaxSignQueueSize: 50,
MaxSimultaneousSignShares: 26,
}
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of tss module's parameters
func (m *Params) ParamSetPairs() params.ParamSetPairs {
/*
because the subspace package makes liberal use of pointers to set and get values from the store,
this method needs to have a pointer receiver AND NewParamSetPair needs to receive the
parameter values as pointer arguments, otherwise either the internal type reflection panics or the value will not be
set on the correct Params data struct
*/
return params.ParamSetPairs{
params.NewParamSetPair(KeyKeyRequirements, &m.KeyRequirements, validateKeyRequirements),
params.NewParamSetPair(KeySuspendDurationInBlocks, &m.SuspendDurationInBlocks, validateSuspendDurationInBlocks),
params.NewParamSetPair(KeyHeartbeatPeriodInBlocks, &m.HeartbeatPeriodInBlocks, validatePosInt64("HeartbeatPeriodInBlocks")),
params.NewParamSetPair(KeyMaxMissedBlocksPerWindow, &m.MaxMissedBlocksPerWindow, validateMaxMissedBlocksPerWindow),
params.NewParamSetPair(KeyUnbondingLockingKeyRotationCount, &m.UnbondingLockingKeyRotationCount, validatePosInt64("UnbondingLockingKeyRotationCount")),
params.NewParamSetPair(KeyExternalMultisigThreshold, &m.ExternalMultisigThreshold, validateExternalMultisigThreshold),
params.NewParamSetPair(KeyMaxSignQueueSize, &m.MaxSignQueueSize, validatePosInt64("MaxSignQueueSize")),
params.NewParamSetPair(MaxSimultaneousSignShares, &m.MaxSimultaneousSignShares, validatePosInt64("MaxSimultaneousSignShares")),
}
}
// Validate checks the validity of the values of the parameter set
func (m Params) Validate() error {
if err := validateKeyRequirements(m.KeyRequirements); err != nil {
return err
}
if err := validateSuspendDurationInBlocks(m.SuspendDurationInBlocks); err != nil {
return err
}
if err := validatePosInt64("HeartbeatPeriodInBlocks")(m.HeartbeatPeriodInBlocks); err != nil {
return err
}
if err := validateMaxMissedBlocksPerWindow(m.MaxMissedBlocksPerWindow); err != nil {
return err
}
if err := validatePosInt64("UnbondingLockingKeyRotationCount")(m.UnbondingLockingKeyRotationCount); err != nil {
return err
}
if err := validateExternalMultisigThreshold(m.ExternalMultisigThreshold); err != nil {
return err
}
if err := validatePosInt64("MaxSignQueueSize")(m.MaxSignQueueSize); err != nil {
return err
}
if err := validatePosInt64("MaxSimultaneousSignShares")(m.MaxSimultaneousSignShares); err != nil {
return err
}
return nil
}
func validateKeyRequirements(keyRequirements interface{}) error {
val, ok := keyRequirements.([]exported.KeyRequirement)
if !ok {
return fmt.Errorf("invalid parameter type for keyRequirements: %T", keyRequirements)
}
keyRoleSeen := map[string]bool{}
for _, keyRequirement := range val {
key := fmt.Sprintf("%s_%s", keyRequirement.KeyRole.SimpleString(), keyRequirement.KeyType.SimpleString())
if keyRoleSeen[key] {
return fmt.Errorf("duplicate key role and key type found in KeyRequirements")
}
if err := keyRequirement.Validate(); err != nil {
return err
}
keyRoleSeen[key] = true
}
return nil
}
func validateSuspendDurationInBlocks(suspendDurationInBlocks interface{}) error {
val, ok := suspendDurationInBlocks.(int64)
if !ok {
return fmt.Errorf("invalid parameter type for SuspendDurationInBlocks: %T", suspendDurationInBlocks)
}
if val <= 0 {
return fmt.Errorf("SuspendDurationInBlocks must be a positive integer")
}
return nil
}
func validatePosInt64(field string) func(value interface{}) error {
return func(value interface{}) error {
val, ok := value.(int64)
if !ok {
return fmt.Errorf("invalid parameter type for %s: %T", field, value)
}
if val <= 0 {
return fmt.Errorf("%s must be a positive integer", field)
}
return nil
}
}
func validateMaxMissedBlocksPerWindow(maxMissedBlocksPerWindow interface{}) error {
val, ok := maxMissedBlocksPerWindow.(utils.Threshold)
if !ok {
return fmt.Errorf("invalid parameter type for MaxMissedBlocksPerWindow: %T", maxMissedBlocksPerWindow)
}
if val.Numerator <= 0 {
return fmt.Errorf("threshold numerator must be a positive integer for MaxMissedBlocksPerWindow")
}
if val.Denominator <= 0 {
return fmt.Errorf("threshold denominator must be a positive integer for MaxMissedBlocksPerWindow")
}
if val.Numerator > val.Denominator {
return fmt.Errorf("threshold must be <=1 for MaxMissedBlocksPerWindow")
}
return nil
}
func validateExternalMultisigThreshold(externalMultisigThreshold interface{}) error {
t, ok := externalMultisigThreshold.(utils.Threshold)
if !ok {
return fmt.Errorf("invalid parameter type for external multisig threshold: %T", externalMultisigThreshold)
}
if t.Numerator <= 0 {
return fmt.Errorf("numerator must be greater than 0 for external multisig threshold")
}
if t.Denominator <= 0 {
return fmt.Errorf("denominator must be greater than 0 for external multisig threshold")
}
if t.Numerator > t.Denominator {
return fmt.Errorf("threshold must be <=1 for external multisig threshold")
}
return nil
}