-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync.go
187 lines (167 loc) · 5.57 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
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
package dcrpg
import (
"database/sql"
"fmt"
"strings"
"sync"
"time"
"github.com/dcrdata/dcrdata/db/dbtypes"
"github.com/dcrdata/dcrdata/rpcutils"
"github.com/decred/dcrd/rpcclient"
)
const (
rescanLogBlockChunk = 250
)
// SyncChainDBAsync is like SyncChainDB except it also takes a result channel on
// which the caller should wait to receive the result. As such, this method
// should be called as a goroutine or it will hang on send if the channel is
// unbuffered.
func (db *ChainDB) SyncChainDBAsync(res chan dbtypes.SyncResult,
client *rpcclient.Client, quit chan struct{}, updateAllAddresses, newIndexes bool) {
if db == nil {
res <- dbtypes.SyncResult{
Height: -1,
Error: fmt.Errorf("ChainDB (psql) disabled"),
}
return
}
height, err := db.SyncChainDB(client, quit, newIndexes, updateAllAddresses)
res <- dbtypes.SyncResult{
Height: height,
Error: err,
}
}
// SyncChainDB stores in the DB all blocks on the main chain available from the
// RPC client. The table indexes may be force-dropped and recreated by setting
// newIndexes to true. The quit channel is used to break the sync loop. For
// example, closing the channel on SIGINT.
func (db *ChainDB) SyncChainDB(client *rpcclient.Client, quit chan struct{},
updateAllAddresses, newIndexes bool) (int64, error) {
// Get chain servers's best block
_, nodeHeight, err := client.GetBestBlock()
if err != nil {
return -1, fmt.Errorf("GetBestBlock failed: %v", err)
}
// Total and rate statistics
var totalTxs, totalVins, totalVouts int64
var lastTxs, lastVins, lastVouts int64
tickTime := 20 * time.Second
ticker := time.NewTicker(tickTime)
startTime := time.Now()
o := sync.Once{}
speedReporter := func() {
ticker.Stop()
totalElapsed := time.Since(startTime).Seconds()
if int64(totalElapsed) == 0 {
return
}
totalVoutPerSec := totalVouts / int64(totalElapsed)
totalTxPerSec := totalTxs / int64(totalElapsed)
log.Infof("Avg. speed: %d tx/s, %d vout/s", totalTxPerSec, totalVoutPerSec)
}
speedReport := func() { o.Do(speedReporter) }
defer speedReport()
startingHeight, err := db.HeightDB()
lastBlock := int64(startingHeight)
if err != nil {
if err == sql.ErrNoRows {
lastBlock = -1
log.Info("blocks table is empty, starting fresh.")
} else {
return -1, fmt.Errorf("RetrieveBestBlockHeight: %v", err)
}
}
// Remove indexes/constraints before bulk import
blocksToSync := nodeHeight - lastBlock
reindexing := newIndexes || blocksToSync > nodeHeight/2
if reindexing {
log.Info("Large bulk load: Removing indexes and disabling duplicate checks.")
err = db.DeindexAll()
if err != nil && !strings.Contains(err.Error(), "does not exist") {
return lastBlock, err
}
db.EnableDuplicateCheckOnInsert(false)
} else {
db.EnableDuplicateCheckOnInsert(true)
}
// Start rebuilding
startHeight := lastBlock + 1
for ib := startHeight; ib <= nodeHeight; ib++ {
// check for quit signal
select {
case <-quit:
log.Infof("Rescan cancelled at height %d.", ib)
return ib - 1, nil
default:
}
if (ib-1)%rescanLogBlockChunk == 0 || ib == startHeight {
if ib == 0 {
log.Infof("Scanning genesis block.")
} else {
endRangeBlock := rescanLogBlockChunk * (1 + (ib-1)/rescanLogBlockChunk)
if endRangeBlock > nodeHeight {
endRangeBlock = nodeHeight
}
log.Infof("Processing blocks %d to %d...", ib, endRangeBlock)
}
}
select {
case <-ticker.C:
blocksPerSec := float64(ib-lastBlock) / tickTime.Seconds()
txPerSec := float64(totalTxs-lastTxs) / tickTime.Seconds()
vinsPerSec := float64(totalVins-lastVins) / tickTime.Seconds()
voutPerSec := float64(totalVouts-lastVouts) / tickTime.Seconds()
log.Infof("(%3d blk/s,%5d tx/s,%5d vin/sec,%5d vout/s)", int64(blocksPerSec),
int64(txPerSec), int64(vinsPerSec), int64(voutPerSec))
lastBlock, lastTxs = ib, totalTxs
lastVins, lastVouts = totalVins, totalVouts
default:
}
block, blockHash, err := rpcutils.GetBlock(ib, client)
if err != nil {
return ib - 1, fmt.Errorf("GetBlock failed (%s): %v", blockHash, err)
}
var numVins, numVouts int64
if numVins, numVouts, err = db.StoreBlock(block.MsgBlock(), true, !updateAllAddresses); err != nil {
return ib - 1, fmt.Errorf("StoreBlock failed: %v", err)
}
totalVins += numVins
totalVouts += numVouts
numSTx := int64(len(block.STransactions()))
numRTx := int64(len(block.Transactions()))
totalTxs += numRTx + numSTx
// totalRTxs += numRTx
// totalSTxs += numSTx
// update height, the end condition for the loop
if _, nodeHeight, err = client.GetBestBlock(); err != nil {
return ib, fmt.Errorf("GetBestBlock failed: %v", err)
}
}
speedReport()
if reindexing || newIndexes {
if err = db.IndexAll(); err != nil {
return nodeHeight, fmt.Errorf("IndexAll failed: %v", err)
}
if !updateAllAddresses {
err = db.IndexAddressTable()
}
}
if updateAllAddresses {
// Remove existing indexes not on funding txns
_ = db.DeindexAddressTable() // ignore errors for non-existent indexes
log.Infof("Populating spending tx info in address table...")
numAddresses, err := db.UpdateSpendingInfoInAllAddresses()
if err != nil {
log.Errorf("UpdateSpendingInfoInAllAddresses FAILED: %v", err)
}
log.Infof("Updated %d rows of address table", numAddresses)
if err = db.IndexAddressTable(); err != nil {
log.Errorf("IndexAddressTable FAILED: %v", err)
}
}
log.Infof("Sync finished at height %d. Delta: %d blocks, %d transactions, %d ins, %d outs",
nodeHeight, nodeHeight-startHeight+1, totalTxs, totalVins, totalVouts)
return nodeHeight, err
}