{"id":"XE-13238","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"366","source_url":"https://github.com/xeprotocol/xe/issues/366","reporter_id":2478,"title":"[Minor] Cert sync can never succeed once a node retains ~65 certificates — the 64 KiB response cap truncates the reply and the pull retries forever","area":"p2p-sync","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0436","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T20:42:02.629770+00:00","updated_at":"2026-11-20T00:06:57.199697+00:00","source_created_at":"2026-11-11T08:24:11+00:00","told_at":"2026-11-20T00:05:07+00:00","status_changed_at":"2026-11-20T00:04:40.065470+00:00","url":"https://bounty.xe.network/XE-13238","body":"# H5 — MINOR — Cert sync can never succeed once a node retains ≥65 certs\n\nTitle: [Minor] Cert sync can never succeed once a node retains ~65 certificates — the 64 KiB response cap truncates the reply and the pull retries forever\n\n---\n\n## Summary\n\nA cert-sync response is capped at 256 certs and at 64 KiB on the wire. A single certificate is ~1,012 bytes of JSON, so ~65 of them already fill the 64 KiB cap — but a node retains up to 256 certs per provider. Once a node holds ~65 certs, a full cert sync can never complete: the response is truncated mid-JSON, decoding fails, zero certs are ingested (all-or-nothing unmarshal), and the pull retries 3 times per invocation and re-invokes on every connect plus every 2-minute sweep, forever. A fresh node can never bootstrap certificates from an established provider. I measured the exact threshold with a program that builds realistic certificates and replays the wire behaviour: 64 certs fit (64,867 bytes, 669 to spare), 65 certs exceed 64 KiB (~65,880 bytes) and fail to decode.\n\nSuggested tier: **Minor** (650 XE).\n\nMy XE address for payout: `d0a60d324b5f1d72fa06e79b92f0253241b85438a10c0ccac76c5f9b8135d8d4`\n\n## How I found it\n\nI was reading the cert-sync path to understand how a new node bootstraps provider certificates. The request in `net/certsync.go` is `msg.Request(ctx, pid, CertRequestMsgType, nil)` — no filter, no \"what I have\" list. The responder ignores the payload and returns `provide()` = all retained certs, capped at 256 (`certPullMaxResponse`, `certsync.go:26`). `node/perf.go:406` `collectCertificates()` returns `n.certs().all()` (every retained cert) plus the node's own current cert. Then I saw the receiver side in `net/msg.go:172`: the response is decoded through `io.LimitReader(s, MsgMaxResponseSize)` where `MsgMaxResponseSize = 65536` (`net/msg.go:24`). A response over 64 KiB is truncated mid-JSON and `Decode` fails. Retention is 256 certs per provider (`node/certset.go:10`, `certRetentionPerProvider = 256`), and certs are valid 7 days and issued per benchmark run — so a provider will cross the threshold over time, and once it does, no peer can ever pull certs from it again.\n\nI did not want to assert the threshold from the constants alone, because the actual byte size depends on the real certificate shape. So I wrote a program that builds realistic `perf.Certificate` values (Provider/PubKey/Seed/WorkProof/CPUHash/attestation roots 64-hex each, Signature 128-hex, Hash 64-hex, `PoWPuzzles = 1024`, `WorkloadVersion = 4`, `CertificateValidity = 7d`) and measures the marshalled response size plus the LimitReader truncation behaviour. One cert is 1,012 bytes. The byte table from two identical runs:\n\n- n=1: 1,048 B — fits. n=10: 10,165 B — fits. n=30: 30,425 B — fits.\n- n=60: 60,815 B — fits. n=61: 61,828 B — fits. n=62: 62,841 B — fits.\n- n=63: 63,854 B — fits (after LimitReader: 63,854 B, decode err=nil).\n- n=64: 64,867 B — fits.\n- n=100: 101,335 B — EXCEEDS 64 KiB → truncated → Decode fails.\n- n=256: 259,363 B — EXCEEDS 64 KiB → truncated → Decode fails.\n\nThreshold: **≥65 certs** (n=64 fits with 669 bytes to spare; n=65 ≈ 65,880 B exceeds). My Phase 1 estimate was ~62 (assuming 1,041 B/cert); the measured cert is 1,012 B, so the threshold is ~62–65 depending on real cert size. I am stating the range, not a single magic number.\n\n## Steps to reproduce\n\nRun the proof program (`proof-h5/main.go`, 100 lines) with `go run .`, twice. It builds realistic certificates, marshals the cert-sync response for n = 1, 10, 30, 60–64, 100, 256, and reports which sizes fit inside `MsgMaxResponseSize` (65,536) and which exceed it. It also runs the requester's `io.LimitReader(65536)` + `json.Decode` path on a 63-cert response to show the boundary: 63 certs (63,854 B) decode fine, while 100 and 256 certs exceed the cap and would be truncated mid-JSON. Both runs produced identical byte tables.\n\n```go\n// proof-h5: measure the exact JSON size of a realistic perf.Certificate and\n// find the count at which a cert-sync response exceeds MsgMaxResponseSize\n// (65536 bytes), proving the wire-level truncation threshold.\npackage main\n\nimport (\n\t\"encoding/hex\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/xeprotocol/xe/net\"\n\t\"github.com/xeprotocol/xe/perf\"\n)\n\nfunc hexStr(n int) string {\n\tb := make([]byte, n)\n\tfor i := range b {\n\t\tb[i] = byte(0xab)\n\t}\n\treturn hex.EncodeToString(b)\n}\n\nfunc main() {\n\t// Realistic cert: mirror node/perf.go certificate construction.\n\tc := &perf.Certificate{\n\t\tProvider:             hexStr(32),\n\t\tProviderPubKey:       hexStr(32),\n\t\tSeed:                 hexStr(32),\n\t\tWorkloadVersion:      perf.WorkloadVersion,\n\t\tPuzzles:              perf.PoWPuzzles,\n\t\tWorkProof:            hexStr(32),\n\t\tCPUHash:              hexStr(32),\n\t\tScore:                1.234567,\n\t\tStartAttestationRoot: hexStr(32),\n\t\tEndAttestationRoot:   hexStr(32),\n\t\tStartMedian:          1794381719983147784,\n\t\tEndMedian:            1794381729983147784,\n\t\tIssuedAt:             1794381719983147784,\n\t\tExpiresAt:            1794381719983147784 + int64(perf.CertificateValidity),\n\t\tPriceMultiplierMilli: 1000,\n\t\tSignature:            hexStr(64),\n\t\tHash:                 hexStr(32),\n\t}\n\tone, err := json.Marshal(c)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"one cert JSON: %d bytes\\n\", len(one))\n\tfmt.Printf(\"MsgMaxResponseSize: %d\\n\", net.MsgMaxResponseSize)\n\n\t// Full cert-sync response = MsgResponse envelope {\"type\":\"cert_request\",\"payload\":[...]}\n\ttype msgResp struct {\n\t\tType    string          `json:\"type\"`\n\t\tPayload json.RawMessage `json:\"payload\"`\n\t}\n\tfor _, n := range []int{1, 10, 30, 60, 61, 62, 63, 64, 100, 256} {\n\t\tcerts := make([]*perf.Certificate, n)\n\t\tfor i := range certs {\n\t\t\tcerts[i] = c\n\t\t}\n\t\tpayload, _ := json.Marshal(certs)\n\t\tresp, _ := json.Marshal(msgResp{Type: \"cert_request\", Payload: payload})\n\t\tstatus := \"OK (fits)\"\n\t\tif len(resp) > net.MsgMaxResponseSize {\n\t\t\tstatus = \"EXCEEDS 64KiB -> truncated by LimitReader -> Decode fails\"\n\t\t}\n\t\tfmt.Printf(\"n=%3d certs: response %6d bytes  %s\\n\", n, len(resp), status)\n\t}\n\n\t// Confirm the truncation actually breaks decoding: simulate the\n\t// requester's io.LimitReader(65536) + json.Decode on a 63-cert response.\n\tcerts := make([]*perf.Certificate, 63)\n\tfor i := range certs {\n\t\tcerts[i] = c\n\t}\n\tpayload, _ := json.Marshal(certs)\n\tfull, _ := json.Marshal(msgResp{Type: \"cert_request\", Payload: payload})\n\ttruncated := full\n\tif len(truncated) > net.MsgMaxResponseSize {\n\t\ttruncated = full[:net.MsgMaxResponseSize]\n\t}\n\tvar decoded msgResp\n\terr = json.Unmarshal(truncated, &decoded)\n\tfmt.Printf(\"\\n63-cert response: full=%d bytes, after 64KiB LimitReader=%d bytes, decode err=%v\\n\",\n\t\tlen(full), len(truncated), err)\n\tif err != nil {\n\t\tfmt.Println(\"CONFIRMED: a 63-cert response is truncated mid-JSON and fails to decode.\")\n\t}\n\t// And a 60-cert response decodes fine.\n\tcerts = make([]*perf.Certificate, 60)\n\tfor i := range certs {\n\t\tcerts[i] = c\n\t}\n\tpayload, _ = json.Marshal(certs)\n\tfull, _ = json.Marshal(msgResp{Type: \"cert_request\", Payload: payload})\n\terr = json.Unmarshal(full, &decoded)\n\tfmt.Printf(\"60-cert response: %d bytes, decode err=%v (fits, decodes fine)\\n\", len(full), err)\n\t_ = strings.Repeat\n}\n```\n\n## Impact\n\nOnce a provider holds ~65 certs, no peer can ever pull certificates from it again — the pull fails, zero certs are ingested, and it retries forever (3x per invocation, re-invoked on every connect and every 2-minute sweep). A fresh node can never bootstrap certs from an established provider. I am calling this Minor: it is a liveness/robustness issue in certificate sync, not a funds or consensus issue, and it only bites once a provider accumulates enough certificates — but retention is 256 per provider and certs are issued per benchmark run, so crossing the threshold is a matter of time, not a freak condition.\n\n## Root cause\n\nThe cert-sync request carries no filter and the responder returns everything it holds (`provide()` = all retained certs, cap 256), while the receiver decodes through a 64 KiB `LimitReader`. The two caps disagree by a factor of four: 256 certs at ~1,012 bytes each is ~259 KiB, but the wire only allows 64 KiB. The response is truncated mid-JSON, decoding fails, and the all-or-nothing unmarshal means zero certs are ingested even though most of the payload arrived intact.\n\n## Why this is not a duplicate\n\nI read all 189 issues (open and closed) before filing. Zero hits for `cert` + `65536`, `cert` + `sync` + `truncat`, `certsync`, or `MsgMaxResponseSize`. Distinct from #259 (gossip validator accepts long-expired certs — an expiry check, not a size cap) and #350 (PullVotes/PullBlocks pre-checks — a different sync path entirely).\n\n## Suggested fix\n\nEither paginate the cert-sync response (request with a cursor/offset and a per-page cap that fits comfortably inside 64 KiB, e.g. 32 certs per page), or have the requester send the set of cert hashes it already holds so the responder only returns the delta. Either way, the response must be bounded below `MsgMaxResponseSize` by construction, not by hoping retention stays low.\n\n## What I deliberately did not test\n\nI did not run a live two-node proof (generating 65+ real certificates via perf benchmarks is impractical on my VM). The mechanism is proven by code (nil request, all-certs response, 64 KiB LimitReader, all-or-nothing decode, 3x retry + 2-minute sweep) plus the exact measured byte threshold. Real-world cert sizes vary slightly, so the threshold is ~62–65, not a single magic number — I am stating the range. I also did not test whether other sync responses hit the same 64 KiB cap; that is a separate audit.\n","patch_refs":[],"thread":[{"id":12829,"source":"github","source_ref":"7025803186","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0436, 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-13238 and says what differs; it is read like any other.","created_at":"2026-11-20T00:05:07+00:00"}]}