-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg_server.go
254 lines (208 loc) · 8.14 KB
/
msg_server.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
package keeper
import (
"context"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/types"
"github.com/sentinel-official/hub/v12/x/node/types"
)
// The following line asserts that the `msgServer` type implements the `types.MsgServiceServer` interface.
var (
_ types.MsgServiceServer = (*msgServer)(nil)
)
// msgServer is a message server that implements the `types.MsgServiceServer` interface.
type msgServer struct {
Keeper // Keeper is an instance of the main keeper for the module.
}
// NewMsgServiceServer creates a new instance of `types.MsgServiceServer` using the provided Keeper.
func NewMsgServiceServer(k Keeper) types.MsgServiceServer {
return &msgServer{k}
}
// MsgRegister registers a new node in the network.
// It validates the registration request, checks prices, and creates a new node.
func (k *msgServer) MsgRegister(c context.Context, msg *types.MsgRegisterRequest) (*types.MsgRegisterResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Check if the provided GigabytePrices are valid, if not, return an error.
if !k.IsValidGigabytePrices(ctx, msg.GigabytePrices) {
return nil, types.NewErrorInvalidPrices(msg.GigabytePrices)
}
// Check if the provided HourlyPrices are valid, if not, return an error.
if !k.IsValidHourlyPrices(ctx, msg.HourlyPrices) {
return nil, types.NewErrorInvalidPrices(msg.HourlyPrices)
}
// Convert the `msg.From` address (in Bech32 format) to an `sdk.AccAddress`.
accAddr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Convert the account address to a `hubtypes.NodeAddress`.
nodeAddr := hubtypes.NodeAddress(accAddr.Bytes())
// Check if the node already exists in the network. If found, return an error to prevent duplicate registration.
if k.HasNode(ctx, nodeAddr) {
return nil, types.NewErrorDuplicateNode(nodeAddr)
}
// Get the required deposit for registering a new node.
deposit := k.Deposit(ctx)
// Fund the community pool with the required deposit from the registrant's account.
if err = k.FundCommunityPool(ctx, accAddr, deposit); err != nil {
return nil, err
}
// Create a new node with the provided information and set its status to `Inactive`.
node := types.Node{
Address: nodeAddr.String(),
GigabytePrices: msg.GigabytePrices,
HourlyPrices: msg.HourlyPrices,
RemoteURL: msg.RemoteURL,
InactiveAt: time.Time{},
Status: hubtypes.StatusInactive,
StatusAt: ctx.BlockTime(),
}
// Save the new node in the Store.
k.SetNode(ctx, node)
// Emit an event to notify that a new node has been registered.
ctx.EventManager().EmitTypedEvent(
&types.EventRegister{
Address: node.Address,
},
)
return &types.MsgRegisterResponse{}, nil
}
// MsgUpdateDetails updates the details of a registered node.
// It validates the update details request, checks prices, and updates the node information.
func (k *msgServer) MsgUpdateDetails(c context.Context, msg *types.MsgUpdateDetailsRequest) (*types.MsgUpdateDetailsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Check if the provided GigabytePrices are valid, if not, return an error.
if msg.GigabytePrices != nil {
if !k.IsValidGigabytePrices(ctx, msg.GigabytePrices) {
return nil, types.NewErrorInvalidPrices(msg.GigabytePrices)
}
}
// Check if the provided HourlyPrices are valid, if not, return an error.
if msg.HourlyPrices != nil {
if !k.IsValidHourlyPrices(ctx, msg.HourlyPrices) {
return nil, types.NewErrorInvalidPrices(msg.HourlyPrices)
}
}
// Convert the `msg.From` address (in Bech32 format) to a `hubtypes.NodeAddress`.
nodeAddr, err := hubtypes.NodeAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Get the node from the Store based on the provided node address.
node, found := k.GetNode(ctx, nodeAddr)
if !found {
return nil, types.NewErrorNodeNotFound(nodeAddr)
}
// Update the node's GigabytePrices, HourlyPrices, and RemoteURL with the provided values.
if msg.GigabytePrices != nil {
node.GigabytePrices = msg.GigabytePrices
}
if msg.HourlyPrices != nil {
node.HourlyPrices = msg.HourlyPrices
}
if msg.RemoteURL != "" {
node.RemoteURL = msg.RemoteURL
}
// Save the updated node in the Store.
k.SetNode(ctx, node)
// Emit an event to notify that the node details have been updated.
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateDetails{
Address: node.Address,
},
)
return &types.MsgUpdateDetailsResponse{}, nil
}
// MsgUpdateStatus updates the status of a registered node.
// It validates the update status request, checks the node's current status, and updates the status and inactive time accordingly.
func (k *msgServer) MsgUpdateStatus(c context.Context, msg *types.MsgUpdateStatusRequest) (*types.MsgUpdateStatusResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Convert the `msg.From` address (in Bech32 format) to a `hubtypes.NodeAddress`.
nodeAddr, err := hubtypes.NodeAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Get the node from the Store based on the provided node address.
node, found := k.GetNode(ctx, nodeAddr)
if !found {
return nil, types.NewErrorNodeNotFound(nodeAddr)
}
// If the current status of the node is `Active`, handle the necessary updates for changing to `Inactive` status.
if node.Status.Equal(hubtypes.StatusActive) {
k.DeleteNodeForInactiveAt(ctx, node.InactiveAt, nodeAddr)
if msg.Status.Equal(hubtypes.StatusInactive) {
k.DeleteActiveNode(ctx, nodeAddr)
}
}
// If the current status of the node is `Inactive`, handle the necessary updates for changing to `Active` status.
if node.Status.Equal(hubtypes.StatusInactive) {
if msg.Status.Equal(hubtypes.StatusActive) {
k.DeleteInactiveNode(ctx, nodeAddr)
}
}
// If the new status is `Active`, update the node's inactive time based on the active duration.
if msg.Status.Equal(hubtypes.StatusActive) {
node.InactiveAt = ctx.BlockTime().Add(
k.ActiveDuration(ctx),
)
k.SetNodeForInactiveAt(ctx, node.InactiveAt, nodeAddr)
}
// If the new status is `Inactive`, set the node's inactive time to zero.
if msg.Status.Equal(hubtypes.StatusInactive) {
node.InactiveAt = time.Time{}
}
// Update the node's status and status timestamp.
node.Status = msg.Status
node.StatusAt = ctx.BlockTime()
// Save the updated node in the Store.
k.SetNode(ctx, node)
// Emit an event to notify that the node status has been updated.
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
Status: node.Status,
Address: node.Address,
},
)
return &types.MsgUpdateStatusResponse{}, nil
}
// MsgSubscribe subscribes to a node for a specific amount of gigabytes or hours.
// It validates the subscription request and creates a new subscription for the provided node and user account.
func (k *msgServer) MsgSubscribe(c context.Context, msg *types.MsgSubscribeRequest) (*types.MsgSubscribeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Check if the provided Gigabytes value is valid, if not, return an error.
if msg.Gigabytes != 0 {
if !k.IsValidSubscriptionGigabytes(ctx, msg.Gigabytes) {
return nil, types.NewErrorInvalidGigabytes(msg.Gigabytes)
}
}
// Check if the provided Hours value is valid, if not, return an error.
if msg.Hours != 0 {
if !k.IsValidSubscriptionHours(ctx, msg.Hours) {
return nil, types.NewErrorInvalidHours(msg.Hours)
}
}
// Convert the `msg.From` address (in Bech32 format) to an `sdk.AccAddress`.
accAddr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Convert the `msg.NodeAddress` (node address) to a `hubtypes.NodeAddress`.
nodeAddr, err := hubtypes.NodeAddressFromBech32(msg.NodeAddress)
if err != nil {
return nil, err
}
// Create a new subscription for the provided node, user account, gigabytes, hours, and denom.
subscription, err := k.CreateSubscriptionForNode(ctx, accAddr, nodeAddr, msg.Gigabytes, msg.Hours, msg.Denom)
if err != nil {
return nil, err
}
// Emit an event to notify that a new subscription has been created.
ctx.EventManager().EmitTypedEvent(
&types.EventCreateSubscription{
Address: subscription.Address,
NodeAddress: subscription.NodeAddress,
ID: subscription.ID,
},
)
return &types.MsgSubscribeResponse{}, nil
}