package gate import ( "net/http" "os" "os/exec" "path/filepath" "strconv" "syscall" "testing" "time" ) // TestDaemonFillsFromANode runs btcd-lited against a node it was started for // and asks it what the plan says an instance is: it fills its own data // directory over p2p and then serves it. // // It fails on btcd's v2transport, not on this project: the handshake does not // find the garbage terminator when the node writes it together with the packet // behind it, which is docs/upstream/v2transport-garbage-terminator.md. The test // stays here and stays failing, because the failure is the measurement. // // TestFillerFromPeer covers the same fill over the same transport and passes, // because a connection opened at a different moment lands on a socket read that // puts the terminator on its own. func TestDaemonFillsFromANode(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skip("go is not on PATH") } r := nodeOn(t) r.createWallet(t) r.mine(t, 20) tip, err := strconv.Atoi(r.must(t, "getblockcount")) if err != nil { t.Fatal(err) } bin := t.TempDir() build := exec.Command("go", "build", "-o", filepath.Join(bin, "btcd-lited"), "./cmd/btcd-lited") build.Dir = "../.." if out, err := build.CombinedOutput(); err != nil { t.Fatalf("build: %v\n%s", err, out) } data := t.TempDir() port := freePort(t) cmd := exec.Command(filepath.Join(bin, "btcd-lited"), "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest") // The log goes to a file, not a pipe: a pipe nobody is reading fills at // 64 KiB and stops the instance mid-fill, which is not the instance's bug. logPath := filepath.Join(data, "instance.log") logFile, err := os.Create(logPath) if err != nil { t.Fatal(err) } defer logFile.Close() cmd.Stdout, cmd.Stderr = logFile, logFile if err := cmd.Start(); err != nil { t.Fatal(err) } defer func() { _ = cmd.Process.Kill() _ = cmd.Wait() }() t.Cleanup(func() { if t.Failed() { logged, _ := os.ReadFile(logPath) t.Logf("instance log:\n%s", logged) } }) waitForBlocks(t, cmd, logPath, "http://127.0.0.1:"+itoa(port), tip+1) } // waitForBlocks waits until the instance answers and serves want blocks. func waitForBlocks(t *testing.T, cmd *exec.Cmd, logPath, base string, want int) { t.Helper() // The instance fills before it serves, so the first answer may take as long // as the fill. Its log says what it is doing; a process that has exited // says that instead of waiting out the deadline. deadline := time.Now().Add(3 * time.Minute) for { if logged, err := os.ReadFile(logPath); err == nil { t.Logf("instance so far:\n%s", logged) } resp, err := http.Get(base + "/v1/status") if err == nil { resp.Body.Close() t.Logf("the instance answers") break } if err := cmd.Process.Signal(syscall.Signal(0)); err != nil { t.Fatalf("the instance is gone before serving: %v", err) } if time.Now().After(deadline) { t.Fatalf("the instance never answered at %s", base) } time.Sleep(500 * time.Millisecond) } // The fill runs before the service starts, so a status that answers with // every height means the fill finished and the view is of the filled store. deadline = time.Now().Add(3 * time.Minute) for { var status struct { Blocks int `json:"blocks"` } get(t, base+"/v1/status", &status) if status.Blocks == want { t.Logf("the instance filled and serves %d blocks", status.Blocks) return } if time.Now().After(deadline) { t.Fatalf("the instance serves %d blocks, the node has %d", status.Blocks, want) } time.Sleep(time.Second) } }