package gate import ( "context" "encoding/hex" "encoding/json" "net/http" "os" "os/exec" "path/filepath" "strconv" "testing" "time" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/fetch" utxostore "git.smesh.lol/btcd-lite/internal/utxo/store" ) // TestServedUTXOChainFetches is the plan's API path end to end: an instance // filled from a node, a checkpoint chain built over the blocks it stored, the // chain published, and a client fetching it over HTTP and reaching every // boundary with the name the source holds. func TestServedUTXOChainFetches(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, 300) 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() instPort := freePort(t) inst := startProcess(t, filepath.Join(bin, "btcd-lited"), "-data", data, "-listen", "127.0.0.1:"+itoa(instPort), "-chain", "btc", "-peer", "127.0.0.1:"+itoa(r.p2p), "-regtest") waitForBlocks(t, inst, filepath.Join(data, "instance.log"), "http://127.0.0.1:"+itoa(instPort), tip+1) t.Logf("the instance filled %d blocks", tip+1) // The serve command builds the checkpoint chain over that store and // publishes it beside the chunks: one command, one address. serveBin := filepath.Join(bin, "btcd-lite-serve") buildServe := exec.Command("go", "build", "-o", serveBin, "./cmd/btcd-lite-serve") buildServe.Dir = "../.." if out, err := buildServe.CombinedOutput(); err != nil { t.Fatalf("build serve: %v\n%s", err, out) } port := freePort(t) serveCmd := startProcess(t, serveBin, "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-regtest") base := "http://127.0.0.1:" + itoa(port) waitForHTTP(t, base+"/v1/utxo/btc/manifest") // The dataset's snapshots are the shared prefix's, because the whole // regtest chain is below the fork. utxoDir := filepath.Join(data, "utxo", "canonical") t.Cleanup(func() { _ = serveCmd }) var manifest json.RawMessage get(t, base+"/v1/utxo/btc/manifest", &manifest) t.Logf("manifest: %s", manifest) dst := t.TempDir() c := &fetch.Client{Base: base} n, err := c.UTXO(context.Background(), "btc", dst) if err != nil { t.Fatalf("client fetch: %v", err) } if n == 0 { t.Fatal("the client reached no checkpoints") } interval := chains.Regtest().Exported.Retarget for h := uint32(0); h <= uint32(tip); h += interval { got, gerr := utxostore.SetName(dst, h) if gerr != nil { src, _ := filepath.Glob(filepath.Join(utxoDir, "set-*.dat")) dstList, _ := filepath.Glob(filepath.Join(dst, "set-*.dat")) t.Fatalf("the client has no set at %d: %v (source %v, client %v)", h, gerr, src, dstList) } want, werr := utxostore.SetName(utxoDir, h) if werr != nil { t.Fatalf("the source has no set at %d: %v", h, werr) } if got != want { t.Fatalf("height %d: the client computes %s, the source holds %s", h, got, want) } } // The filters the same command built are served beside the sets, and the // filter a range returns for a height is the node's filter for that block. for h := uint32(0); h <= uint32(tip); h += interval { hash := r.must(t, "getblockhash", itoa(int(h))) var node struct { Filter string `json:"filter"` } raw, err := r.cli("getblockfilter", hash) if err != nil { t.Fatalf("getblockfilter %d: %v", h, err) } if err := json.Unmarshal([]byte(raw), &node); err != nil { t.Fatalf("height %d: %v", h, err) } want, err := hex.DecodeString(node.Filter) if err != nil { t.Fatal(err) } name := rangeNamesOf(t, base, "btc", "flt", h, h+1) if len(name) != 1 { t.Fatalf("height %d: %d filter chunks", h, len(name)) } served := chunkAt(t, base, name[0]) // The chunk is a length-prefixed filter. filterBytes := served[4:] if string(filterBytes) != string(want) { t.Fatalf("height %d: the served filter is not the node's", h) } } t.Logf("the client reached %d checkpoints from %d blocks, every boundary matching, and the filters are the node's", n, tip+1) } // startProcess runs a command and kills it when the test ends. func startProcess(t *testing.T, path string, args ...string) *exec.Cmd { t.Helper() cmd := exec.Command(path, args...) logFile, err := os.Create(filepath.Join(t.TempDir(), "log")) if err != nil { t.Fatal(err) } cmd.Stdout, cmd.Stderr = logFile, logFile if err := cmd.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = cmd.Process.Kill() _ = cmd.Wait() logFile.Close() }) return cmd } // startHTTP serves handler on addr until the test ends. func startHTTP(t *testing.T, addr string, handler http.Handler) { t.Helper() srv := &http.Server{Addr: addr, Handler: handler} ready := make(chan struct{}) var once bool _ = once go func() { close(ready) _ = srv.ListenAndServe() }() <-ready t.Cleanup(func() { _ = srv.Close() }) } // waitForHTTP waits until the address answers. func waitForHTTP(t *testing.T, url string) { t.Helper() deadline := time.Now().Add(30 * time.Second) for { resp, err := http.Get(url) if err == nil { resp.Body.Close() return } if time.Now().After(deadline) { t.Fatalf("%s never answered: %v", url, err) } time.Sleep(100 * time.Millisecond) } }