Skip to content

Commit 6dfb20e

Browse files
committed
types: composite keys initial utils
Initial work on handling composite keys decently. refs #77
1 parent ea06b99 commit 6dfb20e

32 files changed

+1573
-1051
lines changed

_integration/cmp/deployment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func deployQueryState(daddr vars.Ref, state types.Deployment_DeploymentState) ge
4949

5050
func orderQuery(daddr vars.Ref) gestalt.Component {
5151
parse := js.Do(
52-
js.Str(daddr.Var(), "items", "[0]", "deployment"),
52+
js.Str(daddr.Var(), "items", "[0]", "id", "deployment"),
5353
)
5454

5555
return akash("order-query", "query", "order").
@@ -59,7 +59,7 @@ func orderQuery(daddr vars.Ref) gestalt.Component {
5959

6060
func leaseQuery(daddr vars.Ref) gestalt.Component {
6161
parse := js.Do(
62-
js.Str(daddr.Var(), "items", "[0]", "deployment"),
62+
js.Str(daddr.Var(), "items", "[0]", "id", "deployment"),
6363
)
6464

6565
return akash("lease-query", "query", "lease").

app/deployment/app.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77

88
"github.com/gogo/protobuf/proto"
99
apptypes "github.com/ovrclk/akash/app/types"
10+
"github.com/ovrclk/akash/keys"
1011
"github.com/ovrclk/akash/state"
1112
"github.com/ovrclk/akash/types"
12-
"github.com/ovrclk/akash/types/base"
1313
"github.com/ovrclk/akash/types/code"
1414
tmtypes "github.com/tendermint/abci/types"
1515
"github.com/tendermint/tmlibs/log"
@@ -39,10 +39,11 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
3939
}
4040
}
4141

42-
// todo: need abtraction for multiple query types per app
42+
// TODO: Partial Key Parsing
43+
4344
if strings.HasPrefix(req.GetPath(), state.DeploymentGroupPath) {
4445
id := strings.TrimPrefix(req.Path, state.DeploymentGroupPath)
45-
key, err := base.DecodeString(id)
46+
key, err := keys.ParseGroupPath(id)
4647
if err != nil {
4748
return tmtypes.ResponseQuery{
4849
Code: code.ERROR,
@@ -52,20 +53,19 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
5253
return a.doDeploymentGroupQuery(key)
5354
}
5455

55-
// todo: abstractiion: all queries should have this
5656
id := strings.TrimPrefix(req.Path, state.DeploymentPath)
57-
key, err := base.DecodeString(id)
57+
if len(id) == 0 {
58+
return a.doRangeQuery()
59+
}
60+
61+
key, err := keys.ParseDeploymentPath(id)
5862
if err != nil {
5963
return tmtypes.ResponseQuery{
6064
Code: code.ERROR,
6165
Log: err.Error(),
6266
}
6367
}
6468

65-
// id is empty string, get full range
66-
if len(id) == 0 {
67-
return a.doRangeQuery(key)
68-
}
6969
return a.doQuery(key)
7070
}
7171

@@ -105,9 +105,9 @@ func (a *app) DeliverTx(ctx apptypes.Context, tx interface{}) tmtypes.ResponseDe
105105
}
106106
}
107107

108-
func (a *app) doQuery(key base.Bytes) tmtypes.ResponseQuery {
108+
func (a *app) doQuery(key keys.Deployment) tmtypes.ResponseQuery {
109109

110-
dep, err := a.State().Deployment().Get(key)
110+
dep, err := a.State().Deployment().Get(key.ID())
111111

112112
if err != nil {
113113
return tmtypes.ResponseQuery{
@@ -119,7 +119,7 @@ func (a *app) doQuery(key base.Bytes) tmtypes.ResponseQuery {
119119
if dep == nil {
120120
return tmtypes.ResponseQuery{
121121
Code: code.NOT_FOUND,
122-
Log: fmt.Sprintf("deployment %x not found", key),
122+
Log: fmt.Sprintf("deployment %v not found", key.Path()),
123123
}
124124
}
125125

@@ -137,7 +137,7 @@ func (a *app) doQuery(key base.Bytes) tmtypes.ResponseQuery {
137137
}
138138
}
139139

140-
func (a *app) doRangeQuery(key base.Bytes) tmtypes.ResponseQuery {
140+
func (a *app) doRangeQuery() tmtypes.ResponseQuery {
141141
deps, err := a.State().Deployment().GetMaxRange()
142142
if err != nil {
143143
return tmtypes.ResponseQuery{
@@ -160,9 +160,9 @@ func (a *app) doRangeQuery(key base.Bytes) tmtypes.ResponseQuery {
160160
}
161161
}
162162

163-
func (a *app) doDeploymentGroupQuery(key base.Bytes) tmtypes.ResponseQuery {
163+
func (a *app) doDeploymentGroupQuery(key keys.DeploymentGroup) tmtypes.ResponseQuery {
164164

165-
dep, err := a.State().DeploymentGroup().GetByKey(key)
165+
dep, err := a.State().DeploymentGroup().Get(key.ID())
166166

167167
if err != nil {
168168
return tmtypes.ResponseQuery{
@@ -174,7 +174,7 @@ func (a *app) doDeploymentGroupQuery(key base.Bytes) tmtypes.ResponseQuery {
174174
if dep == nil {
175175
return tmtypes.ResponseQuery{
176176
Code: code.NOT_FOUND,
177-
Log: fmt.Sprintf("deployment group %x not found", key),
177+
Log: fmt.Sprintf("deployment group %v not found", key.Path()),
178178
}
179179
}
180180

@@ -296,8 +296,10 @@ func (a *app) doDeliverCreateTx(ctx apptypes.Context, tx *types.TxCreateDeployme
296296

297297
for _, group := range groups {
298298
g := &types.DeploymentGroup{
299-
Deployment: deployment.Address,
300-
Seq: seq.Advance(),
299+
DeploymentGroupID: types.DeploymentGroupID{
300+
Deployment: deployment.Address,
301+
Seq: seq.Advance(),
302+
},
301303
State: types.DeploymentGroup_OPEN,
302304
Requirements: group.Requirements,
303305
Resources: group.Resources,

app/deployment/app_test.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ovrclk/akash/app/order"
1111
"github.com/ovrclk/akash/app/provider"
1212
apptypes "github.com/ovrclk/akash/app/types"
13+
"github.com/ovrclk/akash/keys"
1314
"github.com/ovrclk/akash/query"
1415
pstate "github.com/ovrclk/akash/state"
1516
"github.com/ovrclk/akash/testutil"
@@ -62,7 +63,7 @@ func TestCreateTx(t *testing.T) {
6263
}
6364

6465
{
65-
path := query.DeploymentGroupPath(depl.Address, groupseq)
66+
path := query.DeploymentGroupPath(groups.Items[0].DeploymentGroupID)
6667
resp := app.Query(tmtypes.RequestQuery{Path: path})
6768
assert.Empty(t, resp.Log)
6869
require.True(t, resp.IsOK())
@@ -81,24 +82,31 @@ func TestCreateTx(t *testing.T) {
8182
require.True(t, resp.IsOK())
8283
}
8384

85+
badgroup := types.DeploymentGroupID{
86+
Deployment: depl.Address,
87+
Seq: 2,
88+
}
89+
90+
goodgroup := groups.GetItems()[0].DeploymentGroupID
91+
8492
{
85-
path := fmt.Sprintf("%v%x",
93+
path := fmt.Sprintf("%v%v",
8694
pstate.DeploymentPath,
87-
pstate.DeploymentGroupID(depl.Address, 1))
95+
keys.DeploymentGroupID(badgroup).Path())
8896
resp := app.Query(tmtypes.RequestQuery{Path: path})
8997
assert.NotEmpty(t, resp.Log)
9098
require.False(t, resp.IsOK())
9199
}
92100

93101
{
94-
path := query.DeploymentGroupPath(depl.Address, 1)
102+
path := query.DeploymentGroupPath(goodgroup)
95103
resp := app.Query(tmtypes.RequestQuery{Path: path})
96104
assert.Empty(t, resp.Log)
97105
require.True(t, resp.IsOK())
98106
}
99107

100108
{
101-
path := query.DeploymentGroupPath(depl.Address, 0)
109+
path := query.DeploymentGroupPath(badgroup)
102110
resp := app.Query(tmtypes.RequestQuery{Path: path})
103111
assert.NotEmpty(t, resp.Log)
104112
require.False(t, resp.IsOK())
@@ -129,20 +137,21 @@ func TestTx_BadTxType(t *testing.T) {
129137
}
130138

131139
func TestCloseTx_1(t *testing.T) {
132-
const gseq = 1
133140
state := testutil.NewState(t, nil)
134141
app, err := deployment.NewApp(state, testutil.Logger())
135142
require.NoError(t, err)
136143
account, key := testutil.CreateAccount(t, state)
137144
nonce := uint64(1)
138145

139-
depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
146+
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)
147+
148+
group := groups.Items[0]
140149

141150
check := func(
142151
dstate types.Deployment_DeploymentState,
143152
gstate types.DeploymentGroup_DeploymentGroupState) {
144153
assertDeploymentState(t, app, depl.Address, dstate)
145-
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
154+
assertDeploymentGroupState(t, app, group.DeploymentGroupID, gstate)
146155
}
147156

148157
check(types.Deployment_ACTIVE, types.DeploymentGroup_OPEN)
@@ -155,7 +164,6 @@ func TestCloseTx_1(t *testing.T) {
155164
func TestCloseTx_2(t *testing.T) {
156165

157166
const (
158-
gseq = 1
159167
oseq = 3
160168
)
161169

@@ -165,20 +173,21 @@ func TestCloseTx_2(t *testing.T) {
165173
account, key := testutil.CreateAccount(t, state)
166174
nonce := uint64(1)
167175

168-
depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
176+
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)
177+
group := groups.Items[0]
169178

170179
oapp, err := order.NewApp(state, testutil.Logger())
171180
require.NoError(t, err)
172181

173-
testutil.CreateOrder(t, oapp, account, key, depl.Address, gseq, oseq)
182+
order := testutil.CreateOrder(t, oapp, account, key, depl.Address, group.Seq, oseq)
174183

175184
check := func(
176185
dstate types.Deployment_DeploymentState,
177186
gstate types.DeploymentGroup_DeploymentGroupState,
178187
ostate types.Order_OrderState) {
179188
assertDeploymentState(t, app, depl.Address, dstate)
180-
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
181-
assertOrderState(t, oapp, depl.Address, gseq, oseq, ostate)
189+
assertDeploymentGroupState(t, app, order.GroupID(), gstate)
190+
assertOrderState(t, oapp, order.OrderID, ostate)
182191
}
183192

184193
check(types.Deployment_ACTIVE, types.DeploymentGroup_OPEN, types.Order_OPEN)
@@ -191,7 +200,6 @@ func TestCloseTx_2(t *testing.T) {
191200
func TestCloseTx_3(t *testing.T) {
192201

193202
const (
194-
gseq = 1
195203
oseq = 3
196204
price = 0
197205
)
@@ -201,27 +209,28 @@ func TestCloseTx_3(t *testing.T) {
201209
require.NoError(t, err)
202210
account, key := testutil.CreateAccount(t, state)
203211
nonce := uint64(1)
204-
depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
212+
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)
213+
group := groups.Items[0]
205214

206215
orderapp, err := order.NewApp(state, testutil.Logger())
207216
require.NoError(t, err)
208-
testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)
217+
order := testutil.CreateOrder(t, orderapp, account, key, depl.Address, group.Seq, oseq)
209218

210219
providerapp, err := provider.NewApp(state, testutil.Logger())
211220
prov := testutil.CreateProvider(t, providerapp, account, key, nonce)
212221

213222
fulfillmentapp, err := fulfillment.NewApp(state, testutil.Logger())
214-
testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, gseq, oseq, price)
223+
fulfillment := testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, group.Seq, order.Seq, price)
215224

216225
check := func(
217226
dstate types.Deployment_DeploymentState,
218227
gstate types.DeploymentGroup_DeploymentGroupState,
219228
ostate types.Order_OrderState,
220229
fstate types.Fulfillment_FulfillmentState) {
221230
assertDeploymentState(t, app, depl.Address, dstate)
222-
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
223-
assertOrderState(t, orderapp, depl.Address, gseq, oseq, ostate)
224-
assertFulfillmentState(t, fulfillmentapp, depl.Address, gseq, oseq, prov.Address, fstate)
231+
assertDeploymentGroupState(t, app, group.DeploymentGroupID, gstate)
232+
assertOrderState(t, orderapp, order.OrderID, ostate)
233+
assertFulfillmentState(t, fulfillmentapp, fulfillment.FulfillmentID, fstate)
225234
}
226235

227236
check(types.Deployment_ACTIVE, types.DeploymentGroup_OPEN, types.Order_OPEN, types.Fulfillment_OPEN)
@@ -248,16 +257,16 @@ func TestCloseTx_4(t *testing.T) {
248257

249258
orderapp, err := order.NewApp(state, testutil.Logger())
250259
require.NoError(t, err)
251-
testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)
260+
order := testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)
252261

253262
providerapp, err := provider.NewApp(state, testutil.Logger())
254263
prov := testutil.CreateProvider(t, providerapp, account, key, nonce)
255264

256265
fulfillmentapp, err := fulfillment.NewApp(state, testutil.Logger())
257-
testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, gseq, oseq, price)
266+
fulfillment := testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, gseq, oseq, price)
258267

259268
leaseapp, err := lease.NewApp(state, testutil.Logger())
260-
testutil.CreateLease(t, leaseapp, prov.Address, key, depl.Address, gseq, oseq, price)
269+
lease := testutil.CreateLease(t, leaseapp, prov.Address, key, depl.Address, gseq, oseq, price)
261270

262271
check := func(
263272
dstate types.Deployment_DeploymentState,
@@ -266,10 +275,10 @@ func TestCloseTx_4(t *testing.T) {
266275
fstate types.Fulfillment_FulfillmentState,
267276
lstate types.Lease_LeaseState) {
268277
assertDeploymentState(t, app, depl.Address, dstate)
269-
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
270-
assertOrderState(t, orderapp, depl.Address, gseq, oseq, ostate)
271-
assertFulfillmentState(t, fulfillmentapp, depl.Address, gseq, oseq, prov.Address, fstate)
272-
assertLeaseState(t, leaseapp, depl.Address, gseq, oseq, prov.Address, lstate)
278+
assertDeploymentGroupState(t, app, order.GroupID(), gstate)
279+
assertOrderState(t, orderapp, order.OrderID, ostate)
280+
assertFulfillmentState(t, fulfillmentapp, fulfillment.FulfillmentID, fstate)
281+
assertLeaseState(t, leaseapp, lease.LeaseID, lstate)
273282
}
274283

275284
check(types.Deployment_ACTIVE, types.DeploymentGroup_OPEN, types.Order_MATCHED, types.Fulfillment_OPEN, types.Lease_ACTIVE)
@@ -301,11 +310,10 @@ func assertDeploymentState(
301310
func assertDeploymentGroupState(
302311
t *testing.T,
303312
app apptypes.Application,
304-
daddr []byte,
305-
gseq uint64,
313+
id types.DeploymentGroupID,
306314
gstate types.DeploymentGroup_DeploymentGroupState) {
307315

308-
path := query.DeploymentGroupPath(daddr, gseq)
316+
path := query.DeploymentGroupPath(id)
309317
resp := app.Query(tmtypes.RequestQuery{Path: path})
310318
assert.Empty(t, resp.Log)
311319
require.True(t, resp.IsOK())
@@ -319,12 +327,10 @@ func assertDeploymentGroupState(
319327
func assertOrderState(
320328
t *testing.T,
321329
app apptypes.Application,
322-
daddr []byte,
323-
gseq uint64,
324-
oseq uint64,
330+
id types.OrderID,
325331
ostate types.Order_OrderState) {
326332

327-
path := query.OrderPath(daddr, gseq, oseq)
333+
path := query.OrderPath(id)
328334
resp := app.Query(tmtypes.RequestQuery{Path: path})
329335
assert.Empty(t, resp.Log)
330336
require.True(t, resp.IsOK())
@@ -337,13 +343,10 @@ func assertOrderState(
337343
func assertFulfillmentState(
338344
t *testing.T,
339345
app apptypes.Application,
340-
daddr []byte,
341-
gseq uint64,
342-
oseq uint64,
343-
paddr []byte,
346+
id types.FulfillmentID,
344347
state types.Fulfillment_FulfillmentState) {
345348

346-
path := query.FulfillmentPath(daddr, gseq, oseq, paddr)
349+
path := query.FulfillmentPath(id)
347350
resp := app.Query(tmtypes.RequestQuery{Path: path})
348351
assert.Empty(t, resp.Log)
349352
require.True(t, resp.IsOK())
@@ -356,14 +359,11 @@ func assertFulfillmentState(
356359
func assertLeaseState(
357360
t *testing.T,
358361
app apptypes.Application,
359-
daddr []byte,
360-
gseq uint64,
361-
oseq uint64,
362-
paddr []byte,
362+
id types.LeaseID,
363363
state types.Lease_LeaseState) {
364364

365365
// check fulfillment state
366-
path := query.LeasePath(daddr, gseq, oseq, paddr)
366+
path := query.LeasePath(id)
367367
resp := app.Query(tmtypes.RequestQuery{Path: path})
368368
assert.Empty(t, resp.Log)
369369
require.True(t, resp.IsOK())

0 commit comments

Comments
 (0)