-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg_server.go
227 lines (185 loc) · 7.42 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/plan/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}
}
// MsgCreate creates a new plan with the provided details and stores it in the Store.
// It validates the creation request, checks for provider existence, and assigns a unique ID to the plan.
func (k *msgServer) MsgCreate(c context.Context, msg *types.MsgCreateRequest) (*types.MsgCreateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Convert the `msg.From` address (in Bech32 format) to a `hubtypes.ProvAddress`.
provAddr, err := hubtypes.ProvAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Check if the provider with the given address exists in the network. If not, return an error.
if !k.HasProvider(ctx, provAddr) {
return nil, types.NewErrorProviderNotFound(provAddr)
}
// Get the current count of plans to assign a unique ID to the new plan.
count := k.GetCount(ctx)
plan := types.Plan{
ID: count + 1,
ProviderAddress: provAddr.String(),
Duration: msg.Duration,
Gigabytes: msg.Gigabytes,
Prices: msg.Prices,
Status: hubtypes.StatusInactive,
StatusAt: ctx.BlockTime(),
}
// Save the new plan in the Store and update the count for future plans.
k.SetCount(ctx, count+1)
k.SetPlan(ctx, plan)
k.SetPlanForProvider(ctx, provAddr, plan.ID)
// Emit an event to notify that a new plan has been created.
ctx.EventManager().EmitTypedEvent(
&types.EventCreate{
Address: plan.ProviderAddress,
ID: plan.ID,
},
)
return &types.MsgCreateResponse{}, nil
}
// MsgUpdateStatus updates the status of a plan.
// It validates the status update request, checks for plan existence, and updates the plan status and timestamp accordingly.
func (k *msgServer) MsgUpdateStatus(c context.Context, msg *types.MsgUpdateStatusRequest) (*types.MsgUpdateStatusResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Get the plan from the Store based on the provided plan ID.
plan, found := k.GetPlan(ctx, msg.ID)
if !found {
return nil, types.NewErrorPlanNotFound(msg.ID)
}
// Check if the provided address (`msg.From`) matches the plan's provider address to verify authorization.
if msg.From != plan.ProviderAddress {
return nil, types.NewErrorUnauthorized(msg.From)
}
// If the current status of the plan is `Active`, handle the necessary updates for changing to `Inactive` status.
if plan.Status.Equal(hubtypes.StatusActive) {
if msg.Status.Equal(hubtypes.StatusInactive) {
k.DeleteActivePlan(ctx, plan.ID)
}
}
// If the current status of the plan is `Inactive`, handle the necessary updates for changing to `Active` status.
if plan.Status.Equal(hubtypes.StatusInactive) {
if msg.Status.Equal(hubtypes.StatusActive) {
k.DeleteInactivePlan(ctx, plan.ID)
}
}
// Update the plan's status and status timestamp with the provided new status and current block time.
plan.Status = msg.Status
plan.StatusAt = ctx.BlockTime()
// Save the updated plan in the Store.
k.SetPlan(ctx, plan)
// Emit an event to notify that the plan status has been updated.
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
Status: plan.Status,
Address: plan.ProviderAddress,
ID: plan.ID,
},
)
return &types.MsgUpdateStatusResponse{}, nil
}
// MsgLinkNode links a node to a plan.
// It validates the link node request, checks for plan and node existence, and links the node to the plan.
func (k *msgServer) MsgLinkNode(c context.Context, msg *types.MsgLinkNodeRequest) (*types.MsgLinkNodeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Get the plan from the Store based on the provided plan ID.
plan, found := k.GetPlan(ctx, msg.ID)
if !found {
return nil, types.NewErrorPlanNotFound(msg.ID)
}
// Check if the provided address (`msg.From`) matches the plan's provider address to verify authorization.
if msg.From != plan.ProviderAddress {
return nil, types.NewErrorUnauthorized(msg.From)
}
// Convert the `msg.NodeAddress` (node address) to a `hubtypes.NodeAddress`.
nodeAddr, err := hubtypes.NodeAddressFromBech32(msg.NodeAddress)
if err != nil {
return nil, err
}
// Check if the node with the given address exists in the network. If not, return an error.
if !k.HasNode(ctx, nodeAddr) {
return nil, types.NewErrorNodeNotFound(nodeAddr)
}
// Link the node to the plan in the Store.
k.SetNodeForPlan(ctx, plan.ID, nodeAddr)
// Emit an event to notify that a node has been linked to the plan.
ctx.EventManager().EmitTypedEvent(
&types.EventLinkNode{
Address: plan.ProviderAddress,
NodeAddress: msg.NodeAddress,
ID: plan.ID,
},
)
return &types.MsgLinkNodeResponse{}, nil
}
// MsgUnlinkNode unlinks a node from a plan.
// It validates the unlink node request, checks for plan and node existence, and unlinks the node from the plan.
func (k *msgServer) MsgUnlinkNode(c context.Context, msg *types.MsgUnlinkNodeRequest) (*types.MsgUnlinkNodeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Get the plan from the Store based on the provided plan ID.
plan, found := k.GetPlan(ctx, msg.ID)
if !found {
return nil, types.NewErrorPlanNotFound(msg.ID)
}
// Check if the provided address (`msg.From`) matches the plan's provider address to verify authorization.
if msg.From != plan.ProviderAddress {
return nil, types.NewErrorUnauthorized(msg.From)
}
// Convert the `msg.NodeAddress` (node address) to a `hubtypes.NodeAddress`.
nodeAddr, err := hubtypes.NodeAddressFromBech32(msg.NodeAddress)
if err != nil {
return nil, err
}
// Unlink the node from the plan in the Store.
k.DeleteNodeForPlan(ctx, plan.ID, nodeAddr)
// Emit an event to notify that a node has been unlinked from the plan.
ctx.EventManager().EmitTypedEvent(
&types.EventUnlinkNode{
Address: plan.ProviderAddress,
NodeAddress: msg.NodeAddress,
ID: plan.ID,
},
)
return &types.MsgUnlinkNodeResponse{}, nil
}
// MsgSubscribe subscribes to a plan for a specific user account.
// It validates the subscription request and creates a new subscription for the provided plan and user account.
func (k *msgServer) MsgSubscribe(c context.Context, msg *types.MsgSubscribeRequest) (*types.MsgSubscribeResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Convert the `msg.From` address (in Bech32 format) to an `sdk.AccAddress`.
accAddr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
// Create a new subscription for the provided plan, user account, and denom.
subscription, err := k.CreateSubscriptionForPlan(ctx, accAddr, msg.ID, 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,
ProviderAddress: "",
ID: subscription.ID,
PlanID: subscription.PlanID,
},
)
return &types.MsgSubscribeResponse{}, nil
}