-
Notifications
You must be signed in to change notification settings - Fork 33
fix(core): validate decimal precision by value, not by scale (unblocks app-session Withdraw) #880
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
Open
panzagianluca
wants to merge
2
commits into
layer-3:main
Choose a base branch
from
panzagianluca:fix/withdraw-amount-decimal-scale
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: layer-3/nitrolite
Length of output: 155
🏁 Script executed:
Repository: layer-3/nitrolite
Length of output: 50374
🏁 Script executed:
Repository: layer-3/nitrolite
Length of output: 8633
🌐 Web query:
github.com/shopspring/decimal v1.4.0 Truncate rescale exponent 10^ power_of_10 decimal.go💡 Result:
In the shopspring/decimal Go library, the Truncate and rescale methods are closely related, with Truncate relying on rescale to adjust the decimal's exponent [1][2][3]. 1. rescale: This internal method changes the exponent of a Decimal while keeping its value represented as an arbitrary-precision integer [1][4]. - It effectively multiplies or divides the underlying integer value by 10^(abs(diff)), where diff is the difference between the current exponent and the target exponent [1][4]. - When decreasing precision (increasing the exponent), it uses big.Int.Quo, which truncates the value toward zero [1][4]. - Documentation explicitly states: "NOTE: this will truncate, NOT round" [1][4]. 2. Truncate(precision int32): This method allows you to truncate digits from a number [5]. - Prior to recent updates (e.g., in version 1.4.0), the Truncate method typically required a non-negative precision (precision >= 0) and used rescale to shift the exponent [1][2][5]. - Developments subsequent to v1.4.0 (such as those discussed in pull request
#426) have updated Truncate to support negative precision [3]. This allows for truncation of the integer part toward zero (e.g., truncating 5432 with a precision of -2 results in 5400) [3]. - The mechanism remains a call to rescale, which inherently performs the required truncation via big.Int.Quo [1][3]. If you are using version v1.4.0 specifically, note that the library was in a state where Truncate often enforced a non-negative precision check, which was later relaxed in development versions of the library to support negative precision [2][3].Citations:
🌐 Web query:
github.com/shopspring/decimal v1.4.0 decimal.go rescale func(rescale)💡 Result:
In the shopspring/decimal library version 1.4.0, the
rescalefunction is a method on theDecimaltype that returns a newDecimalwith a specified exponent [1]. Key characteristics of therescalefunction include: Functionality: It adjusts the decimal to the target exponent provided by the user [1]. If the target exponent is greater than the current exponent, the function performs a truncation (reducing precision); if the target exponent is smaller, it scales the coefficient up by the appropriate power of 10 [1]. Important Behavior: The function explicitly performs truncation rather than rounding when decreasing precision [1]. Implementation: - If the target exponent matches the current exponent, it returns a copy of the originalDecimal[1]. - It utilizesmath.Absto calculate the difference between the current and target exponents to determine the required scaling factor [1]. - It usesbig.Intoperations (QuoorMul) to perform the scaling [1]. Example Usage (as documented in the source):go d := decimal.New(12345, -4) // Represents 1.2345 d2 := d.rescale(-1) // Results in 1.2 d3 := d2.rescale(-4) // Results in 1.2000Note: Whilerescaleis a core internal method used for operations like addition and subtraction (often via theRescalePairhelper), it is worth noting that it is not typically exported for direct package-level use in the same way as public API methods [1]. Users often rely on higher-level operations orRoundfor precision management [1].Citations:
🏁 Script executed:
Repository: layer-3/nitrolite
Length of output: 10867
🏁 Script executed:
Repository: layer-3/nitrolite
Length of output: 50373
Prevent unbounded rescaling for extreme exponents.
ValidateDecimalPrecisionnow depends onamount.Truncate(...), andshopspring/decimalv1.4.0 rescales by computing10^difffrom the exponent gap. String parsing accepts small scientific exponents before validation, so an amount such as1e-2147483648can force an enormous allocation before returning an error. Add an extreme-exponent limit before this rescaling, or implement the precision test with a bounded trailing-zero/coefficient check.🤖 Prompt for AI Agents
Source: MCP tools