headerevents_test.go raw
1 package headerevents
2
3 import (
4 "strings"
5 "testing"
6
7 "git.smesh.lol/nostrino/pkg/btcwire"
8 "git.smesh.lol/nostrino/pkg/crypto/chainhash"
9 "git.smesh.lol/nostrino/pkg/nostr"
10 )
11
12 // bitcoinHeader is one ordinary 80-byte header, built rather than copied so the test says
13 // what every field is.
14 func bitcoinHeader() btcwire.BlockHeader {
15 return btcwire.BlockHeader{
16 Version: 1,
17 PrevBlock: chainhash.Hash{0: 1},
18 MerkleRoot: chainhash.Hash{
19 0: 2, 1: 3, 2: 4, 3: 5,
20 },
21 Timestamp: 1_700_000_000,
22 Bits: 0x1d00ffff,
23 Nonce: 42,
24 }
25 }
26
27 // versionTwoHeader is a forked-chain header: the same 80 bytes plus the mining fields.
28 func versionTwoHeader() btcwire.BlockHeader {
29 header := bitcoinHeader()
30 flag := uint32(btcwire.HeaderV2Flag) | 1
31 header.Version = int32(flag)
32 header.V2 = &btcwire.HeaderV2{
33 Nonce2: 7,
34 Nonce3: 8,
35 Extranonce: [16]byte{0: 9},
36 TimeOffset: 11,
37 TxCount: 12,
38 Flags: 13,
39 XorKeyClear: 14,
40 Height: 1_700_000,
41 }
42
43 return header
44 }
45
46 // TestHeaderRoundTrip: an event carries a header and comes back byte-identical, with the
47 // height and the hash it names.
48 func TestHeaderRoundTrip(t *testing.T) {
49 header := bitcoinHeader()
50
51 event, err := Encode("btc", 1234, &header)
52
53 if err != nil {
54 t.Fatal(err)
55 }
56
57 if event.Kind != Kind {
58 t.Fatalf("kind = %d, want %d", event.Kind, Kind)
59 }
60
61 if event.CreatedAt != int64(header.Timestamp) {
62 t.Fatalf("created_at = %d, want the header's time %d", event.CreatedAt, header.Timestamp)
63 }
64
65 decoded, err := Decode(&event)
66
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 if decoded.Chain != "btc" || decoded.Height != 1234 {
72 t.Fatalf("decoded %q at %d", decoded.Chain, decoded.Height)
73 }
74
75 if decoded.Hash != header.BlockHash() {
76 t.Fatalf("hash = %s, want %s", decoded.Hash.String(), header.BlockHash().String())
77 }
78
79 if got := decoded.Raw.SerializeAll(); string(got) != string(header.SerializeAll()) {
80 t.Fatalf("header round trip changed the bytes")
81 }
82 }
83
84 // TestVersionTwoRoundTrip: the forked chain's header is 164 bytes and must survive the same
85 // path, because the header's size is decided by its own version flag.
86 func TestVersionTwoRoundTrip(t *testing.T) {
87 header := versionTwoHeader()
88
89 event, err := Encode("blake", 77, &header)
90
91 if err != nil {
92 t.Fatal(err)
93 }
94
95 decoded, err := Decode(&event)
96
97 if err != nil {
98 t.Fatal(err)
99 }
100
101 if decoded.Raw.V2 == nil {
102 t.Fatal("the version 2 fields were lost")
103 }
104
105 if decoded.Raw.V2.Nonce2 != 7 || decoded.Raw.V2.Height != 1_700_000 {
106 t.Fatalf("version 2 fields = %+v", *decoded.Raw.V2)
107 }
108
109 if decoded.Hash != header.BlockHash() {
110 t.Fatalf("hash = %s, want %s", decoded.Hash.String(), header.BlockHash().String())
111 }
112
113 // The event's time is the wire time, which for an offset header is the real time.
114 if event.CreatedAt != int64(header.TimeOnWire()) {
115 t.Fatalf("created_at = %d, want %d", event.CreatedAt, header.TimeOnWire())
116 }
117 }
118
119 // TestAHashThatDoesNotMatchIsRefused: the block hash tag is the name the header is checked
120 // against, so an event whose tag names another block is not that block.
121 func TestAHashThatDoesNotMatchIsRefused(t *testing.T) {
122 header := bitcoinHeader()
123
124 event, err := Encode("btc", 10, &header)
125
126 if err != nil {
127 t.Fatal(err)
128 }
129
130 for i := range event.Tags {
131 if event.Tags[i].Name() == BlockTag {
132 event.Tags[i] = nostr.Tag{BlockTag, strings.Repeat("11", 32)}
133 }
134 }
135
136 if _, err := Decode(&event); err == nil {
137 t.Fatal("an event naming another block hash decoded")
138 }
139 }
140
141 // TestAnotherKindIsRefused: a filter batch is not a header, and decoding one as a header has
142 // to say so rather than parse whatever is in it.
143 func TestAnotherKindIsRefused(t *testing.T) {
144 event := nostr.Event{Kind: 3050, Content: "AAAA"}
145
146 if _, err := Decode(&event); err == nil {
147 t.Fatal("a filter batch decoded as a header")
148 }
149 }
150
151 // TestAHeaderWithNoHeightIsRefused: every tag the decoder needs is required, because a header
152 // without a height cannot be placed in a chain.
153 func TestAHeaderWithNoHeightIsRefused(t *testing.T) {
154 header := bitcoinHeader()
155
156 event, err := Encode("btc", 10, &header)
157
158 if err != nil {
159 t.Fatal(err)
160 }
161
162 kept := event.Tags[:0]
163
164 for _, tag := range event.Tags {
165 if tag.Name() != HeightTag {
166 kept = append(kept, tag)
167 }
168 }
169
170 event.Tags = kept
171
172 if _, err := Decode(&event); err == nil {
173 t.Fatal("a header with no height decoded")
174 }
175 }
176
177 // TestQueryNamesEveryHeight: a relay matches tag values, so the filter asks for each height in
178 // the range, and the limit is the number of events that range can produce.
179 func TestQueryNamesEveryHeight(t *testing.T) {
180 query := Query("btc", 100, 104)
181
182 if query.Kinds[0] != Kind {
183 t.Fatalf("kinds = %v", query.Kinds)
184 }
185
186 heights := query.Tags[HeightTag]
187
188 if len(heights) != 5 || heights[0] != "100" || heights[4] != "104" {
189 t.Fatalf("heights = %v", heights)
190 }
191
192 if query.Limit != 5 {
193 t.Fatalf("limit = %d, want 5", query.Limit)
194 }
195
196 if chains := query.Tags[ChainTag]; len(chains) != 1 || chains[0] != "btc" {
197 t.Fatalf("chains = %v", chains)
198 }
199 }
200