-
Notifications
You must be signed in to change notification settings - Fork 155
/
sync.go
704 lines (625 loc) · 18.5 KB
/
sync.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
// Copyright (c) 2017-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chain
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"net"
"runtime/trace"
"sync"
"sync/atomic"
"decred.org/dcrwallet/errors"
"decred.org/dcrwallet/rpc/client/dcrd"
"decred.org/dcrwallet/validate"
"decred.org/dcrwallet/wallet"
"github.com/decred/dcrd/blockchain/stake/v3"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/wire"
"github.com/jrick/wsrpc/v2"
"golang.org/x/sync/errgroup"
)
var requiredAPIVersion = semver{Major: 6, Minor: 0, Patch: 0}
// Syncer implements wallet synchronization services by processing
// notifications from a dcrd JSON-RPC server.
type Syncer struct {
atomicWalletSynced uint32 // CAS (synced=1) when wallet syncing complete
wallet *wallet.Wallet
opts *RPCOptions
rpc *dcrd.RPC
notifier *notifier
discoverAccts bool
mu sync.Mutex
// Sidechain management
sidechains wallet.SidechainForest
sidechainsMu sync.Mutex
relevantTxs map[chainhash.Hash][]*wire.MsgTx
cb *Callbacks
}
// RPCOptions specifies the network and security settings for establishing a
// websocket connection to a dcrd JSON-RPC server.
type RPCOptions struct {
Address string
DefaultPort string
User string
Pass string
Dial func(ctx context.Context, network, address string) (net.Conn, error)
CA []byte
Insecure bool
}
// NewSyncer creates a Syncer that will sync the wallet using dcrd JSON-RPC.
func NewSyncer(w *wallet.Wallet, r *RPCOptions) *Syncer {
return &Syncer{
wallet: w,
opts: r,
discoverAccts: !w.Locked(),
relevantTxs: make(map[chainhash.Hash][]*wire.MsgTx),
}
}
// Callbacks contains optional callback functions to notify events during
// the syncing process. All callbacks are called synchronously and block the
// syncer from continuing.
type Callbacks struct {
Synced func(synced bool)
FetchMissingCFiltersStarted func()
FetchMissingCFiltersProgress func(startCFiltersHeight, endCFiltersHeight int32)
FetchMissingCFiltersFinished func()
FetchHeadersStarted func()
FetchHeadersProgress func(lastHeaderHeight int32, lastHeaderTime int64)
FetchHeadersFinished func()
DiscoverAddressesStarted func()
DiscoverAddressesFinished func()
RescanStarted func()
RescanProgress func(rescannedThrough int32)
RescanFinished func()
}
// SetCallbacks sets the possible various callbacks that are used
// to notify interested parties to the syncing progress.
func (s *Syncer) SetCallbacks(cb *Callbacks) {
s.cb = cb
}
// synced checks the atomic that controls wallet syncness and if previously
// unsynced, updates to synced and notifies the callback, if set.
func (s *Syncer) synced() {
swapped := atomic.CompareAndSwapUint32(&s.atomicWalletSynced, 0, 1)
if swapped && s.cb != nil && s.cb.Synced != nil {
s.cb.Synced(true)
}
}
// unsynced checks the atomic that controls wallet syncness and if previously
// synced, updates to unsynced and notifies the callback, if set.
func (s *Syncer) unsynced() {
swapped := atomic.CompareAndSwapUint32(&s.atomicWalletSynced, 1, 0)
if swapped && s.cb != nil && s.cb.Synced != nil {
s.cb.Synced(false)
}
}
func (s *Syncer) fetchMissingCfiltersStart() {
if s.cb != nil && s.cb.FetchMissingCFiltersStarted != nil {
s.cb.FetchMissingCFiltersStarted()
}
}
func (s *Syncer) fetchMissingCfiltersProgress(startMissingCFilterHeight, endMissinCFilterHeight int32) {
if s.cb != nil && s.cb.FetchMissingCFiltersProgress != nil {
s.cb.FetchMissingCFiltersProgress(startMissingCFilterHeight, endMissinCFilterHeight)
}
}
func (s *Syncer) fetchMissingCfiltersFinished() {
if s.cb != nil && s.cb.FetchMissingCFiltersFinished != nil {
s.cb.FetchMissingCFiltersFinished()
}
}
func (s *Syncer) fetchHeadersStart() {
if s.cb != nil && s.cb.FetchHeadersStarted != nil {
s.cb.FetchHeadersStarted()
}
}
func (s *Syncer) fetchHeadersProgress(fetchedHeadersCount int32, lastHeaderTime int64) {
if s.cb != nil && s.cb.FetchHeadersProgress != nil {
s.cb.FetchHeadersProgress(fetchedHeadersCount, lastHeaderTime)
}
}
func (s *Syncer) fetchHeadersFinished() {
if s.cb != nil && s.cb.FetchHeadersFinished != nil {
s.cb.FetchHeadersFinished()
}
}
func (s *Syncer) discoverAddressesStart() {
if s.cb != nil && s.cb.DiscoverAddressesStarted != nil {
s.cb.DiscoverAddressesStarted()
}
}
func (s *Syncer) discoverAddressesFinished() {
if s.cb != nil && s.cb.DiscoverAddressesFinished != nil {
s.cb.DiscoverAddressesFinished()
}
}
func (s *Syncer) rescanStart() {
if s.cb != nil && s.cb.RescanStarted != nil {
s.cb.RescanStarted()
}
}
func (s *Syncer) rescanProgress(rescannedThrough int32) {
if s.cb != nil && s.cb.RescanProgress != nil {
s.cb.RescanProgress(rescannedThrough)
}
}
func (s *Syncer) rescanFinished() {
if s.cb != nil && s.cb.RescanFinished != nil {
s.cb.RescanFinished()
}
}
func normalizeAddress(addr string, defaultPort string) (hostport string, err error) {
host, port, origErr := net.SplitHostPort(addr)
if origErr == nil {
return net.JoinHostPort(host, port), nil
}
addr = net.JoinHostPort(addr, defaultPort)
_, _, err = net.SplitHostPort(addr)
if err != nil {
return "", origErr
}
return addr, nil
}
// hashStop is a zero value stop hash for fetching all possible data using
// locators.
var hashStop chainhash.Hash
// Run synchronizes the wallet, returning when synchronization fails or the
// context is cancelled. If startupSync is true, all synchronization tasks
// needed to fully register the wallet for notifications and synchronize it with
// the dcrd server are performed. Otherwise, it will listen for notifications
// but not register for any updates.
func (s *Syncer) Run(ctx context.Context) (err error) {
defer func() {
if err != nil {
const op errors.Op = "rpcsyncer.Run"
err = errors.E(op, err)
}
}()
params := s.wallet.ChainParams()
s.notifier = ¬ifier{
syncer: s,
ctx: ctx,
closed: make(chan struct{}),
}
addr, err := normalizeAddress(s.opts.Address, s.opts.DefaultPort)
if err != nil {
return errors.E(errors.Invalid, err)
}
if s.opts.Insecure {
addr = "ws://" + addr + "/ws"
} else {
addr = "wss://" + addr + "/ws"
}
opts := make([]wsrpc.Option, 0, 5)
opts = append(opts, wsrpc.WithBasicAuth(s.opts.User, s.opts.Pass))
opts = append(opts, wsrpc.WithNotifier(s.notifier))
opts = append(opts, wsrpc.WithoutPongDeadline())
if s.opts.Dial != nil {
opts = append(opts, wsrpc.WithDial(s.opts.Dial))
}
if len(s.opts.CA) != 0 && !s.opts.Insecure {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(s.opts.CA)
tc := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP256},
CipherSuites: []uint16{ // Only applies to TLS 1.2. TLS 1.3 ciphersuites are not configurable.
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
RootCAs: pool,
}
opts = append(opts, wsrpc.WithTLSConfig(tc))
}
client, err := wsrpc.Dial(ctx, addr, opts...)
if err != nil {
return err
}
defer client.Close()
s.rpc = dcrd.New(client)
// Verify that the server is running on the expected network.
var netID wire.CurrencyNet
err = s.rpc.Call(ctx, "getcurrentnet", &netID)
if err != nil {
return err
}
if netID != params.Net {
return errors.E("mismatched networks")
}
// Ensure the RPC server has a compatible API version.
var api struct {
Version semver `json:"dcrdjsonrpcapi"`
}
err = s.rpc.Call(ctx, "version", &api)
if err != nil {
return err
}
if !semverCompatible(requiredAPIVersion, api.Version) {
return errors.Errorf("advertised API version %v incompatible "+
"with required version %v", api.Version, requiredAPIVersion)
}
// Associate the RPC client with the wallet and remove the association on return.
s.wallet.SetNetworkBackend(s.rpc)
defer s.wallet.SetNetworkBackend(nil)
tipHash, tipHeight := s.wallet.MainChainTip(ctx)
rescanPoint, err := s.wallet.RescanPoint(ctx)
if err != nil {
return err
}
log.Infof("Headers synced through block %v height %d", &tipHash, tipHeight)
if rescanPoint != nil {
h, err := s.wallet.BlockHeader(ctx, rescanPoint)
if err != nil {
return err
}
// The rescan point is the first block that does not have synced
// transactions, so we are synced with the parent.
log.Infof("Transactions synced through block %v height %d", &h.PrevBlock, h.Height-1)
} else {
log.Infof("Transactions synced through block %v height %d", &tipHash, tipHeight)
}
err = s.rpc.Call(ctx, "notifyspentandmissedtickets", nil)
if err != nil {
return err
}
if s.wallet.VotingEnabled() {
err = s.rpc.Call(ctx, "notifywinningtickets", nil)
if err != nil {
return err
}
vb := s.wallet.VoteBits()
log.Infof("Wallet voting enabled: vote bits = %#04x, "+
"extended vote bits = %x", vb.Bits, vb.ExtendedBits)
log.Infof("Please ensure your wallet remains unlocked so it may vote")
}
// Fetch any missing main chain compact filters.
s.fetchMissingCfiltersStart()
progress := make(chan wallet.MissingCFilterProgress, 1)
go s.wallet.FetchMissingCFiltersWithProgress(ctx, s.rpc, progress)
for p := range progress {
if p.Err != nil {
return p.Err
}
s.fetchMissingCfiltersProgress(p.BlockHeightStart, p.BlockHeightEnd)
}
s.fetchMissingCfiltersFinished()
// Request notifications for connected and disconnected blocks.
err = s.rpc.Call(ctx, "notifyblocks", nil)
if err != nil {
return err
}
// Populate tspends.
tspends, err := s.rpc.GetMempoolTSpends(ctx)
if err != nil {
return err
}
for _, v := range tspends {
s.wallet.AddTSpend(*v)
}
log.Tracef("TSpends in mempool: %v", len(tspends))
// Request notifications for mempool tspennd arrivals.
err = s.rpc.Call(ctx, "notifytspend", nil)
if err != nil {
return err
}
// Fetch new headers and cfilters from the server.
locators, err := s.wallet.BlockLocators(ctx, nil)
if err != nil {
return err
}
cnet := s.wallet.ChainParams().Net
s.fetchHeadersStart()
for {
if err := ctx.Err(); err != nil {
return err
}
headers, err := s.rpc.Headers(ctx, locators, &hashStop)
if err != nil {
return err
}
if len(headers) == 0 {
break
}
nodes := make([]*wallet.BlockNode, len(headers))
var g errgroup.Group
for i := range headers {
i := i
g.Go(func() error {
header := headers[i]
hash := header.BlockHash()
filter, proofIndex, proof, err := s.rpc.CFilterV2(ctx, &hash)
if err != nil {
return err
}
err = validate.CFilterV2HeaderCommitment(cnet, header,
filter, proofIndex, proof)
if err != nil {
return err
}
nodes[i] = wallet.NewBlockNode(header, &hash, filter)
return nil
})
}
err = g.Wait()
if err != nil {
return err
}
var added int
for _, n := range nodes {
haveBlock, _, _ := s.wallet.BlockInMainChain(ctx, n.Hash)
if haveBlock {
continue
}
s.sidechainsMu.Lock()
if s.sidechains.AddBlockNode(n) {
added++
}
s.sidechainsMu.Unlock()
}
s.fetchHeadersProgress(int32(added), headers[len(headers)-1].Timestamp.Unix())
log.Infof("Fetched %d new header(s) ending at height %d from %s",
added, nodes[len(nodes)-1].Header.Height, client)
// Stop fetching headers when no new blocks are returned.
// Because getheaders did return located blocks, this indicates
// that the server is not as far synced as the wallet. Blocks
// the server has not processed are not reorged out of the
// wallet at this time, but a reorg will switch to a better
// chain later if one is discovered.
if added == 0 {
break
}
s.sidechainsMu.Lock()
bestChain, err := s.wallet.EvaluateBestChain(ctx, &s.sidechains)
s.sidechainsMu.Unlock()
if err != nil {
return err
}
if len(bestChain) == 0 {
continue
}
_, err = s.wallet.ValidateHeaderChainDifficulties(ctx, bestChain, 0)
if err != nil {
return err
}
s.sidechainsMu.Lock()
prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
s.sidechainsMu.Unlock()
if err != nil {
return err
}
if len(prevChain) != 0 {
log.Infof("Reorganize from %v to %v (total %d block(s) reorged)",
prevChain[len(prevChain)-1].Hash, bestChain[len(bestChain)-1].Hash, len(prevChain))
s.sidechainsMu.Lock()
for _, n := range prevChain {
s.sidechains.AddBlockNode(n)
}
s.sidechainsMu.Unlock()
}
tip := bestChain[len(bestChain)-1]
if len(bestChain) == 1 {
log.Infof("Connected block %v, height %d", tip.Hash, tip.Header.Height)
} else {
log.Infof("Connected %d blocks, new tip block %v, height %d, date %v",
len(bestChain), tip.Hash, tip.Header.Height, tip.Header.Timestamp)
}
locators, err = s.wallet.BlockLocators(ctx, nil)
if err != nil {
return err
}
}
s.fetchHeadersFinished()
rescanPoint, err = s.wallet.RescanPoint(ctx)
if err != nil {
return err
}
if rescanPoint != nil {
s.mu.Lock()
discoverAccts := s.discoverAccts
s.mu.Unlock()
s.discoverAddressesStart()
err = s.wallet.DiscoverActiveAddresses(ctx, s.rpc, rescanPoint, discoverAccts, s.wallet.GapLimit())
if err != nil {
return err
}
s.discoverAddressesFinished()
s.mu.Lock()
s.discoverAccts = false
s.mu.Unlock()
err = s.wallet.LoadActiveDataFilters(ctx, s.rpc, true)
if err != nil {
return err
}
s.rescanStart()
rescanBlock, err := s.wallet.BlockHeader(ctx, rescanPoint)
if err != nil {
return err
}
progress := make(chan wallet.RescanProgress, 1)
go s.wallet.RescanProgressFromHeight(ctx, s.rpc, int32(rescanBlock.Height), progress)
for p := range progress {
if p.Err != nil {
return p.Err
}
s.rescanProgress(p.ScannedThrough)
}
s.rescanFinished()
} else {
err = s.wallet.LoadActiveDataFilters(ctx, s.rpc, true)
if err != nil {
return err
}
}
s.synced()
// Rebroadcast unmined transactions
err = s.wallet.PublishUnminedTransactions(ctx, s.rpc)
if err != nil {
// Returning this error would end and (likely) restart sync in
// an endless loop. It's possible a transaction should be
// removed, but this is difficult to reliably detect over RPC.
log.Warnf("Could not publish one or more unmined transactions: %v", err)
}
err = s.rpc.Call(ctx, "rebroadcastwinners", nil)
if err != nil {
return err
}
err = s.rpc.Call(ctx, "rebroadcastmissed", nil)
if err != nil {
return err
}
log.Infof("Blockchain sync completed, wallet ready for general usage.")
// Wait for notifications to finish before returning
defer func() {
<-s.notifier.closed
}()
select {
case <-ctx.Done():
client.Close()
return ctx.Err()
case <-client.Done():
return client.Err()
}
}
type notifier struct {
atomicClosed uint32
syncer *Syncer
ctx context.Context
closed chan struct{}
connectingBlocks bool
}
func (n *notifier) Notify(method string, params json.RawMessage) error {
s := n.syncer
op := errors.Op(method)
ctx, task := trace.NewTask(n.ctx, method)
defer task.End()
switch method {
case "winningtickets":
err := s.winningTickets(ctx, params)
if err != nil {
log.Error(errors.E(op, err))
}
case "blockconnected":
err := s.blockConnected(ctx, params)
if err == nil {
n.connectingBlocks = true
return nil
}
err = errors.E(op, err)
if !n.connectingBlocks {
log.Errorf("Failed to connect block: %v", err)
return nil
}
return err
case "relevanttxaccepted":
err := s.relevantTxAccepted(ctx, params)
if err != nil {
log.Error(errors.E(op, err))
}
case "spentandmissedtickets":
err := s.spentAndMissedTickets(ctx, params)
if err != nil {
log.Error(errors.E(op, err))
}
case "tspend":
err := s.storeTSpend(ctx, params)
if err != nil {
log.Error(errors.E(op, err))
}
}
return nil
}
func (n *notifier) Close() error {
if atomic.CompareAndSwapUint32(&n.atomicClosed, 0, 1) {
close(n.closed)
}
return nil
}
func (s *Syncer) winningTickets(ctx context.Context, params json.RawMessage) error {
block, height, winners, err := dcrd.WinningTickets(params)
if err != nil {
return err
}
return s.wallet.VoteOnOwnedTickets(ctx, winners, block, height)
}
func (s *Syncer) blockConnected(ctx context.Context, params json.RawMessage) error {
header, relevant, err := dcrd.BlockConnected(params)
if err != nil {
return err
}
blockHash := header.BlockHash()
filter, proofIndex, proof, err := s.rpc.CFilterV2(ctx, &blockHash)
if err != nil {
return err
}
cnet := s.wallet.ChainParams().Net
err = validate.CFilterV2HeaderCommitment(cnet, header, filter, proofIndex, proof)
if err != nil {
return err
}
s.sidechainsMu.Lock()
defer s.sidechainsMu.Unlock()
blockNode := wallet.NewBlockNode(header, &blockHash, filter)
s.sidechains.AddBlockNode(blockNode)
s.relevantTxs[blockHash] = relevant
bestChain, err := s.wallet.EvaluateBestChain(ctx, &s.sidechains)
if err != nil {
return err
}
if len(bestChain) != 0 {
var prevChain []*wallet.BlockNode
prevChain, err = s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, s.relevantTxs)
if err != nil {
return err
}
if len(prevChain) != 0 {
log.Infof("Reorganize from %v to %v (total %d block(s) reorged)",
prevChain[len(prevChain)-1].Hash, bestChain[len(bestChain)-1].Hash, len(prevChain))
for _, n := range prevChain {
s.sidechains.AddBlockNode(n)
// TODO: should add txs from the removed blocks
// to relevantTxs. Later block connected logs
// will be missing the transaction counts if a
// reorg switches back to this older chain.
}
}
for _, n := range bestChain {
log.Infof("Connected block %v, height %d, %d wallet transaction(s)",
n.Hash, n.Header.Height, len(s.relevantTxs[*n.Hash]))
delete(s.relevantTxs, *n.Hash)
}
} else {
log.Infof("Observed sidechain or orphan block %v (height %d)", &blockHash, header.Height)
}
return nil
}
func (s *Syncer) relevantTxAccepted(ctx context.Context, params json.RawMessage) error {
tx, err := dcrd.RelevantTxAccepted(params)
if err != nil {
return err
}
if s.wallet.ManualTickets() && stake.IsSStx(tx) {
return nil
}
return s.wallet.AddTransaction(ctx, tx, nil)
}
func (s *Syncer) spentAndMissedTickets(ctx context.Context, params json.RawMessage) error {
missed, err := dcrd.MissedTickets(params)
if err != nil {
return err
}
return s.wallet.RevokeOwnedTickets(ctx, missed)
}
func (s *Syncer) storeTSpend(ctx context.Context, params json.RawMessage) error {
tx, err := dcrd.TSpend(params)
if err != nil {
return err
}
return s.wallet.AddTSpend(*tx)
}