fix(bank): avoid empty spendable error message on send coins (backport #17170) (#17179)

Co-authored-by: mmsqe <mavis@crypto.com>
This commit is contained in:
mergify[bot] 2023-07-28 14:53:21 +00:00 committed by GitHub
parent 4d139f83d7
commit 7e7e15d6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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"
@ -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"
@ -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()

View File

@ -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"
@ -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",