{"id":"XE-12677","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":2086,"title":"[Minor] net/sync: requestSync discards every block already received when the stream errors mid-way — all-or-nothing read loop, zero progress per faulty round","area":"p2p-sync","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0023","tracked":true,"internal_ref":"#1053","internal_url":"https://github.com/xeprotocol/core/issues/1053","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T08:53:49.037929+00:00","updated_at":"2026-11-20T19:45:44.521289+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:44.553327+00:00","status_changed_at":"2026-11-20T19:45:44.530585+00:00","url":"https://bounty.xe.network/XE-12677","body":"## Summary\r\n\r\n`requestSync` reads ALL pages of a sync round into `allBlocks` before applying any of them. If the stream errors mid-way — the peer resets, disconnects, or the 60-second stream deadline fires while pages are still coming — the function returns the error before the apply loop ever runs, and every block already received in that round is thrown away. The next round re-requests the same set from the same frontiers and re-downloads it in full. Against a slow or flaky peer, a node can receive thousands of blocks per round and apply zero of them, round after round. Measured locally against the real code: 256 blocks received then a mid-stream reset → 0 applied; 64 blocks received then the 60s deadline → 0 applied; the follow-up healthy round applies all 320, proving round 1 was pure wasted work.\r\n\r\n## Location\r\n\r\n- `xe/net/sync.go:556-585` (read loop: `dec.Decode` per page, `allBlocks = append(allBlocks, resp.Blocks...)` at :571; a non-EOF decode error hits `return err` at :569, before any apply)\r\n- `xe/net/sync.go:589-648` (quarantine filter + apply loop start at :599 — only reachable if the read loop exits cleanly via EOF, terminal page, or the 10,000-block cap)\r\n- `xe/net/sync.go:531,538` (60s context + stream deadline — the organic trigger: a large catch-up over a slow link)\r\n- Contrast — the sibling subsystem does it right: `xe/statechain/sync.go:168-196` applies each page's blocks INSIDE the read loop (`for _, b := range resp.Blocks { chain.AddBlock(b) }` right after each `Decode`), so a mid-stream error there keeps everything applied so far. The ledger sync client is the only all-or-nothing reader.\r\n\r\n## Description\r\n\r\nThe read loop has three clean exits (EOF, `!HasMore`, `maxTotalBlocks`) and one error exit (`return err` on any non-EOF decode failure). The apply loop sits after the read loop, so the error exit skips it entirely — `allBlocks` is a local variable that dies with the call. There is no partial save, no resume, and no record of how far the round got: the next round starts from the same frontiers and asks for the same blocks again.\r\n\r\nTwo triggers, both measured (PoC: two loopback libp2p hosts, server ledger with 320 valid signed mint blocks over 5 pages of 64, fresh client ledger):\r\n\r\n1. **Mid-stream reset (peer crash / disconnect / RST):** the server sends 4 full pages (256 blocks), then writes partial JSON and resets the stream. The client decode fails (`stream reset (remote)`), `requestSync` returns the error, and the client ledger holds **0** of the 256 blocks it received. A follow-up healthy round then applies all 320 — the first round's bandwidth and decode work bought nothing.\r\n2. **60s deadline (slow peer, large catch-up — no attacker needed):** the server sends 1 page (64 blocks) then stalls. The client's stream deadline fires at 60s (`i/o deadline reached`), `requestSync` returns the error, and the client ledger holds **0** of the 64 blocks. Control (clean stream, real `handleSyncStream`): 320 received, 320 applied — so the 0s are the discard, not bad blocks.\r\n\r\nThe discard is total, not partial: it does not matter whether 64 blocks or 9,999 arrived — one error before the terminal page voids all of them. The per-round bound is `maxTotalBlocks` (10,000 blocks, ~5.7 MB at the measured 571 B/block).\r\n\r\nHonest bounds, stated up front: (a) on live testnet-0005 today this rarely fires, because XE-0267 kills full-node rounds server-side first (the >1 MB request never decodes, so no pages are ever received); this matters for networks under the ~7,825-account threshold, or after XE-0267 is fixed; (b) the failure is LOUD — `requestSync` returns an error and the caller records `RecordSyncRound(false)`, so there is no false-success here (that is XE-1050's EOF path, which is different: a clean EOF applies the received blocks); (c) a later healthy round recovers fully — the harm is wasted bandwidth/CPU and zero-progress rounds, not a permanent stall by itself. Against a persistently slow peer the zero-progress rounds repeat indefinitely, which is why this is liveness-relevant rather than cosmetic.\r\n\r\n## Impact\r\n\r\nSync liveness / availability, Minor. A slow peer (or a flaky link) turns every round into a full re-download with zero progress: bandwidth, JSON decode, and a 60s round budget are spent and discarded. On a node syncing a large catch-up over a slow link, this is the difference between converging in N rounds and never converging from that peer. No funds, no finality effect, no false success — the node knows the round failed; it just cannot keep what it already paid to receive.\r\n\r\n## Reproduction\r\n\r\nAttached: `dd3_nw2_poc_test.go` — an internal `net` package test (same two-loopback-host pattern as XE-6880's PoC). Copy it into `xe/net/` and run:\r\n\r\n```\r\nTMPDIR=~/tmp go test ./net/ -run TestDD3NW2 -v -count=1\r\n```\r\n\r\nMeasured at HEAD 89a542e:\r\n\r\n```\r\nCONTROL clean stream: received=320 applied=320\r\nFAULT mid-stream reset: server sent=256 blocks, requestSync err=stream reset (remote), client applied=0\r\nFOLLOW-UP healthy round: applied=320 (round 1 wasted 256 received blocks)\r\nFAULT deadline stall: server sent=64 blocks, requestSync err=i/o deadline reached after 1m0s, client applied=0\r\n```\r\n\r\n## Suggested fix\r\n\r\nApply per page, as the statechain sync client already does (`statechain/sync.go:168-196`): move the `AddSyncedBlock` pass inside the read loop (or flush `allBlocks` through the apply loop before returning the decode error), so a mid-stream error keeps the pages already received. The retryable-block handling in the apply loop already tolerates out-of-order arrival, so per-page apply needs no new ordering logic.\r\n\r\n## Not a duplicate\r\n\r\n- **XE-0268** (apply loop discards dependency-retryable blocks after 10 passes): that discard happens AFTER receipt, inside the apply loop, on blocks that failed validation; this report is the read loop discarding blocks BEFORE the apply loop ever runs, on blocks that would have applied fine (the control proves it). Different code region (:562-585 vs :599-648), different trigger (stream error vs dependency failure), different fix (per-page apply vs more passes / topological sort).\r\n- **XE-1715** (closed, duplicate of XE-0268) is the closest title match (\"discards dependency-blocked blocks\") — but its discard is the APPLY loop's: blocks that reached the apply loop and failed dependency validation after 10 passes, served in random Go map order. This report's discard is the READ loop's, before the apply loop ever runs, on a stream error, on blocks the control proves would have applied fine.\r\n- **XE-1050 defect 2** (nil return on `!gotTerminal` / false success): that is the clean-EOF path, which APPLIES the received blocks and wrongly reports success. This report is the error path, which DISCARDS the received blocks and correctly reports failure. Opposite behaviours on opposite exits.\r\n- **XE-6880** (empty-page spin): no blocks are received there, so nothing is discarded; the missing invariant is a page cap / zero-progress exit, not a partial apply.\r\n- **XE-10240** (retryable failures never quarantined, cross-round re-verification): its blocks REACH the apply loop and fail there; these blocks never reach it. Its fix (quarantine retryables) does not touch the read loop.\r\n- **XE-11908 / XE-5289 / XE-2099 / XE-11599** (per-page byte budget, memory retention): the opposite direction — those are about the client retaining TOO MUCH; this is about the client retaining NOTHING after an error.\r\n- **XE-10571** (maxTotalBlocks checked only between pages), **XE-7064 / XE-0040** (overlapping rounds, OOM), **XE-12232** (server-side `streamPages` drain on disconnected streams), **XE-0107 / XE-7924 / XE-11679** (nil-block panic): different mechanisms in the same function/file, each read in full and distinguished by trigger, code region, and fix.\r\n- **XE-0120** (statechain sync cap mismatch): different subsystem (`statechain/sync.go`) and mechanism (page larger than the client cap); the statechain client is the per-page-apply contrast cited above, not the subject.\r\n- **XE-0267** (1 MB request truncation): the reason this rarely fires on live testnet today (stated in the bounds above); a different defect (server-side request decode) with a different fix.","patch_refs":[],"thread":[{"id":15104,"source":"desk","source_ref":"reply:12995","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0023, 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-12677 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:44.521289+00:00"}]}