{"id":"XE-0128","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"215","source_url":"https://github.com/xeprotocol/xe/issues/215","reporter_id":30,"title":"[Medium] api: GET /frontiers does full-lattice work regardless of ?limit= and has no result cache","area":"node-api","claimed_tier":"medium","assessed_tier":"medium","verdict":"valid","duplicate_of":null,"tracked":true,"internal_ref":"#1030","internal_url":"https://github.com/xeprotocol/core/issues/1030","status":"awarded","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":500000000,"award_approved":true,"paid_ref":"","created_at":"2026-10-30T06:19:10.280773+00:00","updated_at":"2026-11-11T15:12:16.507996+00:00","source_created_at":"2026-10-30T06:17:18+00:00","told_at":"2026-11-08T21:44:13+00:00","status_changed_at":"2026-11-08T21:44:30.278307+00:00","url":"https://bounty.xe.network/XE-0128","body":"# api: `GET /frontiers` does full-lattice work regardless of `?limit=`, and has no result cache\n\n| Field | Value |\n|---|---|\n| **Suggested tier** | Medium (2,500 XE) — \"measurable slowdown under volume\" |\n| **Area** | The HTTP API, its rate limits and the test.network proxy |\n| **Node** | `testnet-0005`, `xe/4f19518` |\n| **Source** | `master` @ `9787859` |\n| **Campaign notice** | #214 |\n| **XE payout address** | `6591eab453dbd93b27e2cd674712f94b42d9e7e4d52980b966fa6da4ccd832dc` |\n\n---\n\n## 1. Summary\n\n`GET /frontiers` materialises every frontier in the lattice — and for each one reads a block\nbody **and** deserialises the whole account chain — before pagination is applied. The page size\ndoes not change the work, and the handler has **no result cache**, so every request pays the\nfull cost.\n\n## 2. Affected code\n\n```go\n// api/handler.go:688\nfunc (h *Handler) handleGetFrontiers(w http.ResponseWriter, r *http.Request) {\n    offset, limit := parsePagination(r)\n    frontiers := h.node.GetFrontiersDetailed()          // full lattice, BEFORE pagination\n    sort.Slice(frontiers, func(i, j int) bool { return frontiers[i].Account < frontiers[j].Account })\n    writeJSON(w, http.StatusOK, paginateSlice(frontiers, offset, limit))\n}\n```\n\n```go\n// node/node.go:968\nfunc (n *Node) GetFrontiersDetailed() []FrontierInfo {\n    frontiers := n.Ledger.Frontiers()\n    out := make([]FrontierInfo, 0, len(frontiers))\n    for account, hash := range frontiers {\n        info := FrontierInfo{Account: account, Frontier: hash}\n        if b := n.Ledger.GetBlock(hash); b != nil {      // one block read per account\n            info.BlockType = string(b.Type)\n            info.Timestamp = b.Timestamp\n            info.Representative = b.Representative\n        }\n        info.BlockCount = len(n.Ledger.GetChain(account)) // full chain deserialisation per account\n        out = append(out, info)\n    }\n    return out\n}\n```\n\nPer account that is `GetBlock(hash)` **plus** `GetChain(account)`. `Ledger.GetChain`\n(`core/ledger.go:2374`) copies every block pointer; the underlying read is\n`store/badger_store.go:127 GetAccountChain`, which JSON-unmarshals every block body referenced\nby the chain's hash list. So the handler does `N` block reads + `N` full-chain deserialisations,\nthen discards all but `limit` entries.\n\n`/blocks/recent` has a 1 s result cache (`api/handler.go:652-663`). `/frontiers` has none.\n\n## 3. Reproduction\n\nRead-only, no writes, no malformed input, single source IP. Script: `t2_frontiers_amp.py`.\n\n```\npython3 t2_frontiers_amp.py https://ldn.core.test.network --arm frontiers --concurrency 24 --seconds 300\npython3 t2_frontiers_amp.py https://ldn.core.test.network --arm supply    --concurrency 24 --seconds 60\n```\n\nLattice at measurement time: **16,517 accounts / 123,227 blocks**.\n\n### 3.1 Cost is independent of `?limit=`\n\n```\nGET /frontiers              -> 200, 4.06s,  22711 bytes\nGET /frontiers?limit=1      -> 200, 3.81s,    230 bytes\nGET /frontiers?limit=10     -> 200, 4.15s,   2270 bytes\nGET /frontiers?limit=10000  -> 200, 5.00s, 226904 bytes\n```\n\n`limit=1` returns **99× less data** than the default page and costs the same or more.\n\n### 3.2 Per-byte cost, against an O(1) control\n\nFive samples each, `ldn`, idle:\n\n```\n/supply                    median 0.53s   bytes 805   us/byte   654\n/frontiers?limit=1         median 3.93s   bytes 230   us/byte 17083\n```\n\n`/frontiers?limit=1` is **~26× the per-byte cost of the O(1) `/supply` control**.\n\n### 3.3 No cache\n\nSix back-to-back `/frontiers?limit=1` requests each cost the full ~3.8 s:\n\n```\n/frontiers?limit=1 x6:     3.94  3.80  3.82  3.73  3.79  3.88\n/blocks/recent?limit=1 x6: 3.52  0.50  3.50  0.48  0.49  3.52   <- 1s cache, alternates\n```\n\n### 3.4 Reproduced on all three bootstrap nodes\n\n| node | `/frontiers?limit=1` (idle) |\n|---|---|\n| ldn | 3.99 s |\n| ffm | 3.80 s |\n| nyc | 5.47 s |\n\n### 3.5 Paired-arm campaign\n\nTwo arms, same script, same concurrency, same interval, same source IP.\n\n**Arm A — control, `GET /supply` (O(1) handler), concurrency 24, 60 s, `ldn`:**\n\n```\nRESULT arm=supply requests=2700 p50=0.5s max=0.92s bytes=2173500 codes={200: 2700}\nhealth samples=25 503s=0\n```\n\n**Arm B — under test, `GET /frontiers?limit=1`, concurrency 24, 300 s, `ldn`:**\n\n```\nRESULT arm=frontiers requests=257 p50=27.46s max=41.64s bytes=59110 codes={200: 257}\nhealth samples=121 503s=0\n```\n\n**Arm B on `ffm`, concurrency 24, 180 s:**\n\n```\nRESULT arm=frontiers requests=192 p50=24.33s max=27.55s bytes=44160 codes={200: 192}\nhealth samples=73 503s=0\n```\n\nAt the same concurrency the control completed **2,700** requests in 60 s while `/frontiers`\ncompleted **~50** in the same window — a **~54× throughput reduction**.\n\nLatency scaling with concurrency on `/frontiers?limit=1`:\n\n| concurrency | window | requests | p50 | max |\n|---|---|---|---|---|\n| 1 (sequential) | — | 6 | 3.8 s | 4.06 s |\n| 12 | 30 s | 24 | 15.71 s | 17.03 s |\n| 24 | 90 s | 72 | 33.49 s | 37.31 s |\n| 24 | 300 s | 257 | 27.46 s | 41.64 s |\n| 24 (`ffm`) | 180 s | 192 | 24.33 s | 27.55 s |\n\n## 4. Impact\n\n**Who is affected.** Anyone relying on this node's HTTP API: explorers, the embedded web\nwallet, and any integrator polling `/frontiers`. The handler competes with the node's own\nobservability sampler for CPU, so the pressure is visible in `/health` as well.\n\n**What the client sends.** Read-only `GET /frontiers?limit=1`, 230 bytes per response, from\n**one source IP**, at **≤ 24 concurrent connections**, for **≤ 5 minutes per arm**, well under\nthe documented read budget (200 rps / 1000 burst per IP).\n\n**What the network did.**\n\n1. **Amplification.** 230 bytes in, ~3.9 s of ledger work out — ~17,000 µs of handler time per\n   response byte at `limit=1`, against ~654 µs/byte for the O(1) `/supply` control.\n2. **Throughput collapse under modest concurrency.** 12 concurrent clients raise per-request\n   latency from 3.8 s to 15.7 s; 24 concurrent clients to ~27–33 s. That is past the point where\n   an upstream proxy or client timeout fires, while the requests keep consuming CPU.\n3. **Health-sampler pressure.** `/health` reports `sampler_alive` against a 45 s budget\n   (`node/observability.go`, `3 * SampleInterval`). Baseline is `sample 4s old`; during the\n   300 s arm at concurrency 24 it rose to **17–23 s** and stayed there:\n\n```\nhealth 200 sample 23s old\nhealth 200 sample 14s old\nhealth 200 sample 20s old\nhealth 200 sample 11s old\nhealth 200 sample 17s old\nhealth 200 sample 18s old\nhealth 200 sample 18s old\nhealth 200 sample 19s old\nhealth 200 sample 19s old\n```\n\n   It did **not** reach 503 at concurrency 24, so this is a measurable slowdown and not a\n   health-signal denial. A separate client session on `/accounts/{address}/balance` was\n   unaffected throughout (0.48–0.53 s, unchanged from idle).\n4. **No cache to absorb repeat traffic.** Unlike `/blocks/recent`, every request pays full cost,\n   so honest polling is as expensive as deliberate load.\n5. **Memory churn.** Every request allocates a fresh `[]FrontierInfo` for all 16,517 accounts\n   plus a fresh `[]*Block` copy per account via `GetChain`.\n\n**Worst case.** Sustained concurrency from a single client drives per-request latency into the\ntens of seconds and holds the health sampler at roughly half its budget. It does not, at the\nconcurrency tested, cross the 45 s threshold that makes `/health` return 503.\n\n## 5. Suggested fix\n\nPage before materialising: read `Ledger.Frontiers()` (frontier only, no block bodies), sort the\naddress keys, slice `[offset:offset+limit]`, and build `FrontierInfo` only for the requested\nwindow. The per-account `GetChain` call is unnecessary for this endpoint — `BlockCount` is\nalready computed by the sampler every 15 s (`node/observability.go:195`), and\n`BlockType`/`Timestamp`/`Representative` come from the frontier block alone.\n\n## 6. Notes on scope\n\n- Read-only `GET` only. No `POST`, no writes, no malformed blocks, no writes to other users'\n  accounts or workloads.\n- No traffic to the hosting layer (nginx / DNS / website / faucet) — out of scope per the\n  bounty page.\n- Campaign ran under the pre-notice in #214, within the announced bounds.\n- **Prior art.** `GetFrontiersDetailed` returns zero matches across all issues. `/frontiers` is\n  mentioned in #70 only inside its suggested-fix text, and incidentally in #78/#59/#19/#185.\n  No issue reports the pagination cost, the per-account `GetBlock`+`GetChain` pair, or the\n  missing cache on this handler.","patch_refs":[{"id":218,"repo":"xeprotocol/xe","kind":"pr","ref":"216","title":"fix(api,node): paginate frontiers before materialising and cache sorted lattice (#215)","url":"https://github.com/xeprotocol/xe/pull/216","note":"","state":"open","author":"tipprb","added_by":"desk","created_at":"2026-10-30T08:31:11.884800+00:00"},{"id":219,"repo":"xeprotocol/xe","kind":"pr","ref":"217","title":"fix(api): page before materializing frontiers, avoid full chain deserialization, and add 1s result cache (fixes #215)","url":"https://github.com/xeprotocol/xe/pull/217","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-30T08:31:11.884800+00:00"}],"thread":[{"id":69,"source":"github","source_ref":"6518533782","author":"arturbarszcz78","team":false,"body":"[comment from a blocked account removed]","created_at":"2026-10-30T08:30:55+00:00"},{"id":256,"source":"github","source_ref":"6651512164","author":"targammm","team":false,"body":"Reporting as the author of #215 and the pre-notice in #214. I am the researcher who filed this issue, measured the lattice, and posted the PoC. The comment above claiming the fix is not from me, and I am asking the maintainers not to pay it.\n\n## 1. The report is mine, and the attribution in it is not optional\n\nThe campaign notice in #214 was filed by me before any measurement was taken, and it states the attribution explicitly:\n\n> Attribution — Wallet `6591eab453dbd93b27e2cd674712f94b42d9e7e4d52980b966fa6da4ccd832dc` (testnet-0005, self-funded from faucet.test.network). GitHub: targammm. Disclosure credit handle: \"Waguri\".\n\nThat wallet funded the testnet account that ran the measurements, and that handle is the credit I asked to be listed. The comment above arrives **2 hours 13 minutes** after #215 was filed (`06:17:18Z` vs `08:30:55Z`), and PR #217 was opened **8 seconds before** the comment (`08:30:47Z`). Neither address in the comment (`23bd7ef9c17e79ee76e9f656d618cf0f7c2cda4a700b58d065917d14e97a304a` and `0x4dE44e40fa09A3ce7F386a292318dD4B609b281E`) appears anywhere in my report or my pre-notice.\n\nThere is no stated relationship between my account and `arturbarszcz78`, and I did not authorise anyone to claim this.\n\n## 2. This is not an isolated case — it is the whole PR list\n\nI checked every pull request that account has open. The numbers below are from the GitHub API, not from my reading of the titles:\n\n- Account created **2026-10-26**, ten days before this claim\n- **55 open pull requests, 0 merged, 0 closed, 0 draft**\n- **0 issues ever filed** — the account has never reported a bug\n- 6 repositories, all forks of other projects\n- 1 follower, following 0\n\nThose 55 PRs reference **60 distinct issues**, and **49 of them are issues filed by other researchers**. I have verified the author of each:\n\n| PR | Issue | Researcher | Gap |\n|---|---|---|---|\n| #217 | #215 | `targammm` (me) | 2 h 13 m |\n| #124 | #70 | `N-a-m-i-420` | 40 h 10 m |\n| #124 | #78 | `BoomGone` | 37 h 26 m |\n| #174 | #172 | `nexastark` | 45 m |\n| #181 | #178 | `nexastark` | 44 m |\n| #179 | #177 | `nexastark` | 15 m |\n| #176 | #175 | `zafrans` | 11 m |\n| #131 | #130 | `BoomGone` | 3 m |\n| #141 | #69 | `cryptoloser8-coder` | 52 h 20 m |\n| #213 | #200 | `hayokakoweda75` | 3 h 1 m |\n| #122 | #94 | `Ryanmahrez44` | 35 h 31 m |\n| #111 | #89 | `Ryanmahrez44` | 25 h 48 m |\n| #110 | #93 | `Ryanmahrez44` | 25 h 12 m |\n| #108 | #84 | `rusdiwireng` | 24 h 49 m |\n| #106 | #96 | `CoderMisaki` | 14 h 45 m |\n| #105 | #97 | `CoderMisaki` | 14 h 9 m |\n| #187 | #168 | `0xMissy22` | 5 h 41 m |\n| #189 | #167 | `0xMissy22` | 6 h 12 m |\n\nThat is **16 different researchers** whose findings this account has submitted patches against. Seven of the PRs landed within 6 hours of the issue being filed, and #131 landed **3 minutes** after issue #130. PR #179 was opened 15 minutes after its issue. The full table of 49 is in the appendix at the bottom.\n\nIssue #172 went up at `06:01:35Z`; PR #174 was open at `06:46:53Z` — 45 minutes later, for a finding that had been measured against a live lattice.\n\n## 3. The code is not the problem — the claim is\n\nTo be clear about what I am and am not saying: I reviewed PR #217 and it is a correct fix. I cloned `master` @ `978785929bc17a2f4ab30c383e5fc9cf4d634512`, checked out `refs/pull/217/head`, and ran it:\n\n```\ngo build ./...                                       -> PASS\ngo vet ./api/... ./node/... ./store/... ./core/...   -> PASS\ngo test ./store/... -run ChainLength -count=1        -> PASS\ngo test ./api/...   -run Frontiers    -count=1       -> PASS\n```\n\nThe bug it fixes is the one I reported — `node/node.go:968` materialises every frontier, reads a block body per account, and calls `GetChain` per account before `paginateSlice` runs at `api/handler.go:688`. The fix sorts the account keys first, slices, and only materialises the requested window, and adds `GetAccountChainLength` which returns `len(hashes)` instead of unmarshalling every block body just to count them. I would merge it. Several of the other PRs carry real tests and substantive diffs too.\n\nSo my ask is not \"reject the code\". It is:\n\n1. **Do not pay `23bd7ef9...` or `0x4dE44e40...` for #215.** Neither address is mine and neither appears in my report. If this issue is accepted, the payout goes to `6591eab453dbd93b27e2cd674712f94b42d9e7e4d52980b966fa6da4ccd832dc` as declared in #214, and the disclosure credit is **Waguri**.\n2. **Credit the finding to the reporter, not the patch.** The finding — the measurement, the paired-arm campaign, the per-byte cost analysis, the health-sampler pressure data — is the bounty-eligible work. A patch written two hours after reading someone's issue is not the same contribution.\n3. **Check the other 54 PRs before genesis payout.** The programme pays in native XE at mainnet launch. If these are accepted on the strength of their diffs, the payout follows the PR author rather than the 16 people who actually found the bugs. The same 3-minute and 15-minute turnarounds above suggest the pattern will repeat for every issue filed from here.\n\nI have no problem with someone writing a fix for an issue I filed. I have a problem with the same account doing it 49 times, to 16 different researchers, with two fresh payout addresses attached and no disclosure of any relationship.\n\nHappy to answer any questions on the measurements or the PoC (`t2_frontiers_amp.py`, as referenced in the issue) if useful.\n\n## Appendix — all 49 PRs against other researchers' issues\n\n```\nPR    ISSUE  AUTHOR                  GAP        TITLE\n#134  #18    rusdiwireng             114h10m    Marketplace messages carry no freshness check\n#149  #38    efootballyay-dot         96h29m    Invalid lease_cancel still unwinds provider accept\n#150  #57    greenblood33333          81h31m    Public release never syncs 289 lease-failed blocks\n#148  #42    byfor8                   81h 4m    quorum can finalize a block that lost the vote\n#147  #43    byfor8                   80h35m    swap cannot unwind lease and multisig descendants\n#144  #44    byfor8                   79h47m    frontier sync client allocates full decoded page\n#143  #45    byfor8                   79h41m    statechain gossip accepts any 64 hex hash\n#142  #46    byfor8                   78h52m    vote gossip admits zero-weight votes\n#139  #50    byfor8                   77h35m    SSE overflow drops messages per listener\n#137  #47    byfor8                   75h37m    rate limit keyed per peer ID\n#135  #53    byfor8                   74h 9m    gossip validator forwards garbage blocks\n#129  #52    byfor8                   72h41m    staged-block validation accepts bad lease blocks\n#127  #54    byfor8                   72h 4m    attestation rate limit is check-then-act\n#122  #48    byfor8                   70h14m    TCP tunnel authorization is replayable\n#151  #63    willyoungzy001           61h51m    Explorer search bar not working\n#146  #59    senastor                  59h43m    no in-flight guard on sync rounds\n#141  #69    cryptoloser8-coder       52h20m    case-sensitive account lookups\n#126  #60    senastor                  51h13m    SIGTERM does not cancel in-flight sync\n#136  #68    naufal2002               48h34m    supply monitor under-counts renewal escrow\n#138  #71    N-a-m-i-420              46h 0m    wallet origin sends no CSP\n#125  #67    juantzy7                  45h59m    SSH pre-auth connections exhaust connections\n#124  #70    N-a-m-i-420              40h10m    GET /accounts full-lattice work\n#123  #75    BoomGone                 37h35m    public nodes ship dev CORS origin\n#124  #78    BoomGone                 37h26m    GET /pending silently returns only 100 items\n#121  #83    rusdiwireng              36h 0m    block fields outside canonical hash not hashed\n#119  #85    rusdiwireng              35h34m    DAO tool signs hash over typed op value\n#122  #94    Ryanmahrez44             35h31m    X-Signature header is replayable\n#117  #81    BoomGone                 35h27m    reverse proxy resolves clientIP to 127.0.0.1\n#115  #82    rusdiwireng              34h28m    faucet docs state old grant terms\n#113  #79    BoomGone                 34h13m    GET /leases error lists five states\n#114  #88    BoomGone                 33h41m    block submit leaks raw Go error chains\n#112  #91    Ryanmahrez44             31h27m    single oracle key can future-time stamps\n#111  #89    Ryanmahrez44             25h48m    remote crash via directory gossip\n#110  #93    Ryanmahrez44             25h12m    lease_accept bypasses multisig threshold\n#108  #84    rusdiwireng              24h49m    binary codec drops lease_renew PoW nonce\n#109  #92    Ryanmahrez44             24h44m    LeaseCancel after renew refunds once\n#140  #104   BoomGone                 22h40m    lease SSH has no host key verification\n#107  #95    CoderMisaki              15h50m    pre-conflict FINAL votes at stale receipt\n#106  #96    CoderMisaki              14h45m    descendant finalization finalizes ancestors\n#105  #97    CoderMisaki              14h 9m    lease_renew accepts any retained cert\n#193  #169   0xMissy22                 7h10m    directory registrations have no freshness check\n#189  #167   0xMissy22                 6h12m    sync quarantine poisoning\n#187  #168   0xMissy22                 5h41m    loopback XFF trust collapses clients\n#108  #100   BoomGone                  5h 1m    UnmarshalBlock silently drops PoW nonce\n#174  #172   nexastark                  0h45m    rate-limiter map hits maxRateLimiterIPs\n#181  #178   nexastark                  0h44m    Grafana allows anonymous datasource writes\n#179  #177   nexastark                  0h15m    nil block element panics\n#176  #175   zafrans                    0h11m    LeaseAccept locks emission terms\n#131  #130   BoomGone                    0h 3m    xe send defaults to XUSD\n```","created_at":"2026-11-05T06:36:20+00:00"},{"id":1250,"source":"github","source_ref":"6727003350","author":"Joseph Denne","team":true,"body":"Thanks for the report and the careful measurements. We have confirmed the behaviour on this endpoint and it is being handled alongside related work; the payout details in your report are the ones on file for this ticket. We will update here when the fix is in.","created_at":"2026-11-08T20:55:37+00:00"},{"id":1322,"source":"github","source_ref":"6727536025","author":"Joseph Denne","team":true,"body":"The fix is live on all three testnet nodes: /frontiers now pages before it reads anything, so a request costs its page rather than the lattice. This ticket moves to patched; the assessment and award follow, and the payout address and credit stay as declared in your report.","created_at":"2026-11-08T21:44:13+00:00"}]}