Multiple fees in min fees to OR instead of AND (#3239)
This commit is contained in:
committed by
Jack Zampolin
parent
74aa9844fc
commit
ed2b6bd9a7
+59
-1
@@ -265,7 +265,29 @@ func (coins Coins) SafeMinus(coinsB Coins) (Coins, bool) {
|
||||
return diff, !diff.IsNotNegative()
|
||||
}
|
||||
|
||||
// IsAllGT returns true iff for every denom in coins, the denom is present at a
|
||||
// IsAnyGT returns true if coins contains at least one denom
|
||||
// that is present at a smaller amount in coinsB; it
|
||||
// returns false otherwise.
|
||||
func (coins Coins) IsAnyGT(coinsB Coins) bool {
|
||||
intersection := coins.intersect(coinsB)
|
||||
if intersection.Empty() {
|
||||
return false
|
||||
}
|
||||
|
||||
diff, _ := intersection.SafeMinus(coinsB)
|
||||
if len(diff) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, coin := range diff {
|
||||
if coin.IsPositive() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsAllGT returns true if for every denom in coins, the denom is present at a
|
||||
// greater amount in coinsB.
|
||||
func (coins Coins) IsAllGT(coinsB Coins) bool {
|
||||
diff, _ := coins.SafeMinus(coinsB)
|
||||
@@ -276,6 +298,28 @@ func (coins Coins) IsAllGT(coinsB Coins) bool {
|
||||
return diff.IsPositive()
|
||||
}
|
||||
|
||||
// IsAnyGT returns true if coins contains at least one denom
|
||||
// that is present at a smaller or equal amount in coinsB; it
|
||||
// returns false otherwise.
|
||||
func (coins Coins) IsAnyGTE(coinsB Coins) bool {
|
||||
intersection := coins.intersect(coinsB)
|
||||
if intersection.Empty() {
|
||||
return false
|
||||
}
|
||||
|
||||
diff, _ := intersection.SafeMinus(coinsB)
|
||||
if len(diff) == 0 || len(diff) < len(intersection) { // zero diff is removed from the diff set
|
||||
return true
|
||||
}
|
||||
|
||||
for _, coin := range diff {
|
||||
if coin.IsNotNegative() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsAllGTE returns true iff for every denom in coins, the denom is present at
|
||||
// an equal or greater amount in coinsB.
|
||||
func (coins Coins) IsAllGTE(coinsB Coins) bool {
|
||||
@@ -430,6 +474,20 @@ func removeZeroCoins(coins Coins) Coins {
|
||||
return coins[:i]
|
||||
}
|
||||
|
||||
func (coins Coins) intersect(coinsB Coins) Coins {
|
||||
intersection := Coins{}
|
||||
for _, coin := range coins {
|
||||
for _, bCoin := range coinsB {
|
||||
if coin.Denom == bCoin.Denom {
|
||||
intersection = append(intersection, coin)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return intersection
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sort interface
|
||||
|
||||
|
||||
@@ -368,6 +368,28 @@ func TestCoinsLTE(t *testing.T) {
|
||||
assert.True(t, Coins{}.IsAllLTE(Coins{{"a", one}}))
|
||||
}
|
||||
|
||||
func TestCoinsIsAnyGT(t *testing.T) {
|
||||
one := NewInt(1)
|
||||
two := NewInt(2)
|
||||
assert.False(t, Coins{}.IsAnyGT(Coins{}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAnyGT(Coins{}))
|
||||
assert.False(t, Coins{}.IsAnyGT(Coins{{"a", one}}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAnyGT(Coins{{"a", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", two}}.IsAnyGT(Coins{{"a", one}, {"b", one}}))
|
||||
assert.True(t, Coins{{"a", two}, {"b", one}}.IsAnyGT(Coins{{"a", one}, {"b", two}}))
|
||||
}
|
||||
|
||||
func TestCoinsIsAnyGTE(t *testing.T) {
|
||||
one := NewInt(1)
|
||||
two := NewInt(2)
|
||||
assert.False(t, Coins{}.IsAnyGTE(Coins{}))
|
||||
assert.False(t, Coins{{"a", one}}.IsAnyGTE(Coins{}))
|
||||
assert.False(t, Coins{}.IsAnyGTE(Coins{{"a", one}}))
|
||||
assert.True(t, Coins{{"a", one}}.IsAnyGTE(Coins{{"a", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", two}}.IsAnyGTE(Coins{{"a", one}, {"b", one}}))
|
||||
assert.True(t, Coins{{"a", one}, {"b", one}}.IsAnyGTE(Coins{{"a", one}, {"b", two}}))
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
one := NewInt(1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user