-
Notifications
You must be signed in to change notification settings - Fork 42
/
params.go
172 lines (145 loc) · 4.64 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
package types
import (
"errors"
"fmt"
"time"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
var (
// DefaultMinLaunchTime ...
// TODO: set back this value to the default one
// time.Hour * 24
DefaultMinLaunchTime = time.Hour * 5
DefaultMaxLaunchTime = time.Hour * 24 * 7
// DefaultRevertDelay is the delay after the launch time when it is possible to revert the launch of the chain
// launch can be reverted on-chain when the actual chain launch failed (incorrect gentx, etc...)
// This delay must be small be big enough to ensure nodes had the time to bootstrap\
// This currently corresponds to 1 hour
DefaultRevertDelay = time.Hour
DefaultFee = sdk.Coins(nil) // EmptyCoins
MaxParametrableLaunchTime = time.Hour * 24 * 31
MaxParametrableRevertDelay = time.Hour * 24
DefaultMaxMetadataLength uint64 = 2000
KeyLaunchTimeRange = []byte("LaunchTimeRange")
KeyRevertDelay = []byte("RevertDelay")
KeyChainCreationFee = []byte("ChainCreationFee")
KeyRequestFee = []byte("RequestFee")
KeyMaxMetadataLength = []byte("MaxMetadataLength")
)
var _ paramtypes.ParamSet = (*Params)(nil)
// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewLaunchTimeRange creates a new LaunchTimeRange instance
func NewLaunchTimeRange(minLaunchTime, maxLaunchTime time.Duration) LaunchTimeRange {
return LaunchTimeRange{
MinLaunchTime: minLaunchTime,
MaxLaunchTime: maxLaunchTime,
}
}
// NewParams creates a new Params instance
func NewParams(
minLaunchTime,
maxLaunchTime,
revertDelay time.Duration,
chainCreationFee,
requestFee sdk.Coins,
maxMetadataLength uint64,
) Params {
return Params{
LaunchTimeRange: NewLaunchTimeRange(minLaunchTime, maxLaunchTime),
RevertDelay: revertDelay,
ChainCreationFee: chainCreationFee,
RequestFee: requestFee,
MaxMetadataLength: maxMetadataLength,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
DefaultMinLaunchTime,
DefaultMaxLaunchTime,
DefaultRevertDelay,
DefaultFee,
DefaultFee,
DefaultMaxMetadataLength,
)
}
// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyLaunchTimeRange, &p.LaunchTimeRange, validateLaunchTimeRange),
paramtypes.NewParamSetPair(KeyRevertDelay, &p.RevertDelay, validateRevertDelay),
paramtypes.NewParamSetPair(KeyChainCreationFee, &p.ChainCreationFee, validateFee),
paramtypes.NewParamSetPair(KeyRequestFee, &p.RequestFee, validateFee),
paramtypes.NewParamSetPair(KeyMaxMetadataLength, &p.MaxMetadataLength, validateMaxMetadataLength),
}
}
// Validate validates the set of params
func (p Params) Validate() error {
if err := validateLaunchTimeRange(p.LaunchTimeRange); err != nil {
return err
}
if err := validateRevertDelay(p.RevertDelay); err != nil {
return err
}
if err := validateMaxMetadataLength(p.MaxMetadataLength); err != nil {
return err
}
if err := p.ChainCreationFee.Validate(); err != nil {
return err
}
return p.RequestFee.Validate()
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func validateLaunchTimeRange(i interface{}) error {
v, ok := i.(LaunchTimeRange)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
// it is enough to check that minLaunchTime is positive since it must be that minLaunchTime < maxLaunchTime
if v.MinLaunchTime < 0 {
return errors.New("MinLaunchTime can't be negative")
}
if v.MinLaunchTime > v.MaxLaunchTime {
return errors.New("MinLaunchTime can't be higher than MaxLaunchTime")
}
// just need to check max launch time due to check above that guarantees correctness of the range
if v.MaxLaunchTime > MaxParametrableLaunchTime {
return errors.New("max parametrable launch time reached")
}
return nil
}
func validateRevertDelay(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v > MaxParametrableRevertDelay {
return errors.New("max parametrable revert delay reached")
}
if v <= 0 {
return errors.New("revert delay parameter must be positive")
}
return nil
}
func validateFee(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return v.Validate()
}
func validateMaxMetadataLength(i interface{}) error {
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}