Skip to content

Commit

Permalink
launchpad: backport BigInt fix (#7087)
Browse files Browse the repository at this point in the history
Backported from #7084

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
fedekunze and Alessio Treglia authored Aug 20, 2020
1 parent 86f953b commit 4d3afea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## [v0.39.2]

### 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.
Expand Down
10 changes: 10 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Int)
}

// addition
func (d Dec) Add(d2 Dec) Dec {
res := new(big.Int).Add(d.Int, d2.Int)
Expand Down
8 changes: 8 additions & 0 deletions types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
2 changes: 2 additions & 0 deletions types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4d3afea

Please sign in to comment.