{"id":"XE-16708","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"649","source_url":"https://github.com/xeprotocol/xe/issues/649","reporter_id":2920,"title":"[Low] GET /statechain/blocks recomputes the full state-block count via a table scan on every request","area":"node-api","claimed_tier":null,"assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-17T05:17:33.068094+00:00","updated_at":"2026-11-22T23:26:17.046173+00:00","source_created_at":"2026-11-17T05:17:16+00:00","told_at":"2026-11-22T23:26:17.064677+00:00","status_changed_at":"2026-11-22T23:26:17.053327+00:00","url":"https://bounty.xe.network/XE-16708","body":"## Summary\n`handleStateChainBlocks` (`api/handler.go:937-956`) calls `GetStateChainBlockCount()` on every request, which runs a full prefix scan over the state-block table with no caching. The count is derivable in O(1) from the tip index (blocks are contiguously indexed; count = tip.Index + 1 -- exactly what `BlockCount` computes as store-count + 1).\n\n## Root Cause\n- `api/handler.go:937-956` -> `node/node.go:2344-2346` -> `statechain/chain.go:310-317` (`BlockCount` -> `StateBlockCount`, +1) -> `store/badger_store.go:2042-2057` (`StateBlockCount` iterates the whole state-block prefix, `PrefetchValues=false`, no cache).\n\n## Impact\nPer-request O(N) scan; measured ~4 ms at 20,000 blocks, scaling linearly. Low severity at current testnet scale, unbounded as the chain grows; at 200 req/s against a large chain it saturates the node.\n\n## Verification (executed PoC)\n20,000 state blocks in BadgerDB, `StateBlockCount` took ~4 ms (full prefix scan per call). Audited at HEAD `8cc209d` (testnet-0006).\n\n## Suggested fix\nCache the count (invalidate on new state block) or derive it from the tip index in O(1).","patch_refs":[],"thread":[{"id":18880,"source":"desk","source_ref":"reply:16557","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-16708 and says what differs; it is read like any other.","created_at":"2026-11-22T23:26:17.046173+00:00"}]}