@@ -11,7 +11,7 @@ class Share {
11
11
private $ oUpstream ;
12
12
private $ iLastUpstreamId ;
13
13
// This defines each share
14
- public $ rem_host , $ username , $ our_result , $ upstream_result , $ reason , $ solution , $ time ;
14
+ public $ rem_host , $ username , $ our_result , $ upstream_result , $ reason , $ solution , $ time, $ difficulty ;
15
15
16
16
public function __construct ($ debug , $ mysqli , $ user , $ block , $ config ) {
17
17
$ this ->debug = $ debug ;
@@ -70,7 +70,7 @@ public function getLastInsertedShareId() {
70
70
**/
71
71
public function getRoundShares ($ previous_upstream =0 , $ current_upstream ) {
72
72
$ stmt = $ this ->mysqli ->prepare ("SELECT
73
- count(id) as total
73
+ ROUND(IFNULL(SUM(IF(difficulty=0, POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), difficulty)), 0) / POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), 8) AS total
74
74
FROM $ this ->table
75
75
WHERE our_result = 'Y'
76
76
AND id > ? AND id <= ?
@@ -98,8 +98,8 @@ public function getSharesForAccounts($previous_upstream=0, $current_upstream) {
98
98
a.id,
99
99
SUBSTRING_INDEX( s.username , '.', 1 ) as username,
100
100
a.no_fees AS no_fees,
101
- IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid,
102
- IFNULL(SUM(IF(our_result='N', 1, 0)), 0) AS invalid
101
+ ROUND( IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), s.difficulty), 0)), 0) / POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), 8 ) AS valid,
102
+ ROUND( IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), s.difficulty), 0)), 0) / POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), 8 ) AS invalid
103
103
FROM $ this ->table AS s
104
104
LEFT JOIN " . $ this ->user ->getTableName () . " AS a
105
105
ON a.username = SUBSTRING_INDEX( s.username , '.', 1 )
@@ -140,15 +140,15 @@ function getMaxArchiveShareId() {
140
140
* return array data Returns an array with usernames as keys for easy access
141
141
**/
142
142
function getArchiveShares ($ iCount ) {
143
- $ iMinId = $ this ->getMaxArchiveShareId () - $ iCount ;
143
+ $ iMinId = $ this ->getMinArchiveShareId ( $ iCount) ;
144
144
$ iMaxId = $ this ->getMaxArchiveShareId ();
145
145
$ stmt = $ this ->mysqli ->prepare ("
146
146
SELECT
147
147
a.id,
148
148
SUBSTRING_INDEX( s.username , '.', 1 ) as account,
149
149
a.no_fees AS no_fees,
150
- IFNULL(SUM(IF(our_result='Y', 1, 0)), 0) AS valid,
151
- IFNULL(SUM(IF(our_result='N', 1, 0)), 0) AS invalid
150
+ ROUND( IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, pow(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), s.difficulty), 0)), 0) / POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), 8 ) AS valid,
151
+ ROUND( IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, pow(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), s.difficulty), 0)), 0) / POW(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), 8 ) AS invalid
152
152
FROM $ this ->tableArchive AS s
153
153
LEFT JOIN " . $ this ->user ->getTableName () . " AS a
154
154
ON a.username = SUBSTRING_INDEX( s.username , '.', 1 )
@@ -198,8 +198,8 @@ public function purgeArchive() {
198
198
**/
199
199
public function moveArchive ($ current_upstream , $ block_id , $ previous_upstream =0 ) {
200
200
$ archive_stmt = $ this ->mysqli ->prepare ("
201
- INSERT INTO $ this ->tableArchive (share_id, username, our_result, upstream_result, block_id, time)
202
- SELECT id, username, our_result, upstream_result, ?, time
201
+ INSERT INTO $ this ->tableArchive (share_id, username, our_result, upstream_result, block_id, time, difficulty )
202
+ SELECT id, username, our_result, upstream_result, ?, time, IF(difficulty=0, pow(2, ( " . $ this -> config [ ' difficulty ' ] . " - 16)), difficulty) AS difficulty
203
203
FROM $ this ->table
204
204
WHERE id > ? AND id <= ? " );
205
205
if ($ this ->checkStmt ($ archive_stmt ) && $ archive_stmt ->bind_param ('iii ' , $ block_id , $ previous_upstream , $ current_upstream ) && $ archive_stmt ->execute ()) {
@@ -317,6 +317,48 @@ public function setUpstream($aBlock, $last=0) {
317
317
return false ;
318
318
}
319
319
320
+ /**
321
+ * Fetch the lowest needed share ID from shares
322
+ **/
323
+ function getMinimumShareId ($ iCount , $ current_upstream ) {
324
+ // We don't use baseline here to be more accurate
325
+ $ iCount = $ iCount * pow (2 , ($ this ->config ['difficulty ' ] - 16 ));
326
+ $ stmt = $ this ->mysqli ->prepare ("
327
+ SELECT MIN(b.id) AS id FROM
328
+ (
329
+ SELECT id, @total := @total + IF(difficulty=0, POW(2, ( " . $ this ->config ['difficulty ' ] . " - 16)), difficulty) AS total
330
+ FROM $ this ->table , (SELECT @total := 0) AS a
331
+ WHERE our_result = 'Y'
332
+ AND id <= ? AND @total < ?
333
+ ORDER BY id DESC
334
+ ) AS b
335
+ WHERE total <= ?
336
+ " );
337
+ if ($ this ->checkStmt ($ stmt ) && $ stmt ->bind_param ('iii ' , $ current_upstream , $ iCount , $ iCount ) && $ stmt ->execute () && $ result = $ stmt ->get_result ())
338
+ return $ result ->fetch_object ()->id ;
339
+ return false ;
340
+ }
341
+
342
+ /**
343
+ * Fetch the lowest needed share ID from archive
344
+ **/
345
+ function getMinArchiveShareId ($ iCount ) {
346
+ $ stmt = $ this ->mysqli ->prepare ("
347
+ SELECT MIN(b.share_id) AS share_id FROM
348
+ (
349
+ SELECT share_id, @total := @total + (IF(difficulty=0, POW(2, ( " . $ this ->config ['difficulty ' ] . " - 16)), difficulty) / POW(2, ( " . $ this ->config ['difficulty ' ] . " - 16))) AS total
350
+ FROM $ this ->tableArchive , (SELECT @total := 0) AS a
351
+ WHERE our_result = 'Y'
352
+ AND @total < ?
353
+ ORDER BY share_id DESC
354
+ ) AS b
355
+ WHERE total <= ?
356
+ " );
357
+ if ($ this ->checkStmt ($ stmt ) && $ stmt ->bind_param ('ii ' , $ iCount , $ iCount ) && $ stmt ->execute () && $ result = $ stmt ->get_result ())
358
+ return $ result ->fetch_object ()->share_id ;
359
+ return false ;
360
+ }
361
+
320
362
/**
321
363
* Helper function
322
364
**/
0 commit comments