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

R4R: Fix coins.IsLT() impl #2686

Merged
merged 8 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ BUG FIXES

* SDK
- #2625 [x/gov] fix AppendTag function usage error
- #2674 [types] Fix coins.IsLT() impl


* Tendermint
50 changes: 40 additions & 10 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func (coins Coins) Plus(coinsB Coins) Coins {
coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1:
sum = append(sum, coinA)
if coinA.IsZero() {
// ignore 0 sum coin type
} else {
sum = append(sum, coinA)
}
indexA++
case 0:
if coinA.Amount.Add(coinB.Amount).IsZero() {
Expand All @@ -153,7 +157,11 @@ func (coins Coins) Plus(coinsB Coins) Coins {
indexA++
indexB++
case 1:
sum = append(sum, coinB)
if coinB.IsZero() {
// ignore 0 sum coin type
} else {
sum = append(sum, coinB)
}
indexB++
}
}
Expand All @@ -176,9 +184,18 @@ func (coins Coins) Minus(coinsB Coins) Coins {
return coins.Plus(coinsB.Negative())
}

// IsGTE returns True iff coins is NonNegative(), and for every
// currency in coinsB, the currency is present at an equal or greater
// amount in coinsB
// IsGTE returns True iff for every denom in coins, the denom is present at a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// IsGTE returns True iff for every denom in coins, the denom is present at a
// IsGT returns True iff for every denom in coins, the denom is present at a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't quite understand these APIs.

assert.True(t, Coins{{"A", one}, {"B", one}}.IsGT(Coins{{"B", one}}))

A is not present in the "other" coins. Shouldn't this return false?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO no, the function's name should be AllGT to mean that the comparison is performed on all denoms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the godoc should be updated.

// greater amount in coinsB.
func (coins Coins) IsGT(coinsB Coins) bool {
diff := coins.Minus(coinsB)
if len(diff) == 0 {
return false
}
return diff.IsPositive()
}

// IsGTE returns True iff for every denom in coins, the denom is present at an
// equal or greater amount in coinsB.
func (coins Coins) IsGTE(coinsB Coins) bool {
diff := coins.Minus(coinsB)
if len(diff) == 0 {
Expand All @@ -187,14 +204,27 @@ func (coins Coins) IsGTE(coinsB Coins) bool {
return diff.IsNotNegative()
}

// IsLT returns True iff every currency in coins, the currency is
// present at a smaller amount in coins
// IsLT returns True iff for every denom in coins, the denom is present at
// a smaller amount in coinsB.
func (coins Coins) IsLT(coinsB Coins) bool {
return !coins.IsGTE(coinsB)
diff := coinsB.Minus(coins)
if len(diff) == 0 {
return false
}
return diff.IsPositive()
}

// IsLTE returns True iff for every denom in coins, the denom is present at
// a smaller or equal amount in coinsB.
func (coins Coins) IsLTE(coinsB Coins) bool {
diff := coinsB.Minus(coins)
if len(diff) == 0 {
return true
}
return diff.IsNotNegative()
}

// IsZero returns true if there are no coins
// or all coins are zero.
// IsZero returns true if there are no coins or all coins are zero.
func (coins Coins) IsZero() bool {
for _, coin := range coins {
if !coin.IsZero() {
Expand Down
54 changes: 54 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,60 @@ func TestCoins(t *testing.T) {

}

func TestCoinsGT(t *testing.T) {
one := NewInt(1)
two := NewInt(2)

assert.False(t, Coins{}.IsGT(Coins{}))
assert.True(t, Coins{{"A", one}}.IsGT(Coins{}))
assert.False(t, Coins{{"A", one}}.IsGT(Coins{{"A", one}}))
assert.False(t, Coins{{"A", one}}.IsGT(Coins{{"B", one}}))
assert.True(t, Coins{{"A", one}, {"B", one}}.IsGT(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsGT(Coins{{"B", two}}))
}

func TestCoinsGTE(t *testing.T) {
one := NewInt(1)
two := NewInt(2)

assert.True(t, Coins{}.IsGTE(Coins{}))
assert.True(t, Coins{{"A", one}}.IsGTE(Coins{}))
assert.True(t, Coins{{"A", one}}.IsGTE(Coins{{"A", one}}))
assert.False(t, Coins{{"A", one}}.IsGTE(Coins{{"B", one}}))
assert.True(t, Coins{{"A", one}, {"B", one}}.IsGTE(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsGTE(Coins{{"B", two}}))
}

func TestCoinsLT(t *testing.T) {
one := NewInt(1)
two := NewInt(2)

assert.False(t, Coins{}.IsLT(Coins{}))
assert.False(t, Coins{{"A", one}}.IsLT(Coins{}))
assert.False(t, Coins{{"A", one}}.IsLT(Coins{{"A", one}}))
assert.False(t, Coins{{"A", one}}.IsLT(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsLT(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsLT(Coins{{"B", two}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsLT(Coins{{"A", one}, {"B", one}}))
assert.True(t, Coins{{"A", one}, {"B", one}}.IsLT(Coins{{"A", one}, {"B", two}}))
assert.True(t, Coins{}.IsLT(Coins{{"A", one}}))
}

func TestCoinsLTE(t *testing.T) {
one := NewInt(1)
two := NewInt(2)

assert.True(t, Coins{}.IsLTE(Coins{}))
assert.False(t, Coins{{"A", one}}.IsLTE(Coins{}))
assert.True(t, Coins{{"A", one}}.IsLTE(Coins{{"A", one}}))
assert.False(t, Coins{{"A", one}}.IsLTE(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsLTE(Coins{{"B", one}}))
assert.False(t, Coins{{"A", one}, {"B", one}}.IsLTE(Coins{{"B", two}}))
assert.True(t, Coins{{"A", one}, {"B", one}}.IsLTE(Coins{{"A", one}, {"B", one}}))
assert.True(t, Coins{{"A", one}, {"B", one}}.IsLTE(Coins{{"A", one}, {"B", two}}))
assert.True(t, Coins{}.IsLTE(Coins{{"A", one}}))
}

func TestPlusCoins(t *testing.T) {
one := NewInt(1)
zero := NewInt(0)
Expand Down
3 changes: 2 additions & 1 deletion x/auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ func ensureSufficientMempoolFees(ctx sdk.Context, stdTx StdTx) sdk.Result {
// TODO: Make the gasPrice not a constant, and account for tx size.
requiredFees := adjustFeesByGas(ctx.MinimumFees(), stdTx.Fee.Gas)

if !ctx.MinimumFees().IsZero() && stdTx.Fee.Amount.IsLT(requiredFees) {
// NOTE: !A.IsGTE(B) is not the same as A.IsLT(B).
if !ctx.MinimumFees().IsZero() && !stdTx.Fee.Amount.IsGTE(requiredFees) {
// validators reject any tx from the mempool with less than the minimum fee per gas * gas factor
return sdk.ErrInsufficientFee(fmt.Sprintf(
"insufficient fee, got: %q required: %q", stdTx.Fee.Amount, requiredFees)).Result()
Expand Down