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

Conversation

DongLieu
Copy link
Contributor

@DongLieu DongLieu commented Oct 25, 2023

Similar to #18066, I think there should also be an api for Uint

Summary by CodeRabbit

  • New Feature: Introduced a new functionality that allows for the conversion of 'Uint' to 'big.Int' in a mutable way. This feature enhances the flexibility of data manipulation, providing users with more control over their data operations.
  • Test: Added a new test to ensure the correct behavior of the new mutable conversion feature, ensuring its reliability and stability.
  • Documentation: Updated the changelog to reflect the new changes, providing users with up-to-date information about the system's capabilities.

@DongLieu DongLieu requested a review from a team as a code owner October 25, 2023 04:19
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 25, 2023

Walkthrough

This change introduces mutative APIs for Uint.BigInt() and Int.BigInt() in the Cosmos SDK, allowing for more efficient memory usage. It also includes corresponding test cases to ensure the correct behavior of these new methods.

Changes

File Summary
math/uint.go Added a new method BigIntMut() to the Uint struct, which converts the Uint to a big.Int mutatively. Returns nil if the Uint is uninitialized.
math/uint_test.go Added a new test function TestConvertToBigIntMutativeForUint to the uintTestSuite test suite. This function verifies the behavior of the new BigIntMut() method.
math/CHANGELOG.md Documented the addition of the mutative APIs for Uint.BigInt() and Int.BigInt(), referencing the corresponding pull requests.

"In the land of code, where the shadows lie,

A rabbit hopped, its eyes on the sky.

With each soft thump, a change was made,

In the realm of bits, where memories fade.

Mutative methods, a gift so grand,

Now in the Cosmos, by the rabbit's hand. 🐇💻🚀"


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fd93ee7 and 5983e90.
Files selected for processing (2)
  • math/uint.go (1 hunks)
  • math/uint_test.go (1 hunks)
Additional comments: 1
math/uint_test.go (1)
  • 102-120: The new test TestConvertToBigIntMutativeForUint is well-structured and covers the expected behavior of the BigIntMut() method. It checks that the BigIntMut() and BigInt() methods return the same initial value, and that modifying the BigIntMut() pointer updates both BigIntMut() and BigInt(), while modifying the BigInt() pointer does not affect either. This is a good practice to ensure the mutative behavior of the BigIntMut() method is working as expected. However, it would be beneficial to add a few more test cases to cover edge cases, such as when the Uint is uninitialized.
+ // Add test case for uninitialized Uint
+ func (s *uintTestSuite) TestConvertToBigIntMutativeForUninitializedUint() {
+     var i sdkmath.Uint
+     s.Require().Nil(i.BigIntMut())
+ }
Committable suggestion (Beta)
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())
}

// Add test case for uninitialized Uint
func (s *uintTestSuite) TestConvertToBigIntMutativeForUninitializedUint() {
	var i sdkmath.Uint
	s.Require().Nil(i.BigIntMut())
}

Comment on lines +24 to +30
// BigInt converts Uint to big.Int, mutative the input
func (u Uint) BigIntMut() *big.Int {
if u.IsNil() {
return nil
}
return u.i
}
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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5983e90 and ca09efc.
Files selected for processing (1)
  • math/CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • math/CHANGELOG.md

@julienrbrt
Copy link
Member

@testinginprod is working on refactoring the API in #18099 to avoid a cluttered API full of XXXMut.

@DongLieu
Copy link
Contributor Author

Great
I need to close this PR, right? @julienrbrt

@DongLieu DongLieu closed this Oct 26, 2023
@julienrbrt julienrbrt reopened this Nov 6, 2023
@julienrbrt
Copy link
Member

Hi, we discussed internally, and we will wait for the API improvement mentioned here for math/v2.
If you still need this, please rebase and we will review again.

@DongLieu
Copy link
Contributor Author

DongLieu commented Nov 7, 2023

I merged with the main branch

@julienrbrt julienrbrt added this pull request to the merge queue Nov 7, 2023
Merged via the queue into cosmos:main with commit a6eea3c Nov 7, 2023
54 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants