Repository navigation
fix(api-server): order holders listings by numeric amount - #2155
nullPointerEnjoyer merged 1 commit into
Conversation
|
Note for reviewers: the red |
|
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 |
|
CI update: the red The fix is going in separately: #2157 adds the Meanwhile, note that 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.
94c0f36 to
dcd367a
Compare
|
Rebased on master after #2157 merged (head is now |
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_amountsselectsamount::textwhile ordering by bareamount:Postgres resolves a bare name in
ORDER BYto 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 mainnetitems=5request returns a9,999.9 MLholder 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 BYbinds to the numeric input column:row.get(1)is positional, so no other code is affected. The cursor predicate already comparesamountnumerically (amount < $2::text::numeric), so keyset pagination becomes consistent with the ordering too.Test
Extended
top_address_amountsin the shared storage test suite with mixed-magnitude amounts whose text order disagrees with the numeric order (9_000_000vs10_000_000atoms: 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)postgres:16: unaliased query returns9_000_000before10_000_000(reproduced), aliased query returns numeric orderNotes