forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescan.go
146 lines (117 loc) · 3.76 KB
/
rescan.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
package dcrlibwallet
import (
"context"
"math"
"time"
"decred.org/dcrwallet/v2/errors"
w "decred.org/dcrwallet/v2/wallet"
)
func (mw *MultiWallet) RescanBlocks(walletID int) error {
return mw.RescanBlocksFromHeight(walletID, 0)
}
func (mw *MultiWallet) RescanBlocksFromHeight(walletID int, startHeight int32) error {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.E(ErrNotExist)
}
netBackend, err := wallet.Internal().NetworkBackend()
if err != nil {
return errors.E(ErrNotConnected)
}
if mw.IsRescanning() || !mw.IsSynced() {
return errors.E(ErrInvalid)
}
go func() {
defer func() {
mw.syncData.mu.Lock()
mw.syncData.rescanning = false
mw.syncData.cancelRescan = nil
mw.syncData.mu.Unlock()
}()
ctx, cancel := wallet.shutdownContextWithCancel()
mw.syncData.mu.Lock()
mw.syncData.rescanning = true
mw.syncData.cancelRescan = cancel
mw.syncData.mu.Unlock()
if mw.blocksRescanProgressListener != nil {
mw.blocksRescanProgressListener.OnBlocksRescanStarted(walletID)
}
progress := make(chan w.RescanProgress, 1)
go wallet.Internal().RescanProgressFromHeight(ctx, netBackend, startHeight, progress)
rescanStartTime := time.Now().Unix()
for p := range progress {
if p.Err != nil {
log.Error(p.Err)
if mw.blocksRescanProgressListener != nil {
mw.blocksRescanProgressListener.OnBlocksRescanEnded(walletID, p.Err)
}
return
}
rescanProgressReport := &HeadersRescanProgressReport{
CurrentRescanHeight: p.ScannedThrough,
TotalHeadersToScan: wallet.GetBestBlock(),
WalletID: walletID,
}
elapsedRescanTime := time.Now().Unix() - rescanStartTime
rescanRate := float64(p.ScannedThrough) / float64(rescanProgressReport.TotalHeadersToScan)
rescanProgressReport.RescanProgress = int32(math.Round(rescanRate * 100))
estimatedTotalRescanTime := int64(math.Round(float64(elapsedRescanTime) / rescanRate))
rescanProgressReport.RescanTimeRemaining = estimatedTotalRescanTime - elapsedRescanTime
rescanProgressReport.GeneralSyncProgress = &GeneralSyncProgress{
TotalSyncProgress: rescanProgressReport.RescanProgress,
TotalTimeRemainingSeconds: rescanProgressReport.RescanTimeRemaining,
}
if mw.blocksRescanProgressListener != nil {
mw.blocksRescanProgressListener.OnBlocksRescanProgress(rescanProgressReport)
}
select {
case <-ctx.Done():
log.Info("Rescan canceled through context")
if mw.blocksRescanProgressListener != nil {
if ctx.Err() != nil && ctx.Err() != context.Canceled {
mw.blocksRescanProgressListener.OnBlocksRescanEnded(walletID, ctx.Err())
} else {
mw.blocksRescanProgressListener.OnBlocksRescanEnded(walletID, nil)
}
}
return
default:
continue
}
}
var err error
if startHeight == 0 {
err = wallet.reindexTransactions()
} else {
err = wallet.walletDataDB.SaveLastIndexPoint(startHeight)
if err != nil {
if mw.blocksRescanProgressListener != nil {
mw.blocksRescanProgressListener.OnBlocksRescanEnded(walletID, err)
}
return
}
err = wallet.IndexTransactions()
}
if mw.blocksRescanProgressListener != nil {
mw.blocksRescanProgressListener.OnBlocksRescanEnded(walletID, err)
}
}()
return nil
}
func (mw *MultiWallet) CancelRescan() {
mw.syncData.mu.Lock()
defer mw.syncData.mu.Unlock()
if mw.syncData.cancelRescan != nil {
mw.syncData.cancelRescan()
mw.syncData.cancelRescan = nil
log.Info("Rescan canceled.")
}
}
func (mw *MultiWallet) IsRescanning() bool {
mw.syncData.mu.RLock()
defer mw.syncData.mu.RUnlock()
return mw.syncData.rescanning
}
func (mw *MultiWallet) SetBlocksRescanProgressListener(blocksRescanProgressListener BlocksRescanProgressListener) {
mw.blocksRescanProgressListener = blocksRescanProgressListener
}