Skip to content

Commit

Permalink
fix(bank): avoid empty spendable error message on send coins (backport
Browse files Browse the repository at this point in the history
…#17170) (#17179)

Co-authored-by: mmsqe <mavis@crypto.com>
  • Loading branch information
mergify[bot] and mmsqe authored Jul 28, 2023
1 parent 4d139f8 commit 7e7e15d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (baseapp) [#17159](https://github.com/cosmos/cosmos-sdk/pull/17159) Validators can propose blocks that exceed the gas limit.
* (x/group) [#17146](https://github.com/cosmos/cosmos-sdk/pull/17146) Rename x/group legacy ORM package's error codespace from "orm" to "legacy_orm", preventing collisions with ORM's error codespace "orm".
* (x/bank) [#17170](https://github.com/cosmos/cosmos-sdk/pull/17170) Avoid empty spendable error message on send coins.

### API Breaking Changes

Expand Down
28 changes: 28 additions & 0 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -704,6 +706,32 @@ func (suite *KeeperTestSuite) TestSendCoins_Invalid_SendLockedCoins() {
suite.Require().Error(suite.bankKeeper.SendCoins(suite.ctx, accAddrs[0], accAddrs[1], sendCoins))
}

func (suite *KeeperTestSuite) TestSendCoins_Invalid_NoSpendableCoins() {
coin := sdk.NewInt64Coin("stake", 10)
coins := sdk.NewCoins(coin)
balances := coins

now := cmttime.Now()
endTime := now.Add(24 * time.Hour)

origCoins := coins
sendCoins := coins

acc0 := authtypes.NewBaseAccountWithAddress(accAddrs[0])
suite.mockFundAccount(accAddrs[0])
suite.Require().NoError(banktestutil.FundAccount(suite.ctx, suite.bankKeeper, accAddrs[0], balances))
vacc, err := vesting.NewContinuousVestingAccount(acc0, origCoins, now.Unix(), endTime.Unix())
suite.Require().NoError(err)

suite.authKeeper.EXPECT().GetAccount(suite.ctx, accAddrs[0]).Return(vacc)
e := errorsmod.Wrapf(
sdkerrors.ErrInsufficientFunds,
"spendable balance 0stake is smaller than %s",
coin,
)
suite.Require().EqualError(suite.bankKeeper.SendCoins(suite.ctx, accAddrs[0], accAddrs[1], sendCoins), e.Error())
}

func (suite *KeeperTestSuite) TestValidateBalance() {
ctx := suite.ctx
require := suite.Require()
Expand Down
4 changes: 4 additions & 0 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -237,6 +238,9 @@ func (k BaseSendKeeper) subUnlockedCoins(ctx context.Context, addr sdk.AccAddres
}

if _, hasNeg := spendable.SafeSub(coin); hasNeg {
if len(spendable) == 0 {
spendable = sdk.Coins{sdk.NewCoin(coin.Denom, math.ZeroInt())}
}
return errorsmod.Wrapf(
sdkerrors.ErrInsufficientFunds,
"spendable balance %s is smaller than %s",
Expand Down

0 comments on commit 7e7e15d

Please sign in to comment.