/
params.go
189 lines (164 loc) · 4.91 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
package types
import (
"fmt"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/tendermint/spn/pkg/chainid"
spntypes "github.com/tendermint/spn/pkg/types"
)
var (
KeyLastBlockHeight = []byte("LastBlockHeight")
KeyConsumerConsensusState = []byte("ConsumerConsensusState")
KeyConsumerChainID = []byte("ConsumerChainID")
KeyConsumerUnbondingPeriod = []byte("ConsumerUnbondingPeriod")
KeyConsumerRevisionHeight = []byte("RevisionHeight")
DefaultLastBlockHeight int64 = 1
DefaultConsumerChainID = "spn-1"
)
var _ paramtypes.ParamSet = (*Params)(nil)
// 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(
lastBlockHeight int64,
consumerChainID string,
ccs spntypes.ConsensusState,
consumerUnbondingpPeriod int64,
consumerRevisionHeight uint64,
) Params {
return Params{
LastBlockHeight: lastBlockHeight,
ConsumerConsensusState: ccs,
ConsumerChainID: consumerChainID,
ConsumerUnbondingPeriod: consumerUnbondingpPeriod,
ConsumerRevisionHeight: consumerRevisionHeight,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
DefaultLastBlockHeight,
DefaultConsumerChainID,
spntypes.ConsensusState{},
spntypes.DefaultUnbondingPeriod,
spntypes.DefaultRevisionHeight,
)
}
// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(
KeyLastBlockHeight,
&p.LastBlockHeight,
validateLastBlockHeight,
),
paramtypes.NewParamSetPair(
KeyConsumerConsensusState,
&p.ConsumerConsensusState,
validateConsumerConsensusState,
),
paramtypes.NewParamSetPair(
KeyConsumerChainID,
&p.ConsumerChainID,
validateConsumerChainID,
),
paramtypes.NewParamSetPair(
KeyConsumerUnbondingPeriod,
&p.ConsumerUnbondingPeriod,
validateConsumerUnbondingPeriod,
),
paramtypes.NewParamSetPair(
KeyConsumerRevisionHeight,
&p.ConsumerRevisionHeight,
validateConsumerRevisionHeight,
),
}
}
// Validate validates the set of params
func (p Params) Validate() error {
if err := validateLastBlockHeight(p.LastBlockHeight); err != nil {
return err
}
if err := validateConsumerChainID(p.ConsumerChainID); err != nil {
return err
}
if err := validateConsumerConsensusState(p.ConsumerConsensusState); err != nil {
return err
}
if err := validateConsumerUnbondingPeriod(p.ConsumerUnbondingPeriod); err != nil {
return err
}
return validateConsumerRevisionHeight(p.ConsumerRevisionHeight)
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// validateLastBlockHeight validates last block height
func validateLastBlockHeight(i interface{}) error {
lastBlockHeight, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if lastBlockHeight <= 0 {
return errors.New("last block height can't be 0 or negative")
}
return nil
}
// validateConsumerConsensusState validates consumer consensus state
func validateConsumerConsensusState(i interface{}) error {
ccs, ok := i.(spntypes.ConsensusState)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// perform the verification only if the Consumer Consensus State is defined
// TODO: remove this check and set an official SPN mainnet consensus state as default
if ccs.Timestamp != "" {
tmConsensusState, err := ccs.ToTendermintConsensusState()
if err != nil {
return errors.Wrap(err, "consumer consensus state can't be converted")
}
if err := tmConsensusState.ValidateBasic(); err != nil {
return errors.Wrap(err, "invalid consumer consensus state")
}
}
return nil
}
// validateConsumerChainID validates consumer chain ID
func validateConsumerChainID(i interface{}) error {
chainID, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
_, _, err := chainid.ParseGenesisChainID(chainID)
if err != nil {
return errors.Wrap(err, "invalid chain ID param")
}
return nil
}
// validateConsumerUnbondingPeriod validates consumer unbonding period
func validateConsumerUnbondingPeriod(i interface{}) error {
unbondingPeriod, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if unbondingPeriod < spntypes.MinimalUnbondingPeriod {
return fmt.Errorf("minimal unbonding period is %d", spntypes.MinimalUnbondingPeriod)
}
return nil
}
// validateConsumerRevisionHeight validates consumer revision height
func validateConsumerRevisionHeight(i interface{}) error {
revisionHeight, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if revisionHeight == 0 {
return fmt.Errorf("minimal revision height is %d", 1)
}
return nil
}