From 733073eedba3449d48debac1d6a5323a2a6462b4 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 18 Aug 2020 17:23:21 +0200 Subject: [PATCH 1/3] launchpad: backport BigInt fix --- CHANGELOG.md | 6 ++++++ types/decimal.go | 10 ++++++++++ types/int.go | 8 ++++++++ types/int_test.go | 2 ++ 4 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dadcf175e0ef..e31b859d4671 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## Unreleased + +### Bug Fixes + +* (types) [\#7084](https://github.com/cosmos/cosmos-sdk/pull/7084) Fix panic when calling `BigInt()` on an uninitialized `Int`. + ## [v0.39.1] * (x/auth) [\#6861](https://github.com/cosmos/cosmos-sdk/pull/6861) Remove public key Bech32 encoding for all account types for JSON serialization, instead relying on direct Amino encoding. In addition, JSON serialization utilizes Amino instead of the Go stdlib, so integers are treated as strings. diff --git a/types/decimal.go b/types/decimal.go index 839523080656..1575ebac473e 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -205,6 +205,16 @@ func (d Dec) LTE(d2 Dec) bool { return (d.Int).Cmp(d2.Int) <= 0 } // less func (d Dec) Neg() Dec { return Dec{new(big.Int).Neg(d.Int)} } // reverse the decimal sign func (d Dec) Abs() Dec { return Dec{new(big.Int).Abs(d.Int)} } // absolute value +// BigInt returns a copy of the underlying big.Int. +func (d Dec) BigInt() *big.Int { + if d.IsNil() { + return nil + } + + copy := new(big.Int) + return copy.Set(d) +} + // addition func (d Dec) Add(d2 Dec) Dec { res := new(big.Int).Add(d.Int, d2.Int) diff --git a/types/int.go b/types/int.go index 0029dfaa4eb4..5d15d069ad17 100644 --- a/types/int.go +++ b/types/int.go @@ -106,9 +106,17 @@ type Int struct { // BigInt converts Int to big.Int func (i Int) BigInt() *big.Int { + if i.IsNil() { + return nil + } return new(big.Int).Set(i.i) } +// IsNil returns true if Int is uninitialized +func (i Int) IsNil() bool { + return i.i == nil +} + // NewInt constructs Int from int64 func NewInt(n int64) Int { return Int{big.NewInt(n)} diff --git a/types/int_test.go b/types/int_test.go index 072b2f47bb9a..d23985467cdf 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -78,6 +78,8 @@ func TestIntPanic(t *testing.T) { // Division-by-zero check require.Panics(t, func() { i1.Quo(NewInt(0)) }) + + require.NotPanics(t, func() { Int{}.BigInt() }) } // Tests below uses randomness From 126f3d2073f4870cc1b8240cf5356d02ed0839e4 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 18 Aug 2020 17:26:39 +0200 Subject: [PATCH 2/3] fixes --- types/coin_benchmark_test.go | 8 ++++---- types/decimal.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/types/coin_benchmark_test.go b/types/coin_benchmark_test.go index 34e93d6229e4..6ceaaf35e598 100644 --- a/types/coin_benchmark_test.go +++ b/types/coin_benchmark_test.go @@ -12,10 +12,10 @@ func BenchmarkCoinsAdditionIntersect(b *testing.B) { coinsB := Coins(make([]Coin, numCoinsB)) for i := 0; i < numCoinsA; i++ { - coinsA[i] = NewCoin("COINZ_"+string(i), NewInt(int64(i))) + coinsA[i] = NewCoin("COINZ_"+fmt.Sprint(i), NewInt(int64(i))) } for i := 0; i < numCoinsB; i++ { - coinsB[i] = NewCoin("COINZ_"+string(i), NewInt(int64(i))) + coinsB[i] = NewCoin("COINZ_"+fmt.Sprint(i), NewInt(int64(i))) } b.ResetTimer() @@ -41,10 +41,10 @@ func BenchmarkCoinsAdditionNoIntersect(b *testing.B) { coinsB := Coins(make([]Coin, numCoinsB)) for i := 0; i < numCoinsA; i++ { - coinsA[i] = NewCoin("COINZ_"+string(numCoinsB+i), NewInt(int64(i))) + coinsA[i] = NewCoin("COINZ_"+fmt.Sprint(numCoinsB+i), NewInt(int64(i))) } for i := 0; i < numCoinsB; i++ { - coinsB[i] = NewCoin("COINZ_"+string(i), NewInt(int64(i))) + coinsB[i] = NewCoin("COINZ_"+fmt.Sprint(i), NewInt(int64(i))) } b.ResetTimer() diff --git a/types/decimal.go b/types/decimal.go index 1575ebac473e..03e27fc356d4 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -212,7 +212,7 @@ func (d Dec) BigInt() *big.Int { } copy := new(big.Int) - return copy.Set(d) + return copy.Set(d.Int) } // addition From 660ff8916ef354c128caf9de252cf75f03a5a83f Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Tue, 18 Aug 2020 18:41:40 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md Co-authored-by: Alessio Treglia --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e31b859d4671..54eb0a126939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog -## Unreleased +## [v0.39.2] ### Bug Fixes