Skip to content

Commit

Permalink
Merge PR #4584: Use expected keeper for bank module
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored and alexanderbez committed Jun 20, 2019
1 parent 6672e70 commit 11566df
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4584-Update-bank-Kee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4584 Update bank Keeper to use expected keeper interface of the AccountKeeper.
10 changes: 5 additions & 5 deletions x/bank/internal/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"fmt"

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

// register bank invariants
func RegisterInvariants(ir sdk.InvariantRegistry, ak auth.AccountKeeper) {
func RegisterInvariants(ir sdk.InvariantRegistry, ak types.AccountKeeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
NonnegativeBalanceInvariant(ak))
}

// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
func NonnegativeBalanceInvariant(ak auth.AccountKeeper) sdk.Invariant {
func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant {
return func(ctx sdk.Context) error {
accts := ak.GetAllAccounts(ctx)
for _, acc := range accts {
Expand All @@ -33,11 +33,11 @@ func NonnegativeBalanceInvariant(ak auth.AccountKeeper) sdk.Invariant {

// TotalCoinsInvariant checks that the sum of the coins across all accounts
// is what is expected
func TotalCoinsInvariant(ak auth.AccountKeeper, totalSupplyFn func() sdk.Coins) sdk.Invariant {
func TotalCoinsInvariant(ak types.AccountKeeper, totalSupplyFn func() sdk.Coins) sdk.Invariant {
return func(ctx sdk.Context) error {
totalCoins := sdk.NewCoins()

chkAccount := func(acc auth.Account) bool {
chkAccount := func(acc authtypes.Account) bool {
coins := acc.GetCoins()
totalCoins = totalCoins.Add(coins)
return false
Expand Down
44 changes: 22 additions & 22 deletions x/bank/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/params"
)
Expand All @@ -30,12 +30,12 @@ type Keeper interface {
type BaseKeeper struct {
BaseSendKeeper

ak auth.AccountKeeper
ak types.AccountKeeper
paramSpace params.Subspace
}

// NewBaseKeeper returns a new BaseKeeper
func NewBaseKeeper(ak auth.AccountKeeper,
func NewBaseKeeper(ak types.AccountKeeper,
paramSpace params.Subspace,
codespace sdk.CodespaceType) BaseKeeper {

Expand Down Expand Up @@ -133,12 +133,12 @@ var _ SendKeeper = (*BaseSendKeeper)(nil)
type BaseSendKeeper struct {
BaseViewKeeper

ak auth.AccountKeeper
ak types.AccountKeeper
paramSpace params.Subspace
}

// NewBaseSendKeeper returns a new BaseSendKeeper.
func NewBaseSendKeeper(ak auth.AccountKeeper,
func NewBaseSendKeeper(ak types.AccountKeeper,
paramSpace params.Subspace, codespace sdk.CodespaceType) BaseSendKeeper {

return BaseSendKeeper{
Expand Down Expand Up @@ -185,12 +185,12 @@ type ViewKeeper interface {

// BaseViewKeeper implements a read only keeper implementation of ViewKeeper.
type BaseViewKeeper struct {
ak auth.AccountKeeper
ak types.AccountKeeper
codespace sdk.CodespaceType
}

// NewBaseViewKeeper returns a new BaseViewKeeper.
func NewBaseViewKeeper(ak auth.AccountKeeper, codespace sdk.CodespaceType) BaseViewKeeper {
func NewBaseViewKeeper(ak types.AccountKeeper, codespace sdk.CodespaceType) BaseViewKeeper {
return BaseViewKeeper{ak: ak, codespace: codespace}
}

Expand All @@ -209,15 +209,15 @@ func (keeper BaseViewKeeper) Codespace() sdk.CodespaceType {
return keeper.codespace
}

func getCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress) sdk.Coins {
func getCoins(ctx sdk.Context, am types.AccountKeeper, addr sdk.AccAddress) sdk.Coins {
acc := am.GetAccount(ctx, addr)
if acc == nil {
return sdk.NewCoins()
}
return acc.GetCoins()
}

func setCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) sdk.Error {
func setCoins(ctx sdk.Context, am types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) sdk.Error {
if !amt.IsValid() {
return sdk.ErrInvalidCoins(amt.String())
}
Expand All @@ -235,22 +235,22 @@ func setCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt s
}

// HasCoins returns whether or not an account has at least amt coins.
func hasCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) bool {
func hasCoins(ctx sdk.Context, am types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) bool {
return getCoins(ctx, am, addr).IsAllGTE(amt)
}

func getAccount(ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress) auth.Account {
func getAccount(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress) authtypes.Account {
return ak.GetAccount(ctx, addr)
}

func setAccount(ctx sdk.Context, ak auth.AccountKeeper, acc auth.Account) {
func setAccount(ctx sdk.Context, ak types.AccountKeeper, acc authtypes.Account) {
ak.SetAccount(ctx, acc)
}

// subtractCoins subtracts amt coins from an account with the given address addr.
//
// CONTRACT: If the account is a vesting account, the amount has to be spendable.
func subtractCoins(ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Error) {
func subtractCoins(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Error) {

if !amt.IsValid() {
return nil, sdk.ErrInvalidCoins(amt.String())
Expand Down Expand Up @@ -280,7 +280,7 @@ func subtractCoins(ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress,
}

// AddCoins adds amt to the coins at the addr.
func addCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Error) {
func addCoins(ctx sdk.Context, am types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Error) {

if !amt.IsValid() {
return nil, sdk.ErrInvalidCoins(amt.String())
Expand All @@ -302,7 +302,7 @@ func addCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt s

// SendCoins moves coins from one account to another
// Returns ErrInvalidCoins if amt is invalid.
func sendCoins(ctx sdk.Context, am auth.AccountKeeper, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
func sendCoins(ctx sdk.Context, am types.AccountKeeper, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
// Safety check ensuring that when sending coins the keeper must maintain the
if !amt.IsValid() {
return sdk.ErrInvalidCoins(amt.String())
Expand All @@ -323,7 +323,7 @@ func sendCoins(ctx sdk.Context, am auth.AccountKeeper, fromAddr sdk.AccAddress,

// InputOutputCoins handles a list of inputs and outputs
// NOTE: Make sure to revert state changes from tx on error
func inputOutputCoins(ctx sdk.Context, am auth.AccountKeeper, inputs []types.Input, outputs []types.Output) (sdk.Tags, sdk.Error) {
func inputOutputCoins(ctx sdk.Context, am types.AccountKeeper, inputs []types.Input, outputs []types.Output) (sdk.Tags, sdk.Error) {
// Safety check ensuring that when sending coins the keeper must maintain the
// Check supply invariant and validity of Coins.
if err := types.ValidateInputsOutputs(inputs, outputs); err != nil {
Expand Down Expand Up @@ -357,7 +357,7 @@ func inputOutputCoins(ctx sdk.Context, am auth.AccountKeeper, inputs []types.Inp
}

func delegateCoins(
ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins,
ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins,
) (sdk.Tags, sdk.Error) {

if !amt.IsValid() {
Expand Down Expand Up @@ -391,7 +391,7 @@ func delegateCoins(
}

func undelegateCoins(
ctx sdk.Context, ak auth.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins,
ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress, amt sdk.Coins,
) (sdk.Tags, sdk.Error) {

if !amt.IsValid() {
Expand All @@ -416,8 +416,8 @@ func undelegateCoins(
}

// CONTRACT: assumes that amt is valid.
func trackDelegation(acc auth.Account, blockTime time.Time, amt sdk.Coins) error {
vacc, ok := acc.(auth.VestingAccount)
func trackDelegation(acc authtypes.Account, blockTime time.Time, amt sdk.Coins) error {
vacc, ok := acc.(authtypes.VestingAccount)
if ok {
vacc.TrackDelegation(blockTime, amt)
return nil
Expand All @@ -427,8 +427,8 @@ func trackDelegation(acc auth.Account, blockTime time.Time, amt sdk.Coins) error
}

// CONTRACT: assumes that amt is valid.
func trackUndelegation(acc auth.Account, amt sdk.Coins) error {
vacc, ok := acc.(auth.VestingAccount)
func trackUndelegation(acc authtypes.Account, amt sdk.Coins) error {
vacc, ok := acc.(authtypes.VestingAccount)
if ok {
vacc.TrackUndelegation(amt)
return nil
Expand Down
17 changes: 17 additions & 0 deletions x/bank/internal/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

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

// AccountKeeper defines the expected account keeper
type AccountKeeper interface {
NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) authtypes.Account

GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.Account
GetAllAccounts(ctx sdk.Context) []authtypes.Account
SetAccount(ctx sdk.Context, acc authtypes.Account)

IterateAccounts(ctx sdk.Context, process func(authtypes.Account) bool)
}
6 changes: 3 additions & 3 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/client/rest"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

var (
Expand Down Expand Up @@ -65,11 +65,11 @@ func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil }
type AppModule struct {
AppModuleBasic
keeper Keeper
accountKeeper auth.AccountKeeper
accountKeeper types.AccountKeeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, accountKeeper auth.AccountKeeper) AppModule {
func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
Expand Down
13 changes: 6 additions & 7 deletions x/bank/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
"github.com/cosmos/cosmos-sdk/x/mock"
Expand All @@ -17,7 +16,7 @@ import (

// SendTx tests and runs a single msg send where both
// accounts already exist.
func SimulateMsgSend(mapper auth.AccountKeeper, bk keeper.Keeper) simulation.Operation {
func SimulateMsgSend(mapper types.AccountKeeper, bk keeper.Keeper) simulation.Operation {
handler := NewHandler(bk)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
Expand All @@ -35,7 +34,7 @@ func SimulateMsgSend(mapper auth.AccountKeeper, bk keeper.Keeper) simulation.Ope
}
}

func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (
func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper types.AccountKeeper) (
fromAcc simulation.Account, comment string, msg types.MsgSend, ok bool) {

fromAcc = simulation.RandomAcc(r, accs)
Expand Down Expand Up @@ -65,7 +64,7 @@ func createMsgSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, map
}

// Sends and verifies the transition of a msg send.
func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg types.MsgSend, ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {
func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper types.AccountKeeper, msg types.MsgSend, ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {
fromAcc := mapper.GetAccount(ctx, msg.FromAddress)
AccountNumbers := []uint64{fromAcc.GetAccountNumber()}
SequenceNumbers := []uint64{fromAcc.GetSequence()}
Expand Down Expand Up @@ -111,7 +110,7 @@ func sendAndVerifyMsgSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg t

// SingleInputSendMsg tests and runs a single msg multisend, with one input and one output, where both
// accounts already exist.
func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk keeper.Keeper) simulation.Operation {
func SimulateSingleInputMsgMultiSend(mapper types.AccountKeeper, bk keeper.Keeper) simulation.Operation {
handler := NewHandler(bk)
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
Expand All @@ -129,7 +128,7 @@ func SimulateSingleInputMsgMultiSend(mapper auth.AccountKeeper, bk keeper.Keeper
}
}

func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper auth.AccountKeeper) (
func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulation.Account, mapper types.AccountKeeper) (
fromAcc simulation.Account, comment string, msg types.MsgMultiSend, ok bool) {

fromAcc = simulation.RandomAcc(r, accs)
Expand Down Expand Up @@ -164,7 +163,7 @@ func createSingleInputMsgMultiSend(r *rand.Rand, ctx sdk.Context, accs []simulat

// Sends and verifies the transition of a msg multisend. This fails if there are repeated inputs or outputs
// pass in handler as nil to handle txs, otherwise handle msgs
func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, mapper auth.AccountKeeper, msg types.MsgMultiSend,
func sendAndVerifyMsgMultiSend(app *baseapp.BaseApp, mapper types.AccountKeeper, msg types.MsgMultiSend,
ctx sdk.Context, privkeys []crypto.PrivKey, handler sdk.Handler) error {

initialInputAddrCoins := make([]sdk.Coins, len(msg.Inputs))
Expand Down

0 comments on commit 11566df

Please sign in to comment.