/traders/leaderboard sorts on unindexed columns (seq scan + sort per request) #280
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#280
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Severity: Medium
Reachability: Unauthenticated HTTP —
/api/v1/traders/leaderboard.Affected:
get_leaderboard(indexer/src/db/queries/traders.rs) + missing indexes ontraders.Root cause: the leaderboard sorts on
volume_24h/volume_7d/volume_30d/total_trades, and none of those columns have an index.Summary
get_leaderboardbuildsSELECT * FROM traders ORDER BY {order_col} DESC LIMIT $1, whereorder_colis one ofvolume_24h,volume_7d,volume_30d,total_trades, plus the PnL/fees columns. The PnL/fees columns andtotal_volume_usdare indexed — butvolume_24h,volume_7d,volume_30d, andtotal_tradesare not. The default UI sort is almost certainly 24h volume, i.e. the unindexed path.So the common case is a full sequential scan of
tradersplus a top-N sort, on every public request, and it gets worse as the trader table grows. (order_colis whitelisted through a match, so there's no injection — purely the index gap.)Current codebase
traders.rsget_leaderboard:format!("SELECT * FROM traders ORDER BY {} DESC LIMIT $1", order_col).total_volume_usd,tier_id, and the four PnL columns; none onvolume_24h/volume_7d/volume_30d/total_trades.Recommended direction
traders(volume_24h DESC),(volume_7d DESC),(volume_30d DESC),(total_trades DESC).Acceptance criteria
EXPLAINshows an index scan + limit, no full sort).Approved both btree and ttl cache, can be 1 minute
Fixed — both parts you approved.
Indexes (migration
20260604120000_traders_leaderboard_sort_indexes.sql): btree ontraders(volume_24h DESC),(volume_7d DESC),(volume_30d DESC),(total_trades DESC), matching the existingidx_traders_volumeformat. (The PnL/fees + total_volume sort columns were already indexed.)Cache: 60s TTL on the
/traders/leaderboardresponse keyed by(sort_by, limit), modeled on the existing route_solver module-level cache (OnceLock<Mutex> + retain/evict).Proven live on the QA Postgres (forced
enable_seqscan=offsince the localnettraderstable is tiny, to make the planner reveal whether an index path exists):So the full top-N sort is gone and the query rides the index. Per the AC, EXPLAIN shows index scan + limit (no full sort); the 60s cache bounds latency under a request burst.
cargo check clean. Branch
qa/280-leaderboard-index-cache, MR fork→main (no closing keyword). @PlasticDigitsmentioned in merge request !741
mentioned in commit
4a95548d53Verified both halves — the four sort indexes and the 60s cache.
AC1 (each sortable column index-backed, EXPLAIN shows index scan + limit, no full sort): the migration created btree on
traders(volume_24h/7d/30d DESC)and(total_trades DESC), live in the DB. EXPLAIN on the leaderboard query (withenable_seqscan=off, since the seededtraderstable is one row so the planner won't reach for an index on its own) showsLimit -> Index Scan using idx_traders_volume_24hwith no Sort node, same for volume_7d / volume_30d / total_trades; worst_trade DESC rides the ASC index backward. The per-request top-N sort is gone on every sortable column.AC2 (latency flat as the table grows): the index removes the sort and the 60s cache (keyed by sort+limit) bounds it under a burst — repeat calls serve byte-identical from cache. Invalid sort key is rejected at the whitelist (400) before the DB.
@PlasticDigits good to close.
Verification (Cloud Agent) — GitLab #280
Verified fix on
main(9f0babe) — migration20260604120000_traders_leaderboard_sort_indexes.sqland 60s leaderboard cache inindexer/src/api/traders.rsare present. No repo changes required.Acceptance criteria
EXPLAINshows index scan + limit, no full sortdex_indexer+dex_indexer_testwithSET enable_seqscan = off. All nineORDER BY … DESC LIMIT 50plans areLimit → Index Scan(orIndex Scan Backwardforworst_trade_pnlonidx_traders_worst_trade); noSortnode. New indexes:idx_traders_volume_24h,_7d,_30d,_total_trades.sort_by|limit(LEADERBOARD_CACHE_TTL = 60s, mirrors route_solver pattern). Two rapidGET /api/v1/traders/leaderboard?sort=volume_24h&limit=5responses byte-identical. Full load test not run (table has 0 rows ondex_indexer).Additional checks
_sqlx_migrationscontains20260604120000;\di idx_traders_volume_24hexistscurl …?sort=hacked_column→ 400 with whitelist messagesort=values → 200 on running indexer (:3001)cargo test -p cl8y-dex-indexer --test api_traders leaderboard(4/4);cargo test -p cl8y-dex-indexer --test security leaderboard_all_documented_sort_columns_accepted;cargo check;cargo test --lib(98/98)order_colwhitelisted inget_leaderboard(traders.rs); APIVALID_SORTSrejects unknown sortsInfrastructure used
make-style compose postgres on:5432)FACTORY_ADDRESSdummy,POLL_INTERVAL_MS=600000)Closing as verified on
main— no MR from this pass.mentioned in issue #657
mentioned in issue #666
marked as related to #666