Bounty
XE-16708 · Bounty

[Low] GET /statechain/blocks recomputes the full state-block count via a table scan on every request

Closed · duplicate — xe#649 filed 2026-11-17 05:17 UTC by kecedanzz77-bit

Report

Summary

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).

Root Cause

  • 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).

Impact

Per-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.

Verification (executed PoC)

20,000 state blocks in BadgerDB, StateBlockCount took ~4 ms (full prefix scan per call). Audited at HEAD 8cc209d (testnet-0006).

Suggested fix

Cache the count (invalidate on new state block) or derive it from the tip index in O(1).

Thread

Joseph Denneteam2026-11-22 23:26 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, 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-16708 and says what differs; it is read like any other.