package filler import ( "context" "encoding/binary" "fmt" "path/filepath" "strings" "sync" "testing" "time" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/neutrino" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/staging" "git.smesh.lol/btcd-lite/internal/store" "git.smesh.lol/btcd-lite/internal/v2peer" ) // TestAcceptStagesWithoutHashing: the pool writes what a peer sent to the // staging log under the claim it asked for. It does not hash the bytes and it // does not touch the body store: both are the processing pass's work, and the // refusals that follow from them are tested there. func TestAcceptStagesWithoutHashing(t *testing.T) { dir := t.TempDir() bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatal(err) } defer bodies.Close() log, err := staging.Open(filepath.Join(dir, "staging")) if err != nil { t.Fatal(err) } defer log.Close() p := &Pool{cfg: PoolConfig{Bodies: bodies, Staging: log}} var want chainhash.Hash want[0] = 1 raw := make([]byte, 96) binary.LittleEndian.PutUint32(raw[0:4], 1) for i := 80; i < len(raw); i++ { raw[i] = byte(i) } if err := p.accept(5, want, raw); err != nil { t.Fatalf("a body was refused at the download: %v", err) } if !log.Has(5) { t.Fatal("the body was not staged") } if bodies.Len() != 0 { t.Fatalf("the pool wrote %d blocks into the store", bodies.Len()) } // A body shorter than a header cannot be one, and is refused before it is // written. if err := p.accept(6, want, []byte{1, 2, 3}); err == nil { t.Fatal("a 3-byte body was staged") } } // TestNotFoundIsReported: a peer that does not have a block is an error, not an // empty block. func TestNotFoundIsReported(t *testing.T) { if v2peer.ErrNotFound == nil { t.Fatal("no not-found error") } } // TestNextHandsOutEachHeightOnce: four workers pulling at the same time never // get the same height twice and between them get every height, which is what // one request in flight per peer needs from the bookkeeping. func TestNextHandsOutEachHeightOnce(t *testing.T) { const heights = 500 dir := t.TempDir() headers, err := store.NewHeaderStore(filepath.Join(dir, "headers")) if err != nil { t.Fatal(err) } defer headers.Close() hdrs := make([]btcwire.BlockHeader, 0, heights) for i := 0; i < heights; i++ { hdrs = append(hdrs, btcwire.BlockHeader{Version: 1, Nonce: uint32(i), Timestamp: 1231006505 + uint32(i)}) } if err := headers.SetAnchor(neutrino.Anchor{Height: 0, BlockHash: hdrs[0].BlockHash(), FilterHeight: -1}); err != nil { t.Fatal(err) } if err := headers.PutHeaders(0, hdrs); err != nil { t.Fatal(err) } bodies, err := store.NewBlockStore(filepath.Join(dir, "blocks")) if err != nil { t.Fatal(err) } defer bodies.Close() staged, err := staging.Open(filepath.Join(dir, "staging")) if err != nil { t.Fatal(err) } defer staged.Close() p, err := NewPool(PoolConfig{ Profile: chains.Regtest().Profile, Headers: headers, Bodies: bodies, Staging: staged, Peers: []Peer{{Addr: "a"}, {Addr: "b"}, {Addr: "c"}, {Addr: "d"}}, }) if err != nil { t.Fatal(err) } p.target.Store(heights) var mu sync.Mutex seen := map[uint32]int{} var wg sync.WaitGroup for i := 0; i < 4; i++ { wg.Add(1) go func() { defer wg.Done() for { h, ok := p.next() if !ok { return } mu.Lock() seen[h]++ mu.Unlock() p.complete(h) } }() } wg.Wait() if len(seen) != heights { t.Fatalf("%d of %d heights were handed out", len(seen), heights) } for h, n := range seen { if n != 1 { t.Fatalf("height %d was handed out %d times", h, n) } } } // TestProgressLine: the line a fill writes on its timer carries the count, the // percentage, the rate and an estimate, and it appears even when nothing has // completed, which is the case that matters when a fill has stalled. func TestProgressLine(t *testing.T) { var mu sync.Mutex var lines []string p := &Pool{cfg: PoolConfig{ ProgressEvery: 5 * time.Millisecond, Logf: func(f string, a ...any) { mu.Lock() lines = append(lines, fmt.Sprintf(f, a...)) mu.Unlock() }, }} p.target.Store(1000) p.stored = map[uint32]bool{0: true, 1: true} p.claimed = map[uint32]bool{} p.reuse = map[string]*conn{} stop := make(chan struct{}) done := make(chan struct{}) go func() { p.progressLoop(context.Background(), stop); close(done) }() // Let one line go by with nothing completing. time.Sleep(30 * time.Millisecond) p.done.Store(20) time.Sleep(30 * time.Millisecond) close(stop) <-done mu.Lock() defer mu.Unlock() if len(lines) == 0 { t.Fatal("no progress line") } first := lines[0] if !strings.Contains(first, "2/1000") || !strings.Contains(first, "(0.2%)") { t.Fatalf("the first line does not carry the count and the percentage: %q", first) } if !strings.Contains(first, "eta never") { t.Fatalf("a stalled fill did not say never: %q", first) } found := false for _, l := range lines[1:] { if strings.Contains(l, "blocks/s") && !strings.Contains(l, "eta never") { found = true break } } if !found { t.Fatalf("no line carried a rate and an estimate:\n%s", strings.Join(lines, "\n")) } }