Skip to content

x/staking ValidatorPowerRank will have incorrect orderings #2439

@ValarDragon

Description

@ValarDragon

Summary of Bug

This issue is demonstrated quite clearly in: https://github.com/cosmos/cosmos-sdk/pull/2438/files#diff-1fe0bb213f8721408b446de8df30add8R41

Basically, we don't do fixed length encoding for the decimals. Therefore a greater decimal will be longer. However, this doesn't preserve ordering as we'd like. From the linked example, the first line corresponds to tokens=1, and the second tokens=2^40:

05303030303030303030303130ffffffffffffffffffff
0531303939353131363237373736ffffffffffffffffffff

The tokens=1 case will occur first in the iteration, since the ff occurs first.

Solution

The solution to this is to create a fixed size byte encoding of decimals. At the same time, we should change the encoding to be more sensible for keys. Currently it converts to a string, and then casts to []byte. Thats good for UI's, not for encoding to state. (You see the 0 decimal encoded as a 30 in the above hex)

Metadata

Metadata

Assignees

Labels

T: API BreakingBreaking changes that impact APIs and the SDK only (not state machine).T: PerformancePerformance improvements

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions