Skip to content

Commit f37ab4a

Browse files
authored
Merge PR #3826: Port IsAllGT from safe-coins PR
2 parents 714168f + 862cc43 commit f37ab4a

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

types/coin.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,23 @@ func (coins Coins) safeAdd(coinsB Coins) Coins {
269269
}
270270
}
271271

272+
// ContainsDenomsOf returns true if coinsB' denom set
273+
// is subset of the receiver's denoms.
274+
func (coins Coins) ContainsDenomsOf(coinsB Coins) bool {
275+
// more denoms in B than in receiver
276+
if len(coinsB) > len(coins) {
277+
return false
278+
}
279+
280+
for _, coinB := range coinsB {
281+
if coins.AmountOf(coinB.Denom).IsZero() {
282+
return false
283+
}
284+
}
285+
286+
return true
287+
}
288+
272289
// Sub subtracts a set of coins from another.
273290
//
274291
// e.g.
@@ -297,12 +314,26 @@ func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
297314
// IsAllGT returns true if for every denom in coins, the denom is present at a
298315
// greater amount in coinsB.
299316
func (coins Coins) IsAllGT(coinsB Coins) bool {
300-
diff, _ := coins.SafeSub(coinsB)
301-
if len(diff) == 0 {
317+
if len(coins) == 0 {
302318
return false
303319
}
304320

305-
return diff.IsAllPositive()
321+
if len(coinsB) == 0 {
322+
return true
323+
}
324+
325+
if !coins.ContainsDenomsOf(coinsB) {
326+
return false
327+
}
328+
329+
for _, coinB := range coinsB {
330+
amountA, amountB := coins.AmountOf(coinB.Denom), coinB.Amount
331+
if !amountA.GT(amountB) {
332+
return false
333+
}
334+
}
335+
336+
return true
306337
}
307338

308339
// IsAllGTE returns true iff for every denom in coins, the denom is present at

types/coin_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ func TestCoins(t *testing.T) {
276276
mixedCase3 := Coins{
277277
{"gAs", NewInt(1)},
278278
}
279-
empty := Coins{
280-
{"gold", NewInt(0)},
281-
}
282-
null := Coins{}
279+
empty := NewCoins()
283280
badSort1 := Coins{
284281
{"tree", NewInt(1)},
285282
{"gas", NewInt(1)},
@@ -312,7 +309,7 @@ func TestCoins(t *testing.T) {
312309
assert.False(t, mixedCase2.IsValid(), "First Coins denoms contain upper case characters")
313310
assert.False(t, mixedCase3.IsValid(), "Single denom in Coins contains upper case characters")
314311
assert.True(t, good.IsAllPositive(), "Expected coins to be positive: %v", good)
315-
assert.False(t, null.IsAllPositive(), "Expected coins to not be positive: %v", null)
312+
assert.False(t, empty.IsAllPositive(), "Expected coins to not be positive: %v", empty)
316313
assert.True(t, good.IsAllGTE(empty), "Expected %v to be >= %v", good, empty)
317314
assert.False(t, good.IsAllLT(empty), "Expected %v to be < %v", good, empty)
318315
assert.True(t, empty.IsAllLT(good), "Expected %v to be < %v", empty, good)
@@ -331,7 +328,7 @@ func TestCoinsGT(t *testing.T) {
331328
assert.True(t, Coins{{testDenom1, one}}.IsAllGT(Coins{}))
332329
assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom1, one}}))
333330
assert.False(t, Coins{{testDenom1, one}}.IsAllGT(Coins{{testDenom2, one}}))
334-
assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGT(Coins{{testDenom2, one}}))
331+
assert.True(t, Coins{{testDenom1, one}, {testDenom2, two}}.IsAllGT(Coins{{testDenom2, one}}))
335332
assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllGT(Coins{{testDenom2, two}}))
336333
}
337334

@@ -358,7 +355,7 @@ func TestCoinsLT(t *testing.T) {
358355
assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom2, one}}))
359356
assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom2, two}}))
360357
assert.False(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom1, one}, {testDenom2, one}}))
361-
assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom1, one}, {testDenom2, two}}))
358+
assert.True(t, Coins{{testDenom1, one}, {testDenom2, one}}.IsAllLT(Coins{{testDenom1, two}, {testDenom2, two}}))
362359
assert.True(t, Coins{}.IsAllLT(Coins{{testDenom1, one}}))
363360
}
364361

0 commit comments

Comments
 (0)