From 3f9cda160f2891944f75ffc4b5676946e39ddb23 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Tue, 11 Dec 2018 10:46:10 +0800 Subject: [PATCH] add account cache prototype --- baseapp/baseapp.go | 41 ++-- client/context/context.go | 10 +- client/context/query.go | 11 +- client/lcd/lcd_test.go | 15 +- cmd/gaia/app/export.go | 4 +- cmd/gaia/app/genesis.go | 2 +- docs/examples/basecoin/app/app.go | 4 +- docs/examples/basecoin/types/account.go | 4 +- docs/examples/democoin/app/app.go | 13 +- docs/examples/democoin/types/account.go | 11 +- docs/examples/democoin/x/cool/app_test.go | 4 +- docs/examples/democoin/x/pow/app_test.go | 7 +- types/account.go | 28 +++ types/account_cache.go | 14 ++ types/context.go | 10 +- x/auth/account.go | 56 +++--- x/auth/ante.go | 14 +- x/auth/ante_test.go | 4 +- x/auth/codec.go | 3 +- x/auth/context.go | 8 +- x/auth/context_test.go | 3 +- x/auth/keeper.go | 225 +++++++++++++++++++--- x/bank/app_test.go | 17 +- x/bank/bench_test.go | 6 +- x/bank/simulation/invariants.go | 2 +- x/ibc/app_test.go | 7 +- x/ibc/ibc_test.go | 3 +- x/mock/app.go | 14 +- x/slashing/app_test.go | 2 +- x/stake/app_test.go | 2 +- x/stake/keeper/test_common.go | 3 +- x/stake/simulation/invariants.go | 2 +- 32 files changed, 392 insertions(+), 157 deletions(-) create mode 100644 types/account.go create mode 100644 types/account_cache.go diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index c3042b58833d..656135b1751b 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -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" @@ -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. @@ -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. @@ -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 { @@ -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), } } @@ -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 @@ -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 @@ -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() } @@ -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 diff --git a/client/context/context.go b/client/context/context.go index df30374de991..fe20684f031f 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -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" @@ -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) diff --git a/client/context/query.go b/client/context/query.go index 0eff95d30bab..16e2b5a68d26 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -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" @@ -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 @@ -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") } diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 34f8edbc90f2..972f820a0072 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -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" @@ -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 diff --git a/cmd/gaia/app/export.go b/cmd/gaia/app/export.go index 2b51c444be48..bb2c8260234a 100644 --- a/cmd/gaia/app/export.go +++ b/cmd/gaia/app/export.go @@ -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 @@ -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 diff --git a/cmd/gaia/app/genesis.go b/cmd/gaia/app/genesis.go index 9c92f60ba51a..1e7aff2538bd 100644 --- a/cmd/gaia/app/genesis.go +++ b/cmd/gaia/app/genesis.go @@ -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(), diff --git a/docs/examples/basecoin/app/app.go b/docs/examples/basecoin/app/app.go index a469e0b2a44d..f8e20518e012 100644 --- a/docs/examples/basecoin/app/app.go +++ b/docs/examples/basecoin/app/app.go @@ -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{} }, ) @@ -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(), diff --git a/docs/examples/basecoin/types/account.go b/docs/examples/basecoin/types/account.go index 41b437718041..6e928591f68c 100644 --- a/docs/examples/basecoin/types/account.go +++ b/docs/examples/basecoin/types/account.go @@ -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 @@ -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") } diff --git a/docs/examples/democoin/app/app.go b/docs/examples/democoin/app/app.go index 99c1e3ed932c..7d739cc3a6d1 100644 --- a/docs/examples/democoin/app/app.go +++ b/docs/examples/democoin/app/app.go @@ -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 ( @@ -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() @@ -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(), diff --git a/docs/examples/democoin/types/account.go b/docs/examples/democoin/types/account.go index 341f6cfc834c..e0b6bd721d2c 100644 --- a/docs/examples/democoin/types/account.go +++ b/docs/examples/democoin/types/account.go @@ -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. @@ -22,7 +21,7 @@ type AppAccount struct { } // Constructor for AppAccount -func ProtoAppAccount() auth.Account { +func ProtoAppAccount() sdk.Account { return &AppAccount{} } @@ -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") } diff --git a/docs/examples/democoin/x/cool/app_test.go b/docs/examples/democoin/x/cool/app_test.go index 7e9e29f2452d..8daf4b3a9e08 100644 --- a/docs/examples/democoin/x/cool/app_test.go +++ b/docs/examples/democoin/x/cool/app_test.go @@ -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" ) @@ -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) diff --git a/docs/examples/democoin/x/pow/app_test.go b/docs/examples/democoin/x/pow/app_test.go index 1556996b37c0..6d6eefaddd29 100644 --- a/docs/examples/democoin/x/pow/app_test.go +++ b/docs/examples/democoin/x/pow/app_test.go @@ -4,14 +4,13 @@ import ( "testing" "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/ed25519" 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/mock" - - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/ed25519" ) var ( @@ -62,7 +61,7 @@ func TestMsgMine(t *testing.T) { Address: addr1, Coins: nil, } - accs := []auth.Account{acc1} + accs := []sdk.Account{acc1} // Initialize the chain (nil) mock.SetGenesis(mapp, accs) diff --git a/types/account.go b/types/account.go new file mode 100644 index 000000000000..7fa6786de734 --- /dev/null +++ b/types/account.go @@ -0,0 +1,28 @@ +package types + +import "github.com/tendermint/tendermint/crypto" + +// Account is an interface used to store coins at a given address within state. +// It presumes a notion of sequence numbers for replay protection, +// a notion of account numbers for replay protection for previously pruned accounts, +// and a pubkey for authentication purposes. +// +// Many complex conditions can be used in the concrete struct which implements Account. +type Account interface { + GetAddress() AccAddress + SetAddress(AccAddress) error // errors if already set. + + GetPubKey() crypto.PubKey // can return nil. + SetPubKey(crypto.PubKey) error + + GetAccountNumber() uint64 + SetAccountNumber(uint64) error + + GetSequence() uint64 + SetSequence(uint64) error + + GetCoins() Coins + SetCoins(Coins) error + + Clone() Account +} diff --git a/types/account_cache.go b/types/account_cache.go new file mode 100644 index 000000000000..a6b8e7624cda --- /dev/null +++ b/types/account_cache.go @@ -0,0 +1,14 @@ +package types + +type AccountStoreCache interface { + GetAccount(addr AccAddress) Account + SetAccount(addr AccAddress, acc Account) + Delete(addr AccAddress) +} + +type AccountCache interface { + AccountStoreCache + + Cache() AccountCache + Write() +} diff --git a/types/context.go b/types/context.go index 71e1f5303e64..00bf4e6404bc 100644 --- a/types/context.go +++ b/types/context.go @@ -7,7 +7,6 @@ import ( "time" "github.com/golang/protobuf/proto" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" ) @@ -143,6 +142,7 @@ const ( contextKeyBlockGasMeter contextKeyMinimumFees contextKeyConsensusParams + contextKeyAccountCache ) func (c Context) MultiStore() MultiStore { @@ -175,6 +175,10 @@ func (c Context) ConsensusParams() *abci.ConsensusParams { return c.Value(contextKeyConsensusParams).(*abci.ConsensusParams) } +func (c Context) AccountCache() AccountCache { + return c.Value(contextKeyAccountCache).(AccountCache) +} + func (c Context) WithMultiStore(ms MultiStore) Context { return c.withValue(contextKeyMultiStore, ms) } @@ -230,6 +234,10 @@ func (c Context) WithConsensusParams(params *abci.ConsensusParams) Context { return c.withValue(contextKeyConsensusParams, params) } +func (c Context) WithAccountCache(cache AccountCache) Context { + return c.withValue(contextKeyAccountCache, cache) +} + // Cache the multistore and return a new cached context. The cached context is // written to the context when writeCache is called. func (c Context) CacheContext() (cc Context, writeCache func()) { diff --git a/x/auth/account.go b/x/auth/account.go index f647601cabcf..6a0c596af68b 100644 --- a/x/auth/account.go +++ b/x/auth/account.go @@ -9,36 +9,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// Account is an interface used to store coins at a given address within state. -// It presumes a notion of sequence numbers for replay protection, -// a notion of account numbers for replay protection for previously pruned accounts, -// and a pubkey for authentication purposes. -// -// Many complex conditions can be used in the concrete struct which implements Account. -type Account interface { - GetAddress() sdk.AccAddress - SetAddress(sdk.AccAddress) error // errors if already set. - - GetPubKey() crypto.PubKey // can return nil. - SetPubKey(crypto.PubKey) error - - GetAccountNumber() uint64 - SetAccountNumber(uint64) error - - GetSequence() uint64 - SetSequence(uint64) error - - GetCoins() sdk.Coins - SetCoins(sdk.Coins) error -} - // AccountDecoder unmarshals account bytes -type AccountDecoder func(accountBytes []byte) (Account, error) +type AccountDecoder func(accountBytes []byte) (sdk.Account, error) //----------------------------------------------------------- // BaseAccount -var _ Account = (*BaseAccount)(nil) +var _ sdk.Account = (*BaseAccount)(nil) // BaseAccount - a base account structure. // This can be extended by embedding within in your AppAccount. @@ -54,7 +31,7 @@ type BaseAccount struct { } // Prototype function for BaseAccount -func ProtoBaseAccount() Account { +func ProtoBaseAccount() sdk.Account { return &BaseAccount{} } @@ -122,12 +99,37 @@ func (acc *BaseAccount) SetSequence(seq uint64) error { return nil } +// Implements sdk.Account. +func (acc *BaseAccount) Clone() sdk.Account { + // given the fact PubKey and Address doesn't change, + // it should be fine if not deep copy them. if both of + // the two interfaces can provide a Clone() method would be terrific. + clonedAcc := &BaseAccount{ + PubKey: acc.PubKey, + Address: acc.Address, + AccountNumber: acc.AccountNumber, + Sequence: acc.Sequence, + } + + if acc.Coins == nil { + clonedAcc.Coins = nil + } else { + coins := make(sdk.Coins, 0, len(acc.Coins)) + for _, coin := range acc.Coins { + coins = append(coins, sdk.Coin{Denom: coin.Denom, Amount: coin.Amount}) + } + clonedAcc.Coins = coins + } + + return clonedAcc +} + //---------------------------------------- // Wire // Most users shouldn't use this, but this comes in handy for tests. func RegisterBaseAccount(cdc *codec.Codec) { - cdc.RegisterInterface((*Account)(nil), nil) + cdc.RegisterInterface((*sdk.Account)(nil), nil) cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/BaseAccount", nil) codec.RegisterCrypto(cdc) } diff --git a/x/auth/ante.go b/x/auth/ante.go index 91789dd22ca2..8d57c559a4cd 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -116,8 +116,8 @@ func NewAnteHandler(am AccountKeeper, fck FeeCollectionKeeper) sdk.AnteHandler { } } -func getSignerAccs(ctx sdk.Context, am AccountKeeper, addrs []sdk.AccAddress) (accs []Account, res sdk.Result) { - accs = make([]Account, len(addrs)) +func getSignerAccs(ctx sdk.Context, am AccountKeeper, addrs []sdk.AccAddress) (accs []sdk.Account, res sdk.Result) { + accs = make([]sdk.Account, len(addrs)) for i := 0; i < len(accs); i++ { accs[i] = am.GetAccount(ctx, addrs[i]) if accs[i] == nil { @@ -131,8 +131,8 @@ func getSignerAccs(ctx sdk.Context, am AccountKeeper, addrs []sdk.AccAddress) (a // verify the signature and increment the sequence. If the account doesn't have // a pubkey, set it. func processSig( - ctx sdk.Context, acc Account, sig StdSignature, signBytes []byte, simulate bool, -) (updatedAcc Account, res sdk.Result) { + ctx sdk.Context, acc sdk.Account, sig StdSignature, signBytes []byte, simulate bool, +) (updatedAcc sdk.Account, res sdk.Result) { pubKey, res := processPubKey(acc, sig, simulate) if !res.IsOK() { @@ -165,7 +165,7 @@ func init() { copy(dummySecp256k1Pubkey[:], bz) } -func processPubKey(acc Account, sig StdSignature, simulate bool) (crypto.PubKey, sdk.Result) { +func processPubKey(acc sdk.Account, sig StdSignature, simulate bool) (crypto.PubKey, sdk.Result) { // If pubkey is not known for account, set it from the StdSignature. pubKey := acc.GetPubKey() if simulate { @@ -222,7 +222,7 @@ func adjustFeesByGas(fees sdk.Coins, gas uint64) sdk.Coins { // Deduct the fee from the account. // We could use the CoinKeeper (in addition to the AccountKeeper, // because the CoinKeeper doesn't give us accounts), but it seems easier to do this. -func deductFees(acc Account, fee StdFee) (Account, sdk.Result) { +func deductFees(acc sdk.Account, fee StdFee) (sdk.Account, sdk.Result) { coins := acc.GetCoins() feeAmount := fee.Amount @@ -280,7 +280,7 @@ func setGasMeter(simulate bool, ctx sdk.Context, stdTx StdTx) sdk.Context { return ctx.WithGasMeter(sdk.NewGasMeter(stdTx.Fee.Gas)) } -func getSignBytesList(chainID string, stdTx StdTx, accs []Account, genesis bool) (signatureBytesList [][]byte) { +func getSignBytesList(chainID string, stdTx StdTx, accs []sdk.Account, genesis bool) (signatureBytesList [][]byte) { signatureBytesList = make([][]byte, len(accs)) for i := 0; i < len(accs); i++ { accNum := accs[i].GetAccountNumber() diff --git a/x/auth/ante_test.go b/x/auth/ante_test.go index 9d76107fe73f..bcb22b006fcb 100644 --- a/x/auth/ante_test.go +++ b/x/auth/ante_test.go @@ -13,7 +13,7 @@ import ( "github.com/tendermint/tendermint/crypto/secp256k1" "github.com/tendermint/tendermint/libs/log" - codec "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -649,7 +649,7 @@ func TestProcessPubKey(t *testing.T) { priv2, _ := privAndAddr() acc1 := mapper.NewAccountWithAddress(ctx, addr1) type args struct { - acc Account + acc sdk.Account sig StdSignature simulate bool } diff --git a/x/auth/codec.go b/x/auth/codec.go index 624bdf4288db..0ab9fc9acc14 100644 --- a/x/auth/codec.go +++ b/x/auth/codec.go @@ -2,11 +2,12 @@ package auth import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types" ) // Register concrete types on codec codec for default AppAccount func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterInterface((*Account)(nil), nil) + cdc.RegisterInterface((*types.Account)(nil), nil) cdc.RegisterConcrete(&BaseAccount{}, "auth/Account", nil) cdc.RegisterConcrete(StdTx{}, "auth/StdTx", nil) } diff --git a/x/auth/context.go b/x/auth/context.go index 28d159c97222..a09d6456f13d 100644 --- a/x/auth/context.go +++ b/x/auth/context.go @@ -34,15 +34,15 @@ const ( ) // add the signers to the context -func WithSigners(ctx types.Context, accounts []Account) types.Context { +func WithSigners(ctx types.Context, accounts []types.Account) types.Context { return ctx.WithValue(contextKeySigners, accounts) } // get the signers from the context -func GetSigners(ctx types.Context) []Account { +func GetSigners(ctx types.Context) []types.Account { v := ctx.Value(contextKeySigners) if v == nil { - return []Account{} + return []types.Account{} } - return v.([]Account) + return v.([]types.Account) } diff --git a/x/auth/context_test.go b/x/auth/context_test.go index b58547328d1a..8ed15eaaa75e 100644 --- a/x/auth/context_test.go +++ b/x/auth/context_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" @@ -26,7 +25,7 @@ func TestContextWithSigners(t *testing.T) { signers := GetSigners(ctx) require.Equal(t, 0, len(signers)) - ctx2 := WithSigners(ctx, []Account{&acc1, &acc2}) + ctx2 := WithSigners(ctx, []sdk.Account{&acc1, &acc2}) // original context is unchanged signers = GetSigners(ctx) diff --git a/x/auth/keeper.go b/x/auth/keeper.go index bf8b92da603b..4dfa202e225c 100644 --- a/x/auth/keeper.go +++ b/x/auth/keeper.go @@ -1,9 +1,13 @@ package auth import ( + "sort" + "sync" + + "github.com/hashicorp/golang-lru" "github.com/tendermint/tendermint/crypto" - codec "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -22,7 +26,7 @@ type AccountKeeper struct { key sdk.StoreKey // The prototypical Account constructor. - proto func() Account + proto func() sdk.Account // The codec codec for binary encoding/decoding of accounts. cdc *codec.Codec @@ -31,7 +35,7 @@ type AccountKeeper struct { // NewAccountKeeper returns a new sdk.AccountKeeper that // uses go-amino to (binary) encode and decode concrete sdk.Accounts. // nolint -func NewAccountKeeper(cdc *codec.Codec, key sdk.StoreKey, proto func() Account) AccountKeeper { +func NewAccountKeeper(cdc *codec.Codec, key sdk.StoreKey, proto func() sdk.Account) AccountKeeper { return AccountKeeper{ key: key, proto: proto, @@ -40,7 +44,7 @@ func NewAccountKeeper(cdc *codec.Codec, key sdk.StoreKey, proto func() Account) } // Implaements sdk.AccountKeeper. -func (am AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) Account { +func (am AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) sdk.Account { acc := am.proto() err := acc.SetAddress(addr) if err != nil { @@ -56,7 +60,7 @@ func (am AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddre } // New Account -func (am AccountKeeper) NewAccount(ctx sdk.Context, acc Account) Account { +func (am AccountKeeper) NewAccount(ctx sdk.Context, acc sdk.Account) sdk.Account { err := acc.SetAccountNumber(am.GetNextAccountNumber(ctx)) if err != nil { // TODO: Handle with #870 @@ -71,33 +75,32 @@ func AddressStoreKey(addr sdk.AccAddress) []byte { } // Implements sdk.AccountKeeper. -func (am AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) Account { - store := ctx.KVStore(am.key) - bz := store.Get(AddressStoreKey(addr)) - if bz == nil { - return nil +func (am AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) sdk.Account { + cache := ctx.AccountCache() + + cVal := cache.GetAccount(addr) + if acc, ok := cVal.(sdk.Account); ok { + return acc } - acc := am.decodeAccount(bz) - return acc + return nil } // Implements sdk.AccountKeeper. -func (am AccountKeeper) SetAccount(ctx sdk.Context, acc Account) { +func (am AccountKeeper) SetAccount(ctx sdk.Context, acc sdk.Account) { addr := acc.GetAddress() - store := ctx.KVStore(am.key) - bz := am.encodeAccount(acc) - store.Set(AddressStoreKey(addr), bz) + cache := ctx.AccountCache() + cache.SetAccount(addr, acc) } // RemoveAccount removes an account for the account mapper store. -func (am AccountKeeper) RemoveAccount(ctx sdk.Context, acc Account) { +func (am AccountKeeper) RemoveAccount(ctx sdk.Context, acc sdk.Account) { addr := acc.GetAddress() - store := ctx.KVStore(am.key) - store.Delete(AddressStoreKey(addr)) + cache := ctx.AccountCache() + cache.Delete(addr) } // Implements sdk.AccountKeeper. -func (am AccountKeeper) IterateAccounts(ctx sdk.Context, process func(Account) (stop bool)) { +func (am AccountKeeper) IterateAccounts(ctx sdk.Context, process func(sdk.Account) (stop bool)) { store := ctx.KVStore(am.key) iter := sdk.KVStorePrefixIterator(store, AddressStoreKeyPrefix) defer iter.Close() @@ -169,7 +172,7 @@ func (am AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 { //---------------------------------------- // misc. -func (am AccountKeeper) encodeAccount(acc Account) []byte { +func (am AccountKeeper) encodeAccount(acc sdk.Account) []byte { bz, err := am.cdc.MarshalBinaryBare(acc) if err != nil { panic(err) @@ -177,10 +180,188 @@ func (am AccountKeeper) encodeAccount(acc Account) []byte { return bz } -func (am AccountKeeper) decodeAccount(bz []byte) (acc Account) { +func (am AccountKeeper) decodeAccount(bz []byte) (acc sdk.Account) { err := am.cdc.UnmarshalBinaryBare(bz, &acc) if err != nil { panic(err) } return } + +func NewAccountStoreCache(cdc *codec.Codec, store sdk.KVStore, cap int) sdk.AccountStoreCache { + cache, err := lru.New(cap) + if err != nil { + panic(err) + } + + return &accountStoreCache{ + cdc: cdc, + cache: cache, + store: store, + } +} + +type accountStoreCache struct { + cdc *codec.Codec + cache *lru.Cache + store sdk.KVStore +} + +func (ac *accountStoreCache) getAccountFromCache(addr sdk.AccAddress) (acc sdk.Account, ok bool) { + cacc, ok := ac.cache.Get(string(addr)) + if !ok { + return nil, ok + } + if acc, ok := cacc.(sdk.Account); ok { + return acc.Clone(), ok + } + return nil, false +} + +func (ac *accountStoreCache) setAccountToCache(addr sdk.AccAddress, acc sdk.Account) { + ac.cache.Add(string(addr), acc.Clone()) +} + +func (ac *accountStoreCache) GetAccount(addr sdk.AccAddress) sdk.Account { + if acc, ok := ac.getAccountFromCache(addr); ok { + return acc + } + + bz := ac.store.Get(AddressStoreKey(addr)) + if bz == nil { + return nil + } + acc := ac.decodeAccount(bz) + ac.setAccountToCache(addr, acc) + return acc +} + +func (ac *accountStoreCache) SetAccount(addr sdk.AccAddress, acc sdk.Account) { + cacc, ok := acc.(sdk.Account) + if !ok { + return + } + + bz := ac.encodeAccount(cacc) + ac.setAccountToCache(addr, cacc) + ac.store.Set(AddressStoreKey(addr), bz) +} + +func (ac *accountStoreCache) Delete(addr sdk.AccAddress) { + ac.setAccountToCache(addr, nil) + ac.store.Delete(AddressStoreKey(addr)) +} + +func (ac *accountStoreCache) encodeAccount(acc sdk.Account) []byte { + bz, err := ac.cdc.MarshalBinaryBare(acc) + if err != nil { + panic(err) + } + return bz +} + +func (ac *accountStoreCache) decodeAccount(bz []byte) (acc sdk.Account) { + err := ac.cdc.UnmarshalBinaryBare(bz, &acc) + if err != nil { + panic(err) + } + return +} + +type cValue struct { + acc sdk.Account + deleted bool + dirty bool +} + +func NewAccountCache(parent sdk.AccountStoreCache) sdk.AccountCache { + return &accountCache{ + parent: parent, + } +} + +type accountCache struct { + cache sync.Map + parent sdk.AccountStoreCache +} + +func (ac *accountCache) GetAccount(addr sdk.AccAddress) sdk.Account { + return ac.getAccountFromCache(addr) +} + +func (ac *accountCache) SetAccount(addr sdk.AccAddress, acc sdk.Account) { + ac.setAccountToCache(addr, acc, false, true) +} + +func (ac *accountCache) Delete(addr sdk.AccAddress) { + ac.setAccountToCache(addr, nil, true, true) +} + +func (ac *accountCache) Cache() sdk.AccountCache { + return &accountCache{ + parent: ac, + } +} + +func (ac *accountCache) Write() { + // We need a copy of all of the keys. + // Not the best, but probably not a bottleneck depending. + // And there is a new problem, we can not get length of map, + // so we can not prepare enough space for keys + keys := make([]string, 0) + ac.cache.Range(func(key, value interface{}) bool { + dbValue := value.(cValue) + if dbValue.dirty { + keys = append(keys, key.(string)) + } + return true + }) + + sort.Strings(keys) + + // TODO: Consider allowing usage of Batch, which would allow the write to + // at least happen atomically. + for _, key := range keys { + // value should exist here, so does not check ok + value, _ := ac.cache.Load(key) + cacheValue := value.(cValue) + + if cacheValue.deleted { + ac.parent.Delete(sdk.AccAddress(key)) + } else if cacheValue.acc == nil { + // Skip, it already doesn't exist in parent. + } else { + ac.parent.SetAccount(sdk.AccAddress(key), cacheValue.acc) + } + } + + // clear the cache + ac.cache = sync.Map{} +} + +func (ac *accountCache) getAccountFromCache(addr sdk.AccAddress) (acc sdk.Account) { + cacheVal, ok := ac.cache.Load(string(addr)) + if !ok { + acc = ac.parent.GetAccount(addr) + ac.setAccountToCache(addr, acc, false, false) + } else { + acc = cacheVal.(cValue).acc + } + + if acc == nil { + return nil + } + return acc.Clone() +} + +func (ac *accountCache) setAccountToCache(addr sdk.AccAddress, acc sdk.Account, deleted bool, dirty bool) { + if acc != nil { + acc = acc.Clone() + } + + ac.cache.Store(string(addr), cValue{ + acc: acc, + deleted: deleted, + dirty: dirty, + }) +} diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 42796d64354c..ea3e2288114d 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -3,15 +3,14 @@ package bank import ( "testing" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/mock" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/mock" ) type ( @@ -98,7 +97,7 @@ func TestMsgSendWithAccounts(t *testing.T) { Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 67)}, } - mock.SetGenesis(mapp, []auth.Account{acc}) + mock.SetGenesis(mapp, []sdk.Account{acc}) ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{}) @@ -150,7 +149,7 @@ func TestMsgSendMultipleOut(t *testing.T) { Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } - mock.SetGenesis(mapp, []auth.Account{acc1, acc2}) + mock.SetGenesis(mapp, []sdk.Account{acc1, acc2}) testCases := []appTestCase{ { @@ -193,7 +192,7 @@ func TestSengMsgMultipleInOut(t *testing.T) { Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } - mock.SetGenesis(mapp, []auth.Account{acc1, acc2, acc4}) + mock.SetGenesis(mapp, []sdk.Account{acc1, acc2, acc4}) testCases := []appTestCase{ { @@ -229,7 +228,7 @@ func TestMsgSendDependent(t *testing.T) { Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 42)}, } - mock.SetGenesis(mapp, []auth.Account{acc1}) + mock.SetGenesis(mapp, []sdk.Account{acc1}) testCases := []appTestCase{ { diff --git a/x/bank/bench_test.go b/x/bank/bench_test.go index a3f69dcdd4d8..93328f4195f0 100644 --- a/x/bank/bench_test.go +++ b/x/bank/bench_test.go @@ -3,11 +3,11 @@ package bank import ( "testing" + abci "github.com/tendermint/tendermint/abci/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/mock" - - abci "github.com/tendermint/tendermint/abci/types" ) // getBenchmarkMockApp initializes a mock application for this module, for purposes of benchmarking @@ -32,7 +32,7 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // Some value conceivably higher than the benchmarks would ever go Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)}, } - accs := []auth.Account{acc} + accs := []sdk.Account{acc} // Construct genesis state mock.SetGenesis(benchmarkApp, accs) diff --git a/x/bank/simulation/invariants.go b/x/bank/simulation/invariants.go index ee826c0e36a3..6c6ed63da52e 100644 --- a/x/bank/simulation/invariants.go +++ b/x/bank/simulation/invariants.go @@ -32,7 +32,7 @@ func TotalCoinsInvariant(mapper auth.AccountKeeper, totalSupplyFn func() sdk.Coi return func(ctx sdk.Context) error { totalCoins := sdk.Coins{} - chkAccount := func(acc auth.Account) bool { + chkAccount := func(acc sdk.Account) bool { coins := acc.GetCoins() totalCoins = totalCoins.Plus(coins) return false diff --git a/x/ibc/app_test.go b/x/ibc/app_test.go index f59b37921b93..10af2d4165bd 100644 --- a/x/ibc/app_test.go +++ b/x/ibc/app_test.go @@ -4,14 +4,13 @@ import ( "testing" "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/ed25519" 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/mock" - - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/ed25519" ) // initialize the mock application for this module @@ -43,7 +42,7 @@ func TestIBCMsgs(t *testing.T) { Address: addr1, Coins: coins, } - accs := []auth.Account{acc} + accs := []sdk.Account{acc} mock.SetGenesis(mapp, accs) diff --git a/x/ibc/ibc_test.go b/x/ibc/ibc_test.go index 2fa24a6c7c3c..6fecb1b2202d 100644 --- a/x/ibc/ibc_test.go +++ b/x/ibc/ibc_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto/ed25519" dbm "github.com/tendermint/tendermint/libs/db" @@ -48,7 +47,7 @@ func makeCodec() *codec.Codec { cdc.RegisterConcrete(IBCReceiveMsg{}, "test/ibc/IBCReceiveMsg", nil) // Register AppAccount - cdc.RegisterInterface((*auth.Account)(nil), nil) + cdc.RegisterInterface((*sdk.Account)(nil), nil) cdc.RegisterConcrete(&auth.BaseAccount{}, "test/ibc/Account", nil) codec.RegisterCrypto(cdc) diff --git a/x/mock/app.go b/x/mock/app.go index 066ac93dcaa5..aac6a0bbcbdd 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -35,7 +35,7 @@ type App struct { AccountKeeper auth.AccountKeeper FeeCollectionKeeper auth.FeeCollectionKeeper - GenesisAccounts []auth.Account + GenesisAccounts []sdk.Account TotalCoinsSupply sdk.Coins } @@ -145,7 +145,7 @@ func (b AddrKeysSlice) Swap(i, j int) { // CreateGenAccounts generates genesis accounts loaded with coins, and returns // their addresses, pubkeys, and privkeys. -func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, addrs []sdk.AccAddress, pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) { +func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []sdk.Account, addrs []sdk.AccAddress, pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) { addrKeysSlice := AddrKeysSlice{} for i := 0; i < numAccs; i++ { @@ -176,7 +176,7 @@ func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (genAccs []auth.Account, } // SetGenesis sets the mock app genesis accounts. -func SetGenesis(app *App, accs []auth.Account) { +func SetGenesis(app *App, accs []sdk.Account) { // Pass the accounts in via the application (lazy) instead of through // RequestInitChain. app.GenesisAccounts = accs @@ -263,7 +263,7 @@ func GeneratePrivKeyAddressPairsFromRand(rand *rand.Rand, n int) (keys []crypto. // provided addresses and coin denominations. // nolint: errcheck func RandomSetGenesis(r *rand.Rand, app *App, addrs []sdk.AccAddress, denoms []string) { - accts := make([]auth.Account, len(addrs), len(addrs)) + accts := make([]sdk.Account, len(addrs), len(addrs)) randCoinIntervals := []BigInterval{ {sdk.NewIntWithDecimal(1, 0), sdk.NewIntWithDecimal(1, 1)}, {sdk.NewIntWithDecimal(1, 2), sdk.NewIntWithDecimal(1, 3)}, @@ -290,9 +290,9 @@ func RandomSetGenesis(r *rand.Rand, app *App, addrs []sdk.AccAddress, denoms []s } // GetAllAccounts returns all accounts in the accountKeeper. -func GetAllAccounts(mapper auth.AccountKeeper, ctx sdk.Context) []auth.Account { - accounts := []auth.Account{} - appendAccount := func(acc auth.Account) (stop bool) { +func GetAllAccounts(mapper auth.AccountKeeper, ctx sdk.Context) []sdk.Account { + accounts := []sdk.Account{} + appendAccount := func(acc sdk.Account) (stop bool) { accounts = append(accounts, acc) return false } diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 4f312acd85a4..f0e5d66e0424 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -103,7 +103,7 @@ func TestSlashingMsgs(t *testing.T) { Address: addr1, Coins: sdk.Coins{genCoin}, } - accs := []auth.Account{acc1} + accs := []sdk.Account{acc1} mock.SetGenesis(mapp, accs) description := stake.NewDescription("foo_moniker", "", "", "") diff --git a/x/stake/app_test.go b/x/stake/app_test.go index de16afde3720..adf232470141 100644 --- a/x/stake/app_test.go +++ b/x/stake/app_test.go @@ -114,7 +114,7 @@ func TestStakeMsgs(t *testing.T) { Address: addr2, Coins: sdk.Coins{genCoin}, } - accs := []auth.Account{acc1, acc2} + accs := []sdk.Account{acc1, acc2} mock.SetGenesis(mApp, accs) mock.CheckBalance(t, mApp, addr1, sdk.Coins{genCoin}) diff --git a/x/stake/keeper/test_common.go b/x/stake/keeper/test_common.go index c3ca811cd6d4..5d29290bef66 100644 --- a/x/stake/keeper/test_common.go +++ b/x/stake/keeper/test_common.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" @@ -67,7 +66,7 @@ func MakeTestCodec() *codec.Codec { cdc.RegisterConcrete(types.MsgBeginRedelegate{}, "test/stake/BeginRedelegate", nil) // Register AppAccount - cdc.RegisterInterface((*auth.Account)(nil), nil) + cdc.RegisterInterface((*sdk.Account)(nil), nil) cdc.RegisterConcrete(&auth.BaseAccount{}, "test/stake/Account", nil) codec.RegisterCrypto(cdc) diff --git a/x/stake/simulation/invariants.go b/x/stake/simulation/invariants.go index 7e875171d340..fcc6a0f96ac8 100644 --- a/x/stake/simulation/invariants.go +++ b/x/stake/simulation/invariants.go @@ -54,7 +54,7 @@ func SupplyInvariants(ck bank.Keeper, k stake.Keeper, loose := sdk.ZeroDec() bonded := sdk.ZeroDec() - am.IterateAccounts(ctx, func(acc auth.Account) bool { + am.IterateAccounts(ctx, func(acc sdk.Account) bool { loose = loose.Add(sdk.NewDecFromInt(acc.GetCoins().AmountOf(stakeTypes.DefaultBondDenom))) return false })