{"id":"XE-12916","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":2401,"title":"statechain sync server: handleSyncStream has no zero-progress exit — one request makes the handler encode empty pages until the stream deadline","area":"p2p-sync","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T14:17:25.118937+00:00","updated_at":"2026-11-20T19:46:11.756705+00:00","source_created_at":null,"told_at":"2026-11-20T19:46:11.766836+00:00","status_changed_at":"2026-11-20T19:46:11.760603+00:00","url":"https://bounty.xe.network/XE-12916","body":"### Summary\nThe statechain sync server's `handleSyncStream` advances `idx` by the number of\nblocks returned. If `GetBlocks` returns an empty page with no error for an index\nthe handler believes is in range — which happens when the node's in-memory tip\nis ahead of its stored tail — the handler loops forever encoding empty\n`HasMore: true` pages: no zero-progress check, no page cap, no exit of its own.\nOne unauthenticated sync request pins the handler (and churns the chain mutex)\nfor the full 60-second stream deadline.\n\n### Reproduction\nLocal PoC with the real handler and real `Chain.GetBlocks` over a diverged\nstore: a single stream produced 679,664 empty pages (21.7 MB encoded) in\n500 ms, stopped only by the test's own cancellation.\n\n### Suggested fix\nTreat a zero-length page as end-of-stream (encode a final `HasMore: false`\npage and return), and/or add a hard page cap per stream.","patch_refs":[],"thread":[{"id":3092,"source":"web","source_ref":"reporter:2401:515a563fabe4c3d6","author":"xe:352fe8fc","team":false,"body":"PoC re-supplied as a message: the attachment on the original filing did not register on this ticket (it was sent under a wrong multipart field name), so the full PoC source is pasted here verbatim.\n\nFile: statechain_server_loop_test.go — tests: TestPoCStatechainServerZeroProgressLoop. Verified PASS against xeprotocol/xe @ 89a542ea51d38375299aee54c6c99102e9bfa666 before filing (see report body).\n\n```\n// PoC for Lead 1: statechain/sync.go handleSyncStream has no zero-progress exit.\n// In-package test (package statechain) — copy into statechain/ as\n// zz_poc_test.go inside the clone, run, then delete from the clone.\n//\n// It drives the REAL handleSyncStream + REAL Chain.GetBlocks with a stub\n// StateChainStore whose GetStateBlockRange returns (nil, nil) — a result\n// both production stores produce for an empty range (see store_probe.go).\n// The chain tip claims index 5 while the store holds nothing at/after 1:\n// the tip/store divergence precondition.\npackage statechain\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"io\"\n\t\"sync/atomic\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/libp2p/go-libp2p/core/network\"\n\t\"github.com/libp2p/go-libp2p/core/protocol\"\n)\n\ntype emptyStore struct{}\n\nfunc (emptyStore) PutStateBlock(*Block) error                    { return nil }\nfunc (emptyStore) GetStateBlock(uint64) (*Block, error)          { return nil, nil }\nfunc (emptyStore) GetStateTip() (*Block, error)                  { return nil, nil }\nfunc (emptyStore) GetStateBlockRange(uint64, uint64) ([]*Block, error) {\n\treturn nil, nil // empty range, no error — exactly like MemStore/BadgerStore\n}\nfunc (emptyStore) StateBlockCount() (uint64, error) { return 0, nil }\n\ntype fakeStream struct {\n\tr      io.Reader\n\twrites atomic.Int64\n\tbytes  atomic.Int64\n\tstop   atomic.Bool\n}\n\nfunc (f *fakeStream) Read(p []byte) (int, error)  { return f.r.Read(p) }\nfunc (f *fakeStream) Write(p []byte) (int, error) {\n\tif f.stop.Load() {\n\t\treturn 0, errors.New(\"poc: stream closed by test\")\n\t}\n\tf.writes.Add(1)\n\tf.bytes.Add(int64(len(p)))\n\treturn len(p), nil\n}\nfunc (f *fakeStream) Close() error              { return nil }\nfunc (f *fakeStream) CloseRead() error          { return nil }\nfunc (f *fakeStream) CloseWrite() error         { return nil }\nfunc (f *fakeStream) Reset() error              { return nil }\nfunc (f *fakeStream) ResetWithError(network.StreamErrorCode) error { return nil }\nfunc (f *fakeStream) SetDeadline(time.Time) error      { return nil }\nfunc (f *fakeStream) SetReadDeadline(time.Time) error  { return nil }\nfunc (f *fakeStream) SetWriteDeadline(time.Time) error { return nil }\nfunc (f *fakeStream) ID() string                       { return \"poc\" }\nfunc (f *fakeStream) Protocol() protocol.ID            { return SyncProtocol }\nfunc (f *fakeStream) SetProtocol(protocol.ID) error    { return nil }\nfunc (f *fakeStream) Stat() network.Stats              { return network.Stats{} }\nfunc (f *fakeStream) Conn() network.Conn               { return nil }\nfunc (f *fakeStream) Scope() network.StreamScope       { return nil }\n\nfunc TestPoCStatechainServerZeroProgressLoop(t *testing.T) {\n\tchain := &Chain{\n\t\tstore:   emptyStore{},\n\t\tgenesis: &Block{Index: 0},\n\t\ttip:     &Block{Index: 5}, // tip ahead of an empty store: divergence precondition\n\t}\n\tfs := &fakeStream{r: bytes.NewReader([]byte(\"{\\\"tip_index\\\":0}\\n\"))}\n\tdone := make(chan struct{})\n\tgo func() { defer close(done); handleSyncStream(fs, chain) }()\n\ttime.Sleep(500 * time.Millisecond)\n\tw := fs.writes.Load()\n\tfs.stop.Store(true)\n\tselect {\n\tcase <-done:\n\tcase <-time.After(5 * time.Second):\n\t\tt.Fatal(\"handler did not exit after stream failure\")\n\t}\n\tt.Logf(\"empty syncResponse pages encoded in 500ms: %d (%d bytes)\", w, fs.bytes.Load())\n\tif w < 1000 {\n\t\tt.Fatalf(\"expected runaway encoding of empty pages, got only %d writes\", w)\n\t}\n}\n\n```","created_at":"2026-11-14T15:21:13.171804+00:00"},{"id":15643,"source":"desk","source_ref":"reply:13534","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, 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-12916 and says what differs; it is read like any other.","created_at":"2026-11-20T19:46:11.756705+00:00"}]}