From 3034a9d54a628b390e3cd947bf81526e73eb941b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Thu, 3 Nov 2022 19:22:13 +0100 Subject: [PATCH] fix(bank): fix unhandled error for vesting (#13690) ## Description Closes: #13691 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + types/coin.go | 2 +- x/bank/keeper/keeper_test.go | 19 +++++++++++++++++++ x/bank/keeper/send.go | 17 ++++++++++++----- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9931e9ea6e..88e832e294 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 cf86af8c03..8e0ee7f86b 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 14fa86537a..45746bd39e 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 afaf44b4d1..dad56a3f88 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 } }