/
params.go
74 lines (62 loc) · 2.13 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
package types
import (
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/gov/types"
params "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
KeyAssets = []byte("assetInfo")
KeyRouteTimeoutWindow = []byte("routeTimeoutWindow")
)
// 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{
SupportedChains: []string{"Bitcoin", "Ethereum"},
RouteTimeoutWindow: 100,
}
}
// 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(KeyAssets, &m.SupportedChains, validateSupportedChains),
params.NewParamSetPair(KeyRouteTimeoutWindow, &m.RouteTimeoutWindow, validateUint64("RouteTimeoutWindow")),
}
}
// Validate checks if the parameters are valid
func (m Params) Validate() error {
return validateSupportedChains(m.SupportedChains)
}
func validateSupportedChains(infos interface{}) error {
supportedChains, ok := infos.([]string)
if !ok {
return sdkerrors.Wrapf(types.ErrInvalidGenesis, "invalid parameter type for %T: %T", []string{}, infos)
}
for _, chain := range supportedChains {
if chain == "" {
return sdkerrors.Wrap(types.ErrInvalidGenesis, "chain name cannot be an empty string")
}
}
return nil
}
func validateUint64(field string) func(value interface{}) error {
return func(value interface{}) error {
_, ok := value.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type for %s: %T", field, value)
}
return nil
}
}