package gate import ( "bytes" "encoding/json" "io" "net/http" "os/exec" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/store" ) // TestDaemonServesAStore runs the instance binary over a data directory it is // given and asks it the questions an operator asks: what it holds, a range's // chunks, and a chunk that hashes to its name. It is the command's own // behaviour, not a package's. func TestDaemonServesAStore(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skip("go is not on PATH") } 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() // A range is heights, and a height is a header. The fixture is a chain: // the regtest genesis and twenty headers after it, with each block's bytes // being its header and filler. genesis, err := chains.Regtest().Genesis() if err != nil { t.Fatal(err) } headers, err := store.NewHeaderStore(filepath.Join(data, "headers", "btc")) if err != nil { t.Fatal(err) } if err := headers.SetAnchor(neutrino.Anchor{Height: 0, BlockHash: genesis.BlockHash(), FilterHeight: -1}); err != nil { t.Fatal(err) } hdrs := []btcwire.BlockHeader{genesis} for i := 1; i < 20; i++ { prev := hdrs[len(hdrs)-1] hdrs = append(hdrs, btcwire.BlockHeader{ Version: 1, PrevBlock: prev.BlockHash(), Timestamp: 1231006505 + uint32(i), Nonce: uint32(i), }) } if err := headers.PutHeaders(0, hdrs); err != nil { t.Fatal(err) } headers.Close() bodies, err := store.NewBlockStore(filepath.Join(data, "blocks")) if err != nil { t.Fatal(err) } for _, h := range hdrs { b := append(h.SerializeAll(), make([]byte, 16)...) if err := bodies.PutBlock(h.BlockHash(), b); err != nil { t.Fatal(err) } } bodies.Close() port := freePort(t) cmd := exec.Command(filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-regtest", "-data", data, "-listen", "127.0.0.1:"+itoa(port)) var log bytes.Buffer cmd.Stdout, cmd.Stderr = &log, &log if err := cmd.Start(); err != nil { t.Fatal(err) } defer func() { _ = cmd.Process.Kill() _ = cmd.Wait() }() base := "http://127.0.0.1:" + itoa(port) waitHTTP(t, base+"/v1/status") var status struct { Blocks int `json:"blocks"` } get(t, base+"/v1/status", &status) if status.Blocks != 20 { t.Fatalf("the daemon reports %d blocks, the store has 20\n%s", status.Blocks, log.String()) } var listed struct { Chunks []string `json:"chunks"` } get(t, base+"/v1/chains/btc/blk/0-20", &listed) if len(listed.Chunks) != 1 { t.Fatalf("%d chunks for 20 blocks", len(listed.Chunks)) } resp, err := http.Get(base + "/v1/chunks/" + listed.Chunks[0]) if err != nil { t.Fatal(err) } defer resp.Body.Close() raw, _ := io.ReadAll(resp.Body) if chainhash.DoubleHashH(raw).String() != listed.Chunks[0] { t.Fatal("the chunk does not hash to its name") } // Nothing answers at the relay's path when no relay is configured. r2, err := http.Post(base+"/v1/chains/btc/tx", "application/json", bytes.NewReader([]byte(`{"tx":"00"}`))) if err != nil { t.Fatal(err) } r2.Body.Close() if r2.StatusCode != http.StatusNotFound { t.Fatalf("the relay path answered %d on a daemon without a relay", r2.StatusCode) } } func get(t *testing.T, u string, v any) { t.Helper() resp, err := http.Get(u) if err != nil { t.Fatal(err) } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { t.Fatalf("%s: %d %s", u, resp.StatusCode, b) } if err := json.Unmarshal(b, v); err != nil { t.Fatal(err) } }