Skip to content
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

[Bug]: Decimal Serialization in Collections Does Not Preserve Numeric Order #23451

Open
1 task done
TraderWithPython opened this issue Jan 18, 2025 · 0 comments
Open
1 task done
Labels

Comments

@TraderWithPython
Copy link

TraderWithPython commented Jan 18, 2025

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

Issue Description

The current decimal serialization in Cosmos SDK does not preserve numeric ordering when used in Collections, which leads to incorrect iteration results. The issue arises because Collections sort decimals based on their string representation rather than their numeric values.

Current Behavior

The current implementation (

func (x Dec) Marshal() ([]byte, error) {
) serializes decimals into string format, causing incorrect ordering due to lexicographical comparison. For example:

  • String "1E+5" is considered less than "2" because '1' < '2'
  • However, numerically 1E+5 (100000) is greater than 2

This leads to counter-intuitive results when:

  1. Iterating through Collections
  2. Using Range queries
  3. Maintaining sorted indexes

Expected Behavior

Collections should iterate through decimals in their true numeric order. For example:

  • 1E+5 (100000) should be greater than 2
  • -1 should be less than 2
  • 1.23E+10 should be greater than 1.23E+9

Proposed Solution

Implement an order-preserving binary encoding for decimals:

Use IEEE 754-like binary encoding format which naturally preserves numeric ordering

Cosmos SDK Version

cosmossdk.io/math

How to reproduce?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant