<!-- XE-13237 · [Medium] GET /accounts/{address}/chain loads the entire account chain before slicing the page — cost scales with chain length, not page size · testnet -->
*Markdown view of <https://bounty.xe.network/XE-13237>. The page is the record; this is the same page for reading.*

XE-13237 · Bounty

# \[Medium\] GET /accounts/{address}/chain loads the entire account chain before slicing the page — cost scales with chain length, not page size

Closed · duplicate
—claimed Medium
[xe#365](https://github.com/xeprotocol/xe/issues/365)
filed 2026-11-11 08:24 UTC
by RahmaMaria

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Medium

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 00:06 UTC

Told
:   2026-11-20 00:04 UTC

### Patches

None linked.

### History

- [543801](https://bounty.xe.network/log/543801) 2026-11-20 00:05 UTC github.state\_pushed
- [543800](https://bounty.xe.network/log/543800) 2026-11-20 00:05 UTC github.labels
- [543743](https://bounty.xe.network/log/543743) 2026-11-20 00:04 UTC reply.mirrored
- [542774](https://bounty.xe.network/log/542774) 2026-11-20 00:04 UTC proposal.adopted
- [542773](https://bounty.xe.network/log/542773) 2026-11-20 00:04 UTC reply.sent
- [542772](https://bounty.xe.network/log/542772) 2026-11-20 00:04 UTC ticket.status new → closed
- [540630](https://bounty.xe.network/log/540630) 2026-11-20 00:03 UTC proposal.adopted
- [540629](https://bounty.xe.network/log/540629) 2026-11-20 00:03 UTC ticket.facts area
- [540628](https://bounty.xe.network/log/540628) 2026-11-20 00:03 UTC proposal.adopted
- [540627](https://bounty.xe.network/log/540627) 2026-11-20 00:03 UTC ticket.facts
- [540626](https://bounty.xe.network/log/540626) 2026-11-20 00:03 UTC proposal.adopted
- [540625](https://bounty.xe.network/log/540625) 2026-11-20 00:03 UTC ticket.facts

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

### Report

# H3 — MEDIUM — GET /accounts/{address}/chain loads the entire chain, then paginates

Title: \[Medium\] GET /accounts/{address}/chain loads the entire account chain before slicing the page — cost scales with chain length, not page size

---

## Summary

`GET /accounts/{address}/chain?limit=1` fetches and copies the whole account chain — one Badger Get plus one JSON unmarshal per block hash — and only then slices out the single requested entry. The response is identical regardless of chain length, but the work is not: on my local node, a 173-block chain took 2.0–3.8 ms per request while a 1-block chain took 0.16–0.73 ms, against a 0.13–0.38 ms health-endpoint control. Same response size, \~3–18x the work. The per-request cost is \~10–21 microseconds per block, so a long chain — or many concurrent requests — turns a cheap-looking unauthenticated GET into unbounded work. The rate limit (200 req/s, burst 1000\) caps the request rate, not the work per request.

Suggested tier: **Medium** (2,500 XE).

My XE address for payout: `d0a60d324b5f1d72fa06e79b92f0253241b85438a10c0ccac76c5f9b8135d8d4`

## How I found it

I was reading the API handlers to see which endpoints do work proportional to the data they return, and which do work proportional to the data they hold. `handleGetChain` in `api/handler.go:451-483` calls `node.GetChain`, which calls `core/ledger.go:2374-2390` `GetChain`, which copies the whole chain — and `store/badger_store.go:127+` `GetAccountChain` does one Badger Get plus one JSON unmarshal per block hash. Only after all of that does `paginateSlice` cut the page out. I had already seen the same shape on two other endpoints (#70 `GET /accounts`, #215 `GET /frontiers`), so I knew the pattern — but this is a different endpoint with its own code path, and I wanted to measure it rather than assume it.

I grew a local chain to 173 blocks on my test node (network `local-0001`), then timed `GET /accounts/{address}/chain?limit=1` against a 1-block account and against `/health`, 25 requests each, keep-alive, same node and conditions. The first two runs showed 3.81 ms and 2.00 ms for the 173-block chain against 0.21 ms and 0.16 ms for the 1-block chain. I restarted the node and ran it twice more to make sure the gap was not a warm-up artifact: 2.18 ms and 2.51 ms against 0.63 ms and 0.73 ms, with the health control at 0.14 ms and 0.18 ms. The gap held across all four runs and a restart. The response size was identical in every case — one entry — so the extra milliseconds are pure chain-length work.

## Steps to reproduce

Local node only (network `local-0001`, Go 1.25 node built from the repo). No live testnet traffic, no faucet.

1. Start the local node and grow one account's chain to \~173 blocks (PoW is the bottleneck, \~0.6–5.7 s/block, mean \~2 s). Keep a second account at 1 block.
2. Run the timing script below (keep-alive — do not use urllib without keep-alive, it is noisy). It issues 25 GETs per target and reports the median.
3. Compare `GET /accounts/{treasury}/chain?limit=1` (173 blocks) against `GET /accounts/{acct1}/chain?limit=1` (1 block) and `/health` (control). Expect the 173-block chain to be consistently \~3–18x slower than the 1-block chain with an identical 1-entry response.

Timing results (median of 25 requests each, keep-alive):

| run | chain\=173 ?limit\=1 | chain\=1 ?limit\=1 | /health (control) |
| --- | --- | --- | --- |
| 1 | 3.81 ms | 0.21 ms | 0.38 ms |
| 2 | 2.00 ms | 0.16 ms | 0.13 ms |
| 3 (after node restart) | 2.18 ms | 0.63 ms | 0.14 ms |
| 4 (same session) | 2.51 ms | 0.73 ms | 0.18 ms |

Per-block cost ≈ 10–21 µs. Baseline at chain\=7 was 0.53 ms (urllib, no keep-alive).

Timing script (`timeit.py`):

```
#!/usr/bin/env python3
"""Keep-alive timing for XE local node endpoints (H3/H4 confirmation)."""
import http.client
import json
import statistics
import sys
import time

BASE_HOST = "127.0.0.1"
BASE_PORT = 8081

TREASURY = "696924bb9256dfca46a8d5f3865dd069ab277fcd131ec1a37eddf0dc74fc59e9"
ACCT1 = "c74daff61c1c35108c8037e52521bd0bebd9e77355fd978fc186a0c0c62395e9"

def measure(path, n=25):
    conn = http.client.HTTPConnection(BASE_HOST, BASE_PORT, timeout=10)
    times = []
    for _ in range(n):
        t0 = time.perf_counter()
        conn.request("GET", path)
        r = conn.getresponse()
        r.read()
        times.append((time.perf_counter() - t0) * 1000)
    conn.close()
    times.sort()
    return statistics.median(times), min(times), max(times)

def main():
    targets = [
        ("health (control)", "/health"),
        ("H3 chain=173 ?limit=1", f"/accounts/{TREASURY}/chain?limit=1"),
        ("H3 chain=1 ?limit=1", f"/accounts/{ACCT1}/chain?limit=1"),
        ("H4 pending=140 ?limit=1", "/pending?limit=1"),
    ]
    for label, path in targets:
        med, mn, mx = measure(path)
        print(f"{label:28s} median={med:6.2f}ms min={mn:6.2f}ms max={mx:6.2f}ms")

if __name__ == "__main__":
    main()
```

## Impact

A cheap unauthenticated request amplifies into work proportional to the target account's chain length. The rate limit (200 req/s at `api/handler.go:296`) bounds how many requests arrive, not how much work each one does — so a long chain (or many concurrent requests against long chains) is a straightforward way to load a node. This is the same shape that already wedged the node's own health sampler on `GET /accounts` (#70). I am calling it Medium, not High: it is a resource-amplification issue, not a correctness or funds issue, and the per-block cost (\~10–21 µs) means a single request is still small — the damage needs volume or very long chains.

## Root cause

`GetChain` copies the whole chain before `paginateSlice` runs. The pagination is applied after the full load (`api/handler.go:451-483` → `node.GetChain` → `core/ledger.go:2374-2390` → `store/badger_store.go:127+` `GetAccountChain`, one Badger Get \+ JSON unmarshal per block hash), so `?limit=1` costs the same as `?limit=1000`. The endpoint never bounds the read to the requested page.

## Why this is not a duplicate

I read all 189 issues (open and closed) before filing. Zero hits for `getchain` \+ `pagination` and `chain` \+ `endpoint` \+ `entire`. This is a different endpoint with a distinct code path from the two awarded findings in the same pattern class — **#70** (`GET /accounts`, full-lattice work regardless of `?limit=`) and **#215** (`GET /frontiers`, full-lattice work regardless of `?limit=`, no result cache). I flag the adjacency honestly: triage may see it as the same pattern class and fold it into #70/#215. The distinction is the endpoint and the code path — this one loads a single account's full chain through `GetAccountChain`, not the whole lattice.

## Suggested fix

Bound the read to the requested page: fetch only the block hashes for the requested slice (or stream the chain and stop after the page is filled) instead of copying the whole chain and slicing after. If the storage layer cannot slice cheaply, add a result cache keyed on `(address, limit, offset)` with a short TTL, as was done for `/blocks/recent`.

## What I deliberately did not test

I did not test this against a live public node, did not run any concurrent load against a public node, and did not measure the effect on a chain longer than 173 blocks — the per-block cost is linear, but I did not verify the top end. I also did not test whether the same full-load-then-slice shape exists on other endpoints beyond the ones already reported (#70, #215) — that is a separate audit and I did not want to expand this report.

### Thread

**Joseph Denne**team2026-11-20 00:04 UTCgithub

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-13237 and says what differs; it is read like any other.
