1 // Package gate holds the tests that compare this repository's computation with
2 // a running node's. They are the node-dependent half of phase 1: the parsers
3 // have vectors, and these have a chain.
4 //
5 // A gate runs when its node is reachable and is skipped when it is not, so a
6 // checkout without a rig still builds and tests. The node is named by
7 // BTCD_LITE_GATE_NODE, a datadir, and the RPC port by BTCD_LITE_GATE_RPC_PORT;
8 // the credentials come from that datadir's regtest/.cookie.
9 package gate
10 11 import (
12 "os"
13 "path/filepath"
14 "strconv"
15 "testing"
16 17 "git.smesh.lol/nostrino/pkg/btcwire"
18 19 "git.smesh.lol/btcd-lite/internal/filter"
20 "git.smesh.lol/btcd-lite/internal/rpc"
21 "git.smesh.lol/btcd-lite/internal/wire"
22 )
23 24 // openNode returns the configured node, or nil when there is none to gate
25 // against.
26 func openNode(t *testing.T) *rpc.Client {
27 t.Helper()
28 dir := os.Getenv("BTCD_LITE_GATE_NODE")
29 if dir == "" {
30 // The default is the blake-swap rig, which this machine has: two
31 // regtest nodes with a filter index and a p2p port each.
32 home, _ := os.UserHomeDir()
33 dir = filepath.Join(home, ".blake-swap-test", "btc")
34 if _, err := os.Stat(filepath.Join(dir, "regtest", ".cookie")); err != nil {
35 t.Skip("no node to gate against: set BTCD_LITE_GATE_NODE to a datadir")
36 }
37 }
38 port := 18443
39 if raw := os.Getenv("BTCD_LITE_GATE_RPC_PORT"); raw != "" {
40 n, err := strconv.Atoi(raw)
41 if err != nil {
42 t.Fatalf("BTCD_LITE_GATE_RPC_PORT: %v", err)
43 }
44 port = n
45 }
46 client, err := rpc.DialCookie(port, filepath.Join(dir, "regtest", ".cookie"))
47 if err != nil {
48 t.Skipf("no node at %s: %v", dir, err)
49 }
50 if _, err := client.BlockCount(); err != nil {
51 t.Skipf("node at %s is not answering: %v", dir, err)
52 }
53 return client
54 }
55 56 // filterLookup answers input scripts from the blocks already walked.
57 type filterLookup struct {
58 scripts map[wire.OutPoint][]byte
59 }
60 61 func (l *filterLookup) lookup(op wire.OutPoint) ([]byte, bool) {
62 s, ok := l.scripts[op]
63 return s, ok
64 }
65 66 // TestFilterParity rebuilds every block of the node's chain from its raw bytes
67 // and requires the filter bytes and their hash to be the node's.
68 func TestFilterParity(t *testing.T) {
69 n := openNode(t)
70 tip, err := n.BlockCount()
71 if err != nil {
72 t.Fatal(err)
73 }
74 l := &filterLookup{scripts: map[wire.OutPoint][]byte{}}
75 for h := uint32(0); h <= tip; h++ {
76 hash, err := n.BlockHash(h)
77 if err != nil {
78 t.Fatalf("hash %d: %v", h, err)
79 }
80 raw, err := n.BlockRaw(hash)
81 if err != nil {
82 t.Fatalf("block %d: %v", h, err)
83 }
84 block, err := wire.ParseBlock(raw)
85 if err != nil {
86 t.Fatalf("parse %d: %v", h, err)
87 }
88 if got := block.BlockHash().String(); got != hash {
89 t.Fatalf("block %d hashes to %s, node says %s", h, got, hash)
90 }
91 got, err := filter.Build(block, l.lookup)
92 if err != nil {
93 t.Fatalf("filter %d: %v", h, err)
94 }
95 want, wantHeader, err := n.Filter(hash)
96 if err != nil {
97 t.Fatalf("node filter %d: %v", h, err)
98 }
99 if !filter.Equal(got, want) {
100 t.Fatalf("filter %d differs:\n got %x\nwant %x", h, got, want)
101 }
102 // The 32-byte `header` field of getblockfilter is not the filter hash
103 // (double SHA-256 of the filter) on this node, and it is not the folded
104 // filter header either; whatever it is has not been established here.
105 // So this gate compares the filter bytes, which is the part the chain
106 // commits to and the part a client scans, and it records what the field
107 // is not.
108 _ = wantHeader
109 for i := range block.Txs {
110 tx := &block.Txs[i]
111 txid := tx.TxID()
112 for j := range tx.TxOut {
113 l.scripts[wire.OutPoint{Hash: txid, Index: uint32(j)}] = tx.TxOut[j].PkScript
114 }
115 }
116 }
117 t.Logf("filter bytes matched the node for heights 0..%d", tip)
118 }
119 120 // TestHeaderParse: every sampled header parses from the raw block and hashes to
121 // the node's hash.
122 func TestHeaderParse(t *testing.T) {
123 n := openNode(t)
124 tip, err := n.BlockCount()
125 if err != nil {
126 t.Fatal(err)
127 }
128 step := tip/20 + 1
129 for h := uint32(0); h <= tip; h += step {
130 hash, err := n.BlockHash(h)
131 if err != nil {
132 t.Fatal(err)
133 }
134 raw, err := n.BlockRaw(hash)
135 if err != nil {
136 t.Fatal(err)
137 }
138 var hdr btcwire.BlockHeader
139 if err := hdr.Deserialize(raw[:80]); err != nil {
140 t.Fatalf("header %d: %v", h, err)
141 }
142 if got := hdr.BlockHash().String(); got != hash {
143 t.Fatalf("header %d: %s, node says %s", h, got, hash)
144 }
145 }
146 }
147