<!-- XE-16726 · [Medium] GET /statechain/kv full-load + full-sort per request, no per-client limit · testnet -->
*Markdown view of <https://bounty.xe.network/XE-16726>. The page is the record; this is the same page for reading.*

XE-16726 · Bounty

# \[Medium\] GET /statechain/kv full-load \+ full-sort per request, no per-client limit

Closed · duplicate
—claimed Medium
[xe#653](https://github.com/xeprotocol/xe/issues/653)
filed 2026-11-17 05:32 UTC
by kecedanzz77-bit

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Medium

Area
:   Node / API

Duplicate of
:   [XE-0861](https://bounty.xe.network/XE-0861) triaging

Tracked
:   —

Updated
:   2026-11-22 23:26 UTC

Told
:   2026-11-22 23:26 UTC

### Patches

None linked.

### History

- [770281](https://bounty.xe.network/log/770281) 2026-11-22 23:26 UTC reply.mirror\_failed
- [770097](https://bounty.xe.network/log/770097) 2026-11-22 23:26 UTC proposal.adopted
- [770096](https://bounty.xe.network/log/770096) 2026-11-22 23:26 UTC reply.sent
- [770095](https://bounty.xe.network/log/770095) 2026-11-22 23:26 UTC ticket.status new → closed
- [769321](https://bounty.xe.network/log/769321) 2026-11-22 23:26 UTC proposal.adopted
- [769320](https://bounty.xe.network/log/769320) 2026-11-22 23:26 UTC ticket.facts area
- [769319](https://bounty.xe.network/log/769319) 2026-11-22 23:26 UTC proposal.adopted
- [769318](https://bounty.xe.network/log/769318) 2026-11-22 23:26 UTC ticket.facts
- [769317](https://bounty.xe.network/log/769317) 2026-11-22 23:26 UTC proposal.adopted
- [769316](https://bounty.xe.network/log/769316) 2026-11-22 23:26 UTC ticket.facts
- [766115](https://bounty.xe.network/log/766115) 2026-11-22 23:17 UTC note.added
- [766110](https://bounty.xe.network/log/766110) 2026-11-22 23:17 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-16726) · [JSON](https://bounty.xe.network/v1/tickets/XE-16726) · [markdown](https://bounty.xe.network/XE-16726.md)

### Report

## Summary

`handleStateKVAll` (`api/handler.go:976-986`) loads the entire statechain KV store and sorts all keys on every request, even for `?limit=1`. Same bug class as #648, but a different endpoint not covered by #648. The KV is unbounded (epoch keys are write-once and never deleted), so per-request cost grows monotonically with chain age.

## Root Cause

- `handleStateKVAll` calls `GetAllStateKV()` -\> `KVStore.GetAll()` (`statechain/kvstore.go:521-537`), which copies the entire KV map.
- `paginateMap` (`api/handler.go:624-637`) then collects and `sort.Strings` ALL keys (O(N log N)) before slicing one page.
- The `?prefix=` variant is no better: `GetByPrefix` scans the whole map with `strings.HasPrefix`.
- No `perClientLimit` wrapper (only `/accounts` and `/pending` have it); the only throttle is the global read rate limit (200 req/s/IP, `api/handler.go:301`).

## Impact

CPU DoS on the node. With 50,000 KV entries, a `limit=1` request copies 50,000 entries and sorts 50,000 keys in \~93 ms. At 200 req/s/IP, one client can drive \~19 CPU-seconds per wall-clock second \-- enough to saturate the node's cores and starve consensus validation (same process). `GetByPrefix("epoch.4999")` scanned all 50,000 entries to match 11\.

## Verification (executed PoC)

Harness with 50,000 KV entries: `limit=1` request took 93 ms (full copy \+ full sort). Audited at HEAD `8cc209d` (testnet-0006).

## Note on related reports

Same class as #648 (`/leases`, `/conflicts`, `/reputation`, `/directory` full-table scans), but this endpoint is not covered by #648. #86 (OpenAPI schema), #284 (lock contention on `/statechain/blocks`), #390 (`/blocks/recent` offset) are different mechanisms/endpoints.

## Suggested fix

Apply the `perClientLimit` wrapper (as on `/accounts`, `/pending`) and/or serve from a sorted index / store-level prefix scan instead of copy-then-sort.

### Thread

**Joseph Denne**team2026-11-22 23:26 UTCdesk

Thanks. This is the same finding as https://bounty.xe.network/XE-0861, which was filed first; the programme pays the
first verifiable submission, so this one closes as a duplicate. If you believe it is a different
bug, file a new report that links https://bounty.xe.network/XE-16726 and says what differs; it is read like any other.
