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(math): add mutative api for Uint.BigInt() #18247

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment on lines +24 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new method BigIntMut() allows direct access to the underlying big.Int of the Uint struct. This can lead to potential issues with data integrity and encapsulation, as it allows external code to modify the internal state of the Uint struct. This is generally not a good practice in object-oriented programming, as it can lead to bugs and inconsistencies.

If you need to allow mutation of the big.Int value, consider providing methods on the Uint struct to perform these mutations, rather than exposing the internal state directly. This way, you can control how the internal state is modified and ensure that it remains consistent.

- // BigInt converts Uint to big.Int, mutative the input
- func (u Uint) BigIntMut() *big.Int {
-	if u.IsNil() {
-		return nil
-	}
-	return u.i
- }
Committable suggestion (Beta)
Suggested change
// BigInt converts Uint to big.Int, mutative the input
func (u Uint) BigIntMut() *big.Int {
if u.IsNil() {
return nil
}
return u.i
}
// Remove the BigIntMut method to maintain encapsulation


// IsNil returns true if Uint is uninitialized
func (u Uint) IsNil() bool {
return u.i == nil
Expand Down
20 changes: 20 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading