-
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
Coins.AmountOf bug #3390
Comments
Here is the thing, this code assumes that the coins are sorted by denom. Are we expected to maintain that as an invariant? |
yes the coins array must always be sorted, deviation from this is an unsupported state AFAICT is this prelaunch? - we are only launching with one token |
Yes. This behavior is expected. We should validate that coins in the genesis file are sorted, or sort them when reading the genesis file. |
From coins, err := sdk.ParseCoins(args[1])
if err != nil {
return err
}
coins.Sort() Did you guys add/modify the offending account manually? |
Yeah the account was added by scripts used to build the large genesis file used before. I can't ensure that coins are sorted in future scripts. |
@jackzampolin @zmanian what exact call is this breaking at? Should be straightforward to ensure coins are manually sorted prior to any essential business logic. |
in gentx.go |
Do you think we should sort inside that function? |
great! I can think of a few options here:
I think the former might be more useful. |
I'm in favor of Sanitize |
Ok, super trivial then. I'm sure @alessio or I can quickly wrap this up for you 👍 |
Add GenesisState.Sanitize(). It sorts genesis accounts and coin sets to ensure genesis state passes validation. gaia app calls GenesisState.Sanitize() on initFromGenesisState() before processing the genesis state. Closes: #3390
Just got burned by this. It's not clear outside of looking at the SDK source that coins need to be sorted by denom. This is an easy mistake to make since A simple fix would be to check |
@shanev nearly all, if not all, |
@alexanderbez I only see sorting mentioned in a handful of places in godoc. To a noob it would be unclear what this means. Sort by amount? Denom? Denom is only mentioned in |
Agreed the documentation should be more clear and updated (although I think sorted by denom is implicit and somewhat obvious). However, if we can find a low performance cost way of further guaranteeing safety and avoiding such a situation altogether, I'm all for it. Do you have suggestions? Perhaps we can can implement radix sort on denoms for near constant time. |
Let me think on it. My only immediate suggestion would be to add something like "Coins must be sorted by denom" to the godoc for all functions that depend on coins being sorted, like |
Hello @shanev, And thanks for taking the time to share your feedback and helping to make the SDK better!
Can you provide more information on your specific use case please? Terminal log excerpts and |
When trying to run
gentx
on the gaia-10k @zmanian and I encountered an issue where unless thebondDenom
was the last token in the array theAmountOf
function didn't return the proper amount (it returned 0). The binary search is not working correctly, it seems like it doesn’t find values in position 0:The text was updated successfully, but these errors were encountered: