package gate import ( "fmt" "net/http" "os" "path/filepath" "strings" "testing" ) // TestStagingIsNotServed: a fill writes bodies to the staging log and nothing // else. Until a processing pass has checked them, the instance serves no block: // the unverified bytes are not reachable from any serving path. func TestStagingIsNotServed(t *testing.T) { r := nodeOn(t) r.createWallet(t) r.mine(t, 20) tip := r.height(t) bin := t.TempDir() buildBinary(t, bin, "btcd-lited") data := t.TempDir() port := freePort(t) base := "http://127.0.0.1:" + itoa(port) cmd := startProcess(t, filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest", "-process-after-fill=false", "-data", data, "-listen", "127.0.0.1:"+itoa(port)) t.Cleanup(func() { _ = cmd.Process.Kill() _ = cmd.Wait() }) waitForHTTP(t, base+"/v1/status") var status struct { Blocks int `json:"blocks"` } get(t, base+"/v1/status", &status) if status.Blocks != 0 { t.Fatalf("the instance serves %d blocks with no processing pass", status.Blocks) } // The staging log holds the whole fill. claims := filepath.Join(data, "blocks", "staging", "btc", "claims.dat") fi, err := os.Stat(claims) if err != nil { t.Fatalf("no staging claims after the fill: %v", err) } if got, want := fi.Size(), int64((tip+1)*44); got != want { t.Fatalf("claims.dat is %d bytes for %d heights", got, tip+1) } resp, err := http.Get(base + "/v1/chains/btc/blk/0-2") if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { t.Fatal("a block range was answered from unprocessed bodies") } t.Logf("staged %d bodies and served none: %s", tip+1, resp.Status) } // TestProcessIsSeparate: the download and the processing are two runs. The // second one needs no peer, and a body whose claim it cannot verify is dropped // and written down. func TestProcessIsSeparate(t *testing.T) { r := nodeOn(t) r.createWallet(t) r.mine(t, 20) tip := r.height(t) bin := t.TempDir() buildBinary(t, bin, "btcd-lited") data := t.TempDir() port := freePort(t) cmd := startProcess(t, filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest", "-process-after-fill=false", "-data", data, "-listen", "127.0.0.1:"+itoa(port)) waitForHTTP(t, "http://127.0.0.1:"+itoa(port)+"/v1/status") _ = cmd.Process.Kill() _ = cmd.Wait() // Corrupt one staged body: the first record's bytes, after its length // prefix. Its claim still names the hash the downloader asked for, so the // pass must refuse it. seg := filepath.Join(data, "blocks", "staging", "btc", "00000000.dat") raw, err := os.ReadFile(seg) if err != nil { t.Fatalf("no staged segment: %v", err) } if len(raw) < 8 { t.Fatalf("the staged segment is %d bytes", len(raw)) } // Inside the 80-byte header, which is what the hash commits to. raw[4+10] ^= 0xff if err := os.WriteFile(seg, raw, 0o644); err != nil { t.Fatal(err) } port2 := freePort(t) base2 := "http://127.0.0.1:" + itoa(port2) cmd2 := startProcess(t, filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-regtest", "-process-only", "-data", data, "-listen", "127.0.0.1:"+itoa(port2)) t.Cleanup(func() { _ = cmd2.Process.Kill() _ = cmd2.Wait() }) waitForBlocks(t, cmd2, "", base2, tip) var status struct { Blocks int `json:"blocks"` } get(t, base2+"/v1/status", &status) if status.Blocks != tip { t.Fatalf("the pass stored %d blocks, one body was corrupt and %d are good", status.Blocks, tip) } // The staging log is drained, and the failure is written down. if _, err := os.Stat(filepath.Join(data, "blocks", "staging", "btc", "claims.dat")); !os.IsNotExist(err) { t.Fatalf("the staging claims survived the pass: %v", err) } missing, err := os.ReadFile(filepath.Join(data, "blocks", "missing", "btc.log")) if err != nil { t.Fatalf("the refused height was not recorded: %v", err) } if !strings.Contains(string(missing), "btc 0 ") { t.Fatalf("the record does not name height 0: %q", missing) } // The stored blocks are the node's, byte for byte, for every height the // pass kept. name := rangeNames(t, base2, "btc", 1, uint32(tip+1)) if len(name) != 1 { t.Fatalf("%d chunks for heights 1..%d", len(name), tip+1) } got := splitRecords(t, chunkAt(t, base2, name[0])) if len(got) != tip { t.Fatalf("%d blocks served, %d expected", len(got), tip) } for i, h := 1, 0; i <= tip; i, h = i+1, h+1 { hash := r.must(t, "getblockhash", itoa(i)) want, err := r.cli("getblock", hash, "0") if err != nil { t.Fatal(err) } if strings.TrimSpace(want) != fmt.Sprintf("%x", got[h]) { t.Fatalf("height %d is not the node's block", i) } } t.Logf("processed %d of %d bodies with no peer; the corrupt one is recorded in blocks/missing", tip, tip+1) }