Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: [x/auth] Add lru cache for account #2678

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
dbm "github.com/tendermint/tendermint/libs/db"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
)

// Key to store the consensus params in the main store.
Expand Down Expand Up @@ -63,9 +63,10 @@ type BaseApp struct {
// checkState is set on initialization and reset on Commit.
// deliverState is set in InitChain and BeginBlock and cleared on Commit.
// See methods setCheckState and setDeliverState.
checkState *state // for CheckTx
deliverState *state // for DeliverTx
voteInfos []abci.VoteInfo // absent validators from begin block
checkState *state // for CheckTx
deliverState *state // for DeliverTx
voteInfos []abci.VoteInfo // absent validators from begin block
accountStoreCache sdk.AccountStoreCache

// consensus params
// TODO move this in the future to baseapp param store on main store.
Expand Down Expand Up @@ -216,14 +217,15 @@ func (app *BaseApp) SetMinimumFees(fees sdk.Coins) { app.minimumFees = fees }
// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, app.Logger).WithMinimumFees(app.minimumFees)
return sdk.NewContext(app.checkState.ms, header, true, app.Logger).WithMinimumFees(app.minimumFees).WithAccountCache(app.checkState.accountCache)
}
return sdk.NewContext(app.deliverState.ms, header, false, app.Logger)
return sdk.NewContext(app.deliverState.ms, header, false, app.Logger).WithAccountCache(app.deliverState.accountCache)
}

type state struct {
ms sdk.CacheMultiStore
ctx sdk.Context
ms sdk.CacheMultiStore
accountCache sdk.AccountCache
ctx sdk.Context
}

func (st *state) CacheMultiStore() sdk.CacheMultiStore {
Expand All @@ -234,19 +236,27 @@ func (st *state) Context() sdk.Context {
return st.ctx
}

func (st *state) writeAccountCache() {
st.accountCache.Write()
}

func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
accountCache := auth.NewAccountCache(app.accountStoreCache)
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.Logger).WithMinimumFees(app.minimumFees),
ms: ms,
accountCache: accountCache,
ctx: sdk.NewContext(ms, header, true, app.Logger).WithMinimumFees(app.minimumFees).WithAccountCache(accountCache),
}
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
accountCache := auth.NewAccountCache(app.accountStoreCache)
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.Logger),
ms: ms,
accountCache: accountCache,
ctx: sdk.NewContext(ms, header, false, app.Logger).WithAccountCache(accountCache),
}
}

Expand Down Expand Up @@ -318,6 +328,9 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC

res = app.initChainer(app.deliverState.ctx, req)

// we need to write updates to underlying cache and storage
app.deliverState.writeAccountCache()

// NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState
return
Expand Down Expand Up @@ -679,7 +692,7 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (
).(sdk.CacheMultiStore)
}

return ctx.WithMultiStore(msCache), msCache
return ctx.WithMultiStore(msCache).WithAccountCache(ctx.AccountCache().Cache()), msCache
}

// runTx processes a transaction. The transactions is proccessed via an
Expand Down Expand Up @@ -786,6 +799,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk

// only update state if all messages pass
if result.IsOK() {
ctx.AccountCache().Write()
msCache.Write()
}

Expand All @@ -810,6 +824,7 @@ func (app *BaseApp) Commit() (res abci.ResponseCommit) {
header := app.deliverState.ctx.BlockHeader()

// Write the Deliver state and commit the MultiStore
app.deliverState.writeAccountCache()
app.deliverState.ms.Write()
commitID := app.cms.Commit()
// TODO: this is missing a module identifier and dumps byte array
Expand Down
10 changes: 4 additions & 6 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"

"github.com/spf13/viper"

"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
tmlite "github.com/tendermint/tendermint/lite"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cskeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)

const ctxAccStoreName = "acc"
Expand Down Expand Up @@ -178,7 +176,7 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext {

// GetAccountDecoder gets the account decoder for auth.DefaultAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (acct auth.Account, err error) {
return func(accBytes []byte) (acct types.Account, err error) {
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
Expand Down
11 changes: 4 additions & 7 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ package context

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"

"github.com/pkg/errors"

"strings"

"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
Expand All @@ -19,6 +14,8 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)

// GetNode returns an RPC client. If the context's client is not defined, an
Expand Down Expand Up @@ -61,7 +58,7 @@ func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sd

// GetAccount queries for an account given an address and a block height. An
// error is returned if the query or decoding fails.
func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
func (ctx CLIContext) GetAccount(address []byte) (sdk.Account, error) {
if ctx.AccDecoder == nil {
return nil, errors.New("account decoder required but not provided")
}
Expand Down
15 changes: 7 additions & 8 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ import (

"github.com/spf13/viper"
"github.com/stretchr/testify/require"

p2p "github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

client "github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
cryptoKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
tests "github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
version "github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/gov"
Expand Down Expand Up @@ -888,10 +887,10 @@ func TestProposalsQuery(t *testing.T) {

//_____________________________________________________________________________
// get the account to get the sequence
func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
func getAccount(t *testing.T, port string, addr sdk.AccAddress) sdk.Account {
res, body := Request(t, port, "GET", fmt.Sprintf("/auth/accounts/%s", addr.String()), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var acc auth.Account
var acc sdk.Account
err := cdc.UnmarshalJSON([]byte(body), &acc)
require.Nil(t, err)
return acc
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
stake "github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake"
)

// export the state of gaia for a genesis file
Expand All @@ -30,7 +30,7 @@ func (app *GaiaApp) ExportAppStateAndValidators(forZeroHeight bool) (

// iterate to get the accounts
accounts := []GenesisAccount{}
appendAccount := func(acc auth.Account) (stop bool) {
appendAccount := func(acc sdk.Account) (stop bool) {
account := NewGenesisAccountI(acc)
accounts = append(accounts, account)
return false
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount {
}
}

func NewGenesisAccountI(acc auth.Account) GenesisAccount {
func NewGenesisAccountI(acc sdk.Account) GenesisAccount {
return GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
app.accountKeeper = auth.NewAccountKeeper(
cdc,
app.keyAccount, // target store
func() auth.Account {
func() sdk.Account {
return &types.AppAccount{}
},
)
Expand Down Expand Up @@ -168,7 +168,7 @@ func (app *BasecoinApp) ExportAppStateAndValidators() (appState json.RawMessage,
ctx := app.NewContext(true, abci.Header{})
accounts := []*types.GenesisAccount{}

appendAccountsFn := func(acc auth.Account) bool {
appendAccountsFn := func(acc sdk.Account) bool {
account := &types.GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/basecoin/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
)

var _ auth.Account = (*AppAccount)(nil)
var _ sdk.Account = (*AppAccount)(nil)

// AppAccount is a custom extension for this application. It is an example of
// extending auth.BaseAccount with custom fields. It is compatible with the
Expand All @@ -31,7 +31,7 @@ func NewAppAccount(name string, baseAcct auth.BaseAccount) *AppAccount {
// GetAccountDecoder returns the AccountDecoder function for the custom
// AppAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (auth.Account, error) {
return func(accBytes []byte) (sdk.Account, error) {
if len(accBytes) == 0 {
return nil, sdk.ErrTxDecode("accBytes are empty")
}
Expand Down
13 changes: 6 additions & 7 deletions docs/examples/democoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import (

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc"

"github.com/cosmos/cosmos-sdk/docs/examples/democoin/types"
"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/cool"
"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/pow"
"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/simplestake"
"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/sketchy"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc"
)

const (
Expand Down Expand Up @@ -121,7 +120,7 @@ func MakeCodec() *codec.Codec {
simplestake.RegisterCodec(cdc)

// Register AppAccount
cdc.RegisterInterface((*auth.Account)(nil), nil)
cdc.RegisterInterface((*sdk.Account)(nil), nil)
cdc.RegisterConcrete(&types.AppAccount{}, "democoin/Account", nil)

cdc.Seal()
Expand Down Expand Up @@ -174,7 +173,7 @@ func (app *DemocoinApp) ExportAppStateAndValidators() (appState json.RawMessage,

// iterate to get the accounts
accounts := []*types.GenesisAccount{}
appendAccount := func(acc auth.Account) (stop bool) {
appendAccount := func(acc sdk.Account) (stop bool) {
account := &types.GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
Expand Down
11 changes: 5 additions & 6 deletions docs/examples/democoin/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package types

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"

"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/cool"
"github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/pow"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)

var _ auth.Account = (*AppAccount)(nil)
var _ sdk.Account = (*AppAccount)(nil)

// Custom extensions for this application. This is just an example of
// extending auth.BaseAccount with custom fields.
Expand All @@ -22,7 +21,7 @@ type AppAccount struct {
}

// Constructor for AppAccount
func ProtoAppAccount() auth.Account {
func ProtoAppAccount() sdk.Account {
return &AppAccount{}
}

Expand All @@ -32,7 +31,7 @@ func (acc *AppAccount) SetName(name string) { acc.Name = name }

// Get the AccountDecoder function for the custom AppAccount
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (res auth.Account, err error) {
return func(accBytes []byte) (res sdk.Account, err error) {
if len(accBytes) == 0 {
return nil, sdk.ErrTxDecode("accBytes are empty")
}
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/democoin/x/cool/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
bank "github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/mock"
)

Expand Down Expand Up @@ -78,7 +78,7 @@ func TestMsgQuiz(t *testing.T) {
Address: addr1,
Coins: nil,
}
accs := []auth.Account{acc1}
accs := []sdk.Account{acc1}

// Initialize the chain (nil)
mock.SetGenesis(mapp, accs)
Expand Down
Loading