Skip to content

Commit a457a54

Browse files
authored
State cache
fixes: #67 * the app containing the sub-apps has all app state. * state is passed to the sub-apps * refactored state into CommitState(for checkTX), and CacheState(for deliverTx) * removed proof queries
1 parent 643a701 commit a457a54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1096
-882
lines changed

app/account/app.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/gogo/protobuf/proto"
99
apptypes "github.com/ovrclk/akash/app/types"
1010
"github.com/ovrclk/akash/keys"
11-
"github.com/ovrclk/akash/state"
11+
appstate "github.com/ovrclk/akash/state"
1212
"github.com/ovrclk/akash/types"
1313
"github.com/ovrclk/akash/types/code"
1414
tmtypes "github.com/tendermint/abci/types"
@@ -23,23 +23,23 @@ type app struct {
2323
*apptypes.BaseApp
2424
}
2525

26-
func NewApp(state state.State, logger log.Logger) (apptypes.Application, error) {
27-
return &app{apptypes.NewBaseApp(Name, state, logger)}, nil
26+
func NewApp(logger log.Logger) (apptypes.Application, error) {
27+
return &app{apptypes.NewBaseApp(Name, logger)}, nil
2828
}
2929

3030
func (a *app) AcceptQuery(req tmtypes.RequestQuery) bool {
31-
return strings.HasPrefix(req.GetPath(), state.AccountPath)
31+
return strings.HasPrefix(req.GetPath(), appstate.AccountPath)
3232
}
3333

34-
func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
34+
func (a *app) Query(state appstate.State, req tmtypes.RequestQuery) tmtypes.ResponseQuery {
3535

3636
if !a.AcceptQuery(req) {
3737
return tmtypes.ResponseQuery{
3838
Code: code.UNKNOWN_QUERY,
3939
Log: "invalid key",
4040
}
4141
}
42-
id := strings.TrimPrefix(req.Path, state.AccountPath)
42+
id := strings.TrimPrefix(req.Path, appstate.AccountPath)
4343
key, err := keys.ParseAccountPath(id)
4444
if err != nil {
4545
return tmtypes.ResponseQuery{
@@ -48,7 +48,7 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
4848
}
4949
}
5050

51-
acct, err := a.State().Account().Get(key.ID())
51+
acct, err := state.Account().Get(key.ID())
5252
if err != nil {
5353
return tmtypes.ResponseQuery{
5454
Code: code.ERROR,
@@ -73,7 +73,7 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
7373

7474
return tmtypes.ResponseQuery{
7575
Value: bytes,
76-
Height: int64(a.State().Version()),
76+
Height: state.Version(),
7777
}
7878
}
7979

@@ -85,29 +85,29 @@ func (a *app) AcceptTx(ctx apptypes.Context, tx interface{}) bool {
8585
return false
8686
}
8787

88-
func (a *app) CheckTx(ctx apptypes.Context, tx interface{}) tmtypes.ResponseCheckTx {
88+
func (a *app) CheckTx(state appstate.State, ctx apptypes.Context, tx interface{}) tmtypes.ResponseCheckTx {
8989
switch tx := tx.(type) {
9090
case *types.TxPayload_TxSend:
91-
return a.doCheckTx(ctx, tx.TxSend)
91+
return a.doCheckTx(state, ctx, tx.TxSend)
9292
}
9393
return tmtypes.ResponseCheckTx{
9494
Code: code.UNKNOWN_TRANSACTION,
9595
Log: "unknown transaction",
9696
}
9797
}
9898

99-
func (a *app) DeliverTx(ctx apptypes.Context, tx interface{}) tmtypes.ResponseDeliverTx {
99+
func (a *app) DeliverTx(state appstate.State, ctx apptypes.Context, tx interface{}) tmtypes.ResponseDeliverTx {
100100
switch tx := tx.(type) {
101101
case *types.TxPayload_TxSend:
102-
return a.doDeliverTx(ctx, tx.TxSend)
102+
return a.doDeliverTx(state, ctx, tx.TxSend)
103103
}
104104
return tmtypes.ResponseDeliverTx{
105105
Code: code.UNKNOWN_TRANSACTION,
106106
Log: "unknown transaction",
107107
}
108108
}
109109

110-
func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.ResponseCheckTx {
110+
func (a *app) doCheckTx(state appstate.State, ctx apptypes.Context, tx *types.TxSend) tmtypes.ResponseCheckTx {
111111

112112
if !bytes.Equal(ctx.Signer().Address(), tx.From) {
113113
return tmtypes.ResponseCheckTx{
@@ -123,7 +123,7 @@ func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.Response
123123
}
124124
}
125125

126-
acct, err := a.State().Account().Get(tx.From)
126+
acct, err := state.Account().Get(tx.From)
127127
if err != nil {
128128
return tmtypes.ResponseCheckTx{
129129
Code: code.INVALID_TRANSACTION,
@@ -147,17 +147,17 @@ func (a *app) doCheckTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.Response
147147
return tmtypes.ResponseCheckTx{}
148148
}
149149

150-
func (a *app) doDeliverTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.ResponseDeliverTx {
150+
func (a *app) doDeliverTx(state appstate.State, ctx apptypes.Context, tx *types.TxSend) tmtypes.ResponseDeliverTx {
151151

152-
cresp := a.doCheckTx(ctx, tx)
152+
cresp := a.doCheckTx(state, ctx, tx)
153153
if !cresp.IsOK() {
154154
return tmtypes.ResponseDeliverTx{
155155
Code: cresp.Code,
156156
Log: cresp.Log,
157157
}
158158
}
159159

160-
acct, err := a.State().Account().Get(tx.From)
160+
acct, err := state.Account().Get(tx.From)
161161
if err != nil {
162162
return tmtypes.ResponseDeliverTx{
163163
Code: code.INVALID_TRANSACTION,
@@ -171,7 +171,7 @@ func (a *app) doDeliverTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.Respon
171171
}
172172
}
173173

174-
toacct, err := a.State().Account().Get(tx.To)
174+
toacct, err := state.Account().Get(tx.To)
175175
if err != nil {
176176
return tmtypes.ResponseDeliverTx{
177177
Code: code.INVALID_TRANSACTION,
@@ -188,14 +188,14 @@ func (a *app) doDeliverTx(ctx apptypes.Context, tx *types.TxSend) tmtypes.Respon
188188
acct.Balance -= tx.Amount
189189
toacct.Balance += tx.Amount
190190

191-
if err := a.State().Account().Save(acct); err != nil {
191+
if err := state.Account().Save(acct); err != nil {
192192
return tmtypes.ResponseDeliverTx{
193193
Code: code.INVALID_TRANSACTION,
194194
Log: err.Error(),
195195
}
196196
}
197197

198-
if err := a.State().Account().Save(toacct); err != nil {
198+
if err := state.Account().Save(toacct); err != nil {
199199
return tmtypes.ResponseDeliverTx{
200200
Code: code.INVALID_TRANSACTION,
201201
Log: err.Error(),

app/account/app_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestAccountApp(t *testing.T) {
3333
},
3434
}
3535

36-
state := testutil.NewState(t, &types.Genesis{
36+
_, cacheState := testutil.NewState(t, &types.Genesis{
3737
Accounts: []types.Account{
3838
types.Account{Address: addrfrom, Balance: balance},
3939
},
@@ -46,25 +46,25 @@ func TestAccountApp(t *testing.T) {
4646
},
4747
})
4848

49-
app, err := account.NewApp(state, testutil.Logger())
49+
app, err := account.NewApp(testutil.Logger())
5050
require.NoError(t, err)
5151

5252
assert.True(t, app.AcceptQuery(tmtypes.RequestQuery{Path: query.AccountPath(addrfrom)}))
5353

5454
assert.True(t, app.AcceptTx(ctx, send))
5555

5656
{
57-
resp := app.CheckTx(ctx, send)
57+
resp := app.CheckTx(cacheState, ctx, send)
5858
assert.True(t, resp.IsOK(), resp.Log)
5959
}
6060

6161
{
62-
resp := app.DeliverTx(ctx, send)
62+
resp := app.DeliverTx(cacheState, ctx, send)
6363
assert.True(t, resp.IsOK(), resp.Log)
6464
}
6565

6666
{
67-
resp := app.Query(tmtypes.RequestQuery{Path: query.AccountPath(addrfrom)})
67+
resp := app.Query(cacheState, tmtypes.RequestQuery{Path: query.AccountPath(addrfrom)})
6868
assert.Empty(t, resp.Log)
6969
require.True(t, resp.IsOK())
7070

@@ -76,7 +76,7 @@ func TestAccountApp(t *testing.T) {
7676
}
7777

7878
{
79-
resp := app.Query(tmtypes.RequestQuery{Path: query.AccountPath(addrto)})
79+
resp := app.Query(cacheState, tmtypes.RequestQuery{Path: query.AccountPath(addrto)})
8080
assert.Empty(t, resp.Log)
8181
require.True(t, resp.IsOK())
8282

@@ -90,15 +90,15 @@ func TestAccountApp(t *testing.T) {
9090
}
9191

9292
func TestTx_BadTxType(t *testing.T) {
93-
state_ := testutil.NewState(t, nil)
94-
app, err := account.NewApp(state_, testutil.Logger())
93+
_, cacheState := testutil.NewState(t, nil)
94+
app, err := account.NewApp(testutil.Logger())
9595
require.NoError(t, err)
96-
account, key := testutil.CreateAccount(t, state_)
96+
account, key := testutil.CreateAccount(t, cacheState)
9797
tx := testutil.ProviderTx(account, key, 10)
9898
ctx := apptypes.NewContext(tx)
9999
assert.False(t, app.AcceptTx(ctx, tx.Payload.Payload))
100-
cresp := app.CheckTx(ctx, tx.Payload.Payload)
100+
cresp := app.CheckTx(cacheState, ctx, tx.Payload.Payload)
101101
assert.False(t, cresp.IsOK())
102-
dresp := app.DeliverTx(ctx, tx.Payload.Payload)
102+
dresp := app.DeliverTx(cacheState, ctx, tx.Payload.Payload)
103103
assert.False(t, dresp.IsOK())
104104
}

0 commit comments

Comments
 (0)