diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index cdf45d6d7617..072fa738e0ad 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j ## [Unreleased] +### Features + +* [#17803](https://github.com/cosmos/cosmos-sdk/pull/17803) Add mutative api for Int.BigInt() + ### Bug Fixes * [#17725](https://github.com/cosmos/cosmos-sdk/pull/17725) Fix state break in ApproxRoot. This has been present since math/v1.0.1. It changed the rounding behavior at precision end in an intermediary division from banker's to truncation. The truncation occurs from binary right shift in the case of square roots. The change is now reverted back to banker's rounding universally for any root. diff --git a/math/int.go b/math/int.go index 1b10781b95f0..8cb20c5c5cf1 100644 --- a/math/int.go +++ b/math/int.go @@ -86,6 +86,14 @@ func (i Int) BigInt() *big.Int { return new(big.Int).Set(i.i) } +// BigInt converts Int to big.Int, mutative the input +func (i Int) BigIntMut() *big.Int { + if i.IsNil() { + return nil + } + return i.i +} + // IsNil returns true if Int is uninitialized func (i Int) IsNil() bool { return i.i == nil diff --git a/math/int_test.go b/math/int_test.go index f6c476410fa1..410e395511c4 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -56,6 +56,26 @@ func (s *intTestSuite) TestNewIntFromBigInt() { s.Require().NotEqual(r, i.BigInt()) } +func (s *intTestSuite) TestConvertToBigIntMutative() { + r := big.NewInt(42) + i := math.NewIntFromBigInt(r) + + // Compare value of BigInt & BigIntMut + s.Require().Equal(i.BigInt(), i.BigIntMut()) + + // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change + p := i.BigIntMut() + p.SetInt64(50) + s.Require().Equal(big.NewInt(50), i.BigIntMut()) + s.Require().Equal(big.NewInt(50), i.BigInt()) + + // Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change + p = i.BigInt() + p.SetInt64(60) + s.Require().NotEqual(big.NewInt(60), i.BigIntMut()) + s.Require().NotEqual(big.NewInt(60), i.BigInt()) +} + func (s *intTestSuite) TestIntPanic() { // Max Int = 2^256-1 = 1.1579209e+77 // Min Int = -(2^256-1) = -1.1579209e+77