From e856bdb90afcd1fcb325d42dcc4dbc057c6594cf 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) (cherry picked from commit 3034a9d54a628b390e3cd947bf81526e73eb941b) # Conflicts: # CHANGELOG.md # types/coin.go # x/bank/keeper/keeper_test.go # x/bank/keeper/send.go --- CHANGELOG.md | 31 +++++++++++++++++++++++++++++++ types/coin.go | 4 ++++ x/bank/keeper/keeper_test.go | 25 +++++++++++++++++++++++++ x/bank/keeper/send.go | 19 +++++++++++++++---- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 059fb6b25f68..cae45c0eceae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,38 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +<<<<<<< HEAD * [#13673](https://github.com/cosmos/cosmos-sdk/pull/13673) Fix `--dry-run` flag not working when using tx command. +======= +* (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. +* (genutil) [#12140](https://github.com/cosmos/cosmos-sdk/pull/12140) Fix staking's genesis JSON migrate in the `simd migrate v0.46` CLI command. +* (types) [#12154](https://github.com/cosmos/cosmos-sdk/pull/12154) Add `baseAccountGetter` to avoid invalid account error when create vesting account. +* (x/authz) [#12184](https://github.com/cosmos/cosmos-sdk/pull/12184) Fix MsgExec not verifying the validity of nested messages. +* (x/staking) [#12303](https://github.com/cosmos/cosmos-sdk/pull/12303) Use bytes instead of string comparison in delete validator queue +* (store/rootmulti) [#12487](https://github.com/cosmos/cosmos-sdk/pull/12487) Fix non-deterministic map iteration. +* (sdk/dec_coins) [#12903](https://github.com/cosmos/cosmos-sdk/pull/12903) Fix nil `DecCoin` creation when converting `Coins` to `DecCoins` +* (store) [#12945](https://github.com/cosmos/cosmos-sdk/pull/12945) Fix nil end semantics in store/cachekv/iterator when iterating a dirty cache. +* (x/gov) [#13051](https://github.com/cosmos/cosmos-sdk/pull/13051) In SubmitPropsal, when a legacy msg fails it's handler call, wrap the error as ErrInvalidProposalContent (instead of ErrNoProposalHandlerExists). +* (x/gov) [#13045](https://github.com/cosmos/cosmos-sdk/pull/13045) Fix gov migrations for v3(0.46). +* (snapshot) [#13400](https://github.com/cosmos/cosmos-sdk/pull/13400) Fix snapshot checksum issue in golang 1.19. +* (x/gov) [#13728](https://github.com/cosmos/cosmos-sdk/pull/13728) Fix propagation of message events to the current context in `EndBlocker`. + +### Deprecated + +* (x/bank) [#11859](https://github.com/cosmos/cosmos-sdk/pull/11859) The Params.SendEnabled field is deprecated and unusable. + The information can now be accessed using the BankKeeper. + Setting can be done using MsgSetSendEnabled as a governance proposal. + A SendEnabled query has been added to both GRPC and CLI. + +## [v0.46.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.4) - 2022-11-01 + +### Features + +* (x/auth) [#13612](https://github.com/cosmos/cosmos-sdk/pull/13612) Add `Query/ModuleAccountByName` endpoint for accessing the module account info by module name. +>>>>>>> 3034a9d54 (fix(bank): fix unhandled error for vesting (#13690)) ### Improvements diff --git a/types/coin.go b/types/coin.go index 01358f414bc0..1b6a4042f6df 100644 --- a/types/coin.go +++ b/types/coin.go @@ -114,7 +114,11 @@ func (coin Coin) Sub(coinB Coin) Coin { res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)} if res.IsNegative() { +<<<<<<< HEAD panic("negative coin amount") +======= + return Coin{}, fmt.Errorf("negative coin amount: %s", res) +>>>>>>> 3034a9d54 (fix(bank): fix unhandled error for vesting (#13690)) } return res diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 6338a83c88ae..9cd1b331b503 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -453,8 +453,33 @@ func (suite *IntegrationTestSuite) TestSendCoins() { suite.Require().Equal(newBarCoin(25), coins[0], "expected only bar coins in the account balance, got: %v", coins) } +<<<<<<< HEAD func (suite *IntegrationTestSuite) TestValidateBalance() { app, ctx := suite.app, suite.ctx +======= +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() +>>>>>>> 3034a9d54 (fix(bank): fix unhandled error for vesting (#13690)) now := tmtime.Now() ctx = ctx.WithBlockHeader(tmproto.Header{Time: now}) endTime := now.Add(24 * time.Hour) diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index c0df6b960e7c..0ac3b7bb0526 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -179,17 +179,28 @@ 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) +<<<<<<< HEAD _, hasNeg := sdk.Coins{spendable}.SafeSub(sdk.Coins{coin}) +======= + spendable, hasNeg := sdk.Coins{balance}.SafeSub(locked) +>>>>>>> 3034a9d54 (fix(bank): fix unhandled error for vesting (#13690)) 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 } }