-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
682 lines (587 loc) · 19.6 KB
/
sqlite.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
package dcrsqlite
import (
"database/sql"
"fmt"
"sync"
"github.com/btcsuite/btclog"
"github.com/coolsnady/hcexplorer/blockdata"
apitypes "github.com/coolsnady/hcexplorer/dcrdataapi"
"github.com/coolsnady/hcd/wire"
_ "github.com/mattn/go-sqlite3" // register sqlite driver with database/sql
)
// StakeInfoDatabaser is the interface for an extended stake info saving database
type StakeInfoDatabaser interface {
StoreStakeInfoExtended(bd *apitypes.StakeInfoExtended) error
RetrieveStakeInfoExtended(ind int64) (*apitypes.StakeInfoExtended, error)
}
// BlockSummaryDatabaser is the interface for a block data saving database
type BlockSummaryDatabaser interface {
StoreBlockSummary(bd *apitypes.BlockDataBasic) error
RetrieveBlockSummary(ind int64) (*apitypes.BlockDataBasic, error)
}
// DBInfo contains db configuration
type DBInfo struct {
FileName string
}
const (
// TableNameSummaries is name of the table used to store block summary data
TableNameSummaries = "hcdata_block_summary"
// TableNameStakeInfo is name of the table used to store extended stake info
TableNameStakeInfo = "hcdata_stakeinfo_extended"
)
// DB is a wrapper around sql.DB that adds methods for storing and retrieving
// chain data. Use InitDB to get a new instance. This may be unexported in the
// future.
type DB struct {
*sql.DB
sync.RWMutex
dbSummaryHeight int64
dbStakeInfoHeight int64
getPoolSQL, getPoolRangeSQL string
getPoolByHashSQL string
getSDiffSQL, getSDiffRangeSQL string
getLatestBlockSQL string
getBlockSQL, insertBlockSQL string
getBlockByHashSQL string
getBlockHashSQL, getBlockHeightSQL string
getBlockSizeRangeSQL string
getBestBlockHashSQL, getBestBlockHeightSQL string
getLatestStakeInfoExtendedSQL string
getStakeInfoExtendedSQL, insertStakeInfoExtendedSQL string
}
// NewDB creates a new DB instance with pre-generated sql statements from an
// existing sql.DB. Use InitDB to create a new DB without having a sql.DB.
// TODO: if this db exists, figure out best heights
func NewDB(db *sql.DB) *DB {
d := DB{
DB: db,
dbSummaryHeight: -1,
dbStakeInfoHeight: -1,
}
// Ticket pool queries
d.getPoolSQL = fmt.Sprintf(`select poolsize, poolval, poolavg from %s where height = ?`,
TableNameSummaries)
d.getPoolByHashSQL = fmt.Sprintf(`select poolsize, poolval, poolavg from %s where hash = ?`,
TableNameSummaries)
d.getPoolRangeSQL = fmt.Sprintf(`select poolsize, poolval, poolavg from %s where height between ? and ?`,
TableNameSummaries)
d.getSDiffSQL = fmt.Sprintf(`select sdiff from %s where height = ?`,
TableNameSummaries)
d.getSDiffRangeSQL = fmt.Sprintf(`select sdiff from %s where height between ? and ?`,
TableNameSummaries)
// Block queries
d.getBlockSQL = fmt.Sprintf(`select * from %s where height = ?`, TableNameSummaries)
d.getBlockByHashSQL = fmt.Sprintf(`select * from %s where hash = ?`, TableNameSummaries)
d.getLatestBlockSQL = fmt.Sprintf(`SELECT * FROM %s ORDER BY height DESC LIMIT 0, 1`,
TableNameSummaries)
d.insertBlockSQL = fmt.Sprintf(`
INSERT OR REPLACE INTO %s(
height, size, hash, diff, sdiff, time, poolsize, poolval, poolavg
) values(?, ?, ?, ?, ?, ?, ?, ?, ?)
`, TableNameSummaries)
d.getBlockSizeRangeSQL = fmt.Sprintf(`select size from %s where height between ? and ?`,
TableNameSummaries)
d.getBestBlockHashSQL = fmt.Sprintf(`select hash from %s ORDER BY height DESC LIMIT 0, 1`, TableNameSummaries)
d.getBestBlockHeightSQL = fmt.Sprintf(`select height from %s ORDER BY height DESC LIMIT 0, 1`, TableNameSummaries)
d.getBlockHashSQL = fmt.Sprintf(`select hash from %s where height = ?`, TableNameSummaries)
d.getBlockHeightSQL = fmt.Sprintf(`select height from %s where hash = ?`, TableNameSummaries)
// Stake info queries
d.getStakeInfoExtendedSQL = fmt.Sprintf(`select * from %s where height = ?`,
TableNameStakeInfo)
d.getLatestStakeInfoExtendedSQL = fmt.Sprintf(
`SELECT * FROM %s ORDER BY height DESC LIMIT 0, 1`, TableNameStakeInfo)
d.insertStakeInfoExtendedSQL = fmt.Sprintf(`
INSERT OR REPLACE INTO %s(
height, num_tickets, fee_min, fee_max, fee_mean, fee_med, fee_std,
sdiff, window_num, window_ind, pool_size, pool_val, pool_valavg
) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, TableNameStakeInfo)
d.dbSummaryHeight = d.GetBlockSummaryHeight()
d.dbStakeInfoHeight = d.GetStakeInfoHeight()
return &d
}
// InitDB creates a new DB instance from a DBInfo containing the name of the
// file used to back the underlying sql database.
func InitDB(dbInfo *DBInfo) (*DB, error) {
db, err := sql.Open("sqlite3", dbInfo.FileName)
if err != nil || db == nil {
return nil, err
}
createBlockSummaryStmt := fmt.Sprintf(`
PRAGMA cache_size = 32768;
pragma synchronous = OFF;
create table if not exists %s(
height INTEGER PRIMARY KEY,
size INTEGER,
hash TEXT,
diff FLOAT,
sdiff FLOAT,
time INTEGER,
poolsize INTEGER,
poolval FLOAT,
poolavg FLOAT
);
`, TableNameSummaries)
_, err = db.Exec(createBlockSummaryStmt)
if err != nil {
log.Errorf("%q: %s\n", err, createBlockSummaryStmt)
return nil, err
}
createStakeInfoExtendedStmt := fmt.Sprintf(`
PRAGMA cache_size = 32768;
pragma synchronous = OFF;
create table if not exists %s(
height INTEGER PRIMARY KEY,
num_tickets INTEGER,
fee_min FLOAT, fee_max FLOAT, fee_mean FLOAT,
fee_med FLOAT, fee_std FLOAT,
sdiff FLOAT, window_num INTEGER, window_ind INTEGER,
pool_size INTEGER, pool_val FLOAT, pool_valavg FLOAT
);
`, TableNameStakeInfo)
_, err = db.Exec(createStakeInfoExtendedStmt)
if err != nil {
log.Errorf("%q: %s\n", err, createStakeInfoExtendedStmt)
return nil, err
}
err = db.Ping()
return NewDB(db), err
}
// DBDataSaver models a DB with a channel to communicate new block height to the web interface
type DBDataSaver struct {
*DB
updateStatusChan chan uint32
}
// Store satisfies the blockdata.BlockDataSaver interface
func (db *DBDataSaver) Store(data *blockdata.BlockData, _ *wire.MsgBlock) error {
summary := data.ToBlockSummary()
err := db.DB.StoreBlockSummary(&summary)
if err != nil {
return err
}
select {
case db.updateStatusChan <- summary.Height:
default:
}
stakeInfoExtended := data.ToStakeInfoExtended()
return db.DB.StoreStakeInfoExtended(&stakeInfoExtended)
}
// StoreBlockSummary attempts to stores the block data in the database and
// returns an error on failure
func (db *DB) StoreBlockSummary(bd *apitypes.BlockDataBasic) error {
stmt, err := db.Prepare(db.insertBlockSQL)
if err != nil {
return err
}
defer stmt.Close()
res, err := stmt.Exec(&bd.Height, &bd.Size, &bd.Hash,
&bd.Difficulty, &bd.StakeDiff, &bd.Time,
&bd.PoolInfo.Size, &bd.PoolInfo.Value, &bd.PoolInfo.ValAvg)
if err != nil {
return err
}
db.Lock()
defer db.Unlock()
if err = logDBResult(res); err == nil {
// TODO: atomic with CAS
//log.Debugf("Store height: %v", bd.Height)
height := int64(bd.Height)
if height > db.dbSummaryHeight {
db.dbSummaryHeight = height
}
}
return err
}
// GetBestBlockHash returns the hash of the best block
func (db *DB) GetBestBlockHash() string {
hash, err := db.RetrieveBestBlockHash()
if err != nil {
log.Errorf("RetrieveBestBlockHash failed: %v", err)
return ""
}
return hash
}
// GetBestBlockHeight returns the height of the best block
func (db *DB) GetBestBlockHeight() int64 {
return db.GetBlockSummaryHeight()
}
// GetBlockSummaryHeight returns the largest block height for which the database
// can provide a block summary
func (db *DB) GetBlockSummaryHeight() int64 {
db.RLock()
defer db.RUnlock()
if db.dbSummaryHeight < 0 {
height, err := db.RetrieveBestBlockHeight()
if err != nil {
log.Errorf("RetrieveBestBlockHeight failed: %v", err)
return -1
}
db.dbSummaryHeight = height
}
return db.dbSummaryHeight
}
// GetStakeInfoHeight returns the largest block height for which the database
// can provide a stake info
func (db *DB) GetStakeInfoHeight() int64 {
db.RLock()
defer db.RUnlock()
if db.dbStakeInfoHeight < 0 {
si, err := db.RetrieveLatestStakeInfoExtended()
if err != nil || si == nil {
log.Errorf("RetrieveLatestStakeInfoExtended failed: %v", err)
return -1
}
db.dbStakeInfoHeight = int64(si.Feeinfo.Height)
}
return db.dbStakeInfoHeight
}
// RetrievePoolInfoRange returns an array of apitypes.TicketPoolInfo for block
// range ind0 to ind1 and a non-nil error on success
func (db *DB) RetrievePoolInfoRange(ind0, ind1 int64) ([]apitypes.TicketPoolInfo, error) {
N := ind1 - ind0 + 1
if N == 0 {
return []apitypes.TicketPoolInfo{}, nil
}
if N < 0 {
return nil, fmt.Errorf("Cannot retrieve pool info range (%d<%d)",
ind1, ind0)
}
db.RLock()
if ind1 > db.dbSummaryHeight || ind0 < 0 {
defer db.RUnlock()
return nil, fmt.Errorf("Cannot retrieve pool info range [%d,%d], have height %d",
ind1, ind0, db.dbSummaryHeight)
}
db.RUnlock()
tpis := make([]apitypes.TicketPoolInfo, 0, N)
stmt, err := db.Prepare(db.getPoolRangeSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(ind0, ind1)
if err != nil {
log.Errorf("Query failed: %v", err)
return nil, err
}
defer rows.Close()
for rows.Next() {
var tpi apitypes.TicketPoolInfo
if err = rows.Scan(&tpi.Size, &tpi.Value, &tpi.ValAvg); err != nil {
log.Errorf("Unable to scan for TicketPoolInfo fields: %v", err)
}
tpis = append(tpis, tpi)
}
if err = rows.Err(); err != nil {
log.Error(err)
}
return tpis, nil
}
// RetrievePoolInfo returns ticket pool info for block height ind
func (db *DB) RetrievePoolInfo(ind int64) (*apitypes.TicketPoolInfo, error) {
tpi := new(apitypes.TicketPoolInfo)
err := db.QueryRow(db.getPoolSQL, ind).Scan(&tpi.Size, &tpi.Value, &tpi.ValAvg)
return tpi, err
}
// RetrievePoolInfoByHash returns ticket pool info for blockhash hash
func (db *DB) RetrievePoolInfoByHash(hash string) (*apitypes.TicketPoolInfo, error) {
tpi := new(apitypes.TicketPoolInfo)
err := db.QueryRow(db.getPoolByHashSQL, hash).Scan(&tpi.Size, &tpi.Value, &tpi.ValAvg)
return tpi, err
}
// RetrievePoolValAndSizeRange returns an array each of the pool values and sizes
// for block range ind0 to ind1
func (db *DB) RetrievePoolValAndSizeRange(ind0, ind1 int64) ([]float64, []float64, error) {
N := ind1 - ind0 + 1
if N == 0 {
return []float64{}, []float64{}, nil
}
if N < 0 {
return nil, nil, fmt.Errorf("Cannot retrieve pool val and size range (%d<%d)",
ind1, ind0)
}
db.RLock()
if ind1 > db.dbSummaryHeight || ind0 < 0 {
defer db.RUnlock()
return nil, nil, fmt.Errorf("Cannot retrieve pool val and size range [%d,%d], have height %d",
ind1, ind0, db.dbSummaryHeight)
}
db.RUnlock()
poolvals := make([]float64, 0, N)
poolsizes := make([]float64, 0, N)
stmt, err := db.Prepare(db.getPoolRangeSQL)
if err != nil {
return nil, nil, err
}
defer stmt.Close()
rows, err := stmt.Query(ind0, ind1)
if err != nil {
log.Errorf("Query failed: %v", err)
return nil, nil, err
}
defer rows.Close()
for rows.Next() {
var pval, psize, pavg float64
if err = rows.Scan(&psize, &pval, &pavg); err != nil {
log.Errorf("Unable to scan for TicketPoolInfo fields: %v", err)
}
poolvals = append(poolvals, pval)
poolsizes = append(poolsizes, psize)
}
if err = rows.Err(); err != nil {
log.Error(err)
}
if len(poolsizes) != int(N) {
log.Warnf("Retrieved pool values (%d) not expected number (%d)", len(poolsizes), N)
}
return poolvals, poolsizes, nil
}
// RetrieveSDiffRange returns an array of stake difficulties for block range ind0 to
// ind1
func (db *DB) RetrieveSDiffRange(ind0, ind1 int64) ([]float64, error) {
N := ind1 - ind0 + 1
if N == 0 {
return []float64{}, nil
}
if N < 0 {
return nil, fmt.Errorf("Cannot retrieve sdiff range (%d<%d)",
ind1, ind0)
}
db.RLock()
if ind1 > db.dbSummaryHeight || ind0 < 0 {
defer db.RUnlock()
return nil, fmt.Errorf("Cannot retrieve sdiff range [%d,%d], have height %d",
ind1, ind0, db.dbSummaryHeight)
}
db.RUnlock()
sdiffs := make([]float64, 0, N)
stmt, err := db.Prepare(db.getSDiffRangeSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(ind0, ind1)
if err != nil {
log.Errorf("Query failed: %v", err)
return nil, err
}
defer rows.Close()
for rows.Next() {
var sdiff float64
if err = rows.Scan(&sdiff); err != nil {
log.Errorf("Unable to scan for sdiff fields: %v", err)
}
sdiffs = append(sdiffs, sdiff)
}
if err = rows.Err(); err != nil {
log.Error(err)
}
return sdiffs, nil
}
// RetrieveSDiff returns the stake difficulty for block ind
func (db *DB) RetrieveSDiff(ind int64) (float64, error) {
var sdiff float64
err := db.QueryRow(db.getSDiffSQL, ind).Scan(&sdiff)
return sdiff, err
}
// RetrieveLatestBlockSummary returns the block summary for the best block
func (db *DB) RetrieveLatestBlockSummary() (*apitypes.BlockDataBasic, error) {
bd := new(apitypes.BlockDataBasic)
err := db.QueryRow(db.getLatestBlockSQL).Scan(&bd.Height, &bd.Size,
&bd.Hash, &bd.Difficulty, &bd.StakeDiff, &bd.Time,
&bd.PoolInfo.Size, &bd.PoolInfo.Value, &bd.PoolInfo.ValAvg)
if err != nil {
return nil, err
}
return bd, nil
}
// RetrieveBlockHash returns the block hash for block ind
func (db *DB) RetrieveBlockHash(ind int64) (string, error) {
var blockHash string
err := db.QueryRow(db.getBlockHashSQL, ind).Scan(&blockHash)
return blockHash, err
}
// RetrieveBlockHeight returns the block height for blockhash hash
func (db *DB) RetrieveBlockHeight(hash string) (int64, error) {
var blockHeight int64
err := db.QueryRow(db.getBlockHeightSQL, hash).Scan(&blockHeight)
return blockHeight, err
}
// RetrieveBestBlockHash returns the block hash for the best block
func (db *DB) RetrieveBestBlockHash() (string, error) {
var blockHash string
err := db.QueryRow(db.getBestBlockHashSQL).Scan(&blockHash)
return blockHash, err
}
// RetrieveBestBlockHeight returns the block height for the best block
func (db *DB) RetrieveBestBlockHeight() (int64, error) {
var blockHeight int64
err := db.QueryRow(db.getBestBlockHeightSQL).Scan(&blockHeight)
return blockHeight, err
}
// RetrieveBlockSummaryByHash returns basic block data for a block given its hash
func (db *DB) RetrieveBlockSummaryByHash(hash string) (*apitypes.BlockDataBasic, error) {
bd := new(apitypes.BlockDataBasic)
err := db.QueryRow(db.getBlockByHashSQL, hash).Scan(&bd.Height, &bd.Size, &bd.Hash,
&bd.Difficulty, &bd.StakeDiff, &bd.Time,
&bd.PoolInfo.Size, &bd.PoolInfo.Value, &bd.PoolInfo.ValAvg)
if err != nil {
return nil, err
}
return bd, nil
}
// RetrieveBlockSummary returns basic block data for block ind
func (db *DB) RetrieveBlockSummary(ind int64) (*apitypes.BlockDataBasic, error) {
bd := new(apitypes.BlockDataBasic)
// Three different ways
// 1. chained QueryRow/Scan only
err := db.QueryRow(db.getBlockSQL, ind).Scan(&bd.Height, &bd.Size, &bd.Hash,
&bd.Difficulty, &bd.StakeDiff, &bd.Time,
&bd.PoolInfo.Size, &bd.PoolInfo.Value, &bd.PoolInfo.ValAvg)
if err != nil {
return nil, err
}
// 2. Prepare + chained QueryRow/Scan
// stmt, err := db.Prepare(getBlockSQL)
// if err != nil {
// return nil, err
// }
// defer stmt.Close()
// err = stmt.QueryRow(ind).Scan(&bd.Height, &bd.Size, &bd.Hash, &bd.Difficulty,
// &bd.StakeDiff, &bd.Time, &bd.PoolInfo.Size, &bd.PoolInfo.Value,
// &bd.PoolInfo.ValAvg)
// if err != nil {
// return nil, err
// }
// 3. Prepare + Query + Scan
// rows, err := stmt.Query(ind)
// if err != nil {
// log.Errorf("Query failed: %v", err)
// return nil, err
// }
// defer rows.Close()
// if rows.Next() {
// err = rows.Scan(&bd.Height, &bd.Size, &bd.Hash, &bd.Difficulty, &bd.StakeDiff,
// &bd.Time, &bd.PoolInfo.Size, &bd.PoolInfo.Value, &bd.PoolInfo.ValAvg)
// if err != nil {
// log.Errorf("Unable to scan for BlockDataBasic fields: %v", err)
// }
// }
// if err = rows.Err(); err != nil {
// log.Error(err)
// }
return bd, nil
}
// RetrieveBlockSizeRange returns an array of block sizes for block range ind0 to ind1
func (db *DB) RetrieveBlockSizeRange(ind0, ind1 int64) ([]int32, error) {
N := ind1 - ind0 + 1
if N == 0 {
return []int32{}, nil
}
if N < 0 {
return nil, fmt.Errorf("Cannot retrieve block size range (%d<%d)",
ind1, ind0)
}
db.RLock()
if ind1 > db.dbSummaryHeight || ind0 < 0 {
defer db.RUnlock()
return nil, fmt.Errorf("Cannot retrieve block size range [%d,%d], have height %d",
ind1, ind0, db.dbSummaryHeight)
}
db.RUnlock()
blockSizes := make([]int32, 0, N)
stmt, err := db.Prepare(db.getBlockSizeRangeSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(ind0, ind1)
if err != nil {
log.Errorf("Query failed: %v", err)
return nil, err
}
defer rows.Close()
for rows.Next() {
var blockSize int32
if err = rows.Scan(&blockSize); err != nil {
log.Errorf("Unable to scan for sdiff fields: %v", err)
}
blockSizes = append(blockSizes, blockSize)
}
if err = rows.Err(); err != nil {
log.Error(err)
}
return blockSizes, nil
}
// StoreStakeInfoExtended stores the extended stake info in the database
func (db *DB) StoreStakeInfoExtended(si *apitypes.StakeInfoExtended) error {
stmt, err := db.Prepare(db.insertStakeInfoExtendedSQL)
if err != nil {
return err
}
defer stmt.Close()
res, err := stmt.Exec(&si.Feeinfo.Height,
&si.Feeinfo.Number, &si.Feeinfo.Min, &si.Feeinfo.Max, &si.Feeinfo.Mean,
&si.Feeinfo.Median, &si.Feeinfo.StdDev,
&si.StakeDiff, // no next or estimates
&si.PriceWindowNum, &si.IdxBlockInWindow, &si.PoolInfo.Size,
&si.PoolInfo.Value, &si.PoolInfo.ValAvg)
if err != nil {
return err
}
db.Lock()
defer db.Unlock()
if err = logDBResult(res); err == nil {
height := int64(si.Feeinfo.Height)
if height > db.dbStakeInfoHeight {
db.dbStakeInfoHeight = height
}
}
return err
}
// RetrieveLatestStakeInfoExtended returns the extended stake info for the best block
func (db *DB) RetrieveLatestStakeInfoExtended() (*apitypes.StakeInfoExtended, error) {
si := new(apitypes.StakeInfoExtended)
err := db.QueryRow(db.getLatestStakeInfoExtendedSQL).Scan(
&si.Feeinfo.Height, &si.Feeinfo.Number, &si.Feeinfo.Min,
&si.Feeinfo.Max, &si.Feeinfo.Mean,
&si.Feeinfo.Median, &si.Feeinfo.StdDev,
&si.StakeDiff, // no next or estimates
&si.PriceWindowNum, &si.IdxBlockInWindow, &si.PoolInfo.Size,
&si.PoolInfo.Value, &si.PoolInfo.ValAvg)
if err != nil {
return nil, err
}
return si, nil
}
// RetrieveStakeInfoExtended returns the extended stake info for block ind
func (db *DB) RetrieveStakeInfoExtended(ind int64) (*apitypes.StakeInfoExtended, error) {
si := new(apitypes.StakeInfoExtended)
err := db.QueryRow(db.getStakeInfoExtendedSQL, ind).Scan(&si.Feeinfo.Height,
&si.Feeinfo.Number, &si.Feeinfo.Min, &si.Feeinfo.Max, &si.Feeinfo.Mean,
&si.Feeinfo.Median, &si.Feeinfo.StdDev,
&si.StakeDiff, // no next or estimates
&si.PriceWindowNum, &si.IdxBlockInWindow, &si.PoolInfo.Size,
&si.PoolInfo.Value, &si.PoolInfo.ValAvg)
if err != nil {
return nil, err
}
return si, nil
}
func logDBResult(res sql.Result) error {
if log.Level() > btclog.LevelTrace {
return nil
}
lastID, err := res.LastInsertId()
if err != nil {
return err
}
rowCnt, err := res.RowsAffected()
if err != nil {
return err
}
log.Tracef("ID = %d, affected = %d", lastID, rowCnt)
return nil
}