diff --git a/app/app.go b/app/app.go index 97a9be44de..903734ba43 100644 --- a/app/app.go +++ b/app/app.go @@ -29,6 +29,8 @@ import ( type Application interface { tmtypes.Application ActivateMarket(market.Actor, *tmtmtypes.EventBus) error + + App(name string) apptypes.Application } type app struct { @@ -107,6 +109,15 @@ func Create(commitState appstate.CommitState, cacheState appstate.CacheState, lo return &app{commitState: commitState, cacheState: cacheState, apps: apps, log: logger}, nil } +func (app *app) App(name string) apptypes.Application { + for _, _app := range app.apps { + if _app.Name() == name { + return _app + } + } + return nil +} + func (app *app) ActivateMarket(actor market.Actor, bus *tmtmtypes.EventBus) error { if app.mfacilitator != nil { @@ -241,12 +252,15 @@ func (app *app) EndBlock(req tmtypes.RequestEndBlock) tmtypes.ResponseEndBlock { func (app *app) Commit() tmtypes.ResponseCommit { app.trace("Commit") - err := app.cacheState.Write() - if err != nil { - app.log.Error("error when writing to cache") + if err := lease.ProcessLeases(app.cacheState); err != nil { + app.log.Error("processing leases", "error", err) + } + + if err := app.cacheState.Write(); err != nil { + panic("error when writing to cache") } - data, _, err := app.commitState.Commit() + data, _, err := app.commitState.Commit() if err != nil { return tmtypes.ResponseCommit{Data: data} } @@ -258,10 +272,6 @@ func (app *app) Commit() tmtypes.ResponseCommit { } } - if err = lease.ProcessLeases(app.commitState); err != nil { - app.log.Error("processing leases", "error", err) - } - return tmtypes.ResponseCommit{Data: data} } diff --git a/app/app_test.go b/app/app_test.go index ed923e888d..8bbe57b4a9 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -4,10 +4,16 @@ import ( "testing" app_ "github.com/ovrclk/akash/app" + dapp_ "github.com/ovrclk/akash/app/deployment" + fapp_ "github.com/ovrclk/akash/app/fulfillment" + lapp_ "github.com/ovrclk/akash/app/lease" + oapp_ "github.com/ovrclk/akash/app/order" + papp_ "github.com/ovrclk/akash/app/provider" "github.com/ovrclk/akash/testutil" "github.com/ovrclk/akash/txutil" "github.com/ovrclk/akash/types" "github.com/ovrclk/akash/types/code" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -25,7 +31,7 @@ func TestApp(t *testing.T) { commitState, cacheState := testutil.NewState(t, &types.Genesis{ Accounts: []types.Account{ - types.Account{Address: addrfrom, Balance: balance, Nonce: nonce}, + {Address: addrfrom, Balance: balance, Nonce: nonce}, }, }) @@ -89,3 +95,80 @@ func TestApp(t *testing.T) { } } + +func TestLeaseTransfer(t *testing.T) { + const ( + price = 10 + balance = 100000 + ) + nonce := uint64(1) + + _, keyfrom := testutil.PrivateKeySigner(t) + addrfrom := keyfrom.PubKey().Address().Bytes() + + keyto := testutil.PrivateKey(t) + addrto := keyto.PubKey().Address().Bytes() + + commitState, cacheState := testutil.NewState(t, &types.Genesis{ + Accounts: []types.Account{ + {Address: addrfrom, Balance: balance, Nonce: nonce}, + {Address: addrto, Balance: 0, Nonce: nonce}, + }, + }) + + tacct, err := cacheState.Account().Get(addrfrom) + require.NoError(t, err) + + pacct, err := cacheState.Account().Get(addrto) + require.NoError(t, err) + + nonce++ + + app, err := app_.Create(commitState, cacheState, testutil.Logger()) + require.NoError(t, err) + + dapp := app.App(dapp_.Name) + require.NotNil(t, dapp) + + oapp := app.App(oapp_.Name) + require.NotNil(t, oapp) + + lapp := app.App(lapp_.Name) + require.NotNil(t, lapp) + + papp := app.App(papp_.Name) + require.NotNil(t, papp) + + fapp := app.App(fapp_.Name) + require.NotNil(t, fapp) + + provider := testutil.CreateProvider(t, cacheState, papp, pacct, keyto, nonce) + + deployment, groups := testutil.CreateDeployment(t, cacheState, dapp, tacct, keyfrom, nonce) + group := groups.Items[0] + + order := testutil.CreateOrder(t, cacheState, oapp, tacct, keyfrom, deployment.Address, group.Seq, group.Seq) + testutil.CreateFulfillment(t, cacheState, fapp, provider.Address, keyto, deployment.Address, group.Seq, order.Seq, price) + lease := testutil.CreateLease(t, cacheState, lapp, provider.Address, keyto, deployment.Address, group.Seq, order.Seq, price) + + app.Commit() + + pacct, err = commitState.Account().Get(addrto) + require.NoError(t, err) + assert.Equal(t, uint64(lease.Price), pacct.Balance) + + tacct, err = commitState.Account().Get(addrfrom) + require.NoError(t, err) + assert.Equal(t, uint64(balance-lease.Price), tacct.Balance) + + app.Commit() + + pacct, err = commitState.Account().Get(addrto) + require.NoError(t, err) + assert.Equal(t, uint64(lease.Price)*2, pacct.Balance) + + tacct, err = commitState.Account().Get(addrfrom) + require.NoError(t, err) + assert.Equal(t, uint64(balance-lease.Price*2), tacct.Balance) + +} diff --git a/testutil/deployment.go b/testutil/deployment.go index adef29a7a1..3ef0360327 100644 --- a/testutil/deployment.go +++ b/testutil/deployment.go @@ -58,7 +58,14 @@ func CreateDeployment(t *testing.T, st state.State, app apptypes.Application, ac dresp := app.DeliverTx(st, ctx, deploymenttx) assert.Len(t, dresp.Log, 0, fmt.Sprint("Log should be empty but is: ", dresp.Log)) assert.True(t, dresp.IsOK()) - return deployment, groups + + deployment, err := st.Deployment().Get(deployment.Address) + assert.NoError(t, err) + + dgroups, err := st.DeploymentGroup().ForDeployment(deployment.Address) + assert.NoError(t, err) + + return deployment, &types.DeploymentGroups{Items: dgroups} } func CloseDeployment(t *testing.T, st state.State, app apptypes.Application, deployment *base.Bytes, key crypto.PrivKey) {