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

Add uint256 package and int256 wrapper #1320

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Add uint256 package and int256 wrapper #1320

merged 1 commit into from
Apr 4, 2024

Conversation

roy-dydx
Copy link
Contributor

@roy-dydx roy-dydx commented Apr 2, 2024

Changelist

  • Add uint256 package
  • Add int256 signed wrapper with some methods implemented

Test Plan

  • Added tests

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Copy link
Contributor

coderabbitai bot commented Apr 2, 2024

Walkthrough

The recent update introduces a signed 256-bit integer type, Int, complemented by a suite of functionalities for arithmetic operations, conversions, and comparisons. This enhancement leverages the existing uint256 package to extend its capabilities to handle signed integers. Additionally, comprehensive testing ensures the reliability of these operations across a range of scenarios, including handling positive, negative, and zero values.

Changes

Files Change Summary
.../int256/int256.go Introduced Int type for signed 256-bit integers with arithmetic, conversion, and comparison functionalities.
.../int256/int256_test.go Added tests for Int type operations, including arithmetic, conversion, and comparison across various scenarios.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 9

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fbeeb7c and 11e5b8d.
Files ignored due to path filters (2)
  • protocol/go.mod is excluded by !**/*.mod, !**/*.mod
  • protocol/go.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing (2)
  • protocol/lib/int256/int256.go (1 hunks)
  • protocol/lib/int256/int256_test.go (1 hunks)
Additional Context Used
Path-based Instructions (2)
protocol/lib/int256/int256.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/lib/int256/int256_test.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments not posted (4)
protocol/lib/int256/int256.go (3)

11-12: Consider adding thread-safety documentation or mechanisms, especially given the warning about pointer usage.


32-37: Ensure that the conversion from int64 to uint64 for negative values is intended and correctly handles potential overflow.


167-185: Verify the correctness of mulExp10 function, especially the handling of negative exponents and the fallback to Exp when the exponent is not in the lookup.

protocol/lib/int256/int256_test.go (1)

34-57: Refactor the test for Set to include assertions that verify the original value of x is not modified by the operation.

protocol/lib/int256/int256.go Show resolved Hide resolved
Comment on lines +64 to +67
// Set sets z to the value of a uint256.
func (z *Int) SetUint64(x uint64) *Int {
return (*Int)((*uint256.Int)(z).SetUint64(x))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment above this method incorrectly states "Set sets z to the value of a uint256." It should be corrected to reflect the actual functionality.

- // Set sets z to the value of a uint256.
+ // SetUint64 sets z to the value of a uint64.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Set sets z to the value of a uint256.
func (z *Int) SetUint64(x uint64) *Int {
return (*Int)((*uint256.Int)(z).SetUint64(x))
}
// SetUint64 sets z to the value of a uint64.
func (z *Int) SetUint64(x uint64) *Int {
return (*Int)((*uint256.Int)(z).SetUint64(x))
}

Comment on lines +152 to +154
// Div sets z = x / y and returns z. If y is 0, z is set to 0.
func (z *Int) Div(x, y *Int) *Int {
return (*Int)((*uint256.Int)(z).SDiv((*uint256.Int)(x), (*uint256.Int)(y)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a check for division by zero to prevent potential runtime panics.

+ if y.IsZero() {
+     return z.Set(NewInt(0))
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Div sets z = x / y and returns z. If y is 0, z is set to 0.
func (z *Int) Div(x, y *Int) *Int {
return (*Int)((*uint256.Int)(z).SDiv((*uint256.Int)(x), (*uint256.Int)(y)))
// Div sets z = x / y and returns z. If y is 0, z is set to 0.
func (z *Int) Div(x, y *Int) *Int {
if y.IsZero() {
return z.Set(NewInt(0))
}
return (*Int)((*uint256.Int)(z).SDiv((*uint256.Int)(x), (*uint256.Int)(y)))

Comment on lines +157 to +164
func createExp10Lookup() map[uint64]uint256.Int {
lookup := make(map[uint64]uint256.Int, 100)
value := uint256.NewInt(1)
for i := 0; i < 100; i++ {
lookup[uint64(i)] = *new(uint256.Int).Set(value)
value.Mul(value, (*uint256.Int)(TenInt256))
}
return lookup
Copy link
Contributor

@coderabbitai coderabbitai bot Apr 2, 2024

Choose a reason for hiding this comment

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

Consider preallocating the exp10Lookup map with a size of 19 instead of 100, as the maximum exponent for base 10 in a uint256 cannot exceed 18.

- lookup := make(map[uint64]uint256.Int, 100)
+ lookup := make(map[uint64]uint256.Int, 19)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func createExp10Lookup() map[uint64]uint256.Int {
lookup := make(map[uint64]uint256.Int, 100)
value := uint256.NewInt(1)
for i := 0; i < 100; i++ {
lookup[uint64(i)] = *new(uint256.Int).Set(value)
value.Mul(value, (*uint256.Int)(TenInt256))
}
return lookup
func createExp10Lookup() map[uint64]uint256.Int {
lookup := make(map[uint64]uint256.Int, 19)
value := uint256.NewInt(1)
for i := 0; i < 100; i++ {
lookup[uint64(i)] = *new(uint256.Int).Set(value)
value.Mul(value, (*uint256.Int)(TenInt256))
}
return lookup

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay fair, but you're confusing it with a uint64. you can fit 10^79 in a uint256 and 10^78 in an int256.

Copy link
Contributor

Choose a reason for hiding this comment

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

@roy-dydx, thank you for pointing out the confusion regarding the capacity of uint256 and int256. You're absolutely right; uint256 can indeed hold values up to 10^79, and int256 up to 10^78, which significantly exceeds the limits of a uint64. My initial suggestion to preallocate the exp10Lookup map with a size of 19 was based on an incorrect assumption about the maximum exponent for base 10 that these types can handle. Given this correction, it's clear that the map size should be reconsidered to accommodate the actual range of values that uint256 and int256 can represent. I appreciate your feedback and the opportunity to correct this misunderstanding.

🐰


New Learnings Added
User: roy-dydx"
URL: https://github.com/dydxprotocol/v4-chain/pull/1320

Timestamp: 2024-04-03T03:59:36.096Z
Learning: The `uint256` type can hold values up to 10^79, and `int256` can hold values up to 10^78, significantly exceeding the limits of a `uint64`.

Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.

protocol/lib/int256/int256_test.go Show resolved Hide resolved
protocol/lib/int256/int256_test.go Show resolved Hide resolved
protocol/lib/int256/int256_test.go Show resolved Hide resolved
protocol/lib/int256/int256_test.go Show resolved Hide resolved
protocol/lib/int256/int256_test.go Show resolved Hide resolved
@roy-dydx roy-dydx merged commit b5ed196 into main Apr 4, 2024
17 checks passed
@roy-dydx roy-dydx deleted the roy/int256 branch April 4, 2024 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants