-
Notifications
You must be signed in to change notification settings - Fork 53
/
params.go
165 lines (136 loc) · 4.17 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
package types
import (
"fmt"
cosmosmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
var _ paramtypes.ParamSet = (*Params)(nil)
var (
KeyTrustedAddresses = []byte("TrustedAddresses")
DefaultTrustedAddresses []string
)
var (
KeyTrustedCounterParties = []byte("TrustedCounterParty")
DefaultTrustedCounterParties []*TrustedCounterParty
)
var (
KeyChannelID = []byte("ChannelID")
DefaultChannelID = ChannelID
KeyMinGasPrice = []byte("MinGasPrice")
DefaultMinGasPrice = sdk.NewCoin("ufairy", cosmosmath.NewInt(300000))
)
// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params instance
func NewParams(
trAddrs []string,
trustedParties []*TrustedCounterParty,
channelID string,
minGasPrice *sdk.Coin,
) Params {
return Params{
TrustedAddresses: trAddrs,
TrustedCounterParties: trustedParties,
ChannelId: channelID,
MinGasPrice: minGasPrice,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultTrustedAddresses, DefaultTrustedCounterParties, DefaultChannelID, &DefaultMinGasPrice)
}
// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyTrustedAddresses, &p.TrustedAddresses, validateTrustedAddresses),
paramtypes.NewParamSetPair(KeyTrustedCounterParties, &p.TrustedCounterParties, validateTrustedCounterParties),
paramtypes.NewParamSetPair(KeyChannelID, &p.ChannelId, validateChannelID),
paramtypes.NewParamSetPair(KeyMinGasPrice, &p.MinGasPrice, validateMinGasPrice),
}
}
// Validate validates the set of params
func (p Params) Validate() error {
if err := validateTrustedAddresses(p.TrustedAddresses); err != nil {
return err
}
if err := validateTrustedCounterParties(p.TrustedCounterParties); err != nil {
return err
}
if err := validateChannelID(p.ChannelId); err != nil {
return err
}
if err := validateMinGasPrice(p.MinGasPrice); err != nil {
return err
}
return nil
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// validateChannelID validates the channelID param
func validateChannelID(v interface{}) error {
channelID, ok := v.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if len(channelID) < 1 {
return fmt.Errorf("invalid Channel ID")
}
return nil
}
func validateMinGasPrice(v interface{}) error {
minGasPrice, ok := v.(*sdk.Coin)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if minGasPrice.Amount.IsZero() || minGasPrice.Amount.IsNegative() {
return fmt.Errorf("invalid min gas price amount, expected > 0, got: %d", minGasPrice.Amount.Int64())
}
return nil
}
// validateTrustedAddresses validates the TrustedAddresses param
func validateTrustedAddresses(v interface{}) error {
trustedList, ok := v.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
// Validate each address in the slice
for i, element := range trustedList {
// Perform validation logic on each element
_, err := sdk.AccAddressFromBech32(element)
if err != nil {
return fmt.Errorf("address at index %d is invalid", i)
}
}
return nil
}
// validateTrustedAddresses validates the TrustedAddresses param
func validateTrustedCounterParties(v interface{}) error {
trustedParties, ok := v.([]*TrustedCounterParty)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
// Validate each party in the slice
for i, element := range trustedParties {
if element == nil {
return fmt.Errorf("trusted Party is null")
}
// Perform validation logic on each element
if len(element.ChannelId) < 1 {
return fmt.Errorf("channel ID at index %d is empty", i)
}
if len(element.ConnectionId) < 1 {
return fmt.Errorf("connection ID at index %d is empty", i)
}
if len(element.ClientId) < 1 {
return fmt.Errorf("client ID at index %d is empty", i)
}
}
return nil
}