From 7af11ece9297040aefa17bba7d7225894ce090a6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 12 Mar 2019 19:13:36 +0100 Subject: [PATCH] Merge PR #3864: Make IsAllGTE() more consistent * Make IsAllGTE() more consistent Co-Authored-By: alessio --- PENDING.md | 2 ++ types/coin.go | 20 +++++++++++++++----- types/coin_test.go | 14 ++------------ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/PENDING.md b/PENDING.md index f3669357c84d..3708b7bae7ea 100644 --- a/PENDING.md +++ b/PENDING.md @@ -48,6 +48,8 @@ ### SDK * [\#3820] Make Coins.IsAllGT() more robust and consistent. +* [\#3864] Make Coins.IsAllGTE() more consistent. + * #3801 `baseapp` saftey improvements ### Tendermint diff --git a/types/coin.go b/types/coin.go index 7d9b78e81c49..5a8292a8f58a 100644 --- a/types/coin.go +++ b/types/coin.go @@ -336,15 +336,25 @@ func (coins Coins) IsAllGT(coinsB Coins) bool { return true } -// IsAllGTE returns true iff for every denom in coins, the denom is present at -// an equal or greater amount in coinsB. +// IsAllGTE returns false if for any denom in coinsB, +// the denom is present at a smaller amount in coins; +// else returns true. func (coins Coins) IsAllGTE(coinsB Coins) bool { - diff, _ := coins.SafeSub(coinsB) - if len(diff) == 0 { + if len(coinsB) == 0 { return true } - return !diff.IsAnyNegative() + if len(coins) == 0 { + return false + } + + for _, coinB := range coinsB { + if coinB.Amount.GT(coins.AmountOf(coinB.Denom)) { + return false + } + } + + return true } // IsAllLT returns True iff for every denom in coins, the denom is present at diff --git a/types/coin_test.go b/types/coin_test.go index f9268b0190ae..030ad9b7beba 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -332,18 +332,6 @@ func TestCoinsGT(t *testing.T) { assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGT(Coins{{testDenom2, two}})) } -func TestCoinsGTE(t *testing.T) { - one := NewInt(1) - two := NewInt(2) - - assert.True(t, Coins{}.IsAllGTE(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{})) - assert.True(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom1, one}})) - assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom2, one}})) - assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, one}})) - assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, two}})) -} - func TestCoinsLT(t *testing.T) { one := NewInt(1) two := NewInt(2) @@ -543,6 +531,8 @@ func TestCoinsIsAllGTE(t *testing.T) { assert.True(t, Coins{}.IsAllGTE(Coins{})) assert.True(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{})) + assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, one}})) + assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGTE(Coins{{testDenom2, two}})) assert.False(t, Coins{}.IsAllGTE(Coins{{testDenom1, one}})) assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom1, two}})) assert.False(t, Coins{{testDenom1, one}}.IsAllGTE(Coins{{testDenom2, one}}))