-
Notifications
You must be signed in to change notification settings - Fork 105
/
params.go
107 lines (87 loc) · 3.84 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
package types
import (
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
params "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/axelarnetwork/axelar-core/utils"
)
var (
// KeyChainActivationThreshold represents the key for chain activation threshold
KeyChainActivationThreshold = []byte("chainActivationThreshold")
// KeyChainMaintainerMissingVoteThreshold represents the key for chain maintainer missing vote threshold
KeyChainMaintainerMissingVoteThreshold = []byte("chainMaintainerMissingVoteThreshold")
// KeyChainMaintainerIncorrectVoteThreshold represents the key for chain maintainer incorrect vote threshold
KeyChainMaintainerIncorrectVoteThreshold = []byte("chainMaintainerIncorrectVoteThreshold")
// KeyChainMaintainerCheckWindow represents the key for chain maintainer check window
KeyChainMaintainerCheckWindow = []byte("chainMaintainerCheckWindow")
)
// KeyTable retrieves a subspace table for the module
func KeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams creates the default genesis parameters
func DefaultParams() Params {
return Params{
ChainActivationThreshold: utils.NewThreshold(55, 100),
ChainMaintainerMissingVoteThreshold: utils.NewThreshold(20, 100),
ChainMaintainerIncorrectVoteThreshold: utils.NewThreshold(15, 100),
ChainMaintainerCheckWindow: 500,
}
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of nexus 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(KeyChainActivationThreshold, &m.ChainActivationThreshold, validateThresholdWith("ChainActivationThreshold")),
params.NewParamSetPair(KeyChainMaintainerMissingVoteThreshold, &m.ChainMaintainerMissingVoteThreshold, validateThresholdWith("ChainMaintainerMissingVoteThreshold")),
params.NewParamSetPair(KeyChainMaintainerIncorrectVoteThreshold, &m.ChainMaintainerIncorrectVoteThreshold, validateThresholdWith("ChainMaintainerIncorrectVoteThreshold")),
params.NewParamSetPair(KeyChainMaintainerCheckWindow, &m.ChainMaintainerCheckWindow, validateChainMaintainerCheckWindow),
}
}
// Validate checks if the parameters are valid
func (m Params) Validate() error {
if err := validateThresholdWith("ChainActivationThreshold")(m.ChainActivationThreshold); err != nil {
return err
}
if err := validateThresholdWith("ChainMaintainerMissingVoteThreshold")(m.ChainMaintainerMissingVoteThreshold); err != nil {
return err
}
if err := validateThresholdWith("ChainMaintainerIncorrectVoteThreshold")(m.ChainMaintainerIncorrectVoteThreshold); err != nil {
return err
}
if err := validateChainMaintainerCheckWindow(m.ChainMaintainerCheckWindow); err != nil {
return err
}
return nil
}
func validateThresholdWith(paramName string) func(interface{}) error {
return func(i interface{}) error {
val, ok := i.(utils.Threshold)
if !ok {
return fmt.Errorf("invalid parameter type for %s: %T", paramName, i)
}
if err := val.Validate(); err != nil {
return sdkerrors.Wrapf(err, "invalid %s", paramName)
}
return nil
}
}
func validateChainMaintainerCheckWindow(i interface{}) error {
val, ok := i.(int32)
if !ok {
return fmt.Errorf("invalid parameter type for ChainMaintainerCheckWindow: %T", i)
}
if val <= 0 {
return fmt.Errorf("ChainMaintainerCheckWindow must be >0")
}
if val >= maxBitmapSize {
return fmt.Errorf("ChainMaintainerCheckWindow must be < %d", maxBitmapSize)
}
return nil
}