-
Notifications
You must be signed in to change notification settings - Fork 212
/
keeper.go
312 lines (243 loc) · 7.76 KB
/
keeper.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
types "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
)
type IKeeper interface {
StoreKey() sdk.StoreKey
Codec() codec.BinaryCodec
GetDeployment(ctx sdk.Context, id types.DeploymentID) (types.Deployment, bool)
GetGroup(ctx sdk.Context, id types.GroupID) (types.Group, bool)
GetGroups(ctx sdk.Context, id types.DeploymentID) []types.Group
Create(ctx sdk.Context, deployment types.Deployment, groups []types.Group) error
UpdateDeployment(ctx sdk.Context, deployment types.Deployment) error
CloseDeployment(ctx sdk.Context, deployment types.Deployment)
OnCloseGroup(ctx sdk.Context, group types.Group, state types.Group_State) error
OnPauseGroup(ctx sdk.Context, group types.Group) error
OnStartGroup(ctx sdk.Context, group types.Group) error
WithDeployments(ctx sdk.Context, fn func(types.Deployment) bool)
OnBidClosed(ctx sdk.Context, id types.GroupID) error
OnLeaseClosed(ctx sdk.Context, id types.GroupID) (types.Group, error)
GetParams(ctx sdk.Context) (params types.Params)
SetParams(ctx sdk.Context, params types.Params)
updateDeployment(ctx sdk.Context, obj types.Deployment)
NewQuerier() Querier
}
// Keeper of the deployment store
type Keeper struct {
skey sdk.StoreKey
cdc codec.BinaryCodec
pspace paramtypes.Subspace
ekeeper EscrowKeeper
}
// NewKeeper creates and returns an instance for deployment keeper
func NewKeeper(cdc codec.BinaryCodec, skey sdk.StoreKey, pspace paramtypes.Subspace, ekeeper EscrowKeeper) IKeeper {
if !pspace.HasKeyTable() {
pspace = pspace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
skey: skey,
cdc: cdc,
pspace: pspace,
ekeeper: ekeeper,
}
}
func (k Keeper) NewQuerier() Querier {
return Querier{k}
}
// Codec returns keeper codec
func (k Keeper) Codec() codec.BinaryCodec {
return k.cdc
}
func (k Keeper) StoreKey() sdk.StoreKey {
return k.skey
}
// GetDeployment returns deployment details with provided DeploymentID
func (k Keeper) GetDeployment(ctx sdk.Context, id types.DeploymentID) (types.Deployment, bool) {
store := ctx.KVStore(k.skey)
key := deploymentKey(id)
if !store.Has(key) {
return types.Deployment{}, false
}
buf := store.Get(key)
var val types.Deployment
k.cdc.MustUnmarshal(buf, &val)
return val, true
}
// GetGroup returns group details with given GroupID from deployment store
func (k Keeper) GetGroup(ctx sdk.Context, id types.GroupID) (types.Group, bool) {
store := ctx.KVStore(k.skey)
key := groupKey(id)
if !store.Has(key) {
return types.Group{}, false
}
buf := store.Get(key)
var val types.Group
k.cdc.MustUnmarshal(buf, &val)
return val, true
}
// GetGroups returns all groups of a deployment with given DeploymentID from deployment store
func (k Keeper) GetGroups(ctx sdk.Context, id types.DeploymentID) []types.Group {
store := ctx.KVStore(k.skey)
key := groupsKey(id)
var vals []types.Group
iter := sdk.KVStorePrefixIterator(store, key)
for ; iter.Valid(); iter.Next() {
var val types.Group
k.cdc.MustUnmarshal(iter.Value(), &val)
vals = append(vals, val)
}
iter.Close()
return vals
}
// Create creates a new deployment with given deployment and group specifications
func (k Keeper) Create(ctx sdk.Context, deployment types.Deployment, groups []types.Group) error {
store := ctx.KVStore(k.skey)
key := deploymentKey(deployment.ID())
if store.Has(key) {
return types.ErrDeploymentExists
}
store.Set(key, k.cdc.MustMarshal(&deployment))
for idx := range groups {
group := groups[idx]
if !group.ID().DeploymentID().Equals(deployment.ID()) {
return types.ErrInvalidGroupID
}
gkey := groupKey(group.ID())
store.Set(gkey, k.cdc.MustMarshal(&group))
}
ctx.EventManager().EmitEvent(
types.NewEventDeploymentCreated(deployment.ID(), deployment.Version).
ToSDKEvent(),
)
telemetry.IncrCounter(1.0, "akash.deployment_created")
return nil
}
// UpdateDeployment updates deployment details
func (k Keeper) UpdateDeployment(ctx sdk.Context, deployment types.Deployment) error {
store := ctx.KVStore(k.skey)
key := deploymentKey(deployment.ID())
if !store.Has(key) {
return types.ErrDeploymentNotFound
}
ctx.EventManager().EmitEvent(
types.NewEventDeploymentUpdated(deployment.ID(), deployment.Version).
ToSDKEvent(),
)
store.Set(key, k.cdc.MustMarshal(&deployment))
return nil
}
// UpdateDeployment updates deployment details
func (k Keeper) CloseDeployment(ctx sdk.Context, deployment types.Deployment) {
if deployment.State == types.DeploymentClosed {
return
}
store := ctx.KVStore(k.skey)
key := deploymentKey(deployment.ID())
if !store.Has(key) {
return
}
deployment.State = types.DeploymentClosed
ctx.EventManager().EmitEvent(
types.NewEventDeploymentClosed(deployment.ID()).
ToSDKEvent(),
)
store.Set(key, k.cdc.MustMarshal(&deployment))
}
// OnCloseGroup provides shutdown API for a Group
func (k Keeper) OnCloseGroup(ctx sdk.Context, group types.Group, state types.Group_State) error {
store := ctx.KVStore(k.skey)
key := groupKey(group.ID())
if !store.Has(key) {
return types.ErrGroupNotFound
}
group.State = state
ctx.EventManager().EmitEvent(
types.NewEventGroupClosed(group.ID()).
ToSDKEvent(),
)
store.Set(key, k.cdc.MustMarshal(&group))
return nil
}
// OnPauseGroup provides shutdown API for a Group
func (k Keeper) OnPauseGroup(ctx sdk.Context, group types.Group) error {
store := ctx.KVStore(k.skey)
key := groupKey(group.ID())
if !store.Has(key) {
return types.ErrGroupNotFound
}
group.State = types.GroupPaused
ctx.EventManager().EmitEvent(
types.NewEventGroupPaused(group.ID()).
ToSDKEvent(),
)
store.Set(key, k.cdc.MustMarshal(&group))
return nil
}
// OnStartGroup provides shutdown API for a Group
func (k Keeper) OnStartGroup(ctx sdk.Context, group types.Group) error {
store := ctx.KVStore(k.skey)
key := groupKey(group.ID())
if !store.Has(key) {
return types.ErrGroupNotFound
}
group.State = types.GroupOpen
ctx.EventManager().EmitEvent(
types.NewEventGroupStarted(group.ID()).
ToSDKEvent(),
)
store.Set(key, k.cdc.MustMarshal(&group))
return nil
}
// WithDeployments iterates all deployments in deployment store
func (k Keeper) WithDeployments(ctx sdk.Context, fn func(types.Deployment) bool) {
store := ctx.KVStore(k.skey)
iter := sdk.KVStorePrefixIterator(store, types.DeploymentPrefix())
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var val types.Deployment
k.cdc.MustUnmarshal(iter.Value(), &val)
if stop := fn(val); stop {
break
}
}
}
// OnBidClosed sets the group to state paused.
func (k Keeper) OnBidClosed(ctx sdk.Context, id types.GroupID) error {
group, ok := k.GetGroup(ctx, id)
if !ok {
return types.ErrGroupNotFound
}
return k.OnPauseGroup(ctx, group)
}
// OnLeaseClosed keeps the group at state open
func (k Keeper) OnLeaseClosed(ctx sdk.Context, id types.GroupID) (types.Group, error) {
group, ok := k.GetGroup(ctx, id)
if !ok {
return types.Group{}, types.ErrGroupNotFound
}
return group, nil
}
// GetParams returns the total set of deployment parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.pspace.GetParamSet(ctx, ¶ms)
return params
}
// SetParams sets the deployment parameters to the paramspace.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.pspace.SetParamSet(ctx, ¶ms)
}
func (k Keeper) updateDeployment(ctx sdk.Context, obj types.Deployment) {
store := ctx.KVStore(k.skey)
key := deploymentKey(obj.ID())
store.Set(key, k.cdc.MustMarshal(&obj))
}
// nolint: unused
func (k Keeper) updateGroup(ctx sdk.Context, group types.Group) {
store := ctx.KVStore(k.skey)
key := groupKey(group.ID())
store.Set(key, k.cdc.MustMarshal(&group))
}