/
sanity.go
338 lines (295 loc) · 9.53 KB
/
sanity.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
// Copyright (c) 2019, The Eacred developers
// See LICENSE for details.
package dcrpg
import (
"context"
"database/sql"
"github.com/Eacred/eacrdata/db/dcrpg/internal"
)
// CheckUnmatchedSpending checks the addresses table for spending rows where the
// matching (funding) txhash has not been set. The row IDs and addresses are
// returned. Non-zero length sizes is an indication of database corruption.
func CheckUnmatchedSpending(ctx context.Context, db *sql.DB) (ids []uint64, addresses []string, err error) {
rows, err := db.QueryContext(ctx, internal.UnmatchedSpending)
if err != nil {
return nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var address string
var id uint64
err = rows.Scan(&id, &address)
if err != nil {
return nil, nil, err
}
ids = append(ids, id)
addresses = append(addresses, address)
}
return
}
// CheckExtraMainchainBlocks checks the blocks table for multiple main chain
// blocks at a given height, which is not possible. The row IDs, block heights,
// and block hashes are returned. Non-zero length sizes is an indication of
// database corruption.
func CheckExtraMainchainBlocks(ctx context.Context, db *sql.DB) (ids, heights []uint64, hashes []string, err error) {
rows, err := db.QueryContext(ctx, internal.ExtraMainchainBlocks)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var hash string
var id, height uint64
err = rows.Scan(&id, &height, &hash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
heights = append(heights, height)
hashes = append(hashes, hash)
}
return
}
// CheckMislabeledInvalidBlocks checks the blocks table for blocks labeled as
// approved, but for which the following block has specified vote bits that
// invalidate it. This indicates likely database corruption.
func CheckMislabeledInvalidBlocks(ctx context.Context, db *sql.DB) (ids []uint64, hashes []string, err error) {
rows, err := db.QueryContext(ctx, internal.MislabeledInvalidBlocks)
if err != nil {
return nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var hash string
var id uint64
err = rows.Scan(&id, &hash)
if err != nil {
return nil, nil, err
}
ids = append(ids, id)
hashes = append(hashes, hash)
}
return
}
// CheckUnspentTicketsWithSpendInfo checks the tickets table for tickets that
// are flagged as unspent, but which have set either a spend height or spending
// transaction row id. This indicates likely database corruption.
func CheckUnspentTicketsWithSpendInfo(ctx context.Context, db *sql.DB) (ids, spendHeights, spendTxDbIDs []uint64, err error) {
rows, err := db.QueryContext(ctx, internal.UnspentTicketsWithSpendInfo)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id, height, txID uint64
err = rows.Scan(&id, &height, &txID)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
spendHeights = append(spendHeights, height)
spendTxDbIDs = append(spendTxDbIDs, txID)
}
return
}
// CheckSpentTicketsWithoutSpendInfo checks the tickets table for tickets that
// are flagged as spent, but which do not have set either a spend height or
// spending transaction row id. This indicates likely database corruption.
func CheckSpentTicketsWithoutSpendInfo(ctx context.Context, db *sql.DB) (ids, spendHeights, spendTxDbIDs []uint64, err error) {
rows, err := db.QueryContext(ctx, internal.SpentTicketsWithoutSpendInfo)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id, height, txID uint64
err = rows.Scan(&id, &height, &txID)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
spendHeights = append(spendHeights, height)
spendTxDbIDs = append(spendTxDbIDs, txID)
}
return
}
// CheckMislabeledTicketTransactions checks the transactions table for ticket
// transactions that also appear in the tickets table, but which do not have the
// proper tx_type (1) set. This indicates likely database corruption.
func CheckMislabeledTicketTransactions(ctx context.Context, db *sql.DB) (ids []uint64, txTypes []int16, txHashes []string, err error) {
rows, err := db.QueryContext(ctx, internal.MislabeledTicketTransactions)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txType int16
var txHash string
err = rows.Scan(&id, &txType, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txTypes = append(txTypes, txType)
txHashes = append(txHashes, txHash)
}
return
}
// CheckMissingTickets checks the transactions table for ticket transactions
// (with tx_type=1) that do NOT appear in the tickets table. This indicates
// likely database corruption.
func CheckMissingTickets(ctx context.Context, db *sql.DB) (ids []uint64, txTypes []int16, txHashes []string, err error) {
rows, err := db.QueryContext(ctx, internal.MissingTickets)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txType int16
var txHash string
err = rows.Scan(&id, &txType, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txTypes = append(txTypes, txType)
txHashes = append(txHashes, txHash)
}
return
}
// CheckMissingTicketTransactions checks the tickets table for tickets that do
// NOT appear in the transactions table at all. This indicates likely database
// corruption.
func CheckMissingTicketTransactions(ctx context.Context, db *sql.DB) (ids []uint64, txHashes []string, err error) {
rows, err := db.QueryContext(ctx, internal.MissingTicketTransactions)
if err != nil {
return nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txHash string
err = rows.Scan(&id, &txHash)
if err != nil {
return nil, nil, err
}
ids = append(ids, id)
txHashes = append(txHashes, txHash)
}
return
}
// CheckBadSpentLiveTickets checks the tickets table for tickets with live pool
// status but not unspent status. This indicates likely database corruption.
func CheckBadSpentLiveTickets(ctx context.Context, db *sql.DB) (ids []uint64, txHashes []string, spendTypes []int16, err error) {
rows, err := db.QueryContext(ctx, internal.BadSpentLiveTickets)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txHash string
var spendType int16
err = rows.Scan(&id, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txHashes = append(txHashes, txHash)
spendTypes = append(spendTypes, spendType)
}
return
}
// CheckBadVotedTickets checks the tickets table for tickets with voted pool
// status but not voted status. This indicates likely database corruption.
func CheckBadVotedTickets(ctx context.Context, db *sql.DB) (ids []uint64, txHashes []string, spendTypes []int16, err error) {
rows, err := db.QueryContext(ctx, internal.BadVotedTickets)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txHash string
var spendType int16
err = rows.Scan(&id, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txHashes = append(txHashes, txHash)
spendTypes = append(spendTypes, spendType)
}
return
}
// CheckBadExpiredVotedTickets checks the tickets table for tickets with expired
// pool status but with voted status. This indicates likely database corruption.
func CheckBadExpiredVotedTickets(ctx context.Context, db *sql.DB) (ids []uint64, txHashes []string, spendTypes []int16, err error) {
rows, err := db.QueryContext(ctx, internal.BadExpiredVotedTickets)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txHash string
var spendType int16
err = rows.Scan(&id, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txHashes = append(txHashes, txHash)
spendTypes = append(spendTypes, spendType)
}
return
}
// CheckBadMissedVotedTickets checks the tickets table for tickets with missed
// pool status but with voted status. This indicates likely database corruption.
func CheckBadMissedVotedTickets(ctx context.Context, db *sql.DB) (ids []uint64, txHashes []string, spendTypes []int16, err error) {
rows, err := db.QueryContext(ctx, internal.BadMissedVotedTickets)
if err != nil {
return nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var id uint64
var txHash string
var spendType int16
err = rows.Scan(&id, &txHash)
if err != nil {
return nil, nil, nil, err
}
ids = append(ids, id)
txHashes = append(txHashes, txHash)
spendTypes = append(spendTypes, spendType)
}
return
}
// CheckBadBlockApproval checks the blocks table for blocks with an incorrect
// approval flag as determined by computing (approvals/total_votes > 0.5).
func CheckBadBlockApproval(ctx context.Context, db *sql.DB) (hashes []string, approvals, disapprovals, totals []int16, approvedActual, approvedSet []bool, err error) {
rows, err := db.QueryContext(ctx, internal.BadBlockApproval)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
defer closeRows(rows)
for rows.Next() {
var hash string
var appr, disappr, tot int16
var apprAct, apprSet bool
err = rows.Scan(&hash, appr, disappr, tot, apprAct, apprSet)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
hashes = append(hashes, hash)
approvals = append(approvals, appr)
disapprovals = append(disapprovals, disappr)
totals = append(totals, tot)
approvedActual = append(approvedActual, apprAct)
approvedSet = append(approvedSet, apprSet)
}
return
}