forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
493 lines (404 loc) · 13.3 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
package dcrlibwallet
import (
"context"
"encoding/json"
"fmt"
"net"
"sort"
"strings"
"sync"
"decred.org/dcrwallet/v2/errors"
"decred.org/dcrwallet/v2/p2p"
w "decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/addrmgr/v2"
"github.com/planetdecred/dcrlibwallet/spv"
)
// reading/writing of properties of this struct are protected by mutex.x
type syncData struct {
mu sync.RWMutex
syncProgressListeners map[string]SyncProgressListener
showLogs bool
synced bool
syncing bool
cancelSync context.CancelFunc
cancelRescan context.CancelFunc
syncCanceled chan struct{}
// Flag to notify syncCanceled callback if the sync was canceled so as to be restarted.
restartSyncRequested bool
rescanning bool
connectedPeers int32
*activeSyncData
}
// reading/writing of properties of this struct are protected by syncData.mu.
type activeSyncData struct {
syncer *spv.Syncer
syncStage int32
cfiltersFetchProgress CFiltersFetchProgressReport
headersFetchProgress HeadersFetchProgressReport
addressDiscoveryProgress AddressDiscoveryProgressReport
headersRescanProgress HeadersRescanProgressReport
addressDiscoveryCompletedOrCanceled chan bool
rescanStartTime int64
totalInactiveSeconds int64
}
const (
InvalidSyncStage = -1
CFiltersFetchSyncStage = 0
HeadersFetchSyncStage = 1
AddressDiscoverySyncStage = 2
HeadersRescanSyncStage = 3
)
func (mw *MultiWallet) initActiveSyncData() {
cfiltersFetchProgress := CFiltersFetchProgressReport{
GeneralSyncProgress: &GeneralSyncProgress{},
beginFetchCFiltersTimeStamp: 0,
startCFiltersHeight: -1,
cfiltersFetchTimeSpent: 0,
totalFetchedCFiltersCount: 0,
}
headersFetchProgress := HeadersFetchProgressReport{
GeneralSyncProgress: &GeneralSyncProgress{},
beginFetchTimeStamp: -1,
headersFetchTimeSpent: -1,
totalFetchedHeadersCount: 0,
}
addressDiscoveryProgress := AddressDiscoveryProgressReport{
GeneralSyncProgress: &GeneralSyncProgress{},
addressDiscoveryStartTime: -1,
totalDiscoveryTimeSpent: -1,
}
headersRescanProgress := HeadersRescanProgressReport{}
headersRescanProgress.GeneralSyncProgress = &GeneralSyncProgress{}
mw.syncData.mu.Lock()
mw.syncData.activeSyncData = &activeSyncData{
syncStage: InvalidSyncStage,
cfiltersFetchProgress: cfiltersFetchProgress,
headersFetchProgress: headersFetchProgress,
addressDiscoveryProgress: addressDiscoveryProgress,
headersRescanProgress: headersRescanProgress,
}
mw.syncData.mu.Unlock()
}
func (mw *MultiWallet) IsSyncProgressListenerRegisteredFor(uniqueIdentifier string) bool {
mw.syncData.mu.RLock()
_, exists := mw.syncData.syncProgressListeners[uniqueIdentifier]
mw.syncData.mu.RUnlock()
return exists
}
func (mw *MultiWallet) AddSyncProgressListener(syncProgressListener SyncProgressListener, uniqueIdentifier string) error {
if mw.IsSyncProgressListenerRegisteredFor(uniqueIdentifier) {
return errors.New(ErrListenerAlreadyExist)
}
mw.syncData.mu.Lock()
mw.syncData.syncProgressListeners[uniqueIdentifier] = syncProgressListener
mw.syncData.mu.Unlock()
// If sync is already on, notify this newly added listener of the current progress report.
return mw.PublishLastSyncProgress(uniqueIdentifier)
}
func (mw *MultiWallet) RemoveSyncProgressListener(uniqueIdentifier string) {
mw.syncData.mu.Lock()
delete(mw.syncData.syncProgressListeners, uniqueIdentifier)
mw.syncData.mu.Unlock()
}
func (mw *MultiWallet) syncProgressListeners() []SyncProgressListener {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
listeners := make([]SyncProgressListener, 0, len(mw.syncData.syncProgressListeners))
for _, listener := range mw.syncData.syncProgressListeners {
listeners = append(listeners, listener)
}
return listeners
}
func (mw *MultiWallet) PublishLastSyncProgress(uniqueIdentifier string) error {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
syncProgressListener, exists := mw.syncData.syncProgressListeners[uniqueIdentifier]
if !exists {
return errors.New(ErrInvalid)
}
if mw.syncData.syncing && mw.syncData.activeSyncData != nil {
switch mw.syncData.activeSyncData.syncStage {
case HeadersFetchSyncStage:
syncProgressListener.OnHeadersFetchProgress(&mw.syncData.headersFetchProgress)
case AddressDiscoverySyncStage:
syncProgressListener.OnAddressDiscoveryProgress(&mw.syncData.addressDiscoveryProgress)
case HeadersRescanSyncStage:
syncProgressListener.OnHeadersRescanProgress(&mw.syncData.headersRescanProgress)
}
}
return nil
}
func (mw *MultiWallet) EnableSyncLogs() {
mw.syncData.mu.Lock()
mw.syncData.showLogs = true
mw.syncData.mu.Unlock()
}
func (mw *MultiWallet) SyncInactiveForPeriod(totalInactiveSeconds int64) {
mw.syncData.mu.Lock()
defer mw.syncData.mu.Unlock()
if !mw.syncData.syncing || mw.syncData.activeSyncData == nil {
log.Debug("Not accounting for inactive time, wallet is not syncing.")
return
}
mw.syncData.totalInactiveSeconds += totalInactiveSeconds
if mw.syncData.connectedPeers == 0 {
// assume it would take another 60 seconds to reconnect to peers
mw.syncData.totalInactiveSeconds += 60
}
}
func (mw *MultiWallet) SpvSync() error {
// prevent an attempt to sync when the previous syncing has not been canceled
if mw.IsSyncing() || mw.IsSynced() {
return errors.New(ErrSyncAlreadyInProgress)
}
addr := &net.TCPAddr{IP: net.ParseIP("::1"), Port: 0}
addrManager := addrmgr.New(mw.rootDir, net.LookupIP) // TODO: be mindful of tor
lp := p2p.NewLocalPeer(mw.chainParams, addr, addrManager)
var validPeerAddresses []string
peerAddresses := mw.ReadStringConfigValueForKey(SpvPersistentPeerAddressesConfigKey)
if peerAddresses != "" {
addresses := strings.Split(peerAddresses, ";")
for _, address := range addresses {
peerAddress, err := NormalizeAddress(address, mw.chainParams.DefaultPort)
if err != nil {
log.Errorf("SPV peer address(%s) is invalid: %v", peerAddress, err)
} else {
validPeerAddresses = append(validPeerAddresses, peerAddress)
}
}
if len(validPeerAddresses) == 0 {
return errors.New(ErrInvalidPeers)
}
}
// init activeSyncData to be used to hold data used
// to calculate sync estimates only during sync
mw.initActiveSyncData()
wallets := make(map[int]*w.Wallet)
for id, wallet := range mw.wallets {
wallets[id] = wallet.Internal()
wallet.waitingForHeaders = true
wallet.syncing = true
}
syncer := spv.NewSyncer(wallets, lp)
syncer.SetNotifications(mw.spvSyncNotificationCallbacks())
if len(validPeerAddresses) > 0 {
syncer.SetPersistentPeers(validPeerAddresses)
}
ctx, cancel := mw.contextWithShutdownCancel()
var restartSyncRequested bool
mw.syncData.mu.Lock()
restartSyncRequested = mw.syncData.restartSyncRequested
mw.syncData.restartSyncRequested = false
mw.syncData.syncing = true
mw.syncData.cancelSync = cancel
mw.syncData.syncCanceled = make(chan struct{})
mw.syncData.syncer = syncer
mw.syncData.mu.Unlock()
for _, listener := range mw.syncProgressListeners() {
listener.OnSyncStarted(restartSyncRequested)
}
// syncer.Run uses a wait group to block the thread until the sync context
// expires or is canceled or some other error occurs such as
// losing connection to all persistent peers.
go func() {
syncError := syncer.Run(ctx)
//sync has ended or errored
if syncError != nil {
if syncError == context.DeadlineExceeded {
mw.notifySyncError(errors.Errorf("SPV synchronization deadline exceeded: %v", syncError))
} else if syncError == context.Canceled {
close(mw.syncData.syncCanceled)
mw.notifySyncCanceled()
} else {
mw.notifySyncError(syncError)
}
}
//reset sync variables
mw.resetSyncData()
}()
return nil
}
func (mw *MultiWallet) RestartSpvSync() error {
mw.syncData.mu.Lock()
mw.syncData.restartSyncRequested = true
mw.syncData.mu.Unlock()
mw.CancelSync() // necessary to unset the network backend.
return mw.SpvSync()
}
func (mw *MultiWallet) CancelSync() {
mw.syncData.mu.RLock()
cancelSync := mw.syncData.cancelSync
mw.syncData.mu.RUnlock()
if cancelSync != nil {
log.Info("Canceling sync. May take a while for sync to fully cancel.")
// Stop running cspp mixers
for _, wallet := range mw.wallets {
if wallet.IsAccountMixerActive() {
log.Infof("[%d] Stopping cspp mixer", wallet.ID)
err := mw.StopAccountMixer(wallet.ID)
if err != nil {
log.Errorf("[%d] Error stopping cspp mixer: %v", wallet.ID, err)
}
}
}
// Cancel the context used for syncer.Run in spvSync().
// This may not immediately cause the sync process to terminate,
// but when it eventually terminates, syncer.Run will return `err == context.Canceled`.
cancelSync()
// When sync terminates and syncer.Run returns `err == context.Canceled`,
// we will get notified on this channel.
<-mw.syncData.syncCanceled
log.Info("Sync fully canceled.")
}
}
func (wallet *Wallet) IsWaiting() bool {
return wallet.waitingForHeaders
}
func (wallet *Wallet) IsSynced() bool {
return wallet.synced
}
func (wallet *Wallet) IsSyncing() bool {
return wallet.syncing
}
func (mw *MultiWallet) IsConnectedToDecredNetwork() bool {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
return mw.syncData.syncing || mw.syncData.synced
}
func (mw *MultiWallet) IsSynced() bool {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
return mw.syncData.synced
}
func (mw *MultiWallet) IsSyncing() bool {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
return mw.syncData.syncing
}
func (mw *MultiWallet) CurrentSyncStage() int32 {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
if mw.syncData != nil && mw.syncData.syncing {
return mw.syncData.syncStage
}
return InvalidSyncStage
}
func (mw *MultiWallet) GeneralSyncProgress() *GeneralSyncProgress {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
if mw.syncData != nil && mw.syncData.syncing {
switch mw.syncData.syncStage {
case HeadersFetchSyncStage:
return mw.syncData.headersFetchProgress.GeneralSyncProgress
case AddressDiscoverySyncStage:
return mw.syncData.addressDiscoveryProgress.GeneralSyncProgress
case HeadersRescanSyncStage:
return mw.syncData.headersRescanProgress.GeneralSyncProgress
case CFiltersFetchSyncStage:
return mw.syncData.cfiltersFetchProgress.GeneralSyncProgress
}
}
return nil
}
func (mw *MultiWallet) ConnectedPeers() int32 {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
return mw.syncData.connectedPeers
}
func (mw *MultiWallet) PeerInfoRaw() ([]PeerInfo, error) {
if !mw.IsConnectedToDecredNetwork() {
return nil, errors.New(ErrNotConnected)
}
syncer := mw.syncData.syncer
infos := make([]PeerInfo, 0, len(syncer.GetRemotePeers()))
for _, rp := range syncer.GetRemotePeers() {
info := PeerInfo{
ID: int32(rp.ID()),
Addr: rp.RemoteAddr().String(),
AddrLocal: rp.LocalAddr().String(),
Services: fmt.Sprintf("%08d", uint64(rp.Services())),
Version: rp.Pver(),
SubVer: rp.UA(),
StartingHeight: int64(rp.InitialHeight()),
BanScore: int32(rp.BanScore()),
}
infos = append(infos, info)
}
sort.Slice(infos, func(i, j int) bool {
return infos[i].ID < infos[j].ID
})
return infos, nil
}
func (mw *MultiWallet) PeerInfo() (string, error) {
infos, err := mw.PeerInfoRaw()
if err != nil {
return "", err
}
result, _ := json.Marshal(infos)
return string(result), nil
}
func (mw *MultiWallet) GetBestBlock() *BlockInfo {
var bestBlock int32 = -1
var blockInfo *BlockInfo
for _, wallet := range mw.wallets {
if !wallet.WalletOpened() {
continue
}
walletBestBLock := wallet.GetBestBlock()
if walletBestBLock > bestBlock || bestBlock == -1 {
bestBlock = walletBestBLock
blockInfo = &BlockInfo{Height: bestBlock, Timestamp: wallet.GetBestBlockTimeStamp()}
}
}
return blockInfo
}
func (mw *MultiWallet) GetLowestBlock() *BlockInfo {
var lowestBlock int32 = -1
var blockInfo *BlockInfo
for _, wallet := range mw.wallets {
if !wallet.WalletOpened() {
continue
}
walletBestBLock := wallet.GetBestBlock()
if walletBestBLock < lowestBlock || lowestBlock == -1 {
lowestBlock = walletBestBLock
blockInfo = &BlockInfo{Height: lowestBlock, Timestamp: wallet.GetBestBlockTimeStamp()}
}
}
return blockInfo
}
func (wallet *Wallet) GetBestBlock() int32 {
if wallet.Internal() == nil {
// This method is sometimes called after a wallet is deleted and causes crash.
log.Error("Attempting to read best block height without a loaded wallet.")
return 0
}
_, height := wallet.Internal().MainChainTip(wallet.shutdownContext())
return height
}
func (wallet *Wallet) GetBestBlockTimeStamp() int64 {
if wallet.Internal() == nil {
// This method is sometimes called after a wallet is deleted and causes crash.
log.Error("Attempting to read best block timestamp without a loaded wallet.")
return 0
}
ctx := wallet.shutdownContext()
_, height := wallet.Internal().MainChainTip(ctx)
identifier := w.NewBlockIdentifierFromHeight(height)
info, err := wallet.Internal().BlockInfo(ctx, identifier)
if err != nil {
log.Error(err)
return 0
}
return info.Timestamp
}
func (mw *MultiWallet) GetLowestBlockTimestamp() int64 {
var timestamp int64 = -1
for _, wallet := range mw.wallets {
bestBlockTimestamp := wallet.GetBestBlockTimeStamp()
if bestBlockTimestamp < timestamp || timestamp == -1 {
timestamp = bestBlockTimestamp
}
}
return timestamp
}