-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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): Upstream GDA based decimal type v2 #20536
Closed
Closed
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0c9b305
update dec w benchmark testing
samricotta 1ac3316
fix version
samricotta dff520a
Update go.mod
samricotta d54f795
Create 18-decimal-handling.md
samricotta bd795e6
Update 18-decimal-handling.md
samricotta a40ab8e
update tests and convert func
samricotta 1f4ed85
Update 18-decimal-handling.md
samricotta f2c294c
Refactor constructors
alpe b2b61dd
Add more numbers to quo benchmark
alpe aef9429
Apd v3 + sum benchmark
alpe aa90438
More benchs
alpe d8d1c39
Marshal/unmarshal
alpe 8af8467
Fix test
alpe bb260f8
x
alpe e89e6ec
wip
samricotta a8cec85
wip
samricotta a03f52a
quo, quo exact, table tests, multiply tests
samricotta ed8ad41
feat(math): Testing Sub for GDA Dec Type (#20626)
samricotta 5da8fc4
feat(math): Upstream GDA based decimal type - Add function coverage (…
samricotta cc09fe8
feat(math): Upstream GDA based decimal type testing (#20763)
samricotta b45aec2
feat(math): Upstream GDA based decimal type - errors (#20827)
samricotta 777b298
Fix tests and minor refactoring (#20861)
alpe 78937cc
feat(math): Upstream GDA based decimal type (docs) (#20950)
samricotta e3bd629
Merge branch 'main' into sam/dec-type
samricotta e96b382
Update 18-decimal-handling.md
samricotta 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
sidebar_position: 1 | ||
--- | ||
# Decimal Handling in Cosmos SDK | ||
|
||
## Introduction | ||
|
||
In the Cosmos SDK, there are two types of decimals: `LegacyDec` and `Dec`. `LegacyDec` is the older decimal type that is still available for use, while `Dec` is the newer, more performant decimal type. The implementation of `Dec` is adapted from Regen Network's `regen-ledger`, specifically from [this module](https://github.com/regen-network/regen-ledger/tree/main/types/math). Migrating from `LegacyDec` to `Dec` involves state-breaking changes, specifically: | ||
|
||
* **Data Format**: The internal representation of decimals changes, affecting how data is stored and processed. | ||
* **Precision Handling**: `Dec` supports flexible precision up to 34 decimal places, unlike `LegacyDec` which has a fixed precision of 18 decimal places. | ||
|
||
These changes require a state migration to update existing decimal values to the new format. It is recommended to use `Dec` for new modules to leverage its enhanced performance and flexibility. | ||
|
||
## Why the Change? | ||
|
||
* **Enhanced Precision**: `Dec` uses the [apd](https://github.com/cockroachdb/apd) library for arbitrary precision decimals, suitable for accurate financial calculations. | ||
* **Immutable Operations**: `Dec` operations are safer for concurrent use as they do not mutate the original values. | ||
* **Better Performance**: `Dec` operations are faster and more efficient than `LegacyDec`.` | ||
|
||
## Using `Dec` in Modules that haven't used `LegacyDec` | ||
|
||
If you are creating a new module or updating an existing module that has not used `LegacyDec`, you can directly use `Dec` without any changes. | ||
|
||
As an example we will use `DecCoin` which is a common type used in the Cosmos SDK. | ||
|
||
|
||
```protobuf | ||
message DecCoin { | ||
option (gogoproto.equal) = true; | ||
|
||
string denom = 1; | ||
string amount = 2 [ | ||
(cosmos_proto.scalar) = "cosmos.Dec", | ||
(gogoproto.customtype) = "cosmossdk.io/math.Dec", | ||
(gogoproto.nullable) = false | ||
]; | ||
} | ||
``` | ||
|
||
How you can implement `Dec` in your module: | ||
|
||
```go | ||
import ( | ||
"cosmossdk.io/math" | ||
) | ||
|
||
example := math.NewDecFromInt64(100) | ||
``` | ||
|
||
## Modules migrating from `LegacyDec` to `Dec` | ||
|
||
When migrating from `LegacyDec` to `Dec`, you need to update your module to use the new decimal type. **These types are state breaking changes and require a migration.** | ||
|
||
## Precision Handling | ||
|
||
The reason for the state breaking change is the difference in precision handling between the two decimal types: | ||
|
||
* **LegacyDec**: Fixed precision of 18 decimal places. | ||
* **Dec**: Flexible precision up to 34 decimal places using the apd library. | ||
|
||
## Byte Representation Changes Example | ||
|
||
The change in precision handling directly impacts the byte representation of decimal values: | ||
|
||
**Legacy Dec Byte Representation:** | ||
`2333435363738393030303030303030303030303030303030303030` | ||
|
||
This example includes the value 123456789 followed by 18 zeros to maintain the fixed precision. | ||
|
||
**New Dec Byte Representation:** | ||
`0a03617364121031323334353637383900000000000000` | ||
samricotta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This example shows the value 123456789 without additional padding, reflecting the flexible precision handling of the new Dec type. | ||
|
||
## Impact of Precision Change | ||
|
||
The increase in precision from 18 to 34 decimal places allows for more detailed decimal values but requires data migration. This change in how data is formatted and stored is a key aspect of why the transition is considered state-breaking. | ||
|
||
## Example of State-Breaking Change | ||
|
||
The protobuf definitions for DecCoin illustrate the change in the custom type for the amount field. | ||
|
||
**Before:** | ||
|
||
```protobuf | ||
message DecCoin { | ||
option (gogoproto.equal) = true; | ||
|
||
string denom = 1; | ||
string amount = 2 [ | ||
(cosmos_proto.scalar) = "cosmos.Dec", | ||
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", | ||
(gogoproto.nullable) = false | ||
]; | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
```protobuf | ||
message DecCoin { | ||
option (gogoproto.equal) = true; | ||
|
||
string denom = 1; | ||
string amount = 2 [ | ||
(cosmos_proto.scalar) = "cosmos.Dec", | ||
(gogoproto.customtype) = "cosmossdk.io/math.Dec", | ||
(gogoproto.nullable) = false | ||
]; | ||
} | ||
``` | ||
|
||
## Converting `LegacyDec` to `Dec` without storing the data | ||
|
||
If you would like to convert a `LegacyDec` to a `Dec` without a state migration changing how the data is handled internally within the application logic and not how it's stored or represented. You can use the following methods. | ||
|
||
```go | ||
func LegacyDecToDec(ld LegacyDec) (Dec, error) { | ||
return NewDecFromString(ld.String()) | ||
} | ||
``` | ||
|
||
```go | ||
func DecToLegacyDec(ld Dec) (LegacyDec, error) { | ||
return LegacyDecFromString(ld.String()) | ||
} | ||
``` | ||
|
This file contains 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
Add a comma after the introductory phrase.
Consider adding a comma after the introductory phrase for better readability.
Committable suggestion
Tools
LanguageTool