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): fix and test LegacyToBigInt #18576

Closed
wants to merge 1 commit into from

Conversation

DongLieu
Copy link
Contributor

@DongLieu DongLieu commented Nov 28, 2023

Description

Closes: #18575


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • run make lint and make test
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Summary by CodeRabbit

  • Refactor

    • Updated methods for converting decimals to big.Int format, ensuring more predictable and safer operations.
  • Tests

    • Enhanced testing for decimal to big.Int conversions to validate new behavior and ensure accuracy.

@DongLieu DongLieu requested a review from a team as a code owner November 28, 2023 06:46
Copy link
Contributor

coderabbitai bot commented Nov 28, 2023

Walkthrough

Walkthrough

The changes involve updating the LegacyDec struct's methods in the Cosmos SDK to correctly handle the conversion to big.Int by dividing the internal integer representation by a precision factor. This ensures that when a big.Int is created or mutated from a LegacyDec, it represents the correct value accounting for the decimal's precision. The test suite has been updated to reflect these changes and to validate the new behavior.

Changes

File(s) Change Summary
math/dec.go Updated BigInt and BigIntMut methods to divide d.i by precisionReuse for correct big.Int conversion.
math/dec_test.go Modified tests to assert new behavior of BigInt and BigIntMut methods, including checks for mutative changes and value correctness.

Assessment against linked issues

Objective Addressed Explanation
Ensure LegacyToBigInt returns the correct value by implementing Quo with precisionReuse or precisionMultiplier(0) (#18575) The changes to BigInt and BigIntMut methods implement division by precisionReuse, which is equivalent to the requested Quo operation for returning the correct value.
Update tests to reflect changes in LegacyDec methods (#18575) The test suite has been updated to check the correctness of the new implementation of BigInt and BigIntMut methods.

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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7d5c2db and 7c762d0.
Files selected for processing (2)
  • math/dec.go (2 hunks)
  • math/dec_test.go (1 hunks)
Additional comments: 3
math/dec.go (2)
  • 234-234: Both BigInt and BigIntMut methods now involve division by precisionReuse, which could potentially lead to precision loss or unexpected behavior. Verify that the precision and value implications of these changes have been thoroughly considered and tested.

Also applies to: 243-243

  • 231-237: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [231-245]

Overall, the changes to the BigInt and BigIntMut methods are significant and could impact the existing functionality. It is crucial to ensure that these changes are well-documented and that all dependent code has been reviewed and updated accordingly.

math/dec_test.go (1)
  • 766-780: The test TestConvertToBigIntMutativeForLegacyDec correctly verifies the behavior of BigInt and BigIntMut methods, ensuring that the mutative method affects the original LegacyDec value as expected.

@@ -231,8 +231,7 @@ func (d LegacyDec) BigInt() *big.Int {
return nil
}

cp := new(big.Int)
return cp.Set(d.i)
return new(big.Int).Quo(d.i, precisionReuse)
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 28, 2023

Choose a reason for hiding this comment

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

The BigInt method now returns a new big.Int by dividing d.i by precisionReuse, which changes the behavior of the method. Ensure that this change is intentional and that all usages of this method have been updated to handle the new behavior correctly.

Copy link
Member

Choose a reason for hiding this comment

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

ditto, that is a pretty significant change, for a behavior that has been there for 3+ years: https://github.com/cosmos/cosmos-sdk/blame/fc52ae5fdbf10f6780f02fec591f01dcb5c294b6/math/dec.go#L234

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt Indeed, the change in behavior for the BigInt method is significant. The method previously returned a copy of the internal big.Int representation, but with the new change, it now returns a new big.Int that is the result of dividing the internal big.Int by precisionReuse. This effectively changes the scale of the number from a decimal representation to an integer representation.

Given the longevity of the previous behavior, it's crucial to review all instances where BigInt is used to ensure that the new behavior does not introduce bugs or unexpected results. It may also be necessary to update documentation and communicate these changes to all developers who rely on this method, as they may need to adjust their code to accommodate the new behavior.

If this change is intentional and all usages have been accounted for, it would be good to proceed. However, if there's any uncertainty, it might be worth considering additional validation or even reverting to the previous behavior to maintain backward compatibility.

@@ -241,7 +240,7 @@ func (d LegacyDec) BigIntMut() *big.Int {
return nil
}

return d.i
return d.i.Quo(d.i, precisionReuse)
Copy link
Contributor

Choose a reason for hiding this comment

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

The BigIntMut method now mutates the internal d.i by dividing it by precisionReuse. This is a significant change that alters the state of the LegacyDec instance. Ensure that this change is intentional and that all usages of this method have been updated to handle the new behavior correctly.

@DongLieu
Copy link
Contributor Author

DongLieu commented Dec 8, 2023

Hey, I reopened with a new PR. Because I'm no longer at Notional, I can't commit to that branch.

@DongLieu DongLieu closed this Dec 8, 2023
@DongLieu
Copy link
Contributor Author

DongLieu commented Dec 8, 2023

To: #18658

Copy link
Contributor

mergify bot commented Dec 8, 2023

⚠️ The sha of the head commit of this PR conflicts with #18658. Mergify cannot evaluate rules on this PR. ⚠️

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.

[Bug]: Revert BigInt
3 participants