1 package gate
2 3 import (
4 "encoding/binary"
5 "fmt"
6 "io"
7 "net/http"
8 "os/exec"
9 "path/filepath"
10 "testing"
11 )
12 13 // TestOneInstanceServesBothChains: one process, one data directory, two chains.
14 //
15 // The two rig nodes are on the same chain, so heights 0..20 are the same blocks
16 // on both and the instance must store them once. What the test asks of the
17 // service is the property that follows: a block chunk for a shared range is the
18 // same chunk whichever chain asked for it, and both chains answer.
19 func TestOneInstanceServesBothChains(t *testing.T) {
20 a := nodeOn(t)
21 a.createWallet(t)
22 a.mine(t, 20)
23 tip := a.height(t)
24 b := nodeConnectedTo(t, a)
25 26 bin := t.TempDir()
27 buildBinary(t, bin, "btcd-lited")
28 data := t.TempDir()
29 port := freePort(t)
30 logPath := filepath.Join(data, "instance.log")
31 cmd := startProcess(t, filepath.Join(bin, "btcd-lited"),
32 "-chains", "btc,xbt",
33 "-peer", fmt.Sprintf("btc=127.0.0.1:%d,xbt=127.0.0.1:%d", a.p2p, b.p2p),
34 "-data", data, "-listen", "127.0.0.1:"+itoa(port), "-regtest")
35 base := "http://127.0.0.1:" + itoa(port)
36 waitForBlocks(t, cmd, logPath, base, tip+1)
37 38 var status struct {
39 Chains map[string]struct {
40 Start uint32 `json:"start"`
41 Tip uint32 `json:"tip"`
42 } `json:"chains"`
43 Blocks int `json:"blocks"`
44 }
45 get(t, base+"/v1/status", &status)
46 for _, name := range []string{"btc", "xbt"} {
47 c, ok := status.Chains[name]
48 if !ok {
49 t.Fatalf("the status does not name %q: %+v", name, status.Chains)
50 }
51 if c.Tip != uint32(tip) {
52 t.Fatalf("%s tips at %d, the node at %d", name, c.Tip, tip)
53 }
54 }
55 if want := tip + 1; status.Blocks != want {
56 t.Fatalf("the instance holds %d blocks for two chains of %d heights", status.Blocks, want)
57 }
58 59 // The same range of both chains is the same chunk, cut from one copy.
60 btcNames := rangeNames(t, base, "btc", 0, 6)
61 xbtNames := rangeNames(t, base, "xbt", 0, 6)
62 if len(btcNames) != 1 || len(xbtNames) != 1 {
63 t.Fatalf("one chunk expected each, got %d and %d", len(btcNames), len(xbtNames))
64 }
65 if btcNames[0] != xbtNames[0] {
66 t.Fatalf("the shared range cuts to %s on btc and %s on xbt", btcNames[0], xbtNames[0])
67 }
68 btcBody := chunkAt(t, base, btcNames[0])
69 xbtBody := chunkAt(t, base, xbtNames[0])
70 if string(btcBody) != string(xbtBody) {
71 t.Fatal("the two chains served different bytes for the same chunk")
72 }
73 // Six blocks, each length-prefixed, and the first is genesis's.
74 if n := len(splitRecords(t, btcBody)); n != 6 {
75 t.Fatalf("the chunk holds %d blocks for heights 0..5", n)
76 }
77 // A chain the instance does not hold is refused, not silently answered.
78 resp, err := http.Get(base + "/v1/chains/ltc/blk/0-6")
79 if err != nil {
80 t.Fatal(err)
81 }
82 defer resp.Body.Close()
83 if resp.StatusCode == http.StatusOK {
84 t.Fatal("a chain the instance does not hold answered a range")
85 }
86 t.Logf("one instance served both chains from %d bodies; the shared range cut to one chunk", status.Blocks)
87 }
88 89 // buildBinary builds one command of this module into dir.
90 func buildBinary(t *testing.T, dir, name string) {
91 t.Helper()
92 cmd := exec.Command("go", "build", "-o", filepath.Join(dir, name), "./cmd/"+name)
93 cmd.Dir = "../.."
94 if out, err := cmd.CombinedOutput(); err != nil {
95 t.Fatalf("build %s: %v\n%s", name, err, out)
96 }
97 }
98 99 // rangeNames asks the service for the block chunk names of a range.
100 func rangeNames(t *testing.T, base, chainName string, from, to uint32) []string {
101 t.Helper()
102 return rangeNamesOf(t, base, chainName, "blk", from, to)
103 }
104 105 // rangeNamesOf asks for the chunk names of one kind over a range.
106 func rangeNamesOf(t *testing.T, base, chainName, kind string, from, to uint32) []string {
107 t.Helper()
108 var listed struct {
109 Chunks []string `json:"chunks"`
110 }
111 get(t, fmt.Sprintf("%s/v1/chains/%s/%s/%d-%d", base, chainName, kind, from, to), &listed)
112 return listed.Chunks
113 }
114 115 // chunkAt fetches a chunk's bytes.
116 func chunkAt(t *testing.T, base, name string) []byte {
117 t.Helper()
118 resp, err := http.Get(base + "/v1/chunks/" + name)
119 if err != nil {
120 t.Fatal(err)
121 }
122 defer resp.Body.Close()
123 if resp.StatusCode != http.StatusOK {
124 t.Fatalf("chunk %s: %d", name, resp.StatusCode)
125 }
126 body, err := io.ReadAll(resp.Body)
127 if err != nil {
128 t.Fatal(err)
129 }
130 return body
131 }
132 133 // splitRecords counts the length-prefixed records in a stream.
134 func splitRecords(t *testing.T, raw []byte) [][]byte {
135 t.Helper()
136 var out [][]byte
137 for at := 0; at < len(raw); {
138 n := int(binary.LittleEndian.Uint32(raw[at:]))
139 out = append(out, raw[at+4:at+4+n])
140 at += 4 + n
141 }
142 return out
143 }
144