Skip to content

Commit

Permalink
Leases: transfer tokens
Browse files Browse the repository at this point in the history
fixes #235
  • Loading branch information
boz committed Jun 12, 2018
1 parent 2a7e6c6 commit e4d5088
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
26 changes: 18 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
type Application interface {
tmtypes.Application
ActivateMarket(market.Actor, *tmtmtypes.EventBus) error

App(name string) apptypes.Application
}

type app struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
}
Expand All @@ -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}
}

Expand Down
85 changes: 84 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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},
},
})

Expand Down Expand Up @@ -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)

}
9 changes: 8 additions & 1 deletion testutil/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e4d5088

Please sign in to comment.