@@ -66,6 +66,8 @@ type DB struct {
6666 getStakeInfoWinnersSQL string
6767 getAllPoolValSize string
6868 getAllFeeInfoPerBlock string
69+ // returns difficulty in 24hrs or immediately after 24hrs.
70+ getDifficulty string
6971}
7072
7173// NewDB creates a new DB instance with pre-generated sql statements from an
@@ -79,29 +81,31 @@ func NewDB(db *sql.DB) (*DB, error) {
7981 }
8082
8183 // Ticket pool queries
82- d .getPoolSQL = fmt .Sprintf (`select hash, poolsize, poolval, poolavg, winners` +
83- ` from %s where height = ?` , TableNameSummaries )
84- d .getPoolByHashSQL = fmt .Sprintf (`select height, poolsize, poolval, poolavg, winners` +
85- ` from %s where hash = ?` , TableNameSummaries )
86- d .getPoolRangeSQL = fmt .Sprintf (`select height, hash, poolsize, poolval, poolavg, winners ` +
87- `from %s where height between ? and ?` , TableNameSummaries )
88- d .getPoolValSizeRangeSQL = fmt .Sprintf (`select poolsize, poolval ` +
89- `from %s where height between ? and ?` , TableNameSummaries )
90- d .getAllPoolValSize = fmt .Sprintf (`select distinct poolsize, poolval, time ` +
91- `from %s order by time` , TableNameSummaries )
92- d .getWinnersSQL = fmt .Sprintf (`select hash, winners from %s where height = ?` ,
84+ d .getPoolSQL = fmt .Sprintf (`SELECT hash, poolsize, poolval, poolavg, winners` +
85+ ` FROM %s WHERE height = ?` , TableNameSummaries )
86+ d .getPoolByHashSQL = fmt .Sprintf (`SELECT height, poolsize, poolval, poolavg, winners` +
87+ ` FROM %s WHERE hash = ?` , TableNameSummaries )
88+ d .getPoolRangeSQL = fmt .Sprintf (`SELECT height, hash, poolsize, poolval, poolavg, winners ` +
89+ `FROM %s WHERE height BETWEEN ? AND ?` , TableNameSummaries )
90+ d .getPoolValSizeRangeSQL = fmt .Sprintf (`SELECT poolsize, poolval ` +
91+ `FROM %s WHERE height BETWEEN ? AND ?` , TableNameSummaries )
92+ d .getAllPoolValSize = fmt .Sprintf (`SELECT distinct poolsize, poolval, time ` +
93+ `FROM %s ORDER BY time` , TableNameSummaries )
94+ d .getWinnersSQL = fmt .Sprintf (`SELECT hash, winners FROM %s WHERE height = ?` ,
9395 TableNameSummaries )
94- d .getWinnersByHashSQL = fmt .Sprintf (`select height, winners from %s where hash = ?` ,
96+ d .getWinnersByHashSQL = fmt .Sprintf (`SELECT height, winners FROM %s WHERE hash = ?` ,
9597 TableNameSummaries )
9698
97- d .getSDiffSQL = fmt .Sprintf (`select sdiff from %s where height = ?` ,
99+ d .getSDiffSQL = fmt .Sprintf (`SELECT sdiff FROM %s WHERE height = ?` ,
98100 TableNameSummaries )
99- d .getSDiffRangeSQL = fmt .Sprintf (`select sdiff from %s where height between ? and ?` ,
101+ d .getDifficulty = fmt .Sprintf (`SELECT diff FROM %s WHERE time >= ? ORDER BY time LIMIT 1` ,
102+ TableNameSummaries )
103+ d .getSDiffRangeSQL = fmt .Sprintf (`SELECT sdiff FROM %s WHERE height BETWEEN ? AND ?` ,
100104 TableNameSummaries )
101105
102106 // Block queries
103- d .getBlockSQL = fmt .Sprintf (`select * from %s where height = ?` , TableNameSummaries )
104- d .getBlockByHashSQL = fmt .Sprintf (`select * from %s where hash = ?` , TableNameSummaries )
107+ d .getBlockSQL = fmt .Sprintf (`SELECT * FROM %s WHERE height = ?` , TableNameSummaries )
108+ d .getBlockByHashSQL = fmt .Sprintf (`SELECT * FROM %s WHERE hash = ?` , TableNameSummaries )
105109 d .getLatestBlockSQL = fmt .Sprintf (`SELECT * FROM %s ORDER BY height DESC LIMIT 0, 1` ,
106110 TableNameSummaries )
107111 d .insertBlockSQL = fmt .Sprintf (`
@@ -110,23 +114,23 @@ func NewDB(db *sql.DB) (*DB, error) {
110114 ) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
111115 ` , TableNameSummaries )
112116
113- d .getBlockSizeRangeSQL = fmt .Sprintf (`select size from %s where height between ? and ?` ,
117+ d .getBlockSizeRangeSQL = fmt .Sprintf (`SELECT size FROM %s WHERE height BETWEEN ? AND ?` ,
114118 TableNameSummaries )
115- d .getBlockByTimeRangeSQL = fmt .Sprintf (`select * from %s where time between ? and ? ORDER BY time LIMIT ?` ,
119+ d .getBlockByTimeRangeSQL = fmt .Sprintf (`SELECT * FROM %s WHERE time BETWEEN ? AND ? ORDER BY time LIMIT ?` ,
116120 TableNameSummaries )
117- d .getBlockByTimeSQL = fmt .Sprintf (`select * from %s where time = ?` ,
121+ d .getBlockByTimeSQL = fmt .Sprintf (`SELECT * FROM %s WHERE time = ?` ,
118122 TableNameSummaries )
119123
120- d .getBestBlockHashSQL = fmt .Sprintf (`select hash from %s ORDER BY height DESC LIMIT 0, 1` , TableNameSummaries )
121- d .getBestBlockHeightSQL = fmt .Sprintf (`select height from %s ORDER BY height DESC LIMIT 0, 1` , TableNameSummaries )
124+ d .getBestBlockHashSQL = fmt .Sprintf (`SELECT hash FROM %s ORDER BY height DESC LIMIT 0, 1` , TableNameSummaries )
125+ d .getBestBlockHeightSQL = fmt .Sprintf (`SELECT height FROM %s ORDER BY height DESC LIMIT 0, 1` , TableNameSummaries )
122126
123- d .getBlockHashSQL = fmt .Sprintf (`select hash from %s where height = ?` , TableNameSummaries )
124- d .getBlockHeightSQL = fmt .Sprintf (`select height from %s where hash = ?` , TableNameSummaries )
127+ d .getBlockHashSQL = fmt .Sprintf (`SELECT hash FROM %s WHERE height = ?` , TableNameSummaries )
128+ d .getBlockHeightSQL = fmt .Sprintf (`SELECT height FROM %s WHERE hash = ?` , TableNameSummaries )
125129
126130 // Stake info queries
127- d .getStakeInfoExtendedSQL = fmt .Sprintf (`select * from %s where height = ?` ,
131+ d .getStakeInfoExtendedSQL = fmt .Sprintf (`SELECT * FROM %s WHERE height = ?` ,
128132 TableNameStakeInfo )
129- d .getStakeInfoWinnersSQL = fmt .Sprintf (`select winners from %s where height = ?` ,
133+ d .getStakeInfoWinnersSQL = fmt .Sprintf (`SELECT winners FROM %s WHERE height = ?` ,
130134 TableNameStakeInfo )
131135 d .getLatestStakeInfoExtendedSQL = fmt .Sprintf (
132136 `SELECT * FROM %s ORDER BY height DESC LIMIT 0, 1` , TableNameStakeInfo )
@@ -651,7 +655,14 @@ func (db *DB) RetrieveBlockSummaryByTimeRange(minTime, maxTime int64, limit int)
651655 return blocks , nil
652656}
653657
654- // RetrieveSDiff returns the stake difficulty for block ind
658+ // RetrieveDiff returns the difficulty in the last 24hrs or immediately after 24hrs.
659+ func (db * DB ) RetrieveDiff (timestamp int64 ) (float64 , error ) {
660+ var diff float64
661+ err := db .QueryRow (db .getDifficulty , timestamp ).Scan (& diff )
662+ return diff , err
663+ }
664+
665+ // RetrieveSDiff returns the stake difficulty for block at the specified chain height.
655666func (db * DB ) RetrieveSDiff (ind int64 ) (float64 , error ) {
656667 var sdiff float64
657668 err := db .QueryRow (db .getSDiffSQL , ind ).Scan (& sdiff )
0 commit comments