This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
forked from lightninglabs/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockmanager_test.go
626 lines (537 loc) · 16.9 KB
/
blockmanager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package neutrino
import (
"encoding/binary"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/wakiyamap/monad/chaincfg"
"github.com/wakiyamap/monad/chaincfg/chainhash"
"github.com/wakiyamap/monad/wire"
"github.com/wakiyamap/monautil/gcs/builder"
"github.com/wakiyamap/monawallet/walletdb"
"github.com/wakiyamap/neutrino/blockntfns"
"github.com/wakiyamap/neutrino/headerfs"
)
// maxHeight is the height we will generate filter headers up to.
const maxHeight = 20 * uint32(wire.CFCheckptInterval)
// setupBlockManager initialises a blockManager to be used in tests.
func setupBlockManager() (*blockManager, headerfs.BlockHeaderStore,
*headerfs.FilterHeaderStore, func(), error) {
// Set up the block and filter header stores.
tempDir, err := ioutil.TempDir("", "neutrino")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("Failed to create "+
"temporary directory: %s", err)
}
db, err := walletdb.Create("bdb", tempDir+"/weks.db")
if err != nil {
os.RemoveAll(tempDir)
return nil, nil, nil, nil, fmt.Errorf("Error opening DB: %s",
err)
}
hdrStore, err := headerfs.NewBlockHeaderStore(
tempDir, db, &chaincfg.SimNetParams,
)
if err != nil {
db.Close()
return nil, nil, nil, nil, fmt.Errorf("Error creating block "+
"header store: %s", err)
}
cleanUp := func() {
defer os.RemoveAll(tempDir)
defer db.Close()
}
cfStore, err := headerfs.NewFilterHeaderStore(
tempDir, db, headerfs.RegularFilter,
&chaincfg.SimNetParams,
)
if err != nil {
cleanUp()
return nil, nil, nil, nil, fmt.Errorf("Error creating filter "+
"header store: %s", err)
}
// Set up a chain service for the block manager. Each test should set
// custom query methods on this chain service.
cs := &ChainService{
chainParams: chaincfg.SimNetParams,
BlockHeaders: hdrStore,
RegFilterHeaders: cfStore,
}
// Set up a blockManager with the chain service we defined.
bm, err := newBlockManager(cs, nil)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("unable to create "+
"blockmanager: %v", err)
}
return bm, hdrStore, cfStore, cleanUp, nil
}
// headers wraps the different headers and filters used throughout the tests.
type headers struct {
blockHeaders []headerfs.BlockHeader
cfHeaders []headerfs.FilterHeader
checkpoints []*chainhash.Hash
filterHashes []chainhash.Hash
}
// generateHeaders generates block headers, filter header and hashes, and
// checkpoints from the given genesis. The onCheckpoint method will be called
// with the current cf header on each checkpoint to modify the derivation of
// the next interval
func generateHeaders(genesisBlockHeader *wire.BlockHeader,
genesisFilterHeader *chainhash.Hash,
onCheckpoint func(*chainhash.Hash)) (*headers, error) {
var blockHeaders []headerfs.BlockHeader
blockHeaders = append(blockHeaders, headerfs.BlockHeader{
BlockHeader: genesisBlockHeader,
Height: 0,
})
var cfHeaders []headerfs.FilterHeader
cfHeaders = append(cfHeaders, headerfs.FilterHeader{
HeaderHash: genesisBlockHeader.BlockHash(),
FilterHash: *genesisFilterHeader,
Height: 0,
})
// The filter hashes (not the filter headers!) will be sent as
// part of the CFHeaders response, so we also keep track of
// them.
genesisFilter, err := builder.BuildBasicFilter(
chaincfg.SimNetParams.GenesisBlock, nil,
)
if err != nil {
return nil, fmt.Errorf("unable to build genesis filter: %v",
err)
}
genesisFilterHash, err := builder.GetFilterHash(genesisFilter)
if err != nil {
return nil, fmt.Errorf("unable to get genesis filter hash: %v",
err)
}
var filterHashes []chainhash.Hash
filterHashes = append(filterHashes, genesisFilterHash)
// Also keep track of the current filter header. We use this to
// calculate the next filter header, as it commits to the
// previous.
currentCFHeader := *genesisFilterHeader
// checkpoints will be the checkpoints passed to
// getCheckpointedCFHeaders.
var checkpoints []*chainhash.Hash
for height := uint32(1); height <= maxHeight; height++ {
header := heightToHeader(height)
blockHeader := headerfs.BlockHeader{
BlockHeader: header,
Height: height,
}
blockHeaders = append(blockHeaders, blockHeader)
// It doesn't really matter what filter the filter
// header commit to, so just use the height as a nonce
// for the filters.
filterHash := chainhash.Hash{}
binary.BigEndian.PutUint32(filterHash[:], height)
filterHashes = append(filterHashes, filterHash)
// Calculate the current filter header, and add to our
// slice.
currentCFHeader = chainhash.DoubleHashH(
append(filterHash[:], currentCFHeader[:]...),
)
cfHeaders = append(cfHeaders, headerfs.FilterHeader{
HeaderHash: header.BlockHash(),
FilterHash: currentCFHeader,
Height: height,
})
// Each interval we must record a checkpoint.
if height%wire.CFCheckptInterval == 0 {
// We must make a copy of the current header to
// avoid mutation.
cfh := currentCFHeader
checkpoints = append(checkpoints, &cfh)
if onCheckpoint != nil {
onCheckpoint(¤tCFHeader)
}
}
}
return &headers{
blockHeaders: blockHeaders,
cfHeaders: cfHeaders,
checkpoints: checkpoints,
filterHashes: filterHashes,
}, nil
}
// generateResponses generates the MsgCFHeaders messages from the given queries
// and headers.
func generateResponses(msgs []wire.Message,
headers *headers) ([]*wire.MsgCFHeaders, error) {
// Craft a response for each message.
var responses []*wire.MsgCFHeaders
for _, msg := range msgs {
// Only GetCFHeaders expected.
q, ok := msg.(*wire.MsgGetCFHeaders)
if !ok {
return nil, fmt.Errorf("got unexpected message %T",
msg)
}
// The start height must be set to a checkpoint height+1.
if q.StartHeight%wire.CFCheckptInterval != 1 {
return nil, fmt.Errorf("unexpexted start height %v",
q.StartHeight)
}
var prevFilterHeader chainhash.Hash
switch q.StartHeight {
// If the start height is 1 the prevFilterHeader is set to the
// genesis header.
case 1:
genesisFilterHeader := headers.cfHeaders[0].FilterHash
prevFilterHeader = genesisFilterHeader
// Otherwise we use one of the created checkpoints.
default:
j := q.StartHeight/wire.CFCheckptInterval - 1
prevFilterHeader = *headers.checkpoints[j]
}
resp := &wire.MsgCFHeaders{
FilterType: q.FilterType,
StopHash: q.StopHash,
PrevFilterHeader: prevFilterHeader,
}
// Keep adding filter hashes until we reach the stop hash.
for h := q.StartHeight; ; h++ {
resp.FilterHashes = append(
resp.FilterHashes, &headers.filterHashes[h],
)
blockHash := headers.blockHeaders[h].BlockHash()
if blockHash == q.StopHash {
break
}
}
responses = append(responses, resp)
}
return responses, nil
}
// TestBlockManagerInitialInterval tests that the block manager is able to
// handle checkpointed filter header query responses in out of order, and when
// a partial interval is already written to the store.
func TestBlockManagerInitialInterval(t *testing.T) {
t.Parallel()
type testCase struct {
// permute indicates whether responses should be permutated.
permute bool
// partialInterval indicates whether we should write parts of
// the first checkpoint interval to the filter header store
// before starting the test.
partialInterval bool
// repeat indicates whether responses should be repeated.
repeat bool
}
// Generate all combinations of testcases.
var testCases []testCase
b := []bool{false, true}
for _, perm := range b {
for _, part := range b {
for _, rep := range b {
testCases = append(testCases, testCase{
permute: perm,
partialInterval: part,
repeat: rep,
})
}
}
}
for _, test := range testCases {
testDesc := fmt.Sprintf("permute=%v, partial=%v, repeat=%v",
test.permute, test.partialInterval, test.repeat)
bm, hdrStore, cfStore, cleanUp, err := setupBlockManager()
if err != nil {
t.Fatalf("unable to set up ChainService: %v", err)
}
defer cleanUp()
// Keep track of the filter headers and block headers. Since
// the genesis headers are written automatically when the store
// is created, we query it to add to the slices.
genesisBlockHeader, _, err := hdrStore.ChainTip()
if err != nil {
t.Fatal(err)
}
genesisFilterHeader, _, err := cfStore.ChainTip()
if err != nil {
t.Fatal(err)
}
headers, err := generateHeaders(genesisBlockHeader,
genesisFilterHeader, nil)
if err != nil {
t.Fatalf("unable to generate headers: %v", err)
}
// Write all block headers but the genesis, since it is already
// in the store.
if err = hdrStore.WriteHeaders(headers.blockHeaders[1:]...); err != nil {
t.Fatalf("Error writing batch of headers: %s", err)
}
// We emulate the case where a few filter headers are already
// written to the store by writing 1/3 of the first interval.
if test.partialInterval {
err = cfStore.WriteHeaders(
headers.cfHeaders[1 : wire.CFCheckptInterval/3]...,
)
if err != nil {
t.Fatalf("Error writing batch of headers: %s",
err)
}
}
// We set up a custom query batch method for this test, as we
// will use this to feed the blockmanager with our crafted
// responses.
bm.server.queryBatch = func(msgs []wire.Message,
f func(*ServerPeer, wire.Message, wire.Message) bool,
q <-chan struct{}, qo ...QueryOption) {
responses, err := generateResponses(msgs, headers)
if err != nil {
t.Fatalf("unable to generate responses: %v",
err)
}
// We permute the response order if the test signals
// that.
perm := rand.Perm(len(responses))
for i, v := range perm {
index := i
if test.permute {
index = v
}
// Before sending we take a copy of the
// message, as we cannot guarantee that it
// won't be modified.
r := *responses[index]
// Let the blockmanager handle the message.
if !f(nil, msgs[index], responses[index]) {
t.Fatalf("got response false on "+
"send of index %d: %v",
index, testDesc)
}
// If we are not testing repeated responses, go
// on to the next response.
if !test.repeat {
continue
}
// Otherwise resend the response we just sent.
if !f(nil, msgs[index], &r) {
t.Fatalf("got response false on "+
"resend of index %d: %v",
index, testDesc)
}
}
}
// We should expect to see notifications for each new filter
// header being connected.
startHeight := uint32(1)
if test.partialInterval {
startHeight = wire.CFCheckptInterval / 3
}
go func() {
for i := startHeight; i <= maxHeight; i++ {
ntfn := <-bm.blockNtfnChan
if _, ok := ntfn.(*blockntfns.Connected); !ok {
t.Fatal("expected block connected " +
"notification")
}
}
}()
// Call the get checkpointed cf headers method with the
// checkpoints we created to start the test.
bm.getCheckpointedCFHeaders(
headers.checkpoints, cfStore, wire.GCSFilterRegular,
)
// Finally make sure the filter header tip is what we expect.
tip, tipHeight, err := cfStore.ChainTip()
if err != nil {
t.Fatal(err)
}
if tipHeight != maxHeight {
t.Fatalf("expected tip height to be %v, was %v",
maxHeight, tipHeight)
}
lastCheckpoint := headers.checkpoints[len(headers.checkpoints)-1]
if *tip != *lastCheckpoint {
t.Fatalf("expected tip to be %v, was %v",
lastCheckpoint, tip)
}
}
}
// TestBlockManagerInvalidInterval tests that the block manager is able to
// determine it is receiving corrupt checkpoints and filter headers.
func TestBlockManagerInvalidInterval(t *testing.T) {
t.Parallel()
type testCase struct {
// wrongGenesis indicates whether we should start deriving the
// filters from a wrong genesis.
wrongGenesis bool
// intervalMisaligned indicates whether each interval prev hash
// should not line up with the previous checkpoint.
intervalMisaligned bool
// invalidPrevHash indicates whether the interval responses
// should have a prev hash that doesn't mathc that interval.
invalidPrevHash bool
// partialInterval indicates whether we should write parts of
// the first checkpoint interval to the filter header store
// before starting the test.
partialInterval bool
// firstInvalid is the first interval we expect the
// blockmanager to determine is invalid.
firstInvalid int
}
testCases := []testCase{
// With a set of checkpoints and filter headers calculated from
// the wrong genesis, the block manager should be able to
// determine that the first interval doesn't line up.
{
wrongGenesis: true,
firstInvalid: 0,
},
// With checkpoints calculated from the wrong genesis, and a
// partial set of filter headers already written, the first
// interval should be considered invalid.
{
wrongGenesis: true,
partialInterval: true,
firstInvalid: 0,
},
// With intervals not lining up, the second interval should
// be determined invalid.
{
intervalMisaligned: true,
firstInvalid: 1,
},
// With misaligned intervals and a partial interval written, the
// second interval should be considered invalid.
{
intervalMisaligned: true,
partialInterval: true,
firstInvalid: 1,
},
// With responses having invalid prev hashes, the second
// interval should be deemed invalid.
{
invalidPrevHash: true,
firstInvalid: 1,
},
}
for _, test := range testCases {
bm, hdrStore, cfStore, cleanUp, err := setupBlockManager()
if err != nil {
t.Fatalf("unable to set up ChainService: %v", err)
}
defer cleanUp()
// Keep track of the filter headers and block headers. Since
// the genesis headers are written automatically when the store
// is created, we query it to add to the slices.
genesisBlockHeader, _, err := hdrStore.ChainTip()
if err != nil {
t.Fatal(err)
}
genesisFilterHeader, _, err := cfStore.ChainTip()
if err != nil {
t.Fatal(err)
}
// To emulate a full node serving us filter headers derived
// from different genesis than what we have, we flip a bit in
// the genesis filter header.
if test.wrongGenesis {
genesisFilterHeader[0] ^= 1
}
headers, err := generateHeaders(genesisBlockHeader,
genesisFilterHeader,
func(currentCFHeader *chainhash.Hash) {
// If we are testing that each interval doesn't
// line up properly with the previous, we flip
// a bit in the current header before
// calculating the next interval checkpoint.
if test.intervalMisaligned {
currentCFHeader[0] ^= 1
}
})
if err != nil {
t.Fatalf("unable to generate headers: %v", err)
}
// Write all block headers but the genesis, since it is already
// in the store.
if err = hdrStore.WriteHeaders(headers.blockHeaders[1:]...); err != nil {
t.Fatalf("Error writing batch of headers: %s", err)
}
// We emulate the case where a few filter headers are already
// written to the store by writing 1/3 of the first interval.
if test.partialInterval {
err = cfStore.WriteHeaders(
headers.cfHeaders[1 : wire.CFCheckptInterval/3]...,
)
if err != nil {
t.Fatalf("Error writing batch of headers: %s",
err)
}
}
bm.server.queryBatch = func(msgs []wire.Message,
f func(*ServerPeer, wire.Message, wire.Message) bool,
q <-chan struct{}, qo ...QueryOption) {
responses, err := generateResponses(msgs, headers)
if err != nil {
t.Fatalf("unable to generate responses: %v",
err)
}
// Since we used the generated checkpoints when
// creating the responses, we must flip the
// PrevFilterHeader bit back before sending them if we
// are checking for misaligned intervals. This to
// ensure we don't hit the invalid prev hash case.
if test.intervalMisaligned {
for i := range responses {
if i == 0 {
continue
}
responses[i].PrevFilterHeader[0] ^= 1
}
}
// If we are testing for intervals with invalid prev
// hashes, we flip a bit to corrup them, regardless of
// whether we are testing misaligned intervals.
if test.invalidPrevHash {
for i := range responses {
if i == 0 {
continue
}
responses[i].PrevFilterHeader[1] ^= 1
}
}
// Check that the success of the callback match what we
// expect.
for i := range responses {
success := f(nil, msgs[i], responses[i])
if i == test.firstInvalid {
if success {
t.Fatalf("expected interval "+
"%d to be invalid", i)
}
break
}
if !success {
t.Fatalf("expected interval %d to be "+
"valid", i)
}
}
}
// We should expect to see notifications for each new filter
// header being connected.
startHeight := uint32(1)
if test.partialInterval {
startHeight = wire.CFCheckptInterval / 3
}
go func() {
for i := startHeight; i <= maxHeight; i++ {
ntfn := <-bm.blockNtfnChan
if _, ok := ntfn.(*blockntfns.Connected); !ok {
t.Fatal("expected block connected " +
"notification")
}
}
}()
// Start the test by calling the get checkpointed cf headers
// method with the checkpoints we created.
bm.getCheckpointedCFHeaders(
headers.checkpoints, cfStore, wire.GCSFilterRegular,
)
}
}