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

x/bank v0.43 Audit updates #9271

Merged
merged 24 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4c18f00
add godoc to keeper functions
technicallyty May 5, 2021
65d228e
re-add ValidateBasic to MsgSend CLI tx
technicallyty May 5, 2021
03abf0b
add comment to reflect new return value on method
technicallyty May 5, 2021
9c12254
remove unecessary variable
technicallyty May 5, 2021
0625691
cleanup key comments
technicallyty May 5, 2021
c1a8e64
typo
technicallyty May 5, 2021
64741dd
unused param
technicallyty May 5, 2021
84d6018
update messages spec
technicallyty May 5, 2021
4a28ee3
move event emission to end of method
technicallyty May 5, 2021
c719776
update keeper spec
technicallyty May 5, 2021
8b90477
update proto message to point correct path to interface
technicallyty May 5, 2021
353926e
keeper spec typos
technicallyty May 5, 2021
5956061
fix test for event emission being moved
technicallyty May 6, 2021
a488491
change to blocklist
technicallyty May 6, 2021
d02ad3a
rename SendEnabledCoin(s) -> IsSendEnabledCoins
technicallyty May 6, 2021
d65de77
typo
technicallyty May 6, 2021
68cfd8f
Merge branch 'master' into ty/9218-bank-audit
technicallyty May 6, 2021
9c0d3a4
Merge branch 'master' into ty/9218-bank-audit
technicallyty May 6, 2021
7e5b6af
Merge branch 'ty/9218-bank-audit' of https://github.com/cosmos/cosmos…
technicallyty May 6, 2021
2f2c0e3
Merge branch 'master' into ty/9218-bank-audit
technicallyty May 6, 2021
c66c9ed
Merge branch 'master' into ty/9218-bank-audit
technicallyty May 7, 2021
78d6a55
remove unecessary check
technicallyty May 7, 2021
06a9af6
Merge branch 'master' into ty/9218-bank-audit
technicallyty May 10, 2021
fc58236
move changelog line
technicallyty May 10, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/bank/v1beta1/bank.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
2 changes: 1 addition & 1 deletion x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion x/auth/vesting/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions x/bank/atlas/atlas-v0.39.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
```
Expand Down
10 changes: 9 additions & 1 deletion x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
43 changes: 16 additions & 27 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func (suite *IntegrationTestSuite) SetupTest() {
}

func (suite *IntegrationTestSuite) TestSupply() {
_, ctx := suite.app, suite.ctx
ctx := suite.ctx

require := suite.Require()

// add module accounts to supply keeper
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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{},
Expand All @@ -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])
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down
40 changes: 20 additions & 20 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
Loading