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

Make IsAllGTE() more consistent #3864

Merged
merged 3 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
alessio marked this conversation as resolved.
Show resolved Hide resolved

* #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 {
alessio marked this conversation as resolved.
Show resolved Hide resolved
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