From 5983e90abb1915a198252f49890fb68dcec6d4f9 Mon Sep 17 00:00:00 2001 From: DongLieu Date: Wed, 25 Oct 2023 11:13:49 +0700 Subject: [PATCH 1/2] add other BigIntMut api for math.Uint --- math/uint.go | 8 ++++++++ math/uint_test.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/math/uint.go b/math/uint.go index be588b0cd342..97177ea7f0fa 100644 --- a/math/uint.go +++ b/math/uint.go @@ -21,6 +21,14 @@ func (u Uint) BigInt() *big.Int { return new(big.Int).Set(u.i) } +// BigInt converts Uint to big.Int, mutative the input +func (u Uint) BigIntMut() *big.Int { + if u.IsNil() { + return nil + } + return u.i +} + // IsNil returns true if Uint is uninitialized func (u Uint) IsNil() bool { return u.i == nil diff --git a/math/uint_test.go b/math/uint_test.go index b75b7514a584..93abdf886dae 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -99,6 +99,26 @@ func (s *uintTestSuite) TestIsNil() { s.Require().True(sdkmath.Uint{}.IsNil()) } +func (s *uintTestSuite) TestConvertToBigIntMutativeForUint() { + r := big.NewInt(30) + i := sdkmath.NewUintFromBigInt(r) + + // Compare value of BigInt & BigIntMut + s.Require().Equal(i.BigInt(), i.BigIntMut()) + + // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change + p1 := i.BigIntMut() + p1.SetInt64(40) + s.Require().Equal(big.NewInt(40), i.BigIntMut()) + s.Require().Equal(big.NewInt(40), i.BigInt()) + + // Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change + p2 := i.BigInt() + p2.SetInt64(50) + s.Require().NotEqual(big.NewInt(50), i.BigIntMut()) + s.Require().NotEqual(big.NewInt(50), i.BigInt()) +} + func (s *uintTestSuite) TestArithUint() { for d := 0; d < 1000; d++ { n1 := uint64(rand.Uint32()) From ca09efc9a691519f38f35572d35c372ea6ecb20f Mon Sep 17 00:00:00 2001 From: DongLieu Date: Wed, 25 Oct 2023 11:21:11 +0700 Subject: [PATCH 2/2] changelog --- math/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 71e3f4b9b4b7..be97b7f21e48 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -38,6 +38,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j ### Features +* [#18247](https://github.com/cosmos/cosmos-sdk/pull/18247) Add mutative api for Uint.BigInt() * [#17803](https://github.com/cosmos/cosmos-sdk/pull/17803) Add mutative api for Int.BigInt() ### Bug Fixes