/
session.go
81 lines (69 loc) · 1.74 KB
/
session.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
package types
import (
"fmt"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/types"
)
func (m *Session) GetAddress() sdk.AccAddress {
if m.Address == "" {
return nil
}
addr, err := sdk.AccAddressFromBech32(m.Address)
if err != nil {
panic(err)
}
return addr
}
func (m *Session) GetNodeAddress() hubtypes.NodeAddress {
if m.NodeAddress == "" {
return nil
}
addr, err := hubtypes.NodeAddressFromBech32(m.NodeAddress)
if err != nil {
panic(err)
}
return addr
}
func (m *Session) Validate() error {
if m.ID == 0 {
return fmt.Errorf("id cannot be zero")
}
if m.SubscriptionID == 0 {
return fmt.Errorf("subscription_id cannot be zero")
}
if m.NodeAddress == "" {
return fmt.Errorf("node_address cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.NodeAddress); err != nil {
return sdkerrors.Wrapf(err, "invalid node_address %s", m.NodeAddress)
}
if m.Address == "" {
return fmt.Errorf("address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return sdkerrors.Wrapf(err, "invalid address %s", m.Address)
}
if m.Bandwidth.IsAnyNil() {
return fmt.Errorf("bandwidth cannot be empty")
}
if m.Bandwidth.IsAnyNegative() {
return fmt.Errorf("bandwidth cannot be negative")
}
if m.Duration < 0 {
return fmt.Errorf("duration cannot be negative")
}
if m.InactiveAt.IsZero() {
return fmt.Errorf("inactive_at cannot be zero")
}
if !m.Status.IsOneOf(hubtypes.StatusActive, hubtypes.StatusInactivePending) {
return fmt.Errorf("status must be oneof [active, inactive_pending]")
}
if m.StatusAt.IsZero() {
return fmt.Errorf("status_at cannot be zero")
}
return nil
}
type (
Sessions []Session
)