1 package gate
2 3 import (
4 "net/http"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strconv"
9 "syscall"
10 "testing"
11 "time"
12 )
13 14 // TestDaemonFillsFromANode runs btcd-lited against a node it was started for
15 // and asks it what the plan says an instance is: it fills its own data
16 // directory over p2p and then serves it.
17 //
18 // It fails on btcd's v2transport, not on this project: the handshake does not
19 // find the garbage terminator when the node writes it together with the packet
20 // behind it, which is docs/upstream/v2transport-garbage-terminator.md. The test
21 // stays here and stays failing, because the failure is the measurement.
22 //
23 // TestFillerFromPeer covers the same fill over the same transport and passes,
24 // because a connection opened at a different moment lands on a socket read that
25 // puts the terminator on its own.
26 func TestDaemonFillsFromANode(t *testing.T) {
27 if _, err := exec.LookPath("go"); err != nil {
28 t.Skip("go is not on PATH")
29 }
30 r := nodeOn(t)
31 r.createWallet(t)
32 r.mine(t, 20)
33 tip, err := strconv.Atoi(r.must(t, "getblockcount"))
34 if err != nil {
35 t.Fatal(err)
36 }
37 bin := t.TempDir()
38 build := exec.Command("go", "build", "-o", filepath.Join(bin, "btcd-lited"), "./cmd/btcd-lited")
39 build.Dir = "../.."
40 if out, err := build.CombinedOutput(); err != nil {
41 t.Fatalf("build: %v\n%s", err, out)
42 }
43 data := t.TempDir()
44 port := freePort(t)
45 cmd := exec.Command(filepath.Join(bin, "btcd-lited"),
46 "-data", data, "-listen", "127.0.0.1:"+itoa(port),
47 "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest")
48 // The log goes to a file, not a pipe: a pipe nobody is reading fills at
49 // 64 KiB and stops the instance mid-fill, which is not the instance's bug.
50 logPath := filepath.Join(data, "instance.log")
51 logFile, err := os.Create(logPath)
52 if err != nil {
53 t.Fatal(err)
54 }
55 defer logFile.Close()
56 cmd.Stdout, cmd.Stderr = logFile, logFile
57 if err := cmd.Start(); err != nil {
58 t.Fatal(err)
59 }
60 defer func() {
61 _ = cmd.Process.Kill()
62 _ = cmd.Wait()
63 }()
64 t.Cleanup(func() {
65 if t.Failed() {
66 logged, _ := os.ReadFile(logPath)
67 t.Logf("instance log:\n%s", logged)
68 }
69 })
70 waitForBlocks(t, cmd, logPath, "http://127.0.0.1:"+itoa(port), tip+1)
71 }
72 73 // waitForBlocks waits until the instance answers and serves want blocks.
74 func waitForBlocks(t *testing.T, cmd *exec.Cmd, logPath, base string, want int) {
75 t.Helper()
76 // The instance fills before it serves, so the first answer may take as long
77 // as the fill. Its log says what it is doing; a process that has exited
78 // says that instead of waiting out the deadline.
79 deadline := time.Now().Add(3 * time.Minute)
80 for {
81 if logged, err := os.ReadFile(logPath); err == nil {
82 t.Logf("instance so far:\n%s", logged)
83 }
84 resp, err := http.Get(base + "/v1/status")
85 if err == nil {
86 resp.Body.Close()
87 t.Logf("the instance answers")
88 break
89 }
90 if err := cmd.Process.Signal(syscall.Signal(0)); err != nil {
91 t.Fatalf("the instance is gone before serving: %v", err)
92 }
93 if time.Now().After(deadline) {
94 t.Fatalf("the instance never answered at %s", base)
95 }
96 time.Sleep(500 * time.Millisecond)
97 }
98 // The fill runs before the service starts, so a status that answers with
99 // every height means the fill finished and the view is of the filled store.
100 deadline = time.Now().Add(3 * time.Minute)
101 for {
102 var status struct {
103 Blocks int `json:"blocks"`
104 }
105 get(t, base+"/v1/status", &status)
106 if status.Blocks == want {
107 t.Logf("the instance filled and serves %d blocks", status.Blocks)
108 return
109 }
110 if time.Now().After(deadline) {
111 t.Fatalf("the instance serves %d blocks, the node has %d", status.Blocks, want)
112 }
113 time.Sleep(time.Second)
114 }
115 }
116