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 LegacyDec.BigInt() #18421

Merged
merged 4 commits into from
Nov 12, 2023

Conversation

DongLieu
Copy link
Contributor

@DongLieu DongLieu commented Nov 9, 2023

I think there should also be an API for LegacyDec before improving the API for math/v2

Similar to #18066

Summary by CodeRabbit

The existing bullet-point list is still valid based on the provided instructions.

@DongLieu DongLieu requested a review from a team as a code owner November 9, 2023 09:35
Copy link
Contributor

coderabbitai bot commented Nov 9, 2023

Walkthrough
<details>
<summary>Walkthrough</summary>

# Walkthrough

The changes introduce a new mutative method `BigIntMut` to the `LegacyDec` type in the `cosmos-sdk` repository. This method converts `LegacyDec` to `*big.Int` mutatively. A corresponding test function `TestConvertToBigIntMutativeForLegacyDec` is also added to ensure the correct behavior of the new method. The `CHANGELOG.md` file is updated to reflect these additions and other mutative APIs for various methods.

# Changes

| File | Change Summary |
| --- | --- |
| `math/dec.go` | Introduced a new method `BigIntMut` to the `LegacyDec` type, which converts `LegacyDec` to `*big.Int` mutatively. |
| `math/dec_test.go` | Added a new test function `TestConvertToBigIntMutativeForLegacyDec` to the `decimalTestSuite` struct to test the new `BigIntMut` method. |
| `math/CHANGELOG.md` | Updated with the addition of mutative APIs for various methods such as `LegacyDec.BigInt()`, `Uint.BigInt()`, `Int.BigInt()`, and `NewIntFromBigInt`. |

</details>

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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 19e57dd and 0c9ed37.
Files selected for processing (2)
  • math/dec.go (1 hunks)
  • math/dec_test.go (1 hunks)
Additional comments: 1
math/dec_test.go (1)
  • 758-776: The test case is well written and covers the expected behavior of the BigIntMut method. It checks both the case where the BigIntMut pointer is modified and the case where the BigInt pointer is modified, ensuring that the behavior is as expected in both cases.

Comment on lines +238 to +245
// BigIntMut converts LegacyDec to big.Int, mutative the input
func (d LegacyDec) BigIntMut() *big.Int {
if d.IsNil() {
return nil
}

return d.i
}
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 9, 2023

Choose a reason for hiding this comment

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

The method BigIntMut is not actually mutating the LegacyDec instance. It's returning the internal big.Int instance which can be mutated, but the changes won't reflect back in the LegacyDec instance because big.Int is not a pointer type in LegacyDec. If you want to make it mutable, you should change the LegacyDec type to hold a pointer to big.Int instead of a value.

- type LegacyDec struct { i big.Int }
+ type LegacyDec struct { i *big.Int }

And then adjust all methods accordingly. Be aware that this is a breaking change and will require updates throughout the codebase where LegacyDec is used.


Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
// BigIntMut converts LegacyDec to big.Int, mutative the input
func (d LegacyDec) BigIntMut() *big.Int {
if d.IsNil() {
return nil
}
return d.i
}
// BigIntMut converts LegacyDec to big.Int, mutative the input
func (d LegacyDec) BigIntMut() *big.Int {
if d.IsNil() {
return nil
}
return d.i
}
type LegacyDec struct { i *big.Int }

Copy link
Member

Choose a reason for hiding this comment

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

Yes our api naming is weird, but this is consistent.

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt Thank you for your response. I understand that the naming is consistent with the existing API. However, the concern here is not just about the naming but the functionality of the BigIntMut method. As it stands, the method does not mutate the LegacyDec instance as the name suggests. Instead, it returns the internal big.Int instance which can be mutated, but the changes won't reflect back in the LegacyDec instance. This could lead to confusion and potential bugs in the future.

If the intention is to allow mutation of the LegacyDec instance through this method, I would recommend changing the LegacyDec type to hold a pointer to big.Int instead of a value. Please note that this would be a breaking change and would require updates throughout the codebase where LegacyDec is used.

Alternatively, if the intention is to simply return the internal big.Int instance without affecting the LegacyDec instance, it might be better to rename the method to something like BigIntCopy to avoid confusion.

I hope this clarifies the concern. Please let me know how you would like to proceed.

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 0c9ed37 and c3c6b82.
Files selected for processing (1)
  • math/CHANGELOG.md (1 hunks)
Additional comments: 1
math/CHANGELOG.md (1)
  • 40-46: The changelog correctly reflects the addition of the mutative APIs for LegacyDec.BigInt(), Uint.BigInt(), Int.BigInt(), and NewIntFromBigInt. Ensure that the documentation for these methods is updated to reflect the mutative behavior.

@@ -40,6 +40,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j

### Features

* [#18421](https://github.com/cosmos/cosmos-sdk/pull/18421) Add mutative api for `LegacyDec.BigInt()`.
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved under unreleased features.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I was confused

@julienrbrt julienrbrt self-assigned this Nov 9, 2023
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 c3c6b82 and ec764e1.
Files selected for processing (1)
  • math/CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • math/CHANGELOG.md

@julienrbrt julienrbrt added this pull request to the merge queue Nov 12, 2023
Merged via the queue into cosmos:main with commit 393eaef Nov 12, 2023
55 of 57 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.

3 participants