Skip to content

Commit

Permalink
Merge PR #3864: Make IsAllGTE() more consistent
Browse files Browse the repository at this point in the history
* Make IsAllGTE() more consistent

Co-Authored-By: alessio <[email protected]>
  • Loading branch information
alessio authored and cwgoes committed Mar 12, 2019
1 parent f97e85e commit 7af11ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
### SDK

* [\#3820] Make Coins.IsAllGT() more robust and consistent.
* [\#3864] Make Coins.IsAllGTE() more consistent.

* #3801 `baseapp` saftey improvements

### Tendermint
Expand Down
20 changes: 15 additions & 5 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}}))
Expand Down

0 comments on commit 7af11ec

Please sign in to comment.