From 709ab089c118370e5733729df50f7e2d7ee4e375 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+technicallyty@users.noreply.github.com> Date: Mon, 10 May 2021 09:17:55 -0700 Subject: [PATCH] x/bank v0.43 Audit updates (#9271) * add godoc to keeper functions * re-add ValidateBasic to MsgSend CLI tx * add comment to reflect new return value on method * remove unecessary variable * cleanup key comments * typo * unused param * update messages spec * move event emission to end of method * update keeper spec * update proto message to point correct path to interface * keeper spec typos * fix test for event emission being moved * change to blocklist * rename SendEnabledCoin(s) -> IsSendEnabledCoins * typo * remove unecessary check * move changelog line Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> --- CHANGELOG.md | 1 + proto/cosmos/bank/v1beta1/bank.proto | 2 +- x/auth/vesting/msg_server.go | 2 +- x/auth/vesting/types/expected_keepers.go | 2 +- x/bank/atlas/atlas-v0.39.1.md | 4 +- x/bank/keeper/keeper.go | 10 ++- x/bank/keeper/keeper_test.go | 43 ++++------ x/bank/keeper/msg_server.go | 4 +- x/bank/keeper/send.go | 40 ++++----- x/bank/module.go | 2 +- x/bank/simulation/operations.go | 4 +- x/bank/spec/02_keepers.md | 100 +++++++++++------------ x/bank/spec/03_messages.md | 27 +++--- x/bank/types/bank.pb.go | 74 ++++++++--------- x/bank/types/key.go | 6 +- 15 files changed, 159 insertions(+), 162 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33f45f7e88..f0ac315e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,7 @@ if input key is empty, or input data contains empty key. * `sdk.Msg` now only contains `ValidateBasic` and `GetSigners` methods. The remaining methods `GetSignBytes`, `Route` and `Type` are moved to `legacytx.LegacyMsg`. * The `RegisterCustomTypeURL` function and the `cosmos.base.v1beta1.ServiceMsg` interface have been removed from the interface registry. * (codec) [\#9251](https://github.com/cosmos/cosmos-sdk/pull/9251) Rename `clientCtx.JSONMarshaler` to `clientCtx.JSONCodec` as per #9226. +* (x/bank) [\#9271](https://github.com/cosmos/cosmos-sdk/pull/9271) SendEnabledCoin(s) renamed to IsSendEnabledCoin(s) to better reflect its functionality. ### State Machine Breaking diff --git a/proto/cosmos/bank/v1beta1/bank.proto b/proto/cosmos/bank/v1beta1/bank.proto index 1a4cc289ea..eb843b2cb6 100644 --- a/proto/cosmos/bank/v1beta1/bank.proto +++ b/proto/cosmos/bank/v1beta1/bank.proto @@ -52,7 +52,7 @@ message Supply { option (gogoproto.equal) = true; option (gogoproto.goproto_getters) = false; - option (cosmos_proto.implements_interface) = "*github.com/cosmos/cosmos-sdk/x/bank/exported.SupplyI"; + option (cosmos_proto.implements_interface) = "*github.com/cosmos/cosmos-sdk/x/bank/legacy/v040.SupplyI"; repeated cosmos.base.v1beta1.Coin total = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"]; diff --git a/x/auth/vesting/msg_server.go b/x/auth/vesting/msg_server.go index dadd65dbcf..795fc2c605 100644 --- a/x/auth/vesting/msg_server.go +++ b/x/auth/vesting/msg_server.go @@ -32,7 +32,7 @@ func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre ak := s.AccountKeeper bk := s.BankKeeper - if err := bk.SendEnabledCoins(ctx, msg.Amount...); err != nil { + if err := bk.IsSendEnabledCoins(ctx, msg.Amount...); err != nil { return nil, err } diff --git a/x/auth/vesting/types/expected_keepers.go b/x/auth/vesting/types/expected_keepers.go index 8212f6e375..5705eea30b 100644 --- a/x/auth/vesting/types/expected_keepers.go +++ b/x/auth/vesting/types/expected_keepers.go @@ -7,7 +7,7 @@ import ( // BankKeeper defines the expected interface contract the vesting module requires // for creating vesting accounts with funds. type BankKeeper interface { - SendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error BlockedAddr(addr sdk.AccAddress) bool } diff --git a/x/bank/atlas/atlas-v0.39.1.md b/x/bank/atlas/atlas-v0.39.1.md index affca5b9e2..cc7a75d951 100644 --- a/x/bank/atlas/atlas-v0.39.1.md +++ b/x/bank/atlas/atlas-v0.39.1.md @@ -35,14 +35,14 @@ with particular kinds of accounts. ``` 4. Create the keeper. Note, the `x/bank` module depends on the `x/auth` module - and a list of blacklisted account addresses which funds are not allowed to be + and a list of blocklisted account addresses which funds are not allowed to be sent to. Your application will need to define this method based your needs. ```go func NewApp(...) *App { // ... app.BankKeeper = bank.NewBaseKeeper( - app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlacklistedAccAddrs(), + app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlocklistedAccAddrs(), ) } ``` diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index c02b11a17e..cc8ded2401 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -55,6 +55,7 @@ type BaseKeeper struct { paramSpace paramtypes.Subspace } +// GetPaginatedTotalSupply queries for the supply, ignoring 0 coins, with a given pagination func (k BaseKeeper) GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error) { store := ctx.KVStore(k.storeKey) supplyStore := prefix.NewStore(store, types.SupplyKey) @@ -213,7 +214,8 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { } } -// GetDenomMetaData retrieves the denomination metadata +// GetDenomMetaData retrieves the denomination metadata. returns the metadata and true if the denom exists, +// false otherwise. func (k BaseKeeper) GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool) { store := ctx.KVStore(k.storeKey) store = prefix.NewStore(store, types.DenomMetadataKey(denom)) @@ -427,6 +429,7 @@ func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Co return nil } +// setSupply sets the supply for the given coin func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) { intBytes, err := coin.Amount.Marshal() if err != nil { @@ -444,6 +447,7 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) { } } +// trackDelegation tracks the delegation of the given account if it is a vesting account func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error { acc := k.ak.GetAccount(ctx, addr) if acc == nil { @@ -460,6 +464,7 @@ func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balanc return nil } +// trackUndelegation trakcs undelegation of the given account if it is a vesting account func (k BaseKeeper) trackUndelegation(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) error { acc := k.ak.GetAccount(ctx, addr) if acc == nil { @@ -476,6 +481,9 @@ func (k BaseKeeper) trackUndelegation(ctx sdk.Context, addr sdk.AccAddress, amt return nil } +// IterateTotalSupply iterates over the total supply calling the given cb (callback) function +// with the balance of each coin. +// The iteration stops if the callback returns true. func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool) { store := ctx.KVStore(k.storeKey) supplyStore := prefix.NewStore(store, types.SupplyKey) diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index e20ce3e21f..e9f35b6a11 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -111,7 +111,7 @@ func (suite *IntegrationTestSuite) SetupTest() { } func (suite *IntegrationTestSuite) TestSupply() { - _, ctx := suite.app, suite.ctx + ctx := suite.ctx require := suite.Require() @@ -119,7 +119,7 @@ func (suite *IntegrationTestSuite) TestSupply() { authKeeper, keeper := suite.initKeepersWithmAccPerms(make(map[string]bool)) initialPower := int64(100) - initTokens := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, initialPower) + initTokens := suite.app.StakingKeeper.TokensFromConsensusPower(ctx, initialPower) totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)) // set burnerAcc balance @@ -140,7 +140,7 @@ func (suite *IntegrationTestSuite) TestSupply() { require.Equal(total.String(), "") } -func (suite *IntegrationTestSuite) TestSendCoinsFromModuleToAccount_Blacklist() { +func (suite *IntegrationTestSuite) TestSendCoinsFromModuleToAccount_Blocklist() { ctx := suite.ctx // add module accounts to supply keeper @@ -484,11 +484,11 @@ func (suite *IntegrationTestSuite) TestSendEnabled() { barCoin := sdk.NewCoin("barcoin", sdk.OneInt()) // assert with default (all denom) send enabled both Bar and Bond Denom are enabled - suite.Require().Equal(enabled, app.BankKeeper.SendEnabledCoin(ctx, barCoin)) - suite.Require().Equal(enabled, app.BankKeeper.SendEnabledCoin(ctx, bondCoin)) + suite.Require().Equal(enabled, app.BankKeeper.IsSendEnabledCoin(ctx, barCoin)) + suite.Require().Equal(enabled, app.BankKeeper.IsSendEnabledCoin(ctx, bondCoin)) // Both coins should be send enabled. - err := app.BankKeeper.SendEnabledCoins(ctx, fooCoin, bondCoin) + err := app.BankKeeper.IsSendEnabledCoins(ctx, fooCoin, bondCoin) suite.Require().NoError(err) // Set default send_enabled to !enabled, add a foodenom that overrides default as enabled @@ -497,20 +497,20 @@ func (suite *IntegrationTestSuite) TestSendEnabled() { app.BankKeeper.SetParams(ctx, params) // Expect our specific override to be enabled, others to be !enabled. - suite.Require().Equal(enabled, app.BankKeeper.SendEnabledCoin(ctx, fooCoin)) - suite.Require().Equal(!enabled, app.BankKeeper.SendEnabledCoin(ctx, barCoin)) - suite.Require().Equal(!enabled, app.BankKeeper.SendEnabledCoin(ctx, bondCoin)) + suite.Require().Equal(enabled, app.BankKeeper.IsSendEnabledCoin(ctx, fooCoin)) + suite.Require().Equal(!enabled, app.BankKeeper.IsSendEnabledCoin(ctx, barCoin)) + suite.Require().Equal(!enabled, app.BankKeeper.IsSendEnabledCoin(ctx, bondCoin)) // Foo coin should be send enabled. - err = app.BankKeeper.SendEnabledCoins(ctx, fooCoin) + err = app.BankKeeper.IsSendEnabledCoins(ctx, fooCoin) suite.Require().NoError(err) // Expect an error when one coin is not send enabled. - err = app.BankKeeper.SendEnabledCoins(ctx, fooCoin, bondCoin) + err = app.BankKeeper.IsSendEnabledCoins(ctx, fooCoin, bondCoin) suite.Require().Error(err) // Expect an error when all coins are not send enabled. - err = app.BankKeeper.SendEnabledCoins(ctx, bondCoin, barCoin) + err = app.BankKeeper.IsSendEnabledCoins(ctx, bondCoin, barCoin) suite.Require().Error(err) } @@ -538,12 +538,9 @@ func (suite *IntegrationTestSuite) TestMsgSendEvents() { app.AccountKeeper.SetAccount(ctx, acc) newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) + suite.Require().NoError(simapp.FundAccount(app, ctx, addr, newCoins)) - suite.Require().Error(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)) - - events := ctx.EventManager().ABCIEvents() - suite.Require().Equal(2, len(events)) - + suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)) event1 := sdk.Event{ Type: types.EventTypeTransfer, Attributes: []abci.EventAttribute{}, @@ -570,17 +567,9 @@ func (suite *IntegrationTestSuite) TestMsgSendEvents() { abci.EventAttribute{Key: []byte(types.AttributeKeySender), Value: []byte(addr.String())}, ) - suite.Require().Equal(abci.Event(event1), events[0]) - suite.Require().Equal(abci.Event(event2), events[1]) - - suite.Require().NoError(simapp.FundAccount(app, ctx, addr, sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)))) - newCoins = sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50)) - - suite.Require().NoError(app.BankKeeper.SendCoins(ctx, addr, addr2, newCoins)) - // events are shifted due to the funding account events - events = ctx.EventManager().ABCIEvents() - suite.Require().Equal(12, len(events)) + events := ctx.EventManager().ABCIEvents() + suite.Require().Equal(10, len(events)) suite.Require().Equal(abci.Event(event1), events[8]) suite.Require().Equal(abci.Event(event2), events[9]) } diff --git a/x/bank/keeper/msg_server.go b/x/bank/keeper/msg_server.go index b318db2cc4..5d940b459e 100644 --- a/x/bank/keeper/msg_server.go +++ b/x/bank/keeper/msg_server.go @@ -26,7 +26,7 @@ var _ types.MsgServer = msgServer{} func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSendResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if err := k.SendEnabledCoins(ctx, msg.Amount...); err != nil { + if err := k.IsSendEnabledCoins(ctx, msg.Amount...); err != nil { return nil, err } @@ -75,7 +75,7 @@ func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*t // NOTE: totalIn == totalOut should already have been checked for _, in := range msg.Inputs { - if err := k.SendEnabledCoins(ctx, in.Coins...); err != nil { + if err := k.IsSendEnabledCoins(ctx, in.Coins...); err != nil { return nil, err } } diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index 8fd7e602a4..675eb67735 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -20,8 +20,8 @@ type SendKeeper interface { GetParams(ctx sdk.Context) types.Params SetParams(ctx sdk.Context, params types.Params) - SendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool - SendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error BlockedAddr(addr sdk.AccAddress) bool } @@ -131,19 +131,6 @@ func (k BaseSendKeeper) InputOutputCoins(ctx sdk.Context, inputs []types.Input, // SendCoins transfers amt coins from a sending account to a receiving account. // An error is returned upon failure. func (k BaseSendKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error { - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - types.EventTypeTransfer, - sdk.NewAttribute(types.AttributeKeyRecipient, toAddr.String()), - sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()), - sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()), - ), - sdk.NewEvent( - sdk.EventTypeMessage, - sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()), - ), - }) - err := k.subUnlockedCoins(ctx, fromAddr, amt) if err != nil { return err @@ -164,6 +151,19 @@ func (k BaseSendKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAd k.ak.SetAccount(ctx, k.ak.NewAccountWithAddress(ctx, toAddr)) } + ctx.EventManager().EmitEvents(sdk.Events{ + sdk.NewEvent( + types.EventTypeTransfer, + sdk.NewAttribute(types.AttributeKeyRecipient, toAddr.String()), + sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()), + sdk.NewAttribute(sdk.AttributeKeyAmount, amt.String()), + ), + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(types.AttributeKeySender, fromAddr.String()), + ), + }) + return nil } @@ -277,20 +277,20 @@ func (k BaseSendKeeper) setBalance(ctx sdk.Context, addr sdk.AccAddress, balance return nil } -// SendEnabledCoins checks the coins provide and returns an ErrSendDisabled if +// IsSendEnabledCoins checks the coins provide and returns an ErrSendDisabled if // any of the coins are not configured for sending. Returns nil if sending is enabled // for all provided coin -func (k BaseSendKeeper) SendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error { +func (k BaseSendKeeper) IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error { for _, coin := range coins { - if !k.SendEnabledCoin(ctx, coin) { + if !k.IsSendEnabledCoin(ctx, coin) { return sdkerrors.Wrapf(types.ErrSendDisabled, "%s transfers are currently disabled", coin.Denom) } } return nil } -// SendEnabledCoin returns the current SendEnabled status of the provided coin's denom -func (k BaseSendKeeper) SendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool { +// IsSendEnabledCoin returns the current SendEnabled status of the provided coin's denom +func (k BaseSendKeeper) IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool { return k.GetParams(ctx).SendEnabledDenom(coin.Denom) } diff --git a/x/bank/module.go b/x/bank/module.go index ea97a9efa3..c43251acc2 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -186,7 +186,7 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { } // RegisterStoreDecoder registers a decoder for supply module's types -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {} +func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} // WeightedOperations returns the all the gov module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index 16e1b77500..8c3aef80f8 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -61,7 +61,7 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Operatio simAccount, toSimAcc, coins, skip := randomSendFields(r, ctx, accs, bk, ak) // Check send_enabled status of each coin denom - if err := bk.SendEnabledCoins(ctx, coins...); err != nil { + if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, err.Error()), nil, nil } @@ -173,7 +173,7 @@ func SimulateMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Ope } // Check send_enabled status of each sent coin denom - if err := bk.SendEnabledCoins(ctx, totalSentCoins...); err != nil { + if err := bk.IsSendEnabledCoins(ctx, totalSentCoins...); err != nil { return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgMultiSend, err.Error()), nil, nil } diff --git a/x/bank/spec/02_keepers.md b/x/bank/spec/02_keepers.md index 8db69c34d0..ea5b9ed692 100644 --- a/x/bank/spec/02_keepers.md +++ b/x/bank/spec/02_keepers.md @@ -12,14 +12,14 @@ require. Best practices dictate careful review of `bank` module code to ensure that permissions are limited in the way that you expect. -## Blacklisting Addresses +## Blocklisting Addresses The `x/bank` module accepts a map of addresses that are considered blocklisted from directly and explicitly receiving funds through means such as `MsgSend` and `MsgMultiSend` and direct API calls like `SendCoinsFromModuleToAccount`. Typically, these addresses are module accounts. If these addresses receive funds -outside of the expected rules of the state machine, invariants are likely to be +outside the expected rules of the state machine, invariants are likely to be broken and could result in a halted network. By providing the `x/bank` module with a blocklisted set of addresses, an error occurs for the operation if a user or client attempts to directly or indirectly send funds to a blocklisted account, for example, by using [IBC](http://docs.cosmos.network/master/ibc/). @@ -58,32 +58,30 @@ The base keeper provides full-permission access: the ability to arbitrary modify // Keeper defines a module interface that facilitates the transfer of coins // between accounts. type Keeper interface { - SendKeeper - - InitGenesis(sdk.Context, *types.GenesisState) - ExportGenesis(sdk.Context) *types.GenesisState - - GetSupply(ctx sdk.Context) exported.SupplyI - SetSupply(ctx sdk.Context, supply exported.SupplyI) - - GetDenomMetaData(ctx sdk.Context, denom string) types.Metadata - SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata) - IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metadata) bool) - - SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error - SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error - SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error - MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - - DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error - UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error - MarshalSupply(supplyI exported.SupplyI) ([]byte, error) - UnmarshalSupply(bz []byte) (exported.SupplyI, error) - - types.QueryServer + SendKeeper + + InitGenesis(sdk.Context, *types.GenesisState) + ExportGenesis(sdk.Context) *types.GenesisState + + GetSupply(ctx sdk.Context, denom string) sdk.Coin + GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error) + IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool) + GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool) + SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata) + IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metadata) bool) + + SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error + SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error + DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error + UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + + DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error + UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error + + types.QueryServer } ``` @@ -96,18 +94,18 @@ accounts. The send keeper does not alter the total supply (mint or burn coins). // SendKeeper defines a module interface that facilitates the transfer of coins // between accounts without the possibility of creating coins. type SendKeeper interface { - ViewKeeper - - InputOutputCoins(ctx sdk.Context, inputs []types.Input, outputs []types.Output) error - SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error - - GetParams(ctx sdk.Context) types.Params - SetParams(ctx sdk.Context, params types.Params) - - SendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool - SendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error - - BlockedAddr(addr sdk.AccAddress) bool + ViewKeeper + + InputOutputCoins(ctx sdk.Context, inputs []types.Input, outputs []types.Output) error + SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error + + GetParams(ctx sdk.Context) types.Params + SetParams(ctx sdk.Context, params types.Params) + + IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + + BlockedAddr(addr sdk.AccAddress) bool } ``` @@ -119,16 +117,16 @@ The view keeper provides read-only access to account balances. The view keeper d // ViewKeeper defines a module interface that facilitates read only access to // account balances. type ViewKeeper interface { - ValidateBalance(ctx sdk.Context, addr sdk.AccAddress) error - HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool - - GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins - GetAccountsBalances(ctx sdk.Context) []types.Balance - GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin - LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins - SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins - - IterateAccountBalances(ctx sdk.Context, addr sdk.AccAddress, cb func(coin sdk.Coin) (stop bool)) - IterateAllBalances(ctx sdk.Context, cb func(address sdk.AccAddress, coin sdk.Coin) (stop bool)) + ValidateBalance(ctx sdk.Context, addr sdk.AccAddress) error + HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool + + GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + GetAccountsBalances(ctx sdk.Context) []types.Balance + GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin + LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + + IterateAccountBalances(ctx sdk.Context, addr sdk.AccAddress, cb func(coin sdk.Coin) (stop bool)) + IterateAllBalances(ctx sdk.Context, cb func(address sdk.AccAddress, coin sdk.Coin) (stop bool)) } ``` diff --git a/x/bank/spec/03_messages.md b/x/bank/spec/03_messages.md index 07c42f5f78..0ef6ce72ef 100644 --- a/x/bank/spec/03_messages.md +++ b/x/bank/spec/03_messages.md @@ -6,20 +6,21 @@ order: 3 ## MsgSend +Send coins from one address to another. +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto#L19-L28 -`handleMsgSend` just runs `inputOutputCoins`. +The message will fail under the following conditions: +- The coins do not have sending enabled +- The `to` address is restricted -```go -handleMsgSend(msg MsgSend) - inputSum = 0 - for input in inputs - inputSum += input.Amount - outputSum = 0 - for output in outputs - outputSum += output.Amount - if inputSum != outputSum: - fail with "input/output amount mismatch" +## MsgMultiSend + +Send coins from and to a series of different address. If any of the receiving addresses do not correspond to an existing account, a new account is created. ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/bank/v1beta1/tx.proto#L33-L39 + +The message will fail under the following conditions: +- Any of the coins do not have sending enabled +- Any of the `to` addresses are restricted +- Any of the coins are locked +- The inputs and outputs do not correctly correspond to one another - return inputOutputCoins(msg.Inputs, msg.Outputs) -``` diff --git a/x/bank/types/bank.pb.go b/x/bank/types/bank.pb.go index 0511d1e961..f190a457b2 100644 --- a/x/bank/types/bank.pb.go +++ b/x/bank/types/bank.pb.go @@ -426,44 +426,44 @@ func init() { func init() { proto.RegisterFile("cosmos/bank/v1beta1/bank.proto", fileDescriptor_dd052eee12edf988) } var fileDescriptor_dd052eee12edf988 = []byte{ - // 587 bytes of a gzipped FileDescriptorProto + // 592 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x54, 0xbf, 0x6f, 0xd3, 0x40, - 0x14, 0xf6, 0x35, 0x8d, 0x49, 0x2f, 0xb0, 0x1c, 0x15, 0x72, 0x2b, 0x61, 0x1b, 0x4b, 0x48, 0x29, - 0xa2, 0x0e, 0x05, 0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0x13, 0xdb, 0xf8, 0x36, 0xb1, 0x8d, 0x17, 0x9b, 0x7f, 0xe3, 0xbb, 0x7a, 0xbc, 0xd0, 0x54, 0x7f, + 0x33, 0xb7, 0x7f, 0x05, 0x00, 0x00, 0xff, 0xff, 0xcc, 0x03, 0xbf, 0xe9, 0xee, 0x04, 0x00, 0x00, } func (this *SendEnabled) Equal(that interface{}) bool { diff --git a/x/bank/types/key.go b/x/bank/types/key.go index a50d04a0a7..5d7f52baa5 100644 --- a/x/bank/types/key.go +++ b/x/bank/types/key.go @@ -21,8 +21,8 @@ const ( // KVStore keys var ( - // BalancesPrefix is the for the account balances store. We use a byte - // (instead of say `[]]byte("balances")` to save some disk space). + // BalancesPrefix is the prefix for the account balances store. We use a byte + // (instead of `[]byte("balances")` to save some disk space). BalancesPrefix = []byte{0x02} SupplyKey = []byte{0x00} DenomMetadataPrefix = []byte{0x1} @@ -35,7 +35,7 @@ func DenomMetadataKey(denom string) []byte { } // AddressFromBalancesStore returns an account address from a balances prefix -// store. The key must not contain the perfix BalancesPrefix as the prefix store +// store. The key must not contain the prefix BalancesPrefix as the prefix store // iterator discards the actual prefix. // // If invalid key is passed, AddressFromBalancesStore returns ErrInvalidKey.