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

feat(types): better tests for math and correct decimal -> integer conversion in x/ecocredit #783

Merged
merged 1 commit into from
Feb 17, 2022
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
1 change: 1 addition & 0 deletions types/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/cockroachdb/apd/v2 v2.0.2
github.com/cosmos/cosmos-sdk v0.44.2
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
Expand Down
35 changes: 30 additions & 5 deletions types/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package math

import (
"fmt"
"math/big"

"github.com/cockroachdb/apd/v2"
"github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -19,7 +20,11 @@ type Dec struct {

const mathCodespace = "math"

var ErrInvalidDecString = errors.Register(mathCodespace, 1, "invalid decimal string")
var (
ErrInvalidDecString = errors.Register(mathCodespace, 1, "invalid decimal string")
ErrUnexpectedRounding = errors.Register(mathCodespace, 2, "unexpected rounding")
ErrNonIntegeral = errors.Register(mathCodespace, 3, "value is non-integral")
)

// In cosmos-sdk#7773, decimal128 (with 34 digits of precision) was suggested for performing
// Quo/Mult arithmetic generically across the SDK. Even though the SDK
Expand Down Expand Up @@ -121,25 +126,29 @@ func (x Dec) Quo(y Dec) (Dec, error) {
return z, errors.Wrap(err, "decimal quotient error")
}

// MulExact returns a new dec with value x * y. The product must not round or an error will be returned.
// MulExact returns a new dec with value x * y. The product must not round or
// ErrUnexpectedRounding will be returned.
func (x Dec) MulExact(y Dec) (Dec, error) {
var z Dec
condition, err := dec128Context.Mul(&z.dec, &x.dec, &y.dec)
if err != nil {
return z, err
}
if condition.Rounded() {
return z, errors.Wrap(err, "exact decimal product error")
return z, ErrUnexpectedRounding
}
return z, nil
}

// QuoExact is a version of Quo that returns an error if any rounding occurred.
// QuoExact is a version of Quo that returns ErrUnexpectedRounding if any rounding occurred.
func (x Dec) QuoExact(y Dec) (Dec, error) {
var z Dec
condition, err := dec128Context.Quo(&z.dec, &x.dec, &y.dec)
if err != nil {
return z, err
}
if condition.Rounded() {
return z, errors.Wrap(err, "exact decimal quotient error")
return z, ErrUnexpectedRounding
}
return z, errors.Wrap(err, "decimal quotient error")
}
Expand Down Expand Up @@ -168,10 +177,24 @@ func (x Dec) Mul(y Dec) (Dec, error) {
return z, errors.Wrap(err, "decimal multiplication error")
}

// Int64 converts x to an int64 or returns an error if x cannot
// fit precisely into an int64.
func (x Dec) Int64() (int64, error) {
return x.dec.Int64()
}

// BigInt converts x to a *big.Int or returns an error if x cannot
// fit precisely into an *big.Int.
func (x Dec) BigInt() (*big.Int, error) {
y, _ := x.Reduce()
z := &big.Int{}
z, ok := z.SetString(y.String(), 10)
if !ok {
return nil, ErrNonIntegeral
}
return z, nil
}

func (x Dec) String() string {
return x.dec.Text('f')
}
Expand Down Expand Up @@ -205,6 +228,8 @@ func (x Dec) NumDecimalPlaces() uint32 {
return uint32(-exp)
}

// Reduce returns a copy of x with all trailing zeros removed and the number
// of trailing zeros removed.
func (x Dec) Reduce() (Dec, int) {
y := Dec{}
_, n := y.dec.Reduce(&x.dec)
Expand Down
111 changes: 111 additions & 0 deletions types/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func TestDec(t *testing.T) {
t.Run("TestAddSub", rapid.MakeCheck(testAddSub))
t.Run("TestMulQuoA", rapid.MakeCheck(testMulQuoA))
t.Run("TestMulQuoB", rapid.MakeCheck(testMulQuoB))
t.Run("TestMulQuoExact", rapid.MakeCheck(testMulQuoExact))
t.Run("TestQuoMulExact", rapid.MakeCheck(testQuoMulExact))

// Properties about comparision and equality
t.Run("TestCmpInverse", rapid.MakeCheck(testCmpInverse))
Expand Down Expand Up @@ -464,6 +466,44 @@ func testMulQuoB(t *rapid.T) {
require.True(t, a.IsEqual(d))
}

// Property: (a * 10^b) / 10^b == a using MulExact and QuoExact
// and a with no more than b decimal places (b <= 32).
func testMulQuoExact(t *rapid.T) {
b := rapid.Uint32Range(0, 32).Draw(t, "b").(uint32)
decPrec := func(d Dec) bool { return d.NumDecimalPlaces() <= b }
a := genDec.Filter(decPrec).Draw(t, "a").(Dec)

c := NewDecFinite(1, int32(b))

d, err := a.MulExact(c)
require.NoError(t, err)

e, err := d.QuoExact(c)
require.NoError(t, err)

require.True(t, a.IsEqual(e))
}

// Property: (a / b) * b == a using QuoExact and MulExact and
// a as an integer.
func testQuoMulExact(t *rapid.T) {
a := rapid.Uint64().Draw(t, "a").(uint64)
aDec, err := NewDecFromString(fmt.Sprintf("%d", a))
require.NoError(t, err)
b := rapid.Uint32Range(0, 32).Draw(t, "b").(uint32)
c := NewDecFinite(1, int32(b))

require.NoError(t, err)

d, err := aDec.QuoExact(c)
require.NoError(t, err)

e, err := d.MulExact(c)
require.NoError(t, err)

require.True(t, aDec.IsEqual(e))
}

// Property: Cmp(a, b) == -Cmp(b, a)
func testCmpInverse(t *rapid.T) {
a := genDec.Draw(t, "a").(Dec)
Expand Down Expand Up @@ -550,3 +590,74 @@ func floatDecimalPlaces(t *rapid.T, f float64) uint32 {
return uint32(res)
}
}

func TestReduce(t *testing.T) {
a, err := NewDecFromString("1.30000")
require.NoError(t, err)
b, n := a.Reduce()
require.Equal(t, 4, n)
require.True(t, a.IsEqual(b))
require.Equal(t, "1.3", b.String())
}

func TestMulExactGood(t *testing.T) {
a, err := NewDecFromString("1.000001")
require.NoError(t, err)
b := NewDecFinite(1, 6)
c, err := a.MulExact(b)
require.NoError(t, err)
d, err := c.Int64()
require.NoError(t, err)
require.Equal(t, int64(1000001), d)
}

func TestMulExactBad(t *testing.T) {
a, err := NewDecFromString("1.000000000000000000000000000000000000123456789")
require.NoError(t, err)
b := NewDecFinite(1, 10)
_, err = a.MulExact(b)
require.ErrorIs(t, err, ErrUnexpectedRounding)
}

func TestQuoExactGood(t *testing.T) {
a, err := NewDecFromString("1000001")
require.NoError(t, err)
b := NewDecFinite(1, 6)
c, err := a.QuoExact(b)
require.NoError(t, err)
require.Equal(t, "1.000001", c.String())
}

func TestQuoExactBad(t *testing.T) {
a, err := NewDecFromString("1000000000000000000000000000000000000123456789")
require.NoError(t, err)
b := NewDecFinite(1, 10)
_, err = a.QuoExact(b)
require.ErrorIs(t, err, ErrUnexpectedRounding)
}

func TestToBigInt(t *testing.T) {
intStr := "1000000000000000000000000000000000000123456789"
a, err := NewDecFromString(intStr)
require.NoError(t, err)
b, err := a.BigInt()
require.Equal(t, intStr, b.String())

intStrWithTrailingZeros := "1000000000000000000000000000000000000123456789.00000000"
a, err = NewDecFromString(intStrWithTrailingZeros)
require.NoError(t, err)
b, err = a.BigInt()
require.Equal(t, intStr, b.String())

intStr2 := "123.456e6"
a, err = NewDecFromString(intStr2)
require.NoError(t, err)
b, err = a.BigInt()
require.Equal(t, "123456000", b.String())

intStr3 := "12345.6"
a, err = NewDecFromString(intStr3)
require.NoError(t, err)
_, err = a.BigInt()
require.ErrorIs(t, err, ErrNonIntegeral)
}
4 changes: 2 additions & 2 deletions x/ecocredit/server/basket/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ func creditAmountToBasketCoins(creditAmt regenmath.Dec, exp uint32, denom string
return coins, err
}

i64Amt, err := tokenAmt.Int64()
amtInt, err := tokenAmt.BigInt()
if err != nil {
return coins, err
}

return sdk.Coins{sdk.NewCoin(denom, sdk.NewInt(i64Amt))}, nil
return sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(amtInt))}, nil
}