Skip to content

Commit

Permalink
Denest tx (#175)
Browse files Browse the repository at this point in the history
fixes #164

* TxCreateOrder

* TxCreateFulfillment

* TxCreateLease

* remove unused messages

* cleanup
  • Loading branch information
aastein authored Apr 3, 2018
1 parent c5d659c commit baa56c6
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 442 deletions.
34 changes: 26 additions & 8 deletions app/fulfillment/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,23 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
}

func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxCreateFulfillment) tmtypes.ResponseCheckTx {
fulfillment := tx.GetFulfillment()

if fulfillment == nil {
return tmtypes.ResponseCheckTx{Code: code.INVALID_TRANSACTION}
if tx.Deployment == nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Log: "Empty deployment",
}
}

if tx.Provider == nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Log: "Empty provider",
}
}

// lookup provider
provider, err := a.State().Provider().Get(fulfillment.Provider)
provider, err := a.State().Provider().Get(tx.Provider)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand Down Expand Up @@ -133,7 +142,7 @@ func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxCreateFulfillment) tmt
}

// ensure order exists
dorder, err := a.State().Order().Get(fulfillment.Deployment, fulfillment.Group, fulfillment.Order)
dorder, err := a.State().Order().Get(tx.Deployment, tx.Group, tx.Order)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand All @@ -156,7 +165,7 @@ func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxCreateFulfillment) tmt
}

// get deployment group
group, err := a.State().DeploymentGroup().Get(fulfillment.Deployment, fulfillment.Group)
group, err := a.State().DeploymentGroup().Get(tx.Deployment, tx.Group)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand All @@ -181,7 +190,7 @@ func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxCreateFulfillment) tmt
}

// ensure there are no other orders for this provider
other, err := a.State().Fulfillment().Get(fulfillment.Deployment, fulfillment.Group, fulfillment.Order, fulfillment.Provider)
other, err := a.State().Fulfillment().Get(tx.Deployment, tx.Group, tx.Order, tx.Provider)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand All @@ -207,7 +216,16 @@ func (a *app) doDeliverTx(ctx apptypes.Context, tx *types.TxCreateFulfillment) t
}
}

if err := a.State().Fulfillment().Save(tx.GetFulfillment()); err != nil {
fulfillment := &types.Fulfillment{
Deployment: tx.Deployment,
Group: tx.Group,
Order: tx.Order,
Provider: tx.Provider,
Price: tx.Price,
State: types.Fulfillment_OPEN,
}

if err := a.State().Fulfillment().Save(fulfillment); err != nil {
return tmtypes.ResponseDeliverTx{
Code: code.INVALID_TRANSACTION,
Log: err.Error(),
Expand Down
32 changes: 24 additions & 8 deletions app/lease/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,22 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
}

func (a *app) doCheckCreateTx(ctx apptypes.Context, tx *types.TxCreateLease) (tmtypes.ResponseCheckTx, *types.Order) {
lease := tx.GetLease()
if tx.Deployment == nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Log: "Empty deployment",
}, nil
}

if lease == nil {
return tmtypes.ResponseCheckTx{Code: code.INVALID_TRANSACTION}, nil
if tx.Provider == nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Log: "Empty provider",
}, nil
}

// lookup provider
provider, err := a.State().Provider().Get(lease.Provider)
provider, err := a.State().Provider().Get(tx.Provider)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand Down Expand Up @@ -138,7 +146,7 @@ func (a *app) doCheckCreateTx(ctx apptypes.Context, tx *types.TxCreateLease) (tm
}

// ensure order exists
order, err := a.State().Order().Get(lease.Deployment, lease.Group, lease.Order)
order, err := a.State().Order().Get(tx.Deployment, tx.Group, tx.Order)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand All @@ -161,7 +169,7 @@ func (a *app) doCheckCreateTx(ctx apptypes.Context, tx *types.TxCreateLease) (tm
}

// ensure fulfillment exists
fulfillment, err := a.State().Fulfillment().Get(lease.Deployment, lease.Group, lease.Order, lease.Provider)
fulfillment, err := a.State().Fulfillment().Get(tx.Deployment, tx.Group, tx.Order, tx.Provider)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand Down Expand Up @@ -208,7 +216,16 @@ func (a *app) doDeliverCreateTx(ctx apptypes.Context, tx *types.TxCreateLease) t
}
}

if err := a.State().Lease().Save(tx.GetLease()); err != nil {
lease := &types.Lease{
Deployment: tx.Deployment,
Group: tx.Group,
Order: tx.Order,
Provider: tx.Provider,
Price: tx.Price,
State: types.Lease_ACTIVE,
}

if err := a.State().Lease().Save(lease); err != nil {
return tmtypes.ResponseDeliverTx{
Code: code.INVALID_TRANSACTION,
Log: err.Error(),
Expand All @@ -223,7 +240,6 @@ func (a *app) doDeliverCreateTx(ctx apptypes.Context, tx *types.TxCreateLease) t
}
}

lease := tx.GetLease()
tags := apptypes.NewTags(a.Name(), apptypes.TxTypeCreateLease)
tags = append(tags, tmcommon.KVPair{Key: []byte(apptypes.TagNameDeployment), Value: lease.Deployment})
tags = append(tags, tmcommon.KVPair{Key: []byte(apptypes.TagNameLease), Value: state.IDForLease(lease)})
Expand Down
23 changes: 9 additions & 14 deletions app/market/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,10 @@ func (e engine) processDeployment(state state.State, w txBuffer, deployment type
// if no active order for the group emit create tx
if !activeFound {
w.put(&types.TxCreateOrder{
Order: &types.Order{
Deployment: deployment.Address,
Group: group.GetSeq(),
Order: nextSeq,
State: types.Order_OPEN,
EndAt: group.OrderTTL + height,
},
Deployment: deployment.Address,
Group: group.GetSeq(),
Seq: nextSeq,
EndAt: group.OrderTTL + height,
})
nextSeq++
}
Expand Down Expand Up @@ -194,13 +191,11 @@ func (e engine) processOrder(state state.State, w txBuffer, order *types.Order)
}

w.put(&types.TxCreateLease{
Lease: &types.Lease{
Deployment: fulfillment.Deployment,
Group: fulfillment.Group,
Order: fulfillment.Order,
Provider: fulfillment.Provider,
Price: fulfillment.Price,
},
Deployment: fulfillment.Deployment,
Group: fulfillment.Group,
Order: fulfillment.Order,
Provider: fulfillment.Provider,
Price: fulfillment.Price,
})

return nil
Expand Down
42 changes: 27 additions & 15 deletions app/market/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestEngine_All(t *testing.T) {
groups := testutil.DeploymentGroups(deployment.Address, tenant.Nonce)
require.NoError(t, state_.Deployment().Save(deployment))

state_, tx := testCreateOrder(t, state_, tenant, deployment, groups)
state_ = testCreateLease(t, state_, provider, deployment, groups, tx)
state_, order := testCreateOrder(t, state_, tenant, deployment, groups)
state_ = testCreateLease(t, state_, provider, deployment, groups, order)
state_ = testCloseDeployment(t, state_, tenant.Address, deployment.Address)
}

func testCreateOrder(t *testing.T, state state.State, tenant *types.Account, deployment *types.Deployment, groups *types.DeploymentGroups) (state.State, *types.TxCreateOrder) {
func testCreateOrder(t *testing.T, state state.State, tenant *types.Account, deployment *types.Deployment, groups *types.DeploymentGroups) (state.State, *types.Order) {
for _, group := range groups.GetItems() {
require.NoError(t, state.DeploymentGroup().Save(group))
}
Expand All @@ -42,16 +42,22 @@ func testCreateOrder(t *testing.T, state state.State, tenant *types.Account, dep
tx, ok := txs[0].(*types.TxCreateOrder)
require.True(t, ok)

require.Equal(t, deployment.Address, tx.Order.Deployment)
require.Equal(t, groups.GetItems()[0].Seq, tx.Order.GetGroup())
require.Equal(t, types.Order_OPEN, tx.Order.GetState())
require.NoError(t, state.Order().Save(tx.Order))
require.Equal(t, deployment.Address, tx.Deployment)
require.Equal(t, groups.GetItems()[0].Seq, tx.Group)
order := &types.Order{
Deployment: tx.Deployment,
Group: tx.Group,
Seq: tx.Seq,
EndAt: tx.EndAt,
State: types.Order_OPEN,
}
require.NoError(t, state.Order().Save(order))

return state, tx
return state, order
}

func testCreateLease(t *testing.T, state state.State, provider *types.Provider, deployment *types.Deployment, groups *types.DeploymentGroups, tx *types.TxCreateOrder) state.State {
fulfillment := testutil.Fulfillment(provider.Address, deployment.Address, tx.Order.GetGroup(), tx.Order.GetOrder(), 1)
func testCreateLease(t *testing.T, state state.State, provider *types.Provider, deployment *types.Deployment, groups *types.DeploymentGroups, order *types.Order) state.State {
fulfillment := testutil.Fulfillment(provider.Address, deployment.Address, order.Group, order.Seq, 1)
require.NoError(t, state.Fulfillment().Save(fulfillment))

for i := int64(0); i <= groups.GetItems()[0].OrderTTL; i++ {
Expand All @@ -63,13 +69,19 @@ func testCreateLease(t *testing.T, state state.State, provider *types.Provider,
require.Len(t, txs, 1)

leaseTx, ok := txs[0].(*types.TxCreateLease)
lease := &types.Lease{
Deployment: leaseTx.Deployment,
Group: leaseTx.Group,
Order: leaseTx.Order,
Provider: leaseTx.Provider,
Price: leaseTx.Price,
State: types.Lease_ACTIVE,
}
require.True(t, ok)
require.NoError(t, state.Lease().Save(leaseTx.GetLease()))
require.NoError(t, state.Lease().Save(leaseTx.GetLease()))
require.NoError(t, state.Lease().Save(lease))

matchedOrder := tx.GetOrder()
matchedOrder.State = types.Order_MATCHED
require.NoError(t, state.Order().Save(matchedOrder))
order.State = types.Order_MATCHED
require.NoError(t, state.Order().Save(order))

return state
}
Expand Down
4 changes: 1 addition & 3 deletions app/market/facilitator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func TestFacilitator(t *testing.T) {

daddr := state.DeploymentAddress(account.Address, nonce)
tx := &types.TxCreateOrder{
Order: &types.Order{
Deployment: daddr,
},
Deployment: daddr,
}

txs := []interface{}{tx}
Expand Down
22 changes: 14 additions & 8 deletions app/order/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ func (a *app) doCheckCreateTx(ctx apptypes.Context, tx *types.TxCreateOrder) tmt
// todo: ensure signed by last block creator / valid market facilitator

// ensure order provided
order := tx.Order
if order == nil {
if tx.Deployment == nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Log: "No order specified",
Log: "No deployment specified",
}
}

// ensure deployment exists
deployment, err := a.State().Deployment().Get(order.Deployment)
deployment, err := a.State().Deployment().Get(tx.Deployment)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.INVALID_TRANSACTION,
Expand All @@ -179,7 +178,7 @@ func (a *app) doCheckCreateTx(ctx apptypes.Context, tx *types.TxCreateOrder) tmt
}

// ensure deployment group exists
group, err := a.State().DeploymentGroup().Get(order.Deployment, order.Group)
group, err := a.State().DeploymentGroup().Get(tx.Deployment, tx.Group)
if err != nil {
return tmtypes.ResponseCheckTx{
Code: code.ERROR,
Expand Down Expand Up @@ -232,10 +231,17 @@ func (a *app) doDeliverCreateTx(ctx apptypes.Context, tx *types.TxCreateOrder) t
}
}

order := tx.Order

oseq := a.State().Deployment().SequenceFor(order.Deployment)
oseq := a.State().Deployment().SequenceFor(tx.Deployment)
oseq.Advance()

order := &types.Order{
Deployment: tx.Deployment,
Group: tx.Group,
Seq: tx.Seq,
EndAt: tx.EndAt,
State: types.Order_OPEN,
}

// order.Order = oseq.Advance()
order.State = types.Order_OPEN

Expand Down
14 changes: 6 additions & 8 deletions cmd/akash/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,15 @@ func createDeployment(ctx context.Context, cmd *cobra.Command, args []string) er
expected := len(groups)
handler := marketplace.NewBuilder().
OnTxCreateFulfillment(func(tx *types.TxCreateFulfillment) {
if bytes.Equal(tx.Fulfillment.Deployment, address) {
f := tx.Fulfillment
fmt.Printf("Group %v/%v Fulfillment: %v\n", f.Group, len(groups),
X(state.FulfillmentID(f.Deployment, f.Group, f.Order, f.Provider)))
if bytes.Equal(tx.Deployment, address) {
fmt.Printf("Group %v/%v Fulfillment: %v\n", tx.Group, len(groups),
X(state.FulfillmentID(tx.Deployment, tx.Group, tx.Order, tx.Provider)))
}
}).
OnTxCreateLease(func(tx *types.TxCreateLease) {
if bytes.Equal(tx.Lease.Deployment, address) {
l := tx.Lease
fmt.Printf("Group %v/%v Lease: %v\n", l.Group, len(groups),
X(state.FulfillmentID(l.Deployment, l.Group, l.Order, l.Provider)))
if bytes.Equal(tx.Deployment, address) {
fmt.Printf("Group %v/%v Lease: %v\n", tx.Group, len(groups),
X(state.FulfillmentID(tx.Deployment, tx.Group, tx.Order, tx.Provider)))
expected--
}
if expected == 0 {
Expand Down
10 changes: 5 additions & 5 deletions cmd/akash/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ func marketplaceMonitorHandler() marketplace.Handler {
}).
OnTxCreateOrder(func(tx *types.TxCreateOrder) {
fmt.Printf("ORDER CREATED\t%v/%v/%v\n",
X(tx.Order.Deployment), tx.Order.Group, tx.Order.Order)
X(tx.Deployment), tx.Group, tx.Seq)
}).
OnTxCreateFulfillment(func(tx *types.TxCreateFulfillment) {
fmt.Printf("FULFILLMENT CREATED\t%v/%v/%v by %v [price=%v]\n",
X(tx.Fulfillment.Deployment), tx.Fulfillment.Group, tx.Fulfillment.Order,
X(tx.Fulfillment.Provider), tx.Fulfillment.Price)
X(tx.Deployment), tx.Group, tx.Order,
X(tx.Provider), tx.Price)
}).
OnTxCreateLease(func(tx *types.TxCreateLease) {
fmt.Printf("LEASE CREATED\t%v/%v/%v by %x [price=%v]\n",
X(tx.Lease.Deployment), tx.Lease.Group, tx.Lease.Order,
X(tx.Lease.Provider), tx.Lease.Price)
X(tx.Deployment), tx.Group, tx.Order,
X(tx.Provider), tx.Price)
}).
OnTxCloseDeployment(func(tx *types.TxCloseDeployment) {
fmt.Printf("DEPLOYMENT CLOSED\t%v", X(tx.Deployment))
Expand Down
Loading

0 comments on commit baa56c6

Please sign in to comment.