headercache_test.go raw
1 package headercache
2
3 import (
4 "testing"
5
6 "git.smesh.lol/nostrino/pkg/btcwire"
7 "git.smesh.lol/nostrino/pkg/headerevents"
8 "git.smesh.lol/nostrino/pkg/nostr"
9 "git.smesh.lol/nostrino/pkg/relay"
10 )
11
12 // headerAt builds one header for the test, distinguished by its merkle root and time.
13 func headerAt(prev [32]byte, tag byte) btcwire.BlockHeader {
14 header := btcwire.BlockHeader{
15 Version: 1,
16 Timestamp: 1_700_000_000 + uint32(tag),
17 Bits: 0x1d00ffff,
18 }
19 copy(header.PrevBlock[:], prev[:])
20 header.MerkleRoot[0] = tag
21
22 return header
23 }
24
25 // eventFor encodes and signs one header event, which is what a relay stores.
26 func eventFor(t *testing.T, height uint32, header *btcwire.BlockHeader) *nostr.Event {
27 t.Helper()
28
29 event, err := headerevents.Encode("btc", height, header)
30
31 if err != nil {
32 t.Fatal(err)
33 }
34
35 if err := event.Sign([32]byte{1, 2, 3}); err != nil {
36 t.Fatal(err)
37 }
38
39 return &event
40 }
41
42 func newTestCache(t *testing.T) *Cache {
43 t.Helper()
44
45 cache, err := New(Config{
46 Chain: "btc",
47 Relays: []string{"wss://relay.example"},
48 Dial: func(string) (relay.Transport, error) { return nil, nil },
49 })
50
51 if err != nil {
52 t.Fatal(err)
53 }
54
55 return cache
56 }
57
58 // TestTheRunIsContiguousFromTheFirstHeight: neutrino appends the answer to its own chain, so a
59 // run must start exactly at the height it asked for and have no holes.
60 func TestTheRunIsContiguousFromTheFirstHeight(t *testing.T) {
61 cache := newTestCache(t)
62
63 prev := [32]byte{0: 9}
64 h1 := headerAt(prev, 1)
65 h2 := headerAt(h1.BlockHash(), 2)
66 h3 := headerAt(h2.BlockHash(), 3)
67
68 events := []*nostr.Event{
69 eventFor(t, 10, &h1),
70 eventFor(t, 11, &h2),
71 eventFor(t, 12, &h3),
72 }
73
74 run, err := cache.run(events, 10, 12)
75
76 if err != nil {
77 t.Fatal(err)
78 }
79
80 if len(run) != 3 {
81 t.Fatalf("run of %d headers, want 3", len(run))
82 }
83
84 if run[0].BlockHash() != h1.BlockHash() || run[2].BlockHash() != h3.BlockHash() {
85 t.Fatalf("the run is not in height order")
86 }
87 }
88
89 // TestAHoleStopsTheRun: a height the relay does not hold ends the run there, because the
90 // headers above it cannot be appended by a client that chains them.
91 func TestAHoleStopsTheRun(t *testing.T) {
92 cache := newTestCache(t)
93
94 prev := [32]byte{0: 9}
95 h1 := headerAt(prev, 1)
96 h3 := headerAt([32]byte{0: 0x33}, 3)
97
98 events := []*nostr.Event{
99 eventFor(t, 10, &h1),
100 eventFor(t, 12, &h3),
101 }
102
103 run, err := cache.run(events, 10, 12)
104
105 if err != nil {
106 t.Fatal(err)
107 }
108
109 if len(run) != 1 {
110 t.Fatalf("run of %d headers, want the one before the hole", len(run))
111 }
112
113 if run[0].BlockHash() != h1.BlockHash() {
114 t.Fatalf("the run kept the wrong header")
115 }
116 }
117
118 // TestAHeaderOutsideTheWindowIsDropped: a relay that answers loosely must not cost the client
119 // anything, so events outside the range are ignored.
120 func TestAHeaderOutsideTheWindowIsDropped(t *testing.T) {
121 cache := newTestCache(t)
122
123 inside := headerAt([32]byte{0: 9}, 1)
124 outside := headerAt([32]byte{0: 8}, 2)
125
126 events := []*nostr.Event{
127 eventFor(t, 4, &outside),
128 eventFor(t, 10, &inside),
129 }
130
131 run, err := cache.run(events, 10, 12)
132
133 if err != nil {
134 t.Fatal(err)
135 }
136
137 if len(run) != 1 || run[0].BlockHash() != inside.BlockHash() {
138 t.Fatalf("run = %d headers, want only the one inside the window", len(run))
139 }
140 }
141
142 // TestAnotherChainsHeaderIsDropped: the chain tag is the whole reason a relay can serve more
143 // than one chain, so a header tagged with another chain is not this chain's.
144 func TestAnotherChainsHeaderIsDropped(t *testing.T) {
145 cache := newTestCache(t)
146
147 header := headerAt([32]byte{0: 9}, 1)
148
149 event, err := headerevents.Encode("blake", 10, &header)
150
151 if err != nil {
152 t.Fatal(err)
153 }
154
155 if err := event.Sign([32]byte{1, 2, 3}); err != nil {
156 t.Fatal(err)
157 }
158
159 run, err := cache.run([]*nostr.Event{&event}, 10, 12)
160
161 if err != nil {
162 t.Fatal(err)
163 }
164
165 if len(run) != 0 {
166 t.Fatalf("a header from another chain was accepted")
167 }
168 }
169
170 // TestTwoHeadersAtOneHeightKeepTheFirst: a reorg publishes a competing header at a height. The
171 // cache is not the arbiter, so it returns one and lets the chain decide whether it connects.
172 func TestTwoHeadersAtOneHeightKeepTheFirst(t *testing.T) {
173 cache := newTestCache(t)
174
175 first := headerAt([32]byte{0: 9}, 1)
176 second := headerAt([32]byte{0: 9}, 2)
177
178 run, err := cache.run([]*nostr.Event{
179 eventFor(t, 10, &first),
180 eventFor(t, 10, &second),
181 }, 10, 10)
182
183 if err != nil {
184 t.Fatal(err)
185 }
186
187 if len(run) != 1 {
188 t.Fatalf("run of %d headers at one height, want 1", len(run))
189 }
190
191 if run[0].BlockHash() != first.BlockHash() {
192 t.Fatalf("the run kept the later header")
193 }
194 }
195