<!-- XE-16708 · [Low] GET /statechain/blocks recomputes the full state-block count via a table scan on every request · testnet -->
*Markdown view of <https://bounty.xe.network/XE-16708>. The page is the record; this is the same page for reading.*

XE-16708 · Bounty

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

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

Verdict
:   Duplicate

Tier
:   —

Claimed
:   —

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-22 23:26 UTC

Told
:   2026-11-22 23:26 UTC

### Patches

None linked.

### History

- [770286](https://bounty.xe.network/log/770286) 2026-11-22 23:26 UTC reply.mirror\_failed
- [770109](https://bounty.xe.network/log/770109) 2026-11-22 23:26 UTC proposal.adopted
- [770108](https://bounty.xe.network/log/770108) 2026-11-22 23:26 UTC reply.sent
- [770107](https://bounty.xe.network/log/770107) 2026-11-22 23:26 UTC ticket.status new → closed
- [769349](https://bounty.xe.network/log/769349) 2026-11-22 23:26 UTC proposal.adopted
- [769348](https://bounty.xe.network/log/769348) 2026-11-22 23:26 UTC ticket.facts area
- [769347](https://bounty.xe.network/log/769347) 2026-11-22 23:26 UTC proposal.adopted
- [769346](https://bounty.xe.network/log/769346) 2026-11-22 23:26 UTC ticket.facts
- [769345](https://bounty.xe.network/log/769345) 2026-11-22 23:26 UTC proposal.adopted
- [769344](https://bounty.xe.network/log/769344) 2026-11-22 23:26 UTC ticket.facts
- [766084](https://bounty.xe.network/log/766084) 2026-11-22 23:17 UTC note.added
- [766083](https://bounty.xe.network/log/766083) 2026-11-22 23:17 UTC proposal.created

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

### 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 Denne**team2026-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.
