-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg.go
270 lines (243 loc) · 9.72 KB
/
msg.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package types
import (
"net/url"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
hubtypes "github.com/sentinel-official/hub/types"
)
// The `types` package contains custom message types for the Cosmos SDK.
// The following variables implement the sdk.Msg interface for the provided message types.
// These variables ensure that the corresponding types can be used as messages in the Cosmos SDK.
var (
_ sdk.Msg = (*MsgRegisterRequest)(nil)
_ sdk.Msg = (*MsgUpdateDetailsRequest)(nil)
_ sdk.Msg = (*MsgUpdateStatusRequest)(nil)
_ sdk.Msg = (*MsgSubscribeRequest)(nil)
)
// NewMsgRegisterRequest creates a new MsgRegisterRequest instance with the given parameters.
func NewMsgRegisterRequest(from sdk.AccAddress, gigabytePrices, hourlyPrices sdk.Coins, remoteURL string) *MsgRegisterRequest {
return &MsgRegisterRequest{
From: from.String(),
GigabytePrices: gigabytePrices,
HourlyPrices: hourlyPrices,
RemoteURL: remoteURL,
}
}
// ValidateBasic performs basic validation checks on the MsgRegisterRequest fields.
// It checks if the 'From' field is not empty and represents a valid account address,
// if the 'GigabytePrices' and 'HourlyPrices' fields are valid coins (not empty, not containing nil coins, and having valid coin denominations),
// and if the 'RemoteURL' field is valid (not empty, not longer than 64 characters, and has a valid "https" scheme and non-empty port).
func (m *MsgRegisterRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.GigabytePrices == nil {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices cannot be nil")
}
if m.GigabytePrices.Len() == 0 {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices length cannot be zero")
}
if m.GigabytePrices.IsAnyNil() {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices cannot contain nil")
}
if !m.GigabytePrices.IsValid() {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices must be valid")
}
if m.HourlyPrices == nil {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices cannot be nil")
}
if m.HourlyPrices.Len() == 0 {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices length cannot be zero")
}
if m.HourlyPrices.IsAnyNil() {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices cannot contain nil")
}
if !m.HourlyPrices.IsValid() {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices must be valid")
}
if m.RemoteURL == "" {
return errors.Wrap(ErrorInvalidMessage, "remote_url cannot be empty")
}
if len(m.RemoteURL) > 64 {
return errors.Wrapf(ErrorInvalidMessage, "remote_url length cannot be greater than %d chars", 64)
}
remoteURL, err := url.ParseRequestURI(m.RemoteURL)
if err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if remoteURL.Scheme != "https" {
return errors.Wrap(ErrorInvalidMessage, "remote_url scheme must be https")
}
if remoteURL.Port() == "" {
return errors.Wrap(ErrorInvalidMessage, "remote_url port cannot be empty")
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgRegisterRequest.
func (m *MsgRegisterRequest) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// NewMsgUpdateDetailsRequest creates a new MsgUpdateDetailsRequest instance with the given parameters.
func NewMsgUpdateDetailsRequest(from hubtypes.NodeAddress, gigabytePrices, hourlyPrices sdk.Coins, remoteURL string) *MsgUpdateDetailsRequest {
return &MsgUpdateDetailsRequest{
From: from.String(),
GigabytePrices: gigabytePrices,
HourlyPrices: hourlyPrices,
RemoteURL: remoteURL,
}
}
// ValidateBasic performs basic validation checks on the MsgUpdateDetailsRequest fields.
// It checks if the 'From' field is not empty and represents a valid node address,
// if the 'GigabytePrices' and 'HourlyPrices' fields are valid coins (not empty, not containing nil coins, and having valid coin denominations),
// and if the 'RemoteURL' field is valid (not empty, not longer than 64 characters, and has a valid "https" scheme and non-empty port).
func (m *MsgUpdateDetailsRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.From); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.GigabytePrices != nil {
if m.GigabytePrices.Len() == 0 {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices length cannot be zero")
}
if m.GigabytePrices.IsAnyNil() {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices cannot contain nil")
}
if !m.GigabytePrices.IsValid() {
return errors.Wrap(ErrorInvalidMessage, "gigabyte_prices must be valid")
}
}
if m.HourlyPrices != nil {
if m.HourlyPrices.Len() == 0 {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices length cannot be zero")
}
if m.HourlyPrices.IsAnyNil() {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices cannot contain nil")
}
if !m.HourlyPrices.IsValid() {
return errors.Wrap(ErrorInvalidMessage, "hourly_prices must be valid")
}
}
if m.RemoteURL != "" {
if len(m.RemoteURL) > 64 {
return errors.Wrapf(ErrorInvalidMessage, "remote_url length cannot be greater than %d chars", 64)
}
remoteURL, err := url.ParseRequestURI(m.RemoteURL)
if err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if remoteURL.Scheme != "https" {
return errors.Wrap(ErrorInvalidMessage, "remote_url scheme must be https")
}
if remoteURL.Port() == "" {
return errors.Wrap(ErrorInvalidMessage, "remote_url port cannot be empty")
}
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgUpdateDetailsRequest.
func (m *MsgUpdateDetailsRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.NodeAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from.Bytes()}
}
// NewMsgUpdateStatusRequest creates a new MsgUpdateStatusRequest instance with the given parameters.
func NewMsgUpdateStatusRequest(from hubtypes.NodeAddress, status hubtypes.Status) *MsgUpdateStatusRequest {
return &MsgUpdateStatusRequest{
From: from.String(),
Status: status,
}
}
// ValidateBasic performs basic validation checks on the MsgUpdateStatusRequest fields.
// It checks if the 'From' field is not empty and represents a valid node address,
// and if the 'Status' field is one of the allowed values [active, inactive].
func (m *MsgUpdateStatusRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.From); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if !m.Status.IsOneOf(hubtypes.StatusActive, hubtypes.StatusInactive) {
return errors.Wrap(ErrorInvalidMessage, "status must be one of [active, inactive]")
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgUpdateStatusRequest.
func (m *MsgUpdateStatusRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.NodeAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from.Bytes()}
}
// NewMsgSubscribeRequest creates a new MsgSubscribeRequest instance with the given parameters.
func NewMsgSubscribeRequest(from sdk.AccAddress, addr hubtypes.NodeAddress, gigabytes, hours int64, denom string) *MsgSubscribeRequest {
return &MsgSubscribeRequest{
From: from.String(),
NodeAddress: addr.String(),
Gigabytes: gigabytes,
Hours: hours,
Denom: denom,
}
}
// ValidateBasic performs basic validation checks on the MsgSubscribeRequest fields.
// It checks if the 'From' field is not empty and represents a valid account address,
// if the 'NodeAddress' field is not empty and represents a valid node address,
// if either 'Gigabytes' or 'Hours' field (but not both) are non-zero and non-negative,
// and if the 'Denom' field is valid according to the Cosmos SDK's ValidateDenom function.
func (m *MsgSubscribeRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.NodeAddress == "" {
return errors.Wrap(ErrorInvalidMessage, "node_address cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.NodeAddress); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.Gigabytes == 0 && m.Hours == 0 {
return errors.Wrapf(ErrorInvalidMessage, "[gigabytes, hours] cannot be empty")
}
if m.Gigabytes != 0 && m.Hours != 0 {
return errors.Wrapf(ErrorInvalidMessage, "[gigabytes, hours] cannot be non-empty")
}
if m.Gigabytes != 0 {
if m.Gigabytes < 0 {
return errors.Wrap(ErrorInvalidMessage, "gigabytes cannot be negative")
}
}
if m.Hours != 0 {
if m.Hours < 0 {
return errors.Wrap(ErrorInvalidMessage, "hours cannot be negative")
}
}
if m.Denom == "" {
return errors.Wrap(ErrorInvalidMessage, "denom cannot be empty")
}
if err := sdk.ValidateDenom(m.Denom); err != nil {
return errors.Wrap(ErrorInvalidMessage, err.Error())
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgSubscribeRequest.
func (m *MsgSubscribeRequest) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}