diff --git a/CHANGELOG.md b/CHANGELOG.md index 9931e9ea6e7..88e832e294a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -162,6 +162,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (bank) [#13691](https://github.com/cosmos/cosmos-sdk/issues/13691) Fix unhandled error for vesting account transfers, when total vesting amount exceeds total balance. * [#13553](https://github.com/cosmos/cosmos-sdk/pull/13553) Ensure all parameter validation for decimal types handles nil decimal values. * [#13145](https://github.com/cosmos/cosmos-sdk/pull/13145) Fix panic when calling `String()` to a Record struct type. * [#13116](https://github.com/cosmos/cosmos-sdk/pull/13116) Fix a dead-lock in the `Group-TotalWeight` `x/group` invariant. diff --git a/types/coin.go b/types/coin.go index cf86af8c039..8e0ee7f86b7 100644 --- a/types/coin.go +++ b/types/coin.go @@ -134,7 +134,7 @@ func (coin Coin) SafeSub(coinB Coin) (Coin, error) { res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)} if res.IsNegative() { - return Coin{}, fmt.Errorf("negative coin amount") + return Coin{}, fmt.Errorf("negative coin amount: %s", res) } return res, nil diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 14fa86537af..45746bd39ec 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -567,6 +567,25 @@ func (suite *KeeperTestSuite) TestSendCoins() { require.Equal(newBarCoin(25), coins[0], "expected only bar coins in the account balance, got: %v", coins) } +func (suite *KeeperTestSuite) TestSendCoins_Invalid_SendLockedCoins() { + balances := sdk.NewCoins(newFooCoin(50)) + + now := tmtime.Now() + endTime := now.Add(24 * time.Hour) + + origCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) + sendCoins := sdk.NewCoins(sdk.NewInt64Coin("stake", 50)) + + acc0 := authtypes.NewBaseAccountWithAddress(accAddrs[0]) + vacc := vesting.NewContinuousVestingAccount(acc0, origCoins, now.Unix(), endTime.Unix()) + + suite.mockFundAccount(accAddrs[1]) + suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, suite.ctx, accAddrs[1], balances)) + + suite.authKeeper.EXPECT().GetAccount(suite.ctx, accAddrs[0]).Return(vacc) + suite.Require().Error(suite.bankKeeper.SendCoins(suite.ctx, accAddrs[0], accAddrs[1], sendCoins)) +} + func (suite *KeeperTestSuite) TestValidateBalance() { ctx := suite.ctx require := suite.Require() diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index afaf44b4d15..dad56a3f882 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -233,17 +233,24 @@ func (k BaseSendKeeper) subUnlockedCoins(ctx sdk.Context, addr sdk.AccAddress, a for _, coin := range amt { balance := k.GetBalance(ctx, addr, coin.Denom) locked := sdk.NewCoin(coin.Denom, lockedCoins.AmountOf(coin.Denom)) - spendable := balance.Sub(locked) - _, hasNeg := sdk.Coins{spendable}.SafeSub(coin) + spendable, hasNeg := sdk.Coins{balance}.SafeSub(locked) if hasNeg { - return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "%s is smaller than %s", spendable, coin) + return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, + "locked amount exceeds account balance funds: %s > %s", locked, balance) + } + + if _, hasNeg := spendable.SafeSub(coin); hasNeg { + return sdkerrors.Wrapf( + sdkerrors.ErrInsufficientFunds, + "spendable balance %s is smaller than %s", + spendable, coin, + ) } newBalance := balance.Sub(coin) - err := k.setBalance(ctx, addr, newBalance) - if err != nil { + if err := k.setBalance(ctx, addr, newBalance); err != nil { return err } }