/
msg.go
251 lines (221 loc) · 8.6 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
package types
import (
"time"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/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 = (*MsgCreateRequest)(nil)
_ sdk.Msg = (*MsgUpdateStatusRequest)(nil)
_ sdk.Msg = (*MsgLinkNodeRequest)(nil)
_ sdk.Msg = (*MsgUnlinkNodeRequest)(nil)
_ sdk.Msg = (*MsgSubscribeRequest)(nil)
)
// NewMsgCreateRequest creates a new MsgCreateRequest instance with the given parameters.
func NewMsgCreateRequest(from hubtypes.ProvAddress, duration time.Duration, gigabytes int64, prices sdk.Coins) *MsgCreateRequest {
return &MsgCreateRequest{
From: from.String(),
Duration: duration,
Gigabytes: gigabytes,
Prices: prices,
}
}
// ValidateBasic performs basic validation checks on the MsgCreateRequest fields.
// It checks if the 'From' field is not empty and represents a valid provider address,
// if the 'Duration' field is not negative or zero,
// if the 'Gigabytes' field is not negative or zero,
// and if the 'Prices' field is valid (not empty, not containing nil coins, and having valid coin denominations).
func (m *MsgCreateRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.Duration < 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "duration cannot be negative")
}
if m.Duration == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "duration cannot be zero")
}
if m.Gigabytes < 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "gigabytes cannot be negative")
}
if m.Gigabytes == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "gigabytes cannot be zero")
}
if m.Prices == nil {
return sdkerrors.Wrap(ErrorInvalidMessage, "prices cannot be nil")
}
if m.Prices.Len() == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "prices cannot be empty")
}
if m.Prices.IsAnyNil() {
return sdkerrors.Wrap(ErrorInvalidMessage, "prices cannot contain nil")
}
if !m.Prices.IsValid() {
return sdkerrors.Wrap(ErrorInvalidMessage, "prices must be valid")
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgCreateRequest.
func (m *MsgCreateRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.ProvAddressFromBech32(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.ProvAddress, id uint64, status hubtypes.Status) *MsgUpdateStatusRequest {
return &MsgUpdateStatusRequest{
From: from.String(),
ID: id,
Status: status,
}
}
// ValidateBasic performs basic validation checks on the MsgUpdateStatusRequest fields.
// It checks if the 'From' field is not empty and represents a valid provider address,
// if the 'ID' field is not zero,
// and if the 'Status' field is one of the allowed values [active, inactive].
func (m *MsgUpdateStatusRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.ID == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "id cannot be zero")
}
if !m.Status.IsOneOf(hubtypes.StatusActive, hubtypes.StatusInactive) {
return sdkerrors.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.ProvAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from.Bytes()}
}
// NewMsgLinkNodeRequest creates a new MsgLinkNodeRequest instance with the given parameters.
func NewMsgLinkNodeRequest(from hubtypes.ProvAddress, id uint64, addr hubtypes.NodeAddress) *MsgLinkNodeRequest {
return &MsgLinkNodeRequest{
From: from.String(),
ID: id,
NodeAddress: addr.String(),
}
}
// ValidateBasic performs basic validation checks on the MsgLinkNodeRequest fields.
// It checks if the 'From' field is not empty and represents a valid provider address,
// if the 'ID' field is not zero,
// and if the 'Address' field is not empty and represents a valid node address.
func (m *MsgLinkNodeRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.ID == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "id cannot be zero")
}
if m.NodeAddress == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "node_address cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.NodeAddress); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgLinkNodeRequest.
func (m *MsgLinkNodeRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.ProvAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from.Bytes()}
}
// NewMsgUnlinkNodeRequest creates a new MsgUnlinkNodeRequest instance with the given parameters.
func NewMsgUnlinkNodeRequest(from hubtypes.ProvAddress, id uint64, addr hubtypes.NodeAddress) *MsgUnlinkNodeRequest {
return &MsgUnlinkNodeRequest{
From: from.String(),
ID: id,
NodeAddress: addr.String(),
}
}
// ValidateBasic performs basic validation checks on the MsgUnlinkNodeRequest fields.
// It checks if the 'From' field is not empty and represents a valid provider address,
// if the 'ID' field is not zero,
// and if the 'Address' field is not empty and represents a valid node address.
func (m *MsgUnlinkNodeRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.ID == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "id cannot be zero")
}
if m.NodeAddress == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "node_address cannot be empty")
}
if _, err := hubtypes.NodeAddressFromBech32(m.NodeAddress); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgUnlinkNodeRequest.
func (m *MsgUnlinkNodeRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.ProvAddressFromBech32(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, id uint64, denom string) *MsgSubscribeRequest {
return &MsgSubscribeRequest{
From: from.String(),
ID: id,
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 'ID' field is not zero,
// and if the 'Denom' field is valid according to the Cosmos SDK's ValidateDenom function.
func (m *MsgSubscribeRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.ID == 0 {
return sdkerrors.Wrap(ErrorInvalidMessage, "id cannot be zero")
}
if m.Denom == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "denom cannot be empty")
}
if err := sdk.ValidateDenom(m.Denom); err != nil {
return sdkerrors.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}
}