Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: null guard for tx fee amounts #10327

Merged
merged 9 commits into from
Oct 12, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [\#10327](https://github.com/cosmos/cosmos-sdk/pull/10327) Add null guard for possible nil `Amount` in tx fee `Coins`
* [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Remove gogoproto `moretags` YAML annotations and add `sigs.k8s.io/yaml` for YAML marshalling.
* (x/bank) [\#10134](https://github.com/cosmos/cosmos-sdk/pull/10134) Add `HasDenomMetadata` function to bank `Keeper` to check if a client coin denom metadata exists in state.
* (store) [\#10026](https://github.com/cosmos/cosmos-sdk/pull/10026) Improve CacheKVStore datastructures / algorithms, to no longer take O(N^2) time when interleaving iterators and insertions.
Expand Down
18 changes: 18 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (coin Coin) IsNegative() bool {
return coin.Amount.Sign() == -1
}

// IsNil returns true if the coin amount is nil and false otherwise.
func (coin Coin) IsNil() bool {
return coin.Amount.i == nil
}

//-----------------------------------------------------------------------------
// Coins

Expand Down Expand Up @@ -590,6 +595,19 @@ func (coins Coins) IsAnyNegative() bool {
return false
}

// IsAnyNil returns true if there is at least one coin whose amount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in a rather cryptic redacted panic response when the basic validation checks fee Coins for negative amounts.

Out of curiosiry, what was the error shown in ValidateBasic before this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None. the IsAnyNegative() validation ran and panic'ed when reaching https://cs.opensource.google/go/go/+/master:src/math/big/int.go;l=39;drc=master called from it.

If you mean the non-redacted panic it was something like "invalid memory address or nil pointer dereference" iirc

// is nil; returns false otherwise. It returns false if the coin set
// is empty too.
func (coins Coins) IsAnyNil() bool {
for _, coin := range coins {
if coin.IsNil() {
return true
}
}

return false
}

// negative returns a set of coins with all amount negative.
//
// TODO: Remove once unsigned integers are used.
Expand Down
27 changes: 27 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ func (s *coinTestSuite) TestCoinIsZero() {
s.Require().False(res)
}

func (s *coinTestSuite) TestCoinIsNil() {
coin := sdk.Coin{}
res := coin.IsNil()
s.Require().True(res)

coin = sdk.Coin{Denom: "uatom"}
res = coin.IsNil()
s.Require().True(res)

coin = sdk.NewInt64Coin(testDenom1, 1)
res = coin.IsNil()
s.Require().False(res)
}

func (s *coinTestSuite) TestFilteredZeroCoins() {
cases := []struct {
name string
Expand Down Expand Up @@ -945,6 +959,19 @@ func (s *coinTestSuite) TestCoinsIsAnyGT() {
}
}

func (s *coinTestSuite) TestCoinsIsAnyNil() {
twoAtom := sdk.NewInt64Coin("atom", 2)
fiveAtom := sdk.NewInt64Coin("atom", 5)
threeEth := sdk.NewInt64Coin("eth", 3)
nilAtom := sdk.Coin{Denom: "atom"}

s.Require().True(sdk.Coins{twoAtom, fiveAtom, threeEth, nilAtom}.IsAnyNil())
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() {
cdc := codec.NewLegacyAmino()
sdk.RegisterLegacyAminoCodec(cdc)
Expand Down
7 changes: 7 additions & 0 deletions types/tx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (t *Tx) ValidateBasic() error {
)
}

if fee.Amount.IsAnyNil() {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFee,
"invalid fee provided: null",
)
}

if fee.Amount.IsAnyNegative() {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFee,
Expand Down