forked from lightninglabs/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_test.go
1521 lines (1461 loc) · 44.5 KB
/
sync_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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO: Break up tests into bite-sized pieces.
package neutrino_test
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/gcs/builder"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/lightninglabs/neutrino"
)
var (
// Try btclog.LevelInfo for output like you'd see in normal operation,
// or btclog.LevelTrace to help debug code. Anything but
// btclog.LevelOff turns on log messages from the tests themselves as
// well. Keep in mind some log messages may not appear in order due to
// use of multiple query goroutines in the tests.
logLevel = btclog.LevelOff
syncTimeout = 30 * time.Second
syncUpdate = time.Second
// Don't set this too high for your platform, or the tests will miss
// messages.
// TODO: Make this a benchmark instead.
// TODO: Implement load limiting for both outgoing and incoming
// messages.
numQueryThreads = 20
queryOptions = []neutrino.QueryOption{}
// The logged sequence of events we want to see. The value of i
// represents the block for which a loop is generating a log entry,
// given for readability only.
// "bc": OnBlockConnected
// "fc" xx: OnFilteredBlockConnected with xx (uint8) relevant TXs
// "rv": OnRecvTx
// "rd": OnRedeemingTx
// "bd": OnBlockDisconnected
// "fd": OnFilteredBlockDisconnected
wantLog = func() (log []byte) {
for i := 796; i <= 800; i++ {
// FilteredBlockConnected
log = append(log, []byte("fc")...)
// 0 relevant TXs
log = append(log, 0x00)
// BlockConnected
log = append(log, []byte("bc")...)
}
// Block with one relevant (receive) transaction
log = append(log, []byte("rvfc")...)
log = append(log, 0x01)
log = append(log, []byte("bc")...)
// 124 blocks with nothing
for i := 802; i <= 925; i++ {
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
}
// Block with 1 redeeming transaction
log = append(log, []byte("rdfc")...)
log = append(log, 0x01)
log = append(log, []byte("bc")...)
// Block with nothing
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
// Update with rewind - rewind back to 795, add another address,
// and see more interesting transactions.
for i := 927; i >= 796; i-- {
// BlockDisconnected and FilteredBlockDisconnected
log = append(log, []byte("bdfd")...)
}
// Forward to 800
for i := 796; i <= 800; i++ {
// FilteredBlockConnected
log = append(log, []byte("fc")...)
// 0 relevant TXs
log = append(log, 0x00)
// BlockConnected
log = append(log, []byte("bc")...)
}
// Block with two relevant (receive) transactions
log = append(log, []byte("rvrvfc")...)
log = append(log, 0x02)
log = append(log, []byte("bc")...)
// 124 blocks with nothing
for i := 802; i <= 925; i++ {
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
}
// 2 blocks with 1 redeeming transaction each
for i := 926; i <= 927; i++ {
log = append(log, []byte("rdfc")...)
log = append(log, 0x01)
log = append(log, []byte("bc")...)
}
// Block with nothing
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
// 3 block rollback
for i := 928; i >= 926; i-- {
log = append(log, []byte("fdbd")...)
}
// 5 block empty reorg
for i := 926; i <= 930; i++ {
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
}
// 5 block rollback
for i := 930; i >= 926; i-- {
log = append(log, []byte("fdbd")...)
}
// 2 blocks with 1 redeeming transaction each
for i := 926; i <= 927; i++ {
log = append(log, []byte("rdfc")...)
log = append(log, 0x01)
log = append(log, []byte("bc")...)
}
// 8 block rest of reorg
for i := 928; i <= 935; i++ {
log = append(log, []byte("fc")...)
log = append(log, 0x00)
log = append(log, []byte("bc")...)
}
return log
}()
// rescanMtx locks all the variables to which the rescan goroutine's
// notifications write.
rescanMtx sync.RWMutex
// gotLog is where we accumulate the event log from the rescan. Then we
// compare it to wantLog to see if the series of events the rescan saw
// happened as expected.
gotLog []byte
// curBlockHeight lets the rescan goroutine track where it thinks the
// chain is based on OnBlockConnected and OnBlockDisconnected.
curBlockHeight int32
// curFilteredBlockHeight lets the rescan goroutine track where it
// thinks the chain is based on OnFilteredBlockConnected and
// OnFilteredBlockDisconnected.
curFilteredBlockHeight int32
// ourKnownTxsByBlock lets the rescan goroutine keep track of
// transactions we're interested in that are in the blockchain we're
// following as signalled by OnBlockConnected, OnBlockDisconnected,
// OnRecvTx, and OnRedeemingTx.
ourKnownTxsByBlock = make(map[chainhash.Hash][]*btcutil.Tx)
// ourKnownTxsByFilteredBlock lets the rescan goroutine keep track of
// transactions we're interested in that are in the blockchain we're
// following as signalled by OnFilteredBlockConnected and
// OnFilteredBlockDisconnected.
ourKnownTxsByFilteredBlock = make(map[chainhash.Hash][]*btcutil.Tx)
)
// secSource is an implementation of btcwallet/txauthor/SecretsSource that
// stores WitnessPubKeyHash addresses.
type secSource struct {
keys map[string]*btcec.PrivateKey
scripts map[string]*[]byte
params *chaincfg.Params
}
func (s *secSource) add(privKey *btcec.PrivateKey) (btcutil.Address, error) {
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
addr, err := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash, s.params)
if err != nil {
return nil, err
}
script, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
s.keys[addr.String()] = privKey
s.scripts[addr.String()] = &script
_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, s.params)
if err != nil {
return nil, err
}
if addrs[0].String() != addr.String() {
return nil, fmt.Errorf("Encoded and decoded addresses don't "+
"match. Encoded: %s, decoded: %s", addr, addrs[0])
}
return addr, nil
}
// GetKey is required by the txscript.KeyDB interface
func (s *secSource) GetKey(addr btcutil.Address) (*btcec.PrivateKey, bool,
error) {
privKey, ok := s.keys[addr.String()]
if !ok {
return nil, true, fmt.Errorf("No key for address %s", addr)
}
return privKey, true, nil
}
// GetScript is required by the txscript.ScriptDB interface
func (s *secSource) GetScript(addr btcutil.Address) ([]byte, error) {
script, ok := s.scripts[addr.String()]
if !ok {
return nil, fmt.Errorf("No script for address %s", addr)
}
return *script, nil
}
// ChainParams is required by the SecretsSource interface
func (s *secSource) ChainParams() *chaincfg.Params {
return s.params
}
func newSecSource(params *chaincfg.Params) *secSource {
return &secSource{
keys: make(map[string]*btcec.PrivateKey),
scripts: make(map[string]*[]byte),
params: params,
}
}
type testLogger struct {
t *testing.T
}
func TestSetup(t *testing.T) {
// Set up logging.
logger := btclog.NewBackend(os.Stdout)
chainLogger := logger.Logger("CHAIN")
chainLogger.SetLevel(logLevel)
neutrino.UseLogger(chainLogger)
rpcLogger := logger.Logger("RPCC")
rpcLogger.SetLevel(logLevel)
rpcclient.UseLogger(rpcLogger)
// Create a btcd SimNet node and generate 500 blocks
h1, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
if err != nil {
t.Fatalf("Couldn't create harness: %s", err)
}
defer h1.TearDown()
err = h1.SetUp(false, 0)
if err != nil {
t.Fatalf("Couldn't set up harness: %s", err)
}
_, err = h1.Node.Generate(500)
if err != nil {
t.Fatalf("Couldn't generate blocks: %s", err)
}
// Create a second btcd SimNet node
h2, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
if err != nil {
t.Fatalf("Couldn't create harness: %s", err)
}
defer h2.TearDown()
err = h2.SetUp(false, 0)
if err != nil {
t.Fatalf("Couldn't set up harness: %s", err)
}
// Create a third btcd SimNet node and generate 900 blocks
h3, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
if err != nil {
t.Fatalf("Couldn't create harness: %s", err)
}
defer h3.TearDown()
err = h3.SetUp(false, 0)
if err != nil {
t.Fatalf("Couldn't set up harness: %s", err)
}
_, err = h3.Node.Generate(900)
if err != nil {
t.Fatalf("Couldn't generate blocks: %s", err)
}
// Connect, sync, and disconnect h1 and h2
err = csd([]*rpctest.Harness{h1, h2})
if err != nil {
t.Fatalf("Couldn't connect/sync/disconnect h1 and h2: %s", err)
}
// Generate 300 blocks on the first node and 350 on the second
_, err = h1.Node.Generate(300)
if err != nil {
t.Fatalf("Couldn't generate blocks: %s", err)
}
_, err = h2.Node.Generate(350)
if err != nil {
t.Fatalf("Couldn't generate blocks: %s", err)
}
// Now we have a node with 800 blocks (h1), 850 blocks (h2), and
// 900 blocks (h3). The chains of nodes h1 and h2 match up to block
// 500. By default, a synchronizing wallet connected to all three
// should synchronize to h3. However, we're going to take checkpoints
// from h1 at 111, 333, 555, and 777, and add those to the
// synchronizing wallet's chain parameters so that it should
// disconnect from h3 at block 111, and from h2 at block 555, and
// then synchronize to block 800 from h1. Order of connection is
// unfortunately not guaranteed, so the reorg may not happen with every
// test.
// Copy parameters and insert checkpoints
modParams := chaincfg.SimNetParams
for _, height := range []int64{111, 333, 555, 777} {
hash, err := h1.Node.GetBlockHash(height)
if err != nil {
t.Fatalf("Couldn't get block hash for height %d: %s",
height, err)
}
modParams.Checkpoints = append(modParams.Checkpoints,
chaincfg.Checkpoint{
Hash: hash,
Height: int32(height),
})
}
// Create a temporary directory, initialize an empty walletdb with an
// SPV chain namespace, and create a configuration for the ChainService.
tempDir, err := ioutil.TempDir("", "neutrino")
if err != nil {
t.Fatalf("Failed to create temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
db, err := walletdb.Create("bdb", tempDir+"/weks.db")
defer db.Close()
if err != nil {
t.Fatalf("Error opening DB: %s\n", err)
}
if err != nil {
t.Fatalf("Error geting namespace: %s\n", err)
}
config := neutrino.Config{
DataDir: tempDir,
Database: db,
ChainParams: modParams,
AddPeers: []string{
h3.P2PAddress(),
h2.P2PAddress(),
h1.P2PAddress(),
},
}
neutrino.MaxPeers = 3
neutrino.BanDuration = 5 * time.Second
neutrino.WaitForMoreCFHeaders = time.Second
neutrino.QueryPeerConnectTimeout = 10 * time.Second
svc, err := neutrino.NewChainService(config)
if err != nil {
t.Fatalf("Error creating ChainService: %s", err)
}
svc.Start()
defer svc.Stop()
// Make sure the client synchronizes with the correct node
err = waitForSync(t, svc, h1)
if err != nil {
t.Fatalf("Couldn't sync ChainService: %s", err)
}
// Generate an address and send it some coins on the h1 chain. We use
// this to test rescans and notifications.
secSrc := newSecSource(&modParams)
privKey1, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("Couldn't generate private key: %s", err)
}
addr1, err := secSrc.add(privKey1)
if err != nil {
t.Fatalf("Couldn't create address from key: %s", err)
}
script1, err := secSrc.GetScript(addr1)
if err != nil {
t.Fatalf("Couldn't create script from address: %s", err)
}
out1 := wire.TxOut{
PkScript: script1,
Value: 1000000000,
}
// Fee rate is satoshis per byte
tx1, err := h1.CreateTransaction([]*wire.TxOut{&out1}, 1000)
if err != nil {
t.Fatalf("Couldn't create transaction from script: %s", err)
}
_, err = h1.Node.SendRawTransaction(tx1, true)
if err != nil {
t.Fatalf("Unable to send raw transaction to node: %s", err)
}
privKey2, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("Couldn't generate private key: %s", err)
}
addr2, err := secSrc.add(privKey2)
if err != nil {
t.Fatalf("Couldn't create address from key: %s", err)
}
script2, err := secSrc.GetScript(addr2)
if err != nil {
t.Fatalf("Couldn't create script from address: %s", err)
}
out2 := wire.TxOut{
PkScript: script2,
Value: 1000000000,
}
// Fee rate is satoshis per byte
tx2, err := h1.CreateTransaction([]*wire.TxOut{&out2}, 1000)
if err != nil {
t.Fatalf("Couldn't create transaction from script: %s", err)
}
_, err = h1.Node.SendRawTransaction(tx2, true)
if err != nil {
t.Fatalf("Unable to send raw transaction to node: %s", err)
}
_, err = h1.Node.Generate(1)
if err != nil {
t.Fatalf("Couldn't generate/submit block: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
t.Fatalf("Couldn't sync ChainService: %s", err)
}
// Do a rescan that searches only for a specific TXID
startBlock := waddrmgr.BlockStamp{Height: 795}
endBlock := waddrmgr.BlockStamp{Height: 801}
var foundTx *btcutil.Tx
err = svc.Rescan(
neutrino.StartBlock(&startBlock),
neutrino.EndBlock(&endBlock),
neutrino.WatchTxIDs(tx1.TxHash()),
neutrino.NotificationHandlers(rpcclient.NotificationHandlers{
OnFilteredBlockConnected: func(height int32,
header *wire.BlockHeader,
relevantTxs []*btcutil.Tx) {
if height == 801 {
if len(relevantTxs) != 1 {
t.Fatalf("Didn't get expected "+
"number of relevant "+
"transactions from "+
"rescan: want 1, got "+
"%d", len(relevantTxs))
}
if *(relevantTxs[0].Hash()) !=
tx1.TxHash() {
t.Fatalf("Didn't get expected "+
"relevant transaction:"+
" want %s, got %s",
tx1.TxHash(),
relevantTxs[0].Hash())
}
foundTx = relevantTxs[0]
}
},
}),
)
if err != nil || foundTx == nil || *(foundTx.Hash()) != tx1.TxHash() {
t.Fatalf("Couldn't rescan chain for transaction %s: %s",
tx1.TxHash(), err)
}
// Check that we got the right transaction index.
blockHeader, err := svc.BlockHeaders.FetchHeaderByHeight(801)
if err != nil {
t.Fatalf("Couldn't get block hash for block 801: %s", err)
}
blockHash := blockHeader.BlockHash()
block, err := h1.Node.GetBlock(&blockHash)
if err != nil {
t.Fatalf("Couldn't get block %s via RPC: %s", blockHash, err)
}
ourIndex := 0
for i, tx := range block.Transactions {
if tx.TxHash() == tx1.TxHash() {
ourIndex = i
}
}
if foundTx.Index() != ourIndex {
t.Fatalf("Index of found transaction incorrect: want 1, got %d",
foundTx.Index())
}
// Call GetUtxo for our output in tx1 to see if it's spent.
ourIndex = 1 << 30 // Should work on 32-bit systems
for i, txo := range tx1.TxOut {
if bytes.Equal(txo.PkScript, script1) {
ourIndex = i
}
}
var ourOutPoint wire.OutPoint
if ourIndex != 1<<30 {
ourOutPoint = wire.OutPoint{
Hash: tx1.TxHash(),
Index: uint32(ourIndex),
}
} else {
t.Fatalf("Couldn't find the index of our output in transaction"+
" %s", tx1.TxHash())
}
spendReport, err := svc.GetUtxo(
neutrino.WatchOutPoints(ourOutPoint),
neutrino.StartBlock(&waddrmgr.BlockStamp{Height: 801}),
)
if err != nil {
t.Fatalf("Couldn't get UTXO %s: %s", ourOutPoint, err)
}
if !bytes.Equal(spendReport.Output.PkScript, script1) {
t.Fatalf("UTXO's script doesn't match expected script for %s",
ourOutPoint)
}
// Start a rescan with notifications in another goroutine. We'll kill
// it with a quit channel at the end and make sure we got the expected
// results.
quitRescan := make(chan struct{})
defer func() {
if quitRescan != nil {
close(quitRescan)
}
}()
startBlock = waddrmgr.BlockStamp{Height: 795}
rescan, errChan := startRescan(t, svc, addr1, &startBlock, quitRescan)
if err != nil {
t.Fatalf("Couldn't start a rescan for %s: %s", addr1, err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err := checkRescanStatus()
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Checking rescan status failed: %s", err)
}
if numTXs != 1 {
t.Fatalf("Wrong number of relevant transactions. Want: 1, got:"+
" %d", numTXs)
}
// Generate 124 blocks on h1 to make sure it reorgs the other nodes.
// Ensure the ChainService instance stays caught up.
h1.Node.Generate(124)
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
// Connect/sync/disconnect h2 to make it reorg to the h1 chain.
err = csd([]*rpctest.Harness{h1, h2})
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync h2 to h1: %s", err)
}
// Spend the outputs we sent ourselves over two blocks.
inSrc := func(tx wire.MsgTx) func(target btcutil.Amount) (
total btcutil.Amount, inputs []*wire.TxIn,
inputValues []btcutil.Amount, scripts [][]byte, err error) {
ourIndex := 1 << 30 // Should work on 32-bit systems
for i, txo := range tx.TxOut {
if bytes.Equal(txo.PkScript, script1) ||
bytes.Equal(txo.PkScript, script2) {
ourIndex = i
}
}
return func(target btcutil.Amount) (total btcutil.Amount,
inputs []*wire.TxIn, inputValues []btcutil.Amount,
scripts [][]byte, err error) {
if ourIndex == 1<<30 {
err = fmt.Errorf("Couldn't find our address " +
"in the passed transaction's outputs.")
return
}
total = target
inputs = []*wire.TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: tx.TxHash(),
Index: uint32(ourIndex),
},
},
}
inputValues = []btcutil.Amount{
btcutil.Amount(tx.TxOut[ourIndex].Value)}
scripts = [][]byte{tx.TxOut[ourIndex].PkScript}
err = nil
return
}
}
// Create another address to send to so we don't trip the rescan with
// the old address and we can test monitoring both OutPoint usage and
// receipt by addresses.
privKey3, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("Couldn't generate private key: %s", err)
}
addr3, err := secSrc.add(privKey3)
if err != nil {
t.Fatalf("Couldn't create address from key: %s", err)
}
script3, err := secSrc.GetScript(addr3)
if err != nil {
t.Fatalf("Couldn't create script from address: %s", err)
}
out3 := wire.TxOut{
PkScript: script3,
Value: 500000000,
}
// Spend the first transaction and mine a block.
authTx1, err := txauthor.NewUnsignedTransaction(
[]*wire.TxOut{
&out3,
},
// Fee rate is satoshis per kilobyte
1024000,
inSrc(*tx1),
func() ([]byte, error) {
return script3, nil
},
)
if err != nil {
t.Fatalf("Couldn't create unsigned transaction: %s", err)
}
err = authTx1.AddAllInputScripts(secSrc)
if err != nil {
t.Fatalf("Couldn't sign transaction: %s", err)
}
banPeer(svc, h2)
err = svc.SendTransaction(authTx1.Tx,
append(queryOptions,
neutrino.PeerConnectTimeout(3*time.Second))...)
if err != nil && !strings.Contains(err.Error(), "already have") {
t.Fatalf("Unable to send transaction to network: %s", err)
}
_, err = h1.Node.Generate(1)
if err != nil {
t.Fatalf("Couldn't generate/submit block: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err = checkRescanStatus()
if numTXs != 2 {
t.Fatalf("Wrong number of relevant transactions. Want: 2, got:"+
" %d", numTXs)
}
// Spend the second transaction and mine a block.
authTx2, err := txauthor.NewUnsignedTransaction(
[]*wire.TxOut{
&out3,
},
// Fee rate is satoshis per kilobyte
1024000,
inSrc(*tx2),
func() ([]byte, error) {
return script3, nil
},
)
if err != nil {
t.Fatalf("Couldn't create unsigned transaction: %s", err)
}
err = authTx2.AddAllInputScripts(secSrc)
if err != nil {
t.Fatalf("Couldn't sign transaction: %s", err)
}
banPeer(svc, h2)
err = svc.SendTransaction(authTx2.Tx,
append(queryOptions,
neutrino.PeerConnectTimeout(3*time.Second))...)
if err != nil && !strings.Contains(err.Error(), "already have") {
t.Fatalf("Unable to send transaction to network: %s", err)
}
_, err = h1.Node.Generate(1)
if err != nil {
t.Fatalf("Couldn't generate/submit block: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err = checkRescanStatus()
if numTXs != 2 {
t.Fatalf("Wrong number of relevant transactions. Want: 2, got:"+
" %d", numTXs)
}
// Update the filter with the second address, and we should have 2 more
// relevant transactions.
err = rescan.Update(neutrino.AddAddrs(addr2), neutrino.Rewind(795))
if err != nil {
t.Fatalf("Couldn't update the rescan filter: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err = checkRescanStatus()
if numTXs != 4 {
t.Fatalf("Wrong number of relevant transactions. Want: 4, got:"+
" %d", numTXs)
}
// Generate a block with a nonstandard coinbase to generate a basic
// filter with 0 entries.
_, err = h1.GenerateAndSubmitBlockWithCustomCoinbaseOutputs(
[]*btcutil.Tx{}, rpctest.BlockVersion, time.Time{},
[]wire.TxOut{{
Value: 0,
PkScript: []byte{},
}})
if err != nil {
t.Fatalf("Couldn't generate/submit block: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
// Check and make sure the previous UTXO is now spent.
spendReport, err = svc.GetUtxo(
neutrino.WatchOutPoints(ourOutPoint),
neutrino.StartBlock(&waddrmgr.BlockStamp{Height: 801}),
)
if err != nil {
t.Fatalf("Couldn't get UTXO %s: %s", ourOutPoint, err)
}
if spendReport.SpendingTx.TxHash() != authTx1.Tx.TxHash() {
t.Fatalf("Redeeming transaction doesn't match expected "+
"transaction: want %s, got %s", authTx1.Tx.TxHash(),
spendReport.SpendingTx.TxHash())
}
// Test that we can get blocks and cfilters via P2P and decide which are
// valid and which aren't.
// TODO: Split this out into a benchmark.
err = testRandomBlocks(t, svc, h1)
if err != nil {
t.Fatalf("Testing blocks and cfilters failed: %s", err)
}
// Generate 5 blocks on h2 and wait for ChainService to sync to the
// newly-best chain on h2. This includes the transactions sent via
// svc.SendTransaction earlier, so we'll have to check that the rescan
// status has updated for the correct number of transactions.
_, err = h2.Node.Generate(5)
if err != nil {
t.Fatalf("Couldn't generate/submit blocks: %s", err)
}
err = waitForSync(t, svc, h2)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err = checkRescanStatus()
if numTXs != 2 {
t.Fatalf("Wrong number of relevant transactions. Want: 2, got:"+
" %d", numTXs)
}
// Generate 7 blocks on h1 and wait for ChainService to sync to the
// newly-best chain on h1.
_, err = h1.Node.Generate(7)
if err != nil {
t.Fatalf("Couldn't generate/submit block: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
numTXs, _, err = checkRescanStatus()
if numTXs != 4 {
t.Fatalf("Wrong number of relevant transactions. Want: 4, got:"+
" %d", numTXs)
}
if !bytes.Equal(wantLog, gotLog) {
leastBytes := len(wantLog)
if len(gotLog) < leastBytes {
leastBytes = len(gotLog)
}
diffIndex := 0
for i := 0; i < leastBytes; i++ {
if wantLog[i] != gotLog[i] {
diffIndex = i
break
}
}
t.Fatalf("Rescan event logs differ starting at %d.\nWant: %v\n"+
"Got: %v\nDifference - want: %v\nDifference -- got: "+
"%v", diffIndex, wantLog, gotLog, wantLog[diffIndex:],
gotLog[diffIndex:])
}
// Connect h1 and h2, wait for them to synchronize and check for the
// ChainService synchronization status.
err = rpctest.ConnectNode(h1, h2)
if err != nil {
t.Fatalf("Couldn't connect h1 to h2: %s", err)
}
err = rpctest.JoinNodes([]*rpctest.Harness{h1, h2}, rpctest.Blocks)
if err != nil {
t.Fatalf("Couldn't sync h1 and h2: %s", err)
}
err = waitForSync(t, svc, h1)
if err != nil {
t.Fatalf("Couldn't sync ChainService: %s", err)
}
// Now generate a bunch of blocks on each while they're connected,
// triggering many tiny reorgs, and wait for sync again. The end result
// is somewhat random, depending on how quickly the nodes process each
// other's notifications vs finding new blocks, but the two nodes should
// remain fully synchronized with each other at the end.
neutrino.CFHMinPeers = 2
go h2.Node.Generate(75)
h1.Node.Generate(50)
err = rpctest.JoinNodes([]*rpctest.Harness{h1, h2}, rpctest.Blocks)
if err != nil {
t.Fatalf("Couldn't sync h1 and h2: %s", err)
}
// We increase the timeout because running on Travis with race
// detection enabled can make this pretty slow.
syncTimeout *= 2
err = waitForSync(t, svc, h1)
if err != nil {
checkErrChan(t, errChan)
t.Fatalf("Couldn't sync ChainService: %s", err)
}
close(quitRescan)
err = <-errChan
quitRescan = nil
if err != nil {
t.Fatalf("Rescan ended with error: %s", err)
}
}
// csd does a connect-sync-disconnect between nodes in order to support
// reorg testing. It brings up and tears down a temporary node, otherwise the
// nodes try to reconnect to each other which results in unintended reorgs.
func csd(harnesses []*rpctest.Harness) error {
hTemp, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
if err != nil {
return err
}
// Tear down node at the end of the function.
defer hTemp.TearDown()
err = hTemp.SetUp(false, 0)
if err != nil {
return err
}
for _, harness := range harnesses {
err = rpctest.ConnectNode(hTemp, harness)
if err != nil {
return err
}
}
return rpctest.JoinNodes(harnesses, rpctest.Blocks)
}
// checkErrChan tries to read the passed error channel if possible and logs the
// error it found, if any. This is useful to help troubleshoot any timeouts
// during a rescan.
func checkErrChan(t *testing.T, errChan <-chan error) {
select {
case err := <-errChan:
t.Logf("Got error from rescan: %s", err)
default:
}
}
// waitForSync waits for the ChainService to sync to the current chain state.
func waitForSync(t *testing.T, svc *neutrino.ChainService,
correctSyncNode *rpctest.Harness) error {
knownBestHash, knownBestHeight, err :=
correctSyncNode.Node.GetBestBlock()
if err != nil {
return err
}
if logLevel != btclog.LevelOff {
t.Logf("Syncing to %d (%s)", knownBestHeight, knownBestHash)
}
var haveBest *waddrmgr.BlockStamp
haveBest, err = svc.BestSnapshot()
if err != nil {
return fmt.Errorf("Couldn't get best snapshot from "+
"ChainService: %s", err)
}
var total time.Duration
for haveBest.Hash != *knownBestHash {
if total > syncTimeout {
return fmt.Errorf("Timed out after %v waiting for "+
"header synchronization.", syncTimeout)
}
if haveBest.Height > knownBestHeight {
return fmt.Errorf("synchronized to the wrong chain")
}
time.Sleep(syncUpdate)
total += syncUpdate
haveBest, err = svc.BestSnapshot()
if err != nil {
return fmt.Errorf("Couldn't get best snapshot from "+
"ChainService: %s", err)
}
}
// Check if we're current.
if !svc.IsCurrent() {
return fmt.Errorf("the ChainService doesn't see itself as " +
"current")
}
// Check if we have all of the cfheaders.
knownBasicHeader, err := correctSyncNode.Node.GetCFilterHeader(
knownBestHash, wire.GCSFilterRegular)
if err != nil {
return fmt.Errorf("Couldn't get latest basic header from "+
"%s: %s", correctSyncNode.P2PAddress(), err)
}
knownExtHeader, err := correctSyncNode.Node.GetCFilterHeader(
knownBestHash, wire.GCSFilterExtended)
if err != nil {
return fmt.Errorf("Couldn't get latest extended header from "+
"%s: %s", correctSyncNode.P2PAddress(), err)
}
haveBasicHeader := &chainhash.Hash{}
haveExtHeader := &chainhash.Hash{}
for (knownBasicHeader.PrevFilterHeader != *haveBasicHeader) &&
(knownExtHeader.PrevFilterHeader != *haveExtHeader) {
if total > syncTimeout {
return fmt.Errorf("Timed out after %v waiting for "+
"cfheaders synchronization.", syncTimeout)
}
haveBasicHeader, err = svc.RegFilterHeaders.FetchHeader(knownBestHash)
if err != nil {
if err == io.EOF {
haveBasicHeader = &chainhash.Hash{}
time.Sleep(syncUpdate)
total += syncUpdate
continue
}
return fmt.Errorf("Couldn't get regular filter header"+
" for %s: %s", knownBestHash, err)
}
haveExtHeader, err = svc.ExtFilterHeaders.FetchHeader(knownBestHash)
if err != nil {
if err == io.EOF {
haveExtHeader = &chainhash.Hash{}
time.Sleep(syncUpdate)
total += syncUpdate
continue
}
return fmt.Errorf("Couldn't get extended filter header"+
" for %s: %s", knownBestHash, err)
}
time.Sleep(syncUpdate)
total += syncUpdate
}
if logLevel != btclog.LevelOff {
t.Logf("Synced cfheaders to %d (%s)", haveBest.Height,
haveBest.Hash)
}
// At this point, we know we have good cfheaders. Now we wait for the
// rescan, if one is going, to catch up.
for {