/
rescan.go
397 lines (357 loc) · 11.8 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
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
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2020 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"
"time"
"github.com/EXCCoin/exccwallet/v2/errors"
"github.com/EXCCoin/exccwallet/v2/wallet/udb"
"github.com/EXCCoin/exccwallet/v2/wallet/walletdb"
"github.com/EXCCoin/exccd/chaincfg/chainhash"
"github.com/EXCCoin/exccd/crypto/ripemd160"
"github.com/EXCCoin/exccd/txscript/v4/stdaddr"
"github.com/EXCCoin/exccd/wire"
)
const maxBlocksPerRescan = 2000
// RescanFilter implements a precise filter intended to hold all watched wallet
// data in memory such as addresses and unspent outputs. The zero value is not
// valid, and filters must be created using NewRescanFilter. RescanFilter is
// not safe for concurrent access.
type RescanFilter struct {
// Implemented fast paths for address lookup.
pubKeyHashes map[[ripemd160.Size]byte]struct{}
scriptHashes map[[ripemd160.Size]byte]struct{}
compressedPubKeys map[[33]byte]struct{}
// A fallback address lookup map in case a fast path doesn't exist.
// Only exists for completeness. If using this shows up in a profile,
// there's a good chance a fast path should be added.
otherAddresses map[string]struct{}
// Outpoints of unspent outputs.
unspent map[wire.OutPoint]struct{}
}
// NewRescanFilter creates and initializes a RescanFilter containing each passed
// address and outpoint.
func NewRescanFilter(addresses []stdaddr.Address, unspentOutPoints []*wire.OutPoint) *RescanFilter {
filter := &RescanFilter{
pubKeyHashes: map[[ripemd160.Size]byte]struct{}{},
scriptHashes: map[[ripemd160.Size]byte]struct{}{},
compressedPubKeys: map[[33]byte]struct{}{},
otherAddresses: map[string]struct{}{},
unspent: make(map[wire.OutPoint]struct{}, len(unspentOutPoints)),
}
for _, s := range addresses {
filter.AddAddress(s)
}
for _, op := range unspentOutPoints {
filter.AddUnspentOutPoint(op)
}
return filter
}
// AddAddress adds an address to the filter if it does not already exist.
func (f *RescanFilter) AddAddress(a stdaddr.Address) {
switch a := a.(type) {
case *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0:
f.pubKeyHashes[*a.Hash160()] = struct{}{}
case *stdaddr.AddressScriptHashV0:
f.scriptHashes[*a.Hash160()] = struct{}{}
case *stdaddr.AddressPubKeyEcdsaSecp256k1V0:
serializedPubKey := a.SerializedPubKey()
switch len(serializedPubKey) {
case 33: // compressed
var compressedPubKey [33]byte
copy(compressedPubKey[:], serializedPubKey)
f.compressedPubKeys[compressedPubKey] = struct{}{}
default:
f.otherAddresses[a.String()] = struct{}{}
}
default:
f.otherAddresses[a.String()] = struct{}{}
}
}
// ExistsAddress returns whether an address is contained in the filter.
func (f *RescanFilter) ExistsAddress(a stdaddr.Address) (ok bool) {
switch a := a.(type) {
case *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0:
_, ok = f.pubKeyHashes[*a.Hash160()]
case *stdaddr.AddressScriptHashV0:
_, ok = f.scriptHashes[*a.Hash160()]
case *stdaddr.AddressPubKeyEcdsaSecp256k1V0:
serializedPubKey := a.SerializedPubKey()
switch len(serializedPubKey) {
case 33: // compressed
var compressedPubKey [33]byte
copy(compressedPubKey[:], serializedPubKey)
_, ok = f.compressedPubKeys[compressedPubKey]
if !ok {
a := a.AddressPubKeyHash().(*stdaddr.AddressPubKeyHashEcdsaSecp256k1V0)
_, ok = f.pubKeyHashes[*a.Hash160()]
}
default:
_, ok = f.otherAddresses[a.String()]
}
default:
_, ok = f.otherAddresses[a.String()]
}
return
}
// RemoveAddress removes an address from the filter if it exists.
func (f *RescanFilter) RemoveAddress(a stdaddr.Address) {
switch a := a.(type) {
case *stdaddr.AddressPubKeyHashEcdsaSecp256k1V0:
delete(f.pubKeyHashes, *a.Hash160())
case *stdaddr.AddressScriptHashV0:
delete(f.scriptHashes, *a.Hash160())
case *stdaddr.AddressPubKeyEcdsaSecp256k1V0:
serializedPubKey := a.SerializedPubKey()
switch len(serializedPubKey) {
case 33: // compressed
var compressedPubKey [33]byte
copy(compressedPubKey[:], serializedPubKey)
delete(f.compressedPubKeys, compressedPubKey)
default:
delete(f.otherAddresses, a.String())
}
default:
delete(f.otherAddresses, a.String())
}
}
// AddUnspentOutPoint adds an outpoint to the filter if it does not already
// exist.
func (f *RescanFilter) AddUnspentOutPoint(op *wire.OutPoint) {
f.unspent[*op] = struct{}{}
}
// ExistsUnspentOutPoint returns whether an outpoint is contained in the filter.
func (f *RescanFilter) ExistsUnspentOutPoint(op *wire.OutPoint) bool {
_, ok := f.unspent[*op]
return ok
}
// RemoveUnspentOutPoint removes an outpoint from the filter if it exists.
func (f *RescanFilter) RemoveUnspentOutPoint(op *wire.OutPoint) {
delete(f.unspent, *op)
}
// SaveRescanned records transactions from a rescanned block. This
// does not update the network backend with data to watch for future
// relevant transactions as the rescanner is assumed to handle this
// task.
func (w *Wallet) SaveRescanned(ctx context.Context, hash *chainhash.Hash, txs []*wire.MsgTx) error {
const op errors.Op = "wallet.SaveRescanned"
defer w.lockedOutpointMu.Unlock()
w.lockedOutpointMu.Lock()
err := walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
txmgrNs := dbtx.ReadWriteBucket(wtxmgrNamespaceKey)
blockMeta, err := w.txStore.GetBlockMetaForHash(txmgrNs, hash)
if err != nil {
return err
}
header, err := w.txStore.GetBlockHeader(dbtx, hash)
if err != nil {
return err
}
for _, tx := range txs {
rec, err := udb.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil {
return err
}
_, err = w.processTransactionRecord(context.Background(), dbtx, rec, header, &blockMeta)
if err != nil {
return err
}
}
return w.txStore.UpdateProcessedTxsBlockMarker(dbtx, hash)
})
if err != nil {
return errors.E(op, err)
}
return nil
}
// 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(ctx, 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 {
break
}
through := height + int32(len(rescanBlocks)) - 1
// Genesis block is not rescanned
if height == 0 {
rescanBlocks = rescanBlocks[1:]
height = 1
if len(rescanBlocks) == 0 {
break
}
}
log.Infof("Rescanning block range [%v, %v]...", height, through)
saveRescanned := func(block *chainhash.Hash, txs []*wire.MsgTx) error {
return w.SaveRescanned(ctx, block, txs)
}
err = n.Rescan(ctx, rescanBlocks, saveRescanned)
if err != nil {
return err
}
err = walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
return w.txStore.UpdateProcessedTxsBlockMarker(dbtx, &rescanBlocks[len(rescanBlocks)-1])
})
if err != nil {
return err
}
if p != nil {
p <- RescanProgress{ScannedThrough: through}
}
rescanFrom = rescanBlocks[len(rescanBlocks)-1]
height += int32(len(rescanBlocks))
inclusive = false
}
log.Infof("Rescan complete")
return nil
}
// 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 {
const op errors.Op = "wallet.Rescan"
var startHeight int32
err := walletdb.View(ctx, 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 errors.E(op, err)
}
err = w.rescan(ctx, n, startHash, startHeight, nil)
if err != nil {
return errors.E(op, err)
}
return 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 {
const op errors.Op = "wallet.RescanFromHeight"
var startHash chainhash.Hash
err := walletdb.View(ctx, 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 errors.E(op, err)
}
err = w.rescan(ctx, n, &startHash, startHeight, nil)
if err != nil {
return errors.E(op, err)
}
return 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) {
const op errors.Op = "wallet.RescanProgressFromHeight"
defer close(p)
var startHash chainhash.Hash
err := walletdb.View(ctx, 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: errors.E(op, err)}
return
}
err = w.rescan(ctx, n, &startHash, startHeight, p)
if err != nil {
p <- RescanProgress{Err: errors.E(op, err)}
}
}
func (w *Wallet) mainChainAncestor(dbtx walletdb.ReadTx, hash *chainhash.Hash) (*chainhash.Hash, error) {
for {
mainChain, _ := w.txStore.BlockInMainChain(dbtx, hash)
if mainChain {
break
}
h, err := w.txStore.GetBlockHeader(dbtx, hash)
if err != nil {
return nil, err
}
hash = &h.PrevBlock
}
return hash, nil
}
// RescanPoint returns the block hash at which a rescan should begin
// (inclusive), or nil when no rescan is necessary.
func (w *Wallet) RescanPoint(ctx context.Context) (*chainhash.Hash, error) {
const op errors.Op = "wallet.RescanPoint"
var rp *chainhash.Hash
err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
var err error
rp, err = w.rescanPoint(dbtx)
return err
})
if err != nil {
return nil, errors.E(op, err)
}
return rp, nil
}
func (w *Wallet) rescanPoint(dbtx walletdb.ReadTx) (*chainhash.Hash, error) {
ns := dbtx.ReadBucket(wtxmgrNamespaceKey)
r := w.txStore.ProcessedTxsBlockMarker(dbtx)
r, err := w.mainChainAncestor(dbtx, r) // Walk back to the main chain ancestor
if err != nil {
return nil, err
}
if tipHash, _ := w.txStore.MainChainTip(dbtx); *r == tipHash {
return nil, nil
}
// r is not the tip, so a child block must exist in the main chain.
h, err := w.txStore.GetBlockHeader(dbtx, r)
if err != nil {
log.Info(err)
return nil, err
}
rescanPoint, err := w.txStore.GetMainChainBlockHashForHeight(ns, int32(h.Height)+1)
if err != nil {
log.Info(err)
return nil, err
}
return &rescanPoint, nil
}