Bounty
XE-12916 · Bounty

statechain sync server: handleSyncStream has no zero-progress exit — one request makes the handler encode empty pages until the stream deadline

Closed · duplicate —claimed Minor web filed 2026-11-14 14:17 UTC by xe:352fe8fc

Report

Summary

The statechain sync server's handleSyncStream advances idx by the number of blocks returned. If GetBlocks returns an empty page with no error for an index the handler believes is in range — which happens when the node's in-memory tip is ahead of its stored tail — the handler loops forever encoding empty HasMore: true pages: no zero-progress check, no page cap, no exit of its own. One unauthenticated sync request pins the handler (and churns the chain mutex) for the full 60-second stream deadline.

Reproduction

Local PoC with the real handler and real Chain.GetBlocks over a diverged store: a single stream produced 679,664 empty pages (21.7 MB encoded) in 500 ms, stopped only by the test's own cancellation.

Suggested fix

Treat a zero-length page as end-of-stream (encode a final HasMore: false page and return), and/or add a hard page cap per stream.

Thread

xe:352fe8fc2026-11-14 15:21 UTCweb

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.

File: statechain_server_loop_test.go — tests: TestPoCStatechainServerZeroProgressLoop. Verified PASS against xeprotocol/xe @ 89a542ea51d38375299aee54c6c99102e9bfa666 before filing (see report body).

// PoC for Lead 1: statechain/sync.go handleSyncStream has no zero-progress exit.
// In-package test (package statechain) — copy into statechain/ as
// zz_poc_test.go inside the clone, run, then delete from the clone.
//
// It drives the REAL handleSyncStream + REAL Chain.GetBlocks with a stub
// StateChainStore whose GetStateBlockRange returns (nil, nil) — a result
// both production stores produce for an empty range (see store_probe.go).
// The chain tip claims index 5 while the store holds nothing at/after 1:
// the tip/store divergence precondition.
package statechain

import (
	"bytes"
	"errors"
	"io"
	"sync/atomic"
	"testing"
	"time"

	"github.com/libp2p/go-libp2p/core/network"
	"github.com/libp2p/go-libp2p/core/protocol"
)

type emptyStore struct{}

func (emptyStore) PutStateBlock(*Block) error                    { return nil }
func (emptyStore) GetStateBlock(uint64) (*Block, error)          { return nil, nil }
func (emptyStore) GetStateTip() (*Block, error)                  { return nil, nil }
func (emptyStore) GetStateBlockRange(uint64, uint64) ([]*Block, error) {
	return nil, nil // empty range, no error — exactly like MemStore/BadgerStore
}
func (emptyStore) StateBlockCount() (uint64, error) { return 0, nil }

type fakeStream struct {
	r      io.Reader
	writes atomic.Int64
	bytes  atomic.Int64
	stop   atomic.Bool
}

func (f *fakeStream) Read(p []byte) (int, error)  { return f.r.Read(p) }
func (f *fakeStream) Write(p []byte) (int, error) {
	if f.stop.Load() {
		return 0, errors.New("poc: stream closed by test")
	}
	f.writes.Add(1)
	f.bytes.Add(int64(len(p)))
	return len(p), nil
}
func (f *fakeStream) Close() error              { return nil }
func (f *fakeStream) CloseRead() error          { return nil }
func (f *fakeStream) CloseWrite() error         { return nil }
func (f *fakeStream) Reset() error              { return nil }
func (f *fakeStream) ResetWithError(network.StreamErrorCode) error { return nil }
func (f *fakeStream) SetDeadline(time.Time) error      { return nil }
func (f *fakeStream) SetReadDeadline(time.Time) error  { return nil }
func (f *fakeStream) SetWriteDeadline(time.Time) error { return nil }
func (f *fakeStream) ID() string                       { return "poc" }
func (f *fakeStream) Protocol() protocol.ID            { return SyncProtocol }
func (f *fakeStream) SetProtocol(protocol.ID) error    { return nil }
func (f *fakeStream) Stat() network.Stats              { return network.Stats{} }
func (f *fakeStream) Conn() network.Conn               { return nil }
func (f *fakeStream) Scope() network.StreamScope       { return nil }

func TestPoCStatechainServerZeroProgressLoop(t *testing.T) {
	chain := &Chain{
		store:   emptyStore{},
		genesis: &Block{Index: 0},
		tip:     &Block{Index: 5}, // tip ahead of an empty store: divergence precondition
	}
	fs := &fakeStream{r: bytes.NewReader([]byte("{\"tip_index\":0}\n"))}
	done := make(chan struct{})
	go func() { defer close(done); handleSyncStream(fs, chain) }()
	time.Sleep(500 * time.Millisecond)
	w := fs.writes.Load()
	fs.stop.Store(true)
	select {
	case <-done:
	case <-time.After(5 * time.Second):
		t.Fatal("handler did not exit after stream failure")
	}
	t.Logf("empty syncResponse pages encoded in 500ms: %d (%d bytes)", w, fs.bytes.Load())
	if w < 1000 {
		t.Fatalf("expected runaway encoding of empty pages, got only %d writes", w)
	}
}

Joseph Denneteam2026-11-20 19:46 UTCdesk

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