Skip to content

fix(api-server): order holders listings by numeric amount - #2155

Merged
nullPointerEnjoyer merged 1 commit into
mintlayer:masterfrom
nullPointerEnjoyer:fix/holders-postgres-order
Nov 7, 2026
Merged

nullPointerEnjoyer merged 1 commit into
mintlayer:masterfrom
nullPointerEnjoyer:fix/holders-postgres-order

Conversation

@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator

Problem

The Postgres storage returned the holders listings (GET /statistics/coin/holders, GET /statistics/token/:id/holders) in lexicographic atom-string order instead of balance order.

get_top_address_amounts selects amount::text while ordering by bare amount:

SELECT address, amount::text
FROM ml.address_amount
...
ORDER BY amount DESC, address COLLATE "C" DESC

Postgres resolves a bare name in ORDER BY to the output column first, so the sort keyed on the text representation of the atoms instead of the numeric value. Observable on any Postgres-backed deployment: e.g. a mainnet items=5 request returns a 9,999.9 ML holder first while the real top holders (hundreds of millions of ML) sort hundreds of thousands of rows deep — explorers' top-address/rich-list surfaces built on these endpoints render wrong data.

The in-memory storage was unaffected (it sorts numerically), which is why the storage test suite did not catch the mismatch: its existing fixture amounts (300/200/100) order identically as text and as numbers.

Fix

Alias the cast so the bare name in ORDER BY binds to the numeric input column:

SELECT address, amount::text AS amount_text

row.get(1) is positional, so no other code is affected. The cursor predicate already compares amount numerically (amount < $2::text::numeric), so keyset pagination becomes consistent with the ordering too.

Test

Extended top_address_amounts in the shared storage test suite with mixed-magnitude amounts whose text order disagrees with the numeric order (9_000_000 vs 10_000_000 atoms: text "9000000" > "10000000", numeric the reverse), asserting numeric-descending output and a cursor page walk across that boundary. The fixture now discriminates the alias bug on every backend.

Verified locally:

  • cargo test -p api-server-backend-test-suite --test in_memory: 8 passed (incl. the new fixture)
  • pre-fix vs post-fix SQL against a throwaway postgres:16: unaliased query returns 9_000_000 before 10_000_000 (reproduced), aliased query returns numeric order

Notes

  • Read-path fix only: no storage version change, no resync required (the scanner is untouched).
  • Fixes wrong data served by production explorers built on the holders endpoints.

@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator Author

Note for reviewers: the red static_checks runs are the pre-existing cargo deny advisory RUSTSEC-2026-0441 (pin-utils 0.1.0 unmaintained — upstream archived 2026-10, pulled in via futures). This PR does not touch dependencies; the same failure hits any PR today, including an empty diff. Local verification: cargo fmt --check clean, clippy clean on the touched crates, in-memory storage suite 8/8 including the new mixed-magnitude regression fixture, and a pre/post-fix SQL demonstration against postgres:16 (unaliased query reproduces the lexicographic order, aliased query returns numeric order).

@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator Author

Forensics note for the 1.4.5 holders work (from production mainnet): with this PR's ordering fix deployed, the listing now crowns a phantom — the genesis premint destination mtc1q9a4m6vmvqhw4ts0uqnpt6mzg957mmqljg7n7t9g at 399,900,000 ML (77% of raw circulating supply) with zero UTXOs and no address entry. Root cause is upstream of this PR: the genesis premint output was spent at block 134 (Jan 2024, premine → distribution); the spend was recorded at the UTXO level (outpoint marked spent) but ml.address_amount was never debited — genesis-input spends are not attributed to the address side, so both scanners carry the identical stale row. Separately, the listing semantics on Postgres need latest-spendable-per-address (as planned for 1.4.5) — selecting the latest address_amount row would still serve this phantom, since its stale row is its latest state. Repro fingerprint: the first 100 rows sum to 724.6M ML vs ≈600M total possible supply; /address/{addr} = not found while /statistics/coin/holders lists it #1.

@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator Author

CI update: the red static_checks runs are not from this diff — the RustSec advisory DB picked up RUSTSEC-2026-0441 (pin-utils unmaintained) on 2026-11-05, and cargo deny now fails repo-wide, including on master itself (scheduled run #13527 fails on c3009f9, whose 11-04/11-05 scheduled runs were green, with no commit in between).

The fix is going in separately: #2157 adds the deny.toml ignore for the unfixable unmaintained advisory (upstream pin-utils archived, no patched version, pulled in transitively via futures). Its static_checks are green on ubuntu and macos already; once it merges I'll rebase this PR so its checks inherit the ignore.

Meanwhile, note that build_ubuntu on this PR's head commit is green — that job runs the full workspace test suite with ML_CONTAINERIZED_TESTS=1, which includes the Postgres storage suite. So the ordering fix and the new mixed-magnitude regression fixture are verified against a real Postgres in CI, not just the in-memory backend.

Also filed #2158 for the phantom genesis-premint holder that becomes visible once this ordering fix ships — out of scope here, needs a maintainer decision (see the issue).

The Postgres storage selected amount::text while ordering by bare
amount, which Postgres resolves to the text OUTPUT alias of the select
instead of the numeric column. The holders listings
(GET /statistics/{coin,token/:id}/holders) therefore came back in
lexicographic atom-string order (e.g. '999999997' after '99999999998'),
breaking the documented biggest-balance-first contract on every
Postgres-backed deployment; the in-memory storage was unaffected, which
is why the storage test suite did not catch it.

Alias the select (amount_text) so ORDER BY amount DESC binds to the
numeric input column, consistent with the numeric cursor predicate and
the in-memory implementation. Extend the shared storage test with
mixed-magnitude amounts whose text order disagrees with the numeric
order, so both backends pin the behavior.

Read-path fix only: no storage version change, no resync required.
@nullPointerEnjoyer
nullPointerEnjoyer force-pushed the fix/holders-postgres-order branch from 94c0f36 to dcd367a Compare November 6, 2026 14:03
@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator Author

Rebased on master after #2157 merged (head is now dcd367a45, no content change — same 3 files, +60/−1). static_checks are green again on all three OSes; the remaining jobs are the same build/test matrix that already passed on the previous head. Local re-verification on the rebased branch: cargo deny green, storage test suite in-memory 8/8 including the mixed-magnitude fixture.

@nullPointerEnjoyer
nullPointerEnjoyer merged commit 7a1a084 into mintlayer:master Nov 7, 2026
34 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants