-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
types: Coins.Add chokes if a denomination is repeated and does not coalesce coins with same denomination {{2A}, {3A}, {1A}}.Add({2A}. {3A}) gives back {{5A}, {5A}, {1A}} but it should return {{10A}} #13234
Comments
For the record, repeating denominations in |
@odeke-em would you like to submit a pr? |
I mailed #13265 @marbar3778 |
… & simplify logic This code correctly coalesces coins with repeated denominations which can come from mistakenly duplicated or split up coins being passed in. However, the code MUST always function correctly regardless of assumptions that no one should ever pass in duplicated denominations. While here simplified the prior clever but hard to understand code, by making the code directly implementing deduplicating the coalescing. Credit to the Ingenuity/Quicksilver codebase which exposed this problem from an observation we made. Fixes #13234
… & simplify logic This code correctly coalesces coins with repeated denominations which can come from mistakenly duplicated or split up coins being passed in. However, the code MUST always function correctly regardless of assumptions that no one should ever pass in duplicated denominations. While here simplified the prior clever but hard to understand code, by making the code directly implementing deduplicating the coalescing. Credit to the Ingenuity/Quicksilver codebase which exposed this problem from an observation we made. Fixes #13234
Yeah it doesn't hurt to provide this protection, just wanted to note the invariants. |
Acknowledged, and thank you @alexanderbez! |
Thanks for the PR! :) |
Summary of Bug
The code for Coins.Add unfortunately can't coalesce coins if there is a repeated denomination for example
fails with
--- FAIL: TestCoinsAdd (0.00s) coin_test.go:1230: Wrong result Got: 5den,5den,1den Want: 11den FAIL exit status 1 FAIL github.com/cosmos/cosmos-sdk/types 0.300s
This bug was discovered in Quicksilver's code
Cause
The reasoning is that the code in Coins.Add tries to be clever with matching indices yet the code chokes for just a repeated denomination but really it should firstly coalesce coins, bucket them by denominations then add them up and sort the amount
Fix
This can simply be fixed by simplifying the code and coalescing like this:
or as pure Go
Version
The latest at e5e9d81
A kind FYI for @elias-orijtech @kirbyquerby @willpoint @marbar3778. I am sending over a PR shortly, if this is the desired behavior but open to discussion.
The text was updated successfully, but these errors were encountered: