block_filterer_test.go raw
1 package chainclient_test
2
3 import (
4 "reflect"
5 "testing"
6 "time"
7
8 "github.com/p9c/p9/pkg/chainclient"
9 "github.com/p9c/p9/pkg/chainhash"
10 "github.com/p9c/p9/pkg/wire"
11 )
12
13 var Block100000 = wire.Block{
14 Header: wire.BlockHeader{
15 Version: 1,
16 PrevBlock: chainhash.Hash(
17 [32]byte{
18 // Make go vet happy.
19 0x50, 0x12, 0x01, 0x19, 0x17, 0x2a, 0x61, 0x04,
20 0x21, 0xa6, 0xc3, 0x01, 0x1d, 0xd3, 0x30, 0xd9,
21 0xdf, 0x07, 0xb6, 0x36, 0x16, 0xc2, 0xcc, 0x1f,
22 0x1c, 0xd0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
23 },
24 ), // 000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250
25 MerkleRoot: chainhash.Hash(
26 [32]byte{
27 // Make go vet happy.
28 0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0,
29 0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22,
30 0x28, 0xc3, 0x06, 0x7c, 0xc3, 0x8d, 0x48, 0x85,
31 0xef, 0xb5, 0xa4, 0xac, 0x42, 0x47, 0xe9, 0xf3,
32 },
33 ), // f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766
34 Timestamp: time.Unix(1293623863, 0), // 2010-12-29 11:57:43 +0000 UTC
35 Bits: 0x1b04864c, // 453281356
36 Nonce: 0x10572b0f, // 274148111
37 },
38 Transactions: []*wire.MsgTx{
39 {
40 Version: 1,
41 TxIn: []*wire.TxIn{
42 {
43 PreviousOutPoint: wire.OutPoint{
44 Hash: chainhash.Hash{},
45 Index: 0xffffffff,
46 },
47 SignatureScript: []byte{
48 0x04, 0x4c, 0x86, 0x04, 0x1b, 0x02, 0x06, 0x02,
49 },
50 Sequence: 0xffffffff,
51 },
52 },
53 TxOut: []*wire.TxOut{
54 {
55 Value: 0x12a05f200, // 5000000000
56 PkScript: []byte{
57 0x41, // OP_DATA_65
58 0x04, 0x1b, 0x0e, 0x8c, 0x25, 0x67, 0xc1, 0x25,
59 0x36, 0xaa, 0x13, 0x35, 0x7b, 0x79, 0xa0, 0x73,
60 0xdc, 0x44, 0x44, 0xac, 0xb8, 0x3c, 0x4e, 0xc7,
61 0xa0, 0xe2, 0xf9, 0x9d, 0xd7, 0x45, 0x75, 0x16,
62 0xc5, 0x81, 0x72, 0x42, 0xda, 0x79, 0x69, 0x24,
63 0xca, 0x4e, 0x99, 0x94, 0x7d, 0x08, 0x7f, 0xed,
64 0xf9, 0xce, 0x46, 0x7c, 0xb9, 0xf7, 0xc6, 0x28,
65 0x70, 0x78, 0xf8, 0x01, 0xdf, 0x27, 0x6f, 0xdf,
66 0x84, // 65-byte signature
67 0xac, // OP_CHECKSIG
68 },
69 },
70 },
71 LockTime: 0,
72 },
73 {
74 Version: 1,
75 TxIn: []*wire.TxIn{
76 {
77 PreviousOutPoint: wire.OutPoint{
78 Hash: chainhash.Hash(
79 [32]byte{
80 // Make go vet happy.
81 0x03, 0x2e, 0x38, 0xe9, 0xc0, 0xa8, 0x4c, 0x60,
82 0x46, 0xd6, 0x87, 0xd1, 0x05, 0x56, 0xdc, 0xac,
83 0xc4, 0x1d, 0x27, 0x5e, 0xc5, 0x5f, 0xc0, 0x07,
84 0x79, 0xac, 0x88, 0xfd, 0xf3, 0x57, 0xa1, 0x87,
85 },
86 ), // 87a157f3fd88ac7907c05fc55e271dc4acdc5605d187d646604ca8c0e9382e03
87 Index: 0,
88 },
89 SignatureScript: []byte{
90 0x49, // OP_DATA_73
91 0x30, 0x46, 0x02, 0x21, 0x00, 0xc3, 0x52, 0xd3,
92 0xdd, 0x99, 0x3a, 0x98, 0x1b, 0xeb, 0xa4, 0xa6,
93 0x3a, 0xd1, 0x5c, 0x20, 0x92, 0x75, 0xca, 0x94,
94 0x70, 0xab, 0xfc, 0xd5, 0x7d, 0xa9, 0x3b, 0x58,
95 0xe4, 0xeb, 0x5d, 0xce, 0x82, 0x02, 0x21, 0x00,
96 0x84, 0x07, 0x92, 0xbc, 0x1f, 0x45, 0x60, 0x62,
97 0x81, 0x9f, 0x15, 0xd3, 0x3e, 0xe7, 0x05, 0x5c,
98 0xf7, 0xb5, 0xee, 0x1a, 0xf1, 0xeb, 0xcc, 0x60,
99 0x28, 0xd9, 0xcd, 0xb1, 0xc3, 0xaf, 0x77, 0x48,
100 0x01, // 73-byte signature
101 0x41, // OP_DATA_65
102 0x04, 0xf4, 0x6d, 0xb5, 0xe9, 0xd6, 0x1a, 0x9d,
103 0xc2, 0x7b, 0x8d, 0x64, 0xad, 0x23, 0xe7, 0x38,
104 0x3a, 0x4e, 0x6c, 0xa1, 0x64, 0x59, 0x3c, 0x25,
105 0x27, 0xc0, 0x38, 0xc0, 0x85, 0x7e, 0xb6, 0x7e,
106 0xe8, 0xe8, 0x25, 0xdc, 0xa6, 0x50, 0x46, 0xb8,
107 0x2c, 0x93, 0x31, 0x58, 0x6c, 0x82, 0xe0, 0xfd,
108 0x1f, 0x63, 0x3f, 0x25, 0xf8, 0x7c, 0x16, 0x1b,
109 0xc6, 0xf8, 0xa6, 0x30, 0x12, 0x1d, 0xf2, 0xb3,
110 0xd3, // 65-byte pubkey
111 },
112 Sequence: 0xffffffff,
113 },
114 },
115 TxOut: []*wire.TxOut{
116 {
117 Value: 0x2123e300, // 556000000
118 PkScript: []byte{
119 0x76, // OP_DUP
120 0xa9, // OP_HASH160
121 0x14, // OP_DATA_20
122 0xc3, 0x98, 0xef, 0xa9, 0xc3, 0x92, 0xba, 0x60,
123 0x13, 0xc5, 0xe0, 0x4e, 0xe7, 0x29, 0x75, 0x5e,
124 0xf7, 0xf5, 0x8b, 0x32,
125 0x88, // OP_EQUALVERIFY
126 0xac, // OP_CHECKSIG
127 },
128 },
129 {
130 Value: 0x108e20f00, // 4444000000
131 PkScript: []byte{
132 0x76, // OP_DUP
133 0xa9, // OP_HASH160
134 0x14, // OP_DATA_20
135 0x94, 0x8c, 0x76, 0x5a, 0x69, 0x14, 0xd4, 0x3f,
136 0x2a, 0x7a, 0xc1, 0x77, 0xda, 0x2c, 0x2f, 0x6b,
137 0x52, 0xde, 0x3d, 0x7c,
138 0x88, // OP_EQUALVERIFY
139 0xac, // OP_CHECKSIG
140 },
141 },
142 },
143 LockTime: 0,
144 },
145 {
146 Version: 1,
147 TxIn: []*wire.TxIn{
148 {
149 PreviousOutPoint: wire.OutPoint{
150 Hash: chainhash.Hash(
151 [32]byte{
152 // Make go vet happy.
153 0xc3, 0x3e, 0xbf, 0xf2, 0xa7, 0x09, 0xf1, 0x3d,
154 0x9f, 0x9a, 0x75, 0x69, 0xab, 0x16, 0xa3, 0x27,
155 0x86, 0xaf, 0x7d, 0x7e, 0x2d, 0xe0, 0x92, 0x65,
156 0xe4, 0x1c, 0x61, 0xd0, 0x78, 0x29, 0x4e, 0xcf,
157 },
158 ), // cf4e2978d0611ce46592e02d7e7daf8627a316ab69759a9f3df109a7f2bf3ec3
159 Index: 1,
160 },
161 SignatureScript: []byte{
162 0x47, // OP_DATA_71
163 0x30, 0x44, 0x02, 0x20, 0x03, 0x2d, 0x30, 0xdf,
164 0x5e, 0xe6, 0xf5, 0x7f, 0xa4, 0x6c, 0xdd, 0xb5,
165 0xeb, 0x8d, 0x0d, 0x9f, 0xe8, 0xde, 0x6b, 0x34,
166 0x2d, 0x27, 0x94, 0x2a, 0xe9, 0x0a, 0x32, 0x31,
167 0xe0, 0xba, 0x33, 0x3e, 0x02, 0x20, 0x3d, 0xee,
168 0xe8, 0x06, 0x0f, 0xdc, 0x70, 0x23, 0x0a, 0x7f,
169 0x5b, 0x4a, 0xd7, 0xd7, 0xbc, 0x3e, 0x62, 0x8c,
170 0xbe, 0x21, 0x9a, 0x88, 0x6b, 0x84, 0x26, 0x9e,
171 0xae, 0xb8, 0x1e, 0x26, 0xb4, 0xfe, 0x01,
172 0x41, // OP_DATA_65
173 0x04, 0xae, 0x31, 0xc3, 0x1b, 0xf9, 0x12, 0x78,
174 0xd9, 0x9b, 0x83, 0x77, 0xa3, 0x5b, 0xbc, 0xe5,
175 0xb2, 0x7d, 0x9f, 0xff, 0x15, 0x45, 0x68, 0x39,
176 0xe9, 0x19, 0x45, 0x3f, 0xc7, 0xb3, 0xf7, 0x21,
177 0xf0, 0xba, 0x40, 0x3f, 0xf9, 0x6c, 0x9d, 0xee,
178 0xb6, 0x80, 0xe5, 0xfd, 0x34, 0x1c, 0x0f, 0xc3,
179 0xa7, 0xb9, 0x0d, 0xa4, 0x63, 0x1e, 0xe3, 0x95,
180 0x60, 0x63, 0x9d, 0xb4, 0x62, 0xe9, 0xcb, 0x85,
181 0x0f, // 65-byte pubkey
182 },
183 Sequence: 0xffffffff,
184 },
185 },
186 TxOut: []*wire.TxOut{
187 {
188 Value: 0xf4240, // 1000000
189 PkScript: []byte{
190 0x76, // OP_DUP
191 0xa9, // OP_HASH160
192 0x14, // OP_DATA_20
193 0xb0, 0xdc, 0xbf, 0x97, 0xea, 0xbf, 0x44, 0x04,
194 0xe3, 0x1d, 0x95, 0x24, 0x77, 0xce, 0x82, 0x2d,
195 0xad, 0xbe, 0x7e, 0x10,
196 0x88, // OP_EQUALVERIFY
197 0xac, // OP_CHECKSIG
198 },
199 },
200 {
201 Value: 0x11d260c0, // 299000000
202 PkScript: []byte{
203 0x76, // OP_DUP
204 0xa9, // OP_HASH160
205 0x14, // OP_DATA_20
206 0x6b, 0x12, 0x81, 0xee, 0xc2, 0x5a, 0xb4, 0xe1,
207 0xe0, 0x79, 0x3f, 0xf4, 0xe0, 0x8a, 0xb1, 0xab,
208 0xb3, 0x40, 0x9c, 0xd9,
209 0x88, // OP_EQUALVERIFY
210 0xac, // OP_CHECKSIG
211 },
212 },
213 },
214 LockTime: 0,
215 },
216 {
217 Version: 1,
218 TxIn: []*wire.TxIn{
219 {
220 PreviousOutPoint: wire.OutPoint{
221 Hash: chainhash.Hash(
222 [32]byte{
223 // Make go vet happy.
224 0x0b, 0x60, 0x72, 0xb3, 0x86, 0xd4, 0xa7, 0x73,
225 0x23, 0x52, 0x37, 0xf6, 0x4c, 0x11, 0x26, 0xac,
226 0x3b, 0x24, 0x0c, 0x84, 0xb9, 0x17, 0xa3, 0x90,
227 0x9b, 0xa1, 0xc4, 0x3d, 0xed, 0x5f, 0x51, 0xf4,
228 },
229 ), // f4515fed3dc4a19b90a317b9840c243bac26114cf637522373a7d486b372600b
230 Index: 0,
231 },
232 SignatureScript: []byte{
233 0x49, // OP_DATA_73
234 0x30, 0x46, 0x02, 0x21, 0x00, 0xbb, 0x1a, 0xd2,
235 0x6d, 0xf9, 0x30, 0xa5, 0x1c, 0xce, 0x11, 0x0c,
236 0xf4, 0x4f, 0x7a, 0x48, 0xc3, 0xc5, 0x61, 0xfd,
237 0x97, 0x75, 0x00, 0xb1, 0xae, 0x5d, 0x6b, 0x6f,
238 0xd1, 0x3d, 0x0b, 0x3f, 0x4a, 0x02, 0x21, 0x00,
239 0xc5, 0xb4, 0x29, 0x51, 0xac, 0xed, 0xff, 0x14,
240 0xab, 0xba, 0x27, 0x36, 0xfd, 0x57, 0x4b, 0xdb,
241 0x46, 0x5f, 0x3e, 0x6f, 0x8d, 0xa1, 0x2e, 0x2c,
242 0x53, 0x03, 0x95, 0x4a, 0xca, 0x7f, 0x78, 0xf3,
243 0x01, // 73-byte signature
244 0x41, // OP_DATA_65
245 0x04, 0xa7, 0x13, 0x5b, 0xfe, 0x82, 0x4c, 0x97,
246 0xec, 0xc0, 0x1e, 0xc7, 0xd7, 0xe3, 0x36, 0x18,
247 0x5c, 0x81, 0xe2, 0xaa, 0x2c, 0x41, 0xab, 0x17,
248 0x54, 0x07, 0xc0, 0x94, 0x84, 0xce, 0x96, 0x94,
249 0xb4, 0x49, 0x53, 0xfc, 0xb7, 0x51, 0x20, 0x65,
250 0x64, 0xa9, 0xc2, 0x4d, 0xd0, 0x94, 0xd4, 0x2f,
251 0xdb, 0xfd, 0xd5, 0xaa, 0xd3, 0xe0, 0x63, 0xce,
252 0x6a, 0xf4, 0xcf, 0xaa, 0xea, 0x4e, 0xa1, 0x4f,
253 0xbb, // 65-byte pubkey
254 },
255 Sequence: 0xffffffff,
256 },
257 },
258 TxOut: []*wire.TxOut{
259 {
260 Value: 0xf4240, // 1000000
261 PkScript: []byte{
262 0x76, // OP_DUP
263 0xa9, // OP_HASH160
264 0x14, // OP_DATA_20
265 0x39, 0xaa, 0x3d, 0x56, 0x9e, 0x06, 0xa1, 0xd7,
266 0x92, 0x6d, 0xc4, 0xbe, 0x11, 0x93, 0xc9, 0x9b,
267 0xf2, 0xeb, 0x9e, 0xe0,
268 0x88, // OP_EQUALVERIFY
269 0xac, // OP_CHECKSIG
270 },
271 },
272 },
273 LockTime: 0,
274 },
275 },
276 }
277
278 // // TestBlockFiltererOneInOneOut tests the correctness of the BlockFilterer in finding outpoints that spend from a
279 // // "watched outpoint", even if they do not send to an address controlled by the wallet.
280 // func TestBlockFiltererOneInOneOut(t *testing.T) {
281 // // Watch for spend from prev in in first and last tx, both of which are single input/single output.
282 // firstTx := Block100000.Transactions[1]
283 // lastTx := Block100000.Transactions[3]
284 // // Add each of their single previous outpoints to the set of watched outpoints to filter for.
285 // watchedOutPoints := make(map[wire.OutPoint]util.Address)
286 // watchedOutPoints[firstTx.TxIn[0].PreviousOutPoint] = &util.AddressWitnessPubKeyHash{}
287 // watchedOutPoints[lastTx.TxIn[0].PreviousOutPoint] = &util.AddressWitnessPubKeyHash{}
288 // // Construct a filter request, watching only for the outpoints above, and construct a block filterer.
289 // req := &chain.FilterBlocksRequest{
290 // WatchedOutPoints: watchedOutPoints,
291 // }
292 // blockFilterer := chain.NewBlockFilterer(&chaincfg.SimNetParams, req)
293 // // Filter block 100000, which should find matches for the watched outpoints.
294 // match := blockFilterer.FilterBlock(&Block100000)
295 // if !match {
296 // t.Fatalf(
297 // "failed to find matches when filtering for " +
298 // "1-in-1-out txns",
299 // )
300 // }
301 // // We should find exactly two relevant transactions added to the block filterer, then we check that both the first
302 // // and last txns are found in that list.
303 // assertNumRelevantTxns(t, blockFilterer, 2)
304 // assertRelevantTxnsContains(t, blockFilterer, firstTx)
305 // assertRelevantTxnsContains(t, blockFilterer, lastTx)
306 // }
307
308 // assertNumRelevantTxns checks that the set of relevant txns found in a block filterer is of a specific size.
309 func assertNumRelevantTxns(t *testing.T, bf *chainclient.BlockFilterer, size int) {
310 count := len(bf.RelevantTxns)
311 if count != size {
312 t.Fatalf(
313 "unexpected number of relevant txns: "+
314 "want %v, got %v", size, count,
315 )
316 }
317 }
318
319 // assertRelevantTxnsContains checks that the wantTx is found in the block filterers set of relevant txns.
320 func assertRelevantTxnsContains(t *testing.T, bf *chainclient.BlockFilterer, wantTx *wire.MsgTx) {
321 for _, relevantTx := range bf.RelevantTxns {
322 if reflect.DeepEqual(relevantTx, wantTx) {
323 return
324 }
325 }
326 t.Fatalf(
327 "unable to find tx: %v in %d relevant txns", wantTx,
328 len(bf.RelevantTxns),
329 )
330 }
331