Skip to content

Commit

Permalink
Merge pull request #193 from ovrclk/keyid
Browse files Browse the repository at this point in the history
types: composite keys initial utils
  • Loading branch information
nalesandro authored May 21, 2018
2 parents ea06b99 + 6dfb20e commit 0ddc681
Show file tree
Hide file tree
Showing 32 changed files with 1,573 additions and 1,051 deletions.
4 changes: 2 additions & 2 deletions _integration/cmp/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func deployQueryState(daddr vars.Ref, state types.Deployment_DeploymentState) ge

func orderQuery(daddr vars.Ref) gestalt.Component {
parse := js.Do(
js.Str(daddr.Var(), "items", "[0]", "deployment"),
js.Str(daddr.Var(), "items", "[0]", "id", "deployment"),
)

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

func leaseQuery(daddr vars.Ref) gestalt.Component {
parse := js.Do(
js.Str(daddr.Var(), "items", "[0]", "deployment"),
js.Str(daddr.Var(), "items", "[0]", "id", "deployment"),
)

return akash("lease-query", "query", "lease").
Expand Down
38 changes: 20 additions & 18 deletions app/deployment/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/gogo/protobuf/proto"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/ovrclk/akash/types/code"
tmtypes "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
Expand Down Expand Up @@ -39,10 +39,11 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
}
}

// todo: need abtraction for multiple query types per app
// TODO: Partial Key Parsing

if strings.HasPrefix(req.GetPath(), state.DeploymentGroupPath) {
id := strings.TrimPrefix(req.Path, state.DeploymentGroupPath)
key, err := base.DecodeString(id)
key, err := keys.ParseGroupPath(id)
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Expand All @@ -52,20 +53,19 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
return a.doDeploymentGroupQuery(key)
}

// todo: abstractiion: all queries should have this
id := strings.TrimPrefix(req.Path, state.DeploymentPath)
key, err := base.DecodeString(id)
if len(id) == 0 {
return a.doRangeQuery()
}

key, err := keys.ParseDeploymentPath(id)
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Log: err.Error(),
}
}

// id is empty string, get full range
if len(id) == 0 {
return a.doRangeQuery(key)
}
return a.doQuery(key)
}

Expand Down Expand Up @@ -105,9 +105,9 @@ func (a *app) DeliverTx(ctx apptypes.Context, tx interface{}) tmtypes.ResponseDe
}
}

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

dep, err := a.State().Deployment().Get(key)
dep, err := a.State().Deployment().Get(key.ID())

if err != nil {
return tmtypes.ResponseQuery{
Expand All @@ -119,7 +119,7 @@ func (a *app) doQuery(key base.Bytes) tmtypes.ResponseQuery {
if dep == nil {
return tmtypes.ResponseQuery{
Code: code.NOT_FOUND,
Log: fmt.Sprintf("deployment %x not found", key),
Log: fmt.Sprintf("deployment %v not found", key.Path()),
}
}

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

func (a *app) doRangeQuery(key base.Bytes) tmtypes.ResponseQuery {
func (a *app) doRangeQuery() tmtypes.ResponseQuery {
deps, err := a.State().Deployment().GetMaxRange()
if err != nil {
return tmtypes.ResponseQuery{
Expand All @@ -160,9 +160,9 @@ func (a *app) doRangeQuery(key base.Bytes) tmtypes.ResponseQuery {
}
}

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

dep, err := a.State().DeploymentGroup().GetByKey(key)
dep, err := a.State().DeploymentGroup().Get(key.ID())

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

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

for _, group := range groups {
g := &types.DeploymentGroup{
Deployment: deployment.Address,
Seq: seq.Advance(),
DeploymentGroupID: types.DeploymentGroupID{
Deployment: deployment.Address,
Seq: seq.Advance(),
},
State: types.DeploymentGroup_OPEN,
Requirements: group.Requirements,
Resources: group.Resources,
Expand Down
88 changes: 44 additions & 44 deletions app/deployment/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ovrclk/akash/app/order"
"github.com/ovrclk/akash/app/provider"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/query"
pstate "github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/testutil"
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestCreateTx(t *testing.T) {
}

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

badgroup := types.DeploymentGroupID{
Deployment: depl.Address,
Seq: 2,
}

goodgroup := groups.GetItems()[0].DeploymentGroupID

{
path := fmt.Sprintf("%v%x",
path := fmt.Sprintf("%v%v",
pstate.DeploymentPath,
pstate.DeploymentGroupID(depl.Address, 1))
keys.DeploymentGroupID(badgroup).Path())
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.NotEmpty(t, resp.Log)
require.False(t, resp.IsOK())
}

{
path := query.DeploymentGroupPath(depl.Address, 1)
path := query.DeploymentGroupPath(goodgroup)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.Empty(t, resp.Log)
require.True(t, resp.IsOK())
}

{
path := query.DeploymentGroupPath(depl.Address, 0)
path := query.DeploymentGroupPath(badgroup)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.NotEmpty(t, resp.Log)
require.False(t, resp.IsOK())
Expand Down Expand Up @@ -129,20 +137,21 @@ func TestTx_BadTxType(t *testing.T) {
}

func TestCloseTx_1(t *testing.T) {
const gseq = 1
state := testutil.NewState(t, nil)
app, err := deployment.NewApp(state, testutil.Logger())
require.NoError(t, err)
account, key := testutil.CreateAccount(t, state)
nonce := uint64(1)

depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)

group := groups.Items[0]

check := func(
dstate types.Deployment_DeploymentState,
gstate types.DeploymentGroup_DeploymentGroupState) {
assertDeploymentState(t, app, depl.Address, dstate)
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
assertDeploymentGroupState(t, app, group.DeploymentGroupID, gstate)
}

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

const (
gseq = 1
oseq = 3
)

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

depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)
group := groups.Items[0]

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

testutil.CreateOrder(t, oapp, account, key, depl.Address, gseq, oseq)
order := testutil.CreateOrder(t, oapp, account, key, depl.Address, group.Seq, oseq)

check := func(
dstate types.Deployment_DeploymentState,
gstate types.DeploymentGroup_DeploymentGroupState,
ostate types.Order_OrderState) {
assertDeploymentState(t, app, depl.Address, dstate)
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
assertOrderState(t, oapp, depl.Address, gseq, oseq, ostate)
assertDeploymentGroupState(t, app, order.GroupID(), gstate)
assertOrderState(t, oapp, order.OrderID, ostate)
}

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

const (
gseq = 1
oseq = 3
price = 0
)
Expand All @@ -201,27 +209,28 @@ func TestCloseTx_3(t *testing.T) {
require.NoError(t, err)
account, key := testutil.CreateAccount(t, state)
nonce := uint64(1)
depl, _ := testutil.CreateDeployment(t, app, account, key, nonce)
depl, groups := testutil.CreateDeployment(t, app, account, key, nonce)
group := groups.Items[0]

orderapp, err := order.NewApp(state, testutil.Logger())
require.NoError(t, err)
testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)
order := testutil.CreateOrder(t, orderapp, account, key, depl.Address, group.Seq, oseq)

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

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

check := func(
dstate types.Deployment_DeploymentState,
gstate types.DeploymentGroup_DeploymentGroupState,
ostate types.Order_OrderState,
fstate types.Fulfillment_FulfillmentState) {
assertDeploymentState(t, app, depl.Address, dstate)
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
assertOrderState(t, orderapp, depl.Address, gseq, oseq, ostate)
assertFulfillmentState(t, fulfillmentapp, depl.Address, gseq, oseq, prov.Address, fstate)
assertDeploymentGroupState(t, app, group.DeploymentGroupID, gstate)
assertOrderState(t, orderapp, order.OrderID, ostate)
assertFulfillmentState(t, fulfillmentapp, fulfillment.FulfillmentID, fstate)
}

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

orderapp, err := order.NewApp(state, testutil.Logger())
require.NoError(t, err)
testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)
order := testutil.CreateOrder(t, orderapp, account, key, depl.Address, gseq, oseq)

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

fulfillmentapp, err := fulfillment.NewApp(state, testutil.Logger())
testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, gseq, oseq, price)
fulfillment := testutil.CreateFulfillment(t, fulfillmentapp, prov.Address, key, depl.Address, gseq, oseq, price)

leaseapp, err := lease.NewApp(state, testutil.Logger())
testutil.CreateLease(t, leaseapp, prov.Address, key, depl.Address, gseq, oseq, price)
lease := testutil.CreateLease(t, leaseapp, prov.Address, key, depl.Address, gseq, oseq, price)

check := func(
dstate types.Deployment_DeploymentState,
Expand All @@ -266,10 +275,10 @@ func TestCloseTx_4(t *testing.T) {
fstate types.Fulfillment_FulfillmentState,
lstate types.Lease_LeaseState) {
assertDeploymentState(t, app, depl.Address, dstate)
assertDeploymentGroupState(t, app, depl.Address, gseq, gstate)
assertOrderState(t, orderapp, depl.Address, gseq, oseq, ostate)
assertFulfillmentState(t, fulfillmentapp, depl.Address, gseq, oseq, prov.Address, fstate)
assertLeaseState(t, leaseapp, depl.Address, gseq, oseq, prov.Address, lstate)
assertDeploymentGroupState(t, app, order.GroupID(), gstate)
assertOrderState(t, orderapp, order.OrderID, ostate)
assertFulfillmentState(t, fulfillmentapp, fulfillment.FulfillmentID, fstate)
assertLeaseState(t, leaseapp, lease.LeaseID, lstate)
}

check(types.Deployment_ACTIVE, types.DeploymentGroup_OPEN, types.Order_MATCHED, types.Fulfillment_OPEN, types.Lease_ACTIVE)
Expand Down Expand Up @@ -301,11 +310,10 @@ func assertDeploymentState(
func assertDeploymentGroupState(
t *testing.T,
app apptypes.Application,
daddr []byte,
gseq uint64,
id types.DeploymentGroupID,
gstate types.DeploymentGroup_DeploymentGroupState) {

path := query.DeploymentGroupPath(daddr, gseq)
path := query.DeploymentGroupPath(id)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.Empty(t, resp.Log)
require.True(t, resp.IsOK())
Expand All @@ -319,12 +327,10 @@ func assertDeploymentGroupState(
func assertOrderState(
t *testing.T,
app apptypes.Application,
daddr []byte,
gseq uint64,
oseq uint64,
id types.OrderID,
ostate types.Order_OrderState) {

path := query.OrderPath(daddr, gseq, oseq)
path := query.OrderPath(id)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.Empty(t, resp.Log)
require.True(t, resp.IsOK())
Expand All @@ -337,13 +343,10 @@ func assertOrderState(
func assertFulfillmentState(
t *testing.T,
app apptypes.Application,
daddr []byte,
gseq uint64,
oseq uint64,
paddr []byte,
id types.FulfillmentID,
state types.Fulfillment_FulfillmentState) {

path := query.FulfillmentPath(daddr, gseq, oseq, paddr)
path := query.FulfillmentPath(id)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.Empty(t, resp.Log)
require.True(t, resp.IsOK())
Expand All @@ -356,14 +359,11 @@ func assertFulfillmentState(
func assertLeaseState(
t *testing.T,
app apptypes.Application,
daddr []byte,
gseq uint64,
oseq uint64,
paddr []byte,
id types.LeaseID,
state types.Lease_LeaseState) {

// check fulfillment state
path := query.LeasePath(daddr, gseq, oseq, paddr)
path := query.LeasePath(id)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.Empty(t, resp.Log)
require.True(t, resp.IsOK())
Expand Down
Loading

0 comments on commit 0ddc681

Please sign in to comment.