-
Notifications
You must be signed in to change notification settings - Fork 112
/
shared_params.go
114 lines (98 loc) · 2.64 KB
/
shared_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
package types
import (
fmt "fmt"
"time"
ibchost "github.com/cosmos/ibc-go/v7/modules/core/24-host"
sdktypes "github.com/cosmos/cosmos-sdk/types"
)
const (
// Default timeout period is 4 weeks to ensure channel doesn't close on timeout
DefaultCCVTimeoutPeriod = 4 * 7 * 24 * time.Hour
)
var KeyCCVTimeoutPeriod = []byte("CcvTimeoutPeriod")
func ValidateDuration(i interface{}) error {
period, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if period <= time.Duration(0) {
return fmt.Errorf("duration must be positive")
}
return nil
}
func ValidateBool(i interface{}) error {
if _, ok := i.(bool); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func ValidateInt64(i interface{}) error {
if _, ok := i.(int64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func ValidatePositiveInt64(i interface{}) error {
if err := ValidateInt64(i); err != nil {
return err
}
if i.(int64) <= int64(0) {
return fmt.Errorf("int must be positive")
}
return nil
}
func ValidateString(i interface{}) error {
if _, ok := i.(string); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func ValidateDistributionTransmissionChannel(i interface{}) error {
// Accept empty string as valid, since this means a new
// distribution transmission channel will be created
if i == "" {
return nil
}
// Otherwise validate as usual for a channelID
return ValidateChannelIdentifier(i)
}
func ValidateChannelIdentifier(i interface{}) error {
value, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return ibchost.ChannelIdentifierValidator(value)
}
func ValidateBech32(i interface{}) error {
value, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
_, err := sdktypes.AccAddressFromBech32(value)
return err
}
func ValidateStringFraction(i interface{}) error {
str, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
dec, err := sdktypes.NewDecFromStr(str)
if err != nil {
return err
}
if dec.IsNegative() {
return fmt.Errorf("param cannot be negative, got %s", str)
}
if dec.Sub(sdktypes.NewDec(1)).IsPositive() {
return fmt.Errorf("param cannot be greater than 1, got %s", str)
}
return nil
}
func CalculateTrustPeriod(unbondingPeriod time.Duration, defaultTrustPeriodFraction string) (time.Duration, error) {
trustDec, err := sdktypes.NewDecFromStr(defaultTrustPeriodFraction)
if err != nil {
return time.Duration(0), err
}
trustPeriod := time.Duration(trustDec.MulInt64(unbondingPeriod.Nanoseconds()).TruncateInt64())
return trustPeriod, nil
}