forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxindex.go
94 lines (76 loc) · 2.51 KB
/
txindex.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
package dcrlibwallet
import (
w "decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/planetdecred/dcrlibwallet/walletdata"
)
func (wallet *Wallet) IndexTransactions() error {
ctx := wallet.shutdownContext()
var totalIndex int32
var txEndHeight uint32
rangeFn := func(block *w.Block) (bool, error) {
for _, transaction := range block.Transactions {
var blockHash *chainhash.Hash
if block.Header != nil {
hash := block.Header.BlockHash()
blockHash = &hash
} else {
blockHash = nil
}
tx, err := wallet.decodeTransactionWithTxSummary(&transaction, blockHash)
if err != nil {
return false, err
}
_, err = wallet.walletDataDB.SaveOrUpdate(&Transaction{}, tx)
if err != nil {
log.Errorf("[%d] Index tx replace tx err : %v", wallet.ID, err)
return false, err
}
totalIndex++
}
if block.Header != nil {
txEndHeight = block.Header.Height
err := wallet.walletDataDB.SaveLastIndexPoint(int32(txEndHeight))
if err != nil {
log.Errorf("[%d] Set tx index end block height error: ", wallet.ID, err)
return false, err
}
log.Debugf("[%d] Index saved for transactions in block %d", wallet.ID, txEndHeight)
}
select {
case <-ctx.Done():
return true, ctx.Err()
default:
return false, nil
}
}
beginHeight, err := wallet.walletDataDB.ReadIndexingStartBlock()
if err != nil {
log.Errorf("[%d] Get tx indexing start point error: %v", wallet.ID, err)
return err
}
endHeight := wallet.GetBestBlock()
startBlock := w.NewBlockIdentifierFromHeight(beginHeight)
endBlock := w.NewBlockIdentifierFromHeight(endHeight)
defer func() {
count, err := wallet.walletDataDB.Count(walletdata.TxFilterAll, wallet.RequiredConfirmations(), endHeight, &Transaction{})
if err != nil {
log.Errorf("[%d] Post-indexing tx count error :%v", wallet.ID, err)
} else if count > 0 {
log.Infof("[%d] Transaction index finished at %d, %d transaction(s) indexed in total", wallet.ID, endHeight, count)
}
err = wallet.walletDataDB.SaveLastIndexPoint(endHeight)
if err != nil {
log.Errorf("[%d] Set tx index end block height error: ", wallet.ID, err)
}
}()
log.Infof("[%d] Indexing transactions start height: %d, end height: %d", wallet.ID, beginHeight, endHeight)
return wallet.Internal().GetTransactions(ctx, rangeFn, startBlock, endBlock)
}
func (wallet *Wallet) reindexTransactions() error {
err := wallet.walletDataDB.ClearSavedTransactions(&Transaction{})
if err != nil {
return err
}
return wallet.IndexTransactions()
}