-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
301 lines (261 loc) · 9.12 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
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
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package dcrsqlite
import (
"fmt"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
apitypes "github.com/decred/dcrdata/api/types"
"github.com/decred/dcrdata/rpcutils"
"github.com/decred/dcrdata/txhelpers"
)
const (
rescanLogBlockChunk = 1000
)
// DBHeights returns the best block heights of: SQLite database tables (block
// summary and stake info tables), the stake database (ffldb_stake), and the
// lowest of these. An error value is returned if any database is inaccessible.
func (db *wiredDB) DBHeights() (lowest int64, summaryHeight int64, stakeInfoHeight int64,
stakeDatabaseHeight int64, err error) {
// Get DB's best block (for block summary and stake info tables)
if summaryHeight, err = db.GetBlockSummaryHeight(); err != nil {
return 0, 0, 0, -1, fmt.Errorf("GetBlockSummaryHeight failed: %v", err)
}
if stakeInfoHeight, err = db.GetStakeInfoHeight(); err != nil {
return 0, 0, 0, -1, fmt.Errorf("GetStakeInfoHeight failed: %v", err)
}
// Create a new database to store the accepted stake node data into.
if db.sDB == nil || db.sDB.BestNode == nil {
return 0, 0, 0, -1, fmt.Errorf("stake DB is missing")
}
stakeDatabaseHeight = int64(db.sDB.Height())
lowest = stakeInfoHeight
if summaryHeight < stakeInfoHeight {
lowest = summaryHeight
}
if stakeDatabaseHeight < lowest {
lowest = stakeDatabaseHeight
}
return
}
func (db *wiredDB) initWaitChan(waitChan chan chainhash.Hash) {
db.waitChan = waitChan
}
// RewindStakeDB attempts to disconnect blocks from the stake database to reach
// the specified height. A channel must be provided for signaling if the rewind
// should abort. If the specified height is greater than the current stake DB
// height, RewindStakeDB will exit without error, returning the current stake DB
// height and a nil error.
func (db *wiredDB) RewindStakeDB(toHeight int64, quit chan struct{}) (stakeDBHeight int64, err error) {
// rewind best node in ticket db
stakeDBHeight = int64(db.sDB.Height())
if toHeight < 0 {
toHeight = 0
}
log.Infof("Rewinding from %d to %d", stakeDBHeight, toHeight)
for stakeDBHeight > toHeight {
log.Infof("Rewinding from %d to %d", stakeDBHeight, toHeight)
// check for quit signal
select {
case <-quit:
log.Infof("Rewind cancelled at height %d.", stakeDBHeight)
return
default:
}
if err = db.sDB.DisconnectBlock(false); err != nil {
return
}
stakeDBHeight = int64(db.sDB.Height())
log.Tracef("Stake db now at height %d.", stakeDBHeight)
}
return
}
func (db *wiredDB) resyncDB(quit chan struct{}, blockGetter rpcutils.BlockGetter,
fetchToHeight int64) (int64, error) {
// Determine if we're in lite mode, when we are the "master" who sets the
// pace rather than waiting on other consumers to get done with the stakedb.
master := blockGetter == nil || blockGetter.(*rpcutils.BlockGate) == nil
// Get chain servers's best block
_, height, err := db.client.GetBestBlock()
if err != nil {
return -1, fmt.Errorf("GetBestBlock failed: %v", err)
}
// Time this function
defer func(start time.Time, perr *error) {
if *perr != nil {
log.Infof("resyncDBWithPoolValue() completed in %v", time.Since(start))
}
}(time.Now(), &err)
// Check and report heights of the DBs
startHeight, summaryHeight, stakeInfoHeight, stakeDBHeight, err := db.DBHeights()
if err != nil {
return -1, fmt.Errorf("DBHeights failed: %v", err)
}
if startHeight < -1 {
panic("invalid starting height")
}
log.Info("Current best block (chain server): ", height)
log.Info("Current best block (sqlite block DB): ", summaryHeight)
if stakeInfoHeight != summaryHeight {
log.Error("Current best block (sqlite stake DB): ", stakeInfoHeight)
return -1, fmt.Errorf("SQLite database (dcrdata.sqlt.db) is corrupted")
}
log.Info("Current best block (stakedb): ", stakeDBHeight)
// Attempt to rewind stake database, if needed
if stakeDBHeight > startHeight && stakeDBHeight > 0 {
if startHeight < 0 || stakeDBHeight > 2*startHeight {
return -1, fmt.Errorf("delete stake db (ffldb_stake) and try again")
}
log.Infof("Rewinding stake node from %d to %d", stakeDBHeight, startHeight)
// rewind best node in ticket db
stakeDBHeight, err = db.RewindStakeDB(startHeight, quit)
if err != nil {
return startHeight, fmt.Errorf("RewindStakeDB failed: %v", err)
}
}
if fetchToHeight < stakeDBHeight && !master {
return startHeight, fmt.Errorf("fetchToHeight may not be less than stakedb height")
}
// At least this many blocks to check (another may come in before finishing)
minBlocksToCheck := height - startHeight
if minBlocksToCheck < 1 {
if minBlocksToCheck < 0 {
return startHeight, fmt.Errorf("chain server behind DBs")
}
return startHeight, nil
}
// Start at next block we don't have in every DB
startHeight++
for i := startHeight; i <= height; i++ {
// check for quit signal
select {
case <-quit:
log.Infof("Rescan cancelled at height %d.", i)
return i - 1, nil
default:
}
// Either fetch the block or wait for a signal that it is ready
var block *dcrutil.Block
var blockhash chainhash.Hash
if master || i < fetchToHeight {
// Not coordinating with blockGetter for this block
var h *chainhash.Hash
block, h, err = db.getBlock(i)
if err != nil {
return i - 1, fmt.Errorf("getBlock failed (%d): %v", i, err)
}
blockhash = *h
} else {
// Wait for this block to become available in the MasterBlockGetter
select {
case blockhash = <-db.waitChan:
case <-quit:
log.Infof("Rescan cancelled at height %d.", i)
return i - 1, nil
}
block, err = blockGetter.Block(blockhash)
if err != nil {
return i - 1, fmt.Errorf("blockGetter.Block failed (%s): %v", blockhash, err)
}
// Before connecting the block in the StakeDatabase, request
// notification for the next block.
db.waitChan = blockGetter.WaitForHeight(i + 1)
}
if i > stakeDBHeight {
if i != int64(db.sDB.Height()+1) {
panic(fmt.Sprintf("about to connect the wrong block: %d, %d", i, db.sDB.Height()))
}
if err = db.sDB.ConnectBlock(block); err != nil {
return i - 1, err
}
}
numLive := db.sDB.PoolSize()
//liveTickets := db.sDB.BestNode.LiveTickets()
// TODO: winning tickets
//winningTickets := db.sDB.BestNode.Winners()
if (i-1)%rescanLogBlockChunk == 0 && i-1 != startHeight || i == startHeight {
if i == 0 {
log.Infof("Scanning genesis block.")
} else {
endRangeBlock := rescanLogBlockChunk * (1 + (i-1)/rescanLogBlockChunk)
if endRangeBlock > height {
endRangeBlock = height
}
log.Infof("Scanning blocks %d to %d (%d live)...",
i, endRangeBlock, numLive)
}
}
var tpi *apitypes.TicketPoolInfo
var found bool
if tpi, found = db.sDB.PoolInfo(blockhash); !found {
if i != 0 {
log.Warnf("Unable to find block (%s) in pool info cache. Resync is malfunctioning!", blockhash.String())
}
tpi = db.sDB.PoolInfoBest()
if int64(tpi.Height) != i {
log.Errorf("Collected block height %d != stake db height %d. Pool info "+
"will not match the rest of this block's data.", tpi.Height, i)
}
}
header := block.MsgBlock().Header
diffRatio := txhelpers.GetDifficultyRatio(header.Bits, db.params)
blockSummary := apitypes.BlockDataBasic{
Height: header.Height,
Size: header.Size,
Hash: blockhash.String(),
Difficulty: diffRatio,
StakeDiff: dcrutil.Amount(header.SBits).ToCoin(),
Time: header.Timestamp.Unix(),
PoolInfo: *tpi,
}
if i > summaryHeight {
if err = db.StoreBlockSummary(&blockSummary); err != nil {
return i - 1, fmt.Errorf("Unable to store block summary in database: %v", err)
}
}
if i <= stakeInfoHeight {
// update height, the end condition for the loop
if _, height, err = db.client.GetBestBlock(); err != nil {
return i - 1, fmt.Errorf("GetBestBlock failed: %v", err)
}
continue
}
// Stake info
si := apitypes.StakeInfoExtended{}
// Ticket fee info
fib := txhelpers.FeeRateInfoBlock(block)
if fib == nil {
return i - 1, fmt.Errorf("FeeRateInfoBlock failed")
}
si.Feeinfo = *fib
// Price window number and block index
winSize := uint32(db.params.StakeDiffWindowSize)
si.PriceWindowNum = int(i) / int(winSize)
si.IdxBlockInWindow = int(i)%int(winSize) + 1
// Ticket pool info
si.PoolInfo = blockSummary.PoolInfo
if err = db.StoreStakeInfoExtended(&si); err != nil {
return i - 1, fmt.Errorf("Unable to store stake info in database: %v", err)
}
// update height, the end condition for the loop
if _, height, err = db.client.GetBestBlock(); err != nil {
return i, fmt.Errorf("GetBestBlock failed: %v", err)
}
}
log.Infof("Rescan finished successfully at height %d.", height)
return height, nil
}
func (db *wiredDB) getBlock(ind int64) (*dcrutil.Block, *chainhash.Hash, error) {
blockhash, err := db.client.GetBlockHash(ind)
if err != nil {
return nil, nil, fmt.Errorf("GetBlockHash(%d) failed: %v", ind, err)
}
msgBlock, err := db.client.GetBlock(blockhash)
if err != nil {
return nil, blockhash,
fmt.Errorf("GetBlock failed (%s): %v", blockhash, err)
}
block := dcrutil.NewBlock(msgBlock)
return block, blockhash, nil
}