-
Notifications
You must be signed in to change notification settings - Fork 40
/
subscription.go
165 lines (144 loc) · 3.84 KB
/
subscription.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"
"time"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
hubtypes "github.com/sentinel-official/hub/v12/types"
)
type (
Subscription interface {
proto.Message
Type() SubscriptionType
Validate() error
GetID() uint64
GetAddress() sdk.AccAddress
GetInactiveAt() time.Time
GetStatus() hubtypes.Status
GetStatusAt() time.Time
SetInactiveAt(v time.Time)
SetStatus(v hubtypes.Status)
SetStatusAt(v time.Time)
}
Subscriptions []Subscription
)
var (
_ Subscription = (*NodeSubscription)(nil)
_ Subscription = (*PlanSubscription)(nil)
)
func (s *BaseSubscription) GetID() uint64 { return s.ID }
func (s *BaseSubscription) GetInactiveAt() time.Time { return s.InactiveAt }
func (s *BaseSubscription) GetStatus() hubtypes.Status { return s.Status }
func (s *BaseSubscription) GetStatusAt() time.Time { return s.StatusAt }
func (s *BaseSubscription) GetAddress() sdk.AccAddress {
if s.Address == "" {
return nil
}
addr, err := sdk.AccAddressFromBech32(s.Address)
if err != nil {
panic(err)
}
return addr
}
func (s *BaseSubscription) SetInactiveAt(v time.Time) { s.InactiveAt = v }
func (s *BaseSubscription) SetStatus(v hubtypes.Status) { s.Status = v }
func (s *BaseSubscription) SetStatusAt(v time.Time) { s.StatusAt = v }
func (s *BaseSubscription) Validate() error {
if s.ID == 0 {
return fmt.Errorf("id cannot be zero")
}
if s.Address == "" {
return fmt.Errorf("address cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(s.Address); err != nil {
return sdkerrors.Wrapf(err, "invalid address %s", s.Address)
}
if s.InactiveAt.IsZero() {
return fmt.Errorf("inactive_at cannot be zero")
}
if !s.Status.IsValid() {
return fmt.Errorf("status must be valid")
}
if s.StatusAt.IsZero() {
return fmt.Errorf("status_at cannot be zero")
}
return nil
}
func (s *NodeSubscription) Type() SubscriptionType {
return TypeNode
}
func (s *NodeSubscription) Validate() error {
if s.BaseSubscription == nil {
return fmt.Errorf("base_subscription cannot be nil")
}
if err := s.BaseSubscription.Validate(); err != nil {
return err
}
if s.NodeAddress == "" {
return fmt.Errorf("node_address cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(s.NodeAddress); err != nil {
return sdkerrors.Wrapf(err, "invalid node_address %s", s.NodeAddress)
}
if s.Gigabytes == 0 && s.Hours == 0 {
return fmt.Errorf("[gigabytes, hours] cannot be empty")
}
if s.Gigabytes != 0 && s.Hours != 0 {
return fmt.Errorf("[gigabytes, hours] cannot be non-empty")
}
if s.Gigabytes != 0 {
if s.Gigabytes < 0 {
return fmt.Errorf("gigabytes cannot be negative")
}
}
if s.Hours != 0 {
if s.Hours < 0 {
return fmt.Errorf("hours cannot be negative")
}
}
if s.Deposit.IsNil() {
return fmt.Errorf("deposit cannot be nil")
}
if s.Deposit.IsNegative() {
return fmt.Errorf("deposit cannot be negative")
}
if s.Deposit.IsZero() {
return fmt.Errorf("deposit cannot be zero")
}
if !s.Deposit.IsValid() {
return fmt.Errorf("deposit must be valid")
}
return nil
}
func (s *NodeSubscription) GetNodeAddress() hubtypes.NodeAddress {
if s.NodeAddress == "" {
return nil
}
addr, err := hubtypes.NodeAddressFromBech32(s.NodeAddress)
if err != nil {
panic(err)
}
return addr
}
func (s *PlanSubscription) Type() SubscriptionType {
return TypePlan
}
func (s *PlanSubscription) Validate() error {
if s.BaseSubscription == nil {
return fmt.Errorf("base_subscription cannot be nil")
}
if err := s.BaseSubscription.Validate(); err != nil {
return err
}
if s.PlanID == 0 {
return fmt.Errorf("plan_id cannot be zero")
}
if s.Denom == "" {
return fmt.Errorf("denom cannot be empty")
}
if err := sdk.ValidateDenom(s.Denom); err != nil {
return sdkerrors.Wrapf(err, "invalid denom %s", s.Denom)
}
return nil
}