daemon_test.go raw

   1  package gate
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/json"
   6  	"io"
   7  	"net/http"
   8  	"os/exec"
   9  	"path/filepath"
  10  	"testing"
  11  
  12  	"git.smesh.lol/nostrino/pkg/btcwire"
  13  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  14  	"git.smesh.lol/nostrino/pkg/neutrino"
  15  
  16  	"git.smesh.lol/btcd-lite/internal/chains"
  17  	"git.smesh.lol/btcd-lite/internal/store"
  18  )
  19  
  20  // TestDaemonServesAStore runs the instance binary over a data directory it is
  21  // given and asks it the questions an operator asks: what it holds, a range's
  22  // chunks, and a chunk that hashes to its name. It is the command's own
  23  // behaviour, not a package's.
  24  func TestDaemonServesAStore(t *testing.T) {
  25  	if _, err := exec.LookPath("go"); err != nil {
  26  		t.Skip("go is not on PATH")
  27  	}
  28  	bin := t.TempDir()
  29  	build := exec.Command("go", "build", "-o", filepath.Join(bin, "btcd-lited"), "./cmd/btcd-lited")
  30  	build.Dir = "../.."
  31  	if out, err := build.CombinedOutput(); err != nil {
  32  		t.Fatalf("build: %v\n%s", err, out)
  33  	}
  34  	data := t.TempDir()
  35  	// A range is heights, and a height is a header. The fixture is a chain:
  36  	// the regtest genesis and twenty headers after it, with each block's bytes
  37  	// being its header and filler.
  38  	genesis, err := chains.Regtest().Genesis()
  39  	if err != nil {
  40  		t.Fatal(err)
  41  	}
  42  	headers, err := store.NewHeaderStore(filepath.Join(data, "headers", "btc"))
  43  	if err != nil {
  44  		t.Fatal(err)
  45  	}
  46  	if err := headers.SetAnchor(neutrino.Anchor{Height: 0, BlockHash: genesis.BlockHash(), FilterHeight: -1}); err != nil {
  47  		t.Fatal(err)
  48  	}
  49  	hdrs := []btcwire.BlockHeader{genesis}
  50  	for i := 1; i < 20; i++ {
  51  		prev := hdrs[len(hdrs)-1]
  52  		hdrs = append(hdrs, btcwire.BlockHeader{
  53  			Version: 1, PrevBlock: prev.BlockHash(), Timestamp: 1231006505 + uint32(i), Nonce: uint32(i),
  54  		})
  55  	}
  56  	if err := headers.PutHeaders(0, hdrs); err != nil {
  57  		t.Fatal(err)
  58  	}
  59  	headers.Close()
  60  	bodies, err := store.NewBlockStore(filepath.Join(data, "blocks"))
  61  	if err != nil {
  62  		t.Fatal(err)
  63  	}
  64  	for _, h := range hdrs {
  65  		b := append(h.SerializeAll(), make([]byte, 16)...)
  66  		if err := bodies.PutBlock(h.BlockHash(), b); err != nil {
  67  			t.Fatal(err)
  68  		}
  69  	}
  70  	bodies.Close()
  71  
  72  	port := freePort(t)
  73  	cmd := exec.Command(filepath.Join(bin, "btcd-lited"), "-chain", "btc", "-regtest", "-data", data, "-listen", "127.0.0.1:"+itoa(port))
  74  	var log bytes.Buffer
  75  	cmd.Stdout, cmd.Stderr = &log, &log
  76  	if err := cmd.Start(); err != nil {
  77  		t.Fatal(err)
  78  	}
  79  	defer func() {
  80  		_ = cmd.Process.Kill()
  81  		_ = cmd.Wait()
  82  	}()
  83  	base := "http://127.0.0.1:" + itoa(port)
  84  	waitHTTP(t, base+"/v1/status")
  85  
  86  	var status struct {
  87  		Blocks int `json:"blocks"`
  88  	}
  89  	get(t, base+"/v1/status", &status)
  90  	if status.Blocks != 20 {
  91  		t.Fatalf("the daemon reports %d blocks, the store has 20\n%s", status.Blocks, log.String())
  92  	}
  93  	var listed struct {
  94  		Chunks []string `json:"chunks"`
  95  	}
  96  	get(t, base+"/v1/chains/btc/blk/0-20", &listed)
  97  	if len(listed.Chunks) != 1 {
  98  		t.Fatalf("%d chunks for 20 blocks", len(listed.Chunks))
  99  	}
 100  	resp, err := http.Get(base + "/v1/chunks/" + listed.Chunks[0])
 101  	if err != nil {
 102  		t.Fatal(err)
 103  	}
 104  	defer resp.Body.Close()
 105  	raw, _ := io.ReadAll(resp.Body)
 106  	if chainhash.DoubleHashH(raw).String() != listed.Chunks[0] {
 107  		t.Fatal("the chunk does not hash to its name")
 108  	}
 109  	// Nothing answers at the relay's path when no relay is configured.
 110  	r2, err := http.Post(base+"/v1/chains/btc/tx", "application/json", bytes.NewReader([]byte(`{"tx":"00"}`)))
 111  	if err != nil {
 112  		t.Fatal(err)
 113  	}
 114  	r2.Body.Close()
 115  	if r2.StatusCode != http.StatusNotFound {
 116  		t.Fatalf("the relay path answered %d on a daemon without a relay", r2.StatusCode)
 117  	}
 118  }
 119  
 120  func get(t *testing.T, u string, v any) {
 121  	t.Helper()
 122  	resp, err := http.Get(u)
 123  	if err != nil {
 124  		t.Fatal(err)
 125  	}
 126  	defer resp.Body.Close()
 127  	b, _ := io.ReadAll(resp.Body)
 128  	if resp.StatusCode != http.StatusOK {
 129  		t.Fatalf("%s: %d %s", u, resp.StatusCode, b)
 130  	}
 131  	if err := json.Unmarshal(b, v); err != nil {
 132  		t.Fatal(err)
 133  	}
 134  }
 135