From d6db24b2d43a4f7d68547bd5fc5212e636a860f2 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:47:54 +0200 Subject: [PATCH] fix: null guard for tx fee amounts (backport #10327) (#10342) * fix: null guard for tx fee amounts (#10327) ## Description It is possible to submit a TX with a fees object containing a Coin with a nil amount. This results in a rather cryptic redacted panic response when the basic validation checks fee Coins for negative amounts. This PR adds an additional check for nil to provide a friendlier error message. --- ### 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... - [x] 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 - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification (note: No issue exists) - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) (note: First PR against the SDK so please comment with what needs to be done) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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 96e162b8a3939c7999c0c12c39d196698594306c) # Conflicts: # CHANGELOG.md * solve conflicts * move sections Co-authored-by: Alex Megalokonomos Co-authored-by: marbar3778 --- CHANGELOG.md | 1 + types/coin.go | 2 +- types/coin_test.go | 1 + types/tx/types.go | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d999ef21db13..cc9a32b78b5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * [\#10262](https://github.com/cosmos/cosmos-sdk/pull/10262) Remove unnecessary logging in `x/feegrant` simulation. +* [\#10327](https://github.com/cosmos/cosmos-sdk/pull/10327) Add null guard for possible nil `Amount` in tx fee `Coins` ### Bug Fixes diff --git a/types/coin.go b/types/coin.go index f6627c4c3a81..fd81cb58203a 100644 --- a/types/coin.go +++ b/types/coin.go @@ -181,7 +181,7 @@ func (coin Coin) IsNegative() bool { // IsNil returns true if the coin amount is nil and false otherwise. func (coin Coin) IsNil() bool { - return coin.Amount.BigInt() == nil + return coin.Amount.i == nil } //----------------------------------------------------------------------------- diff --git a/types/coin_test.go b/types/coin_test.go index 0c746ffb6206..2f9873ab42a8 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -1319,6 +1319,7 @@ func (s *coinTestSuite) TestCoinsIsAnyNil() { s.Require().True(sdk.Coins{twoAtom, nilAtom, fiveAtom, threeEth}.IsAnyNil()) s.Require().True(sdk.Coins{nilAtom, twoAtom, fiveAtom, threeEth}.IsAnyNil()) s.Require().False(sdk.Coins{twoAtom, fiveAtom, threeEth}.IsAnyNil()) + } func (s *coinTestSuite) TestMarshalJSONCoins() { diff --git a/types/tx/types.go b/types/tx/types.go index b8a61946538c..ac2f01e404ca 100644 --- a/types/tx/types.go +++ b/types/tx/types.go @@ -66,7 +66,7 @@ func (t *Tx) ValidateBasic() error { } if fee.Amount.IsAnyNil() { - return errorsmod.Wrapf( + return sdkerrors.Wrapf( sdkerrors.ErrInsufficientFee, "invalid fee provided: null", )