forked from decred/dcrwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescan.go
177 lines (158 loc) · 5.11 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
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
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"context"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrwallet/wallet/udb"
"github.com/decred/dcrwallet/walletdb"
)
const maxBlocksPerRescan = 2000
// RescannedBlock models the relevant data returned during a rescan from a
// single block.
type RescannedBlock struct {
BlockHash chainhash.Hash
Transactions [][]byte
}
// TODO: track whether a rescan is already in progress, and cancel either it or
// this new rescan, keeping the one that still has the most blocks to scan.
// rescan synchronously scans over all blocks on the main chain starting at
// startHash and height up through the recorded main chain tip block. The
// progress channel, if non-nil, is sent non-error progress notifications with
// the heights the rescan has completed through, starting with the start height.
func (w *Wallet) rescan(ctx context.Context, n NetworkBackend,
startHash *chainhash.Hash, height int32, p chan<- RescanProgress) error {
blockHashStorage := make([]chainhash.Hash, maxBlocksPerRescan)
rescanFrom := *startHash
inclusive := true
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
var rescanBlocks []chainhash.Hash
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
var err error
rescanBlocks, err = w.TxStore.GetMainChainBlockHashes(txmgrNs,
&rescanFrom, inclusive, blockHashStorage)
return err
})
if err != nil {
return err
}
if len(rescanBlocks) == 0 {
return nil
}
scanningThrough := height + int32(len(rescanBlocks)) - 1
log.Infof("Rescanning blocks %v-%v...", height,
scanningThrough)
rescanResults, err := n.Rescan(ctx, rescanBlocks)
if err != nil {
return err
}
var rawBlockHeader udb.RawBlockHeader
err = walletdb.Update(w.db, func(dbtx walletdb.ReadWriteTx) error {
txmgrNs := dbtx.ReadWriteBucket(wtxmgrNamespaceKey)
for _, r := range rescanResults {
blockMeta, err := w.TxStore.GetBlockMetaForHash(txmgrNs, &r.BlockHash)
if err != nil {
return err
}
serHeader, err := w.TxStore.GetSerializedBlockHeader(txmgrNs,
&r.BlockHash)
if err != nil {
return err
}
err = copyHeaderSliceToArray(&rawBlockHeader, serHeader)
if err != nil {
return err
}
for _, tx := range r.Transactions {
err = w.processSerializedTransaction(dbtx, tx,
&rawBlockHeader, &blockMeta)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
if p != nil {
p <- RescanProgress{ScannedThrough: scanningThrough}
}
rescanFrom = rescanBlocks[len(rescanBlocks)-1]
height += int32(len(rescanBlocks))
inclusive = false
}
}
// Rescan starts a rescan of the wallet for all blocks on the main chain
// beginning at startHash. This function blocks until the rescan completes.
func (w *Wallet) Rescan(ctx context.Context, n NetworkBackend, startHash *chainhash.Hash) error {
var startHeight int32
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
header, err := w.TxStore.GetSerializedBlockHeader(txmgrNs, startHash)
if err != nil {
return err
}
startHeight = udb.ExtractBlockHeaderHeight(header)
return nil
})
if err != nil {
return err
}
return w.rescan(ctx, n, startHash, startHeight, nil)
}
// RescanFromHeight is an alternative to Rescan that takes a block height
// instead of a hash. See Rescan for more details.
func (w *Wallet) RescanFromHeight(ctx context.Context, n NetworkBackend, startHeight int32) error {
var startHash chainhash.Hash
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
startHash, err = w.TxStore.GetMainChainBlockHashForHeight(
txmgrNs, startHeight)
return err
})
if err != nil {
return err
}
return w.rescan(ctx, n, &startHash, startHeight, nil)
}
// RescanProgress records the height the rescan has completed through and any
// errors during processing of the rescan.
type RescanProgress struct {
Err error
ScannedThrough int32
}
// RescanProgressFromHeight rescans for relevant transactions in all blocks in
// the main chain starting at startHeight. Progress notifications and any
// errors are sent to the channel p. This function blocks until the rescan
// completes or ends in an error. p is closed before returning.
func (w *Wallet) RescanProgressFromHeight(ctx context.Context, n NetworkBackend,
startHeight int32, p chan<- RescanProgress) {
defer close(p)
var startHash chainhash.Hash
err := walletdb.View(w.db, func(tx walletdb.ReadTx) error {
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
var err error
startHash, err = w.TxStore.GetMainChainBlockHashForHeight(
txmgrNs, startHeight)
return err
})
if err != nil {
p <- RescanProgress{Err: err}
return
}
err = w.rescan(ctx, n, &startHash, startHeight, p)
if err != nil {
p <- RescanProgress{Err: err}
}
}