ftool_test.go raw
1 package gate
2
3 import (
4 "bytes"
5 "os/exec"
6 "path/filepath"
7 "testing"
8
9 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
10
11 "git.smesh.lol/btcd-lite/internal/store"
12 )
13
14 // TestFToolSync is the seeding command, run as an operator runs it: a dataset
15 // built from the node's bytes is served by btcd-lite-serve, and ftool sync
16 // fills a second instance from it over the network.
17 //
18 // It runs the two binaries, not their packages, so what it proves is the thing
19 // an operator does on a storage VPS.
20 func TestFToolSync(t *testing.T) {
21 if _, err := exec.LookPath("go"); err != nil {
22 t.Skip("go is not on PATH")
23 }
24 bin := t.TempDir()
25 for _, name := range []string{"ftool", "btcd-lite-serve"} {
26 cmd := exec.Command("go", "build", "-o", filepath.Join(bin, name), "./cmd/"+name)
27 cmd.Dir = "../.."
28 if out, err := cmd.CombinedOutput(); err != nil {
29 t.Fatalf("build %s: %v\n%s", name, err, out)
30 }
31 }
32
33 n := openNode(t)
34 tip, err := n.BlockCount()
35 if err != nil {
36 t.Fatal(err)
37 }
38 if tip < 20 {
39 t.Skipf("the node has %d blocks", tip)
40 }
41
42 // Instance A holds the node's blocks 0..20.
43 a := t.TempDir()
44 bodies, err := store.NewBlockStore(filepath.Join(a, "blocks"))
45 if err != nil {
46 t.Fatal(err)
47 }
48 want := map[int32][]byte{}
49 for h := uint32(0); h < 20; h++ {
50 hashHex, _ := n.BlockHash(h)
51 hash, err := chainhash.NewHashFromStr(hashHex)
52 if err != nil {
53 t.Fatal(err)
54 }
55 raw, err := n.BlockRaw(hashHex)
56 if err != nil {
57 t.Fatal(err)
58 }
59 if err := bodies.PutBlock(*hash, raw); err != nil {
60 t.Fatal(err)
61 }
62 want[int32(h)] = raw
63 }
64 if err := bodies.Close(); err != nil {
65 t.Fatal(err)
66 }
67 headers, err := store.NewHeaderStore(filepath.Join(a, "headers", "btc"))
68 if err != nil {
69 t.Fatal(err)
70 }
71 seedHeaderChain(t, headers, func(h uint32) []byte { return want[int32(h)] }, 19)
72 headers.Close()
73
74 // A's key, from the same command an operator runs.
75 keyFile := filepath.Join(t.TempDir(), "instance.key")
76 keygen := exec.Command(filepath.Join(bin, "ftool"), "keygen", "-out", keyFile)
77 if out, err := keygen.CombinedOutput(); err != nil {
78 t.Fatalf("ftool keygen: %v\n%s", err, out)
79 }
80
81 // Serve A.
82 port := freePort(t)
83 srv := exec.Command(filepath.Join(bin, "btcd-lite-serve"),
84 "-data", a, "-listen", "127.0.0.1:"+itoa(port), "-key", keyFile)
85 var log bytes.Buffer
86 srv.Stdout, srv.Stderr = &log, &log
87 if err := srv.Start(); err != nil {
88 t.Fatal(err)
89 }
90 defer func() {
91 _ = srv.Process.Kill()
92 _ = srv.Wait()
93 }()
94 waitHTTP(t, "http://127.0.0.1:"+itoa(port)+"/v1/status")
95
96 // B is filled by the command.
97 b := t.TempDir()
98 sync := exec.Command(filepath.Join(bin, "ftool"), "sync",
99 "-from", "http://127.0.0.1:"+itoa(port), "-chain", "btc",
100 "-from-height", "0", "-to-height", "20", "-data", b, "-key", keyFile)
101 if out, err := sync.CombinedOutput(); err != nil {
102 t.Fatalf("ftool sync: %v\n%s\nserver: %s", err, out, log.String())
103 }
104 got, err := store.NewBlockStore(filepath.Join(b, "blocks"))
105 if err != nil {
106 t.Fatal(err)
107 }
108 defer got.Close()
109 if got.Len() != 20 {
110 t.Fatalf("B holds %d blocks", got.Len())
111 }
112 for h := int32(0); h < 20; h++ {
113 hash := chainhash.DoubleHashH(want[h][:80])
114 block, err := got.GetBlock(hash)
115 if err != nil {
116 t.Fatalf("B block %d: %v", h, err)
117 }
118 if string(block) != string(want[h]) {
119 t.Fatalf("B block %d is not the node's", h)
120 }
121 }
122 }
123