From c9a8f8dc65f06fbc3d14695716ff511a58df6c2a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 22 Aug 2013 14:39:46 +0200 Subject: [PATCH] adjusting pplns target to baseline shares --- cronjobs/pplns_payout.php | 13 ++++++-- public/include/classes/share.class.php | 42 +++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/cronjobs/pplns_payout.php b/cronjobs/pplns_payout.php index c71f5659..6d718870 100755 --- a/cronjobs/pplns_payout.php +++ b/cronjobs/pplns_payout.php @@ -45,9 +45,12 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { if ($config['pplns']['shares']['type'] == 'blockavg' && $block->getBlockCount() > 0) { $pplns_target = round($block->getAvgBlockShares($config['pplns']['blockavg']['blockcount'])); } else { - $pplns_target = $config['pplns']['shares']['default'] ; + $pplns_target = $config['pplns']['shares']['default']; } + // We use baseline shares, so we have to calculate back to diff shares + $pplns_target = $pplns_target * pow(2, ($config['difficulty'] - 16)); + if (!$aBlock['accounted']) { $iPreviousShareId = @$aAllBlocks[$iIndex - 1]['share_id'] ? $aAllBlocks[$iIndex - 1]['share_id'] : 0; $iCurrentUpstreamId = $aBlock['share_id']; @@ -65,7 +68,8 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { if ($iRoundShares >= $pplns_target) { $log->logDebug("Matching or exceeding PPLNS target of $pplns_target with $iRoundShares"); - $aAccountShares = $share->getSharesForAccounts($aBlock['share_id'] - $pplns_target, $aBlock['share_id']); + $iMinimumShareId = $share->getMinimumShareId($pplns_target, $aBlock['share_id']); + $aAccountShares = $share->getSharesForAccounts($iMinimumShareId, $aBlock['share_id']); if (empty($aAccountShares)) { $log->logFatal("No shares found for this block, aborted! Block Height : " . $aBlock['height']); $monitoring->setStatus($cron_name . "_active", "yesno", 0); @@ -73,8 +77,11 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { $monitoring->setStatus($cron_name . "_status", "okerror", 1); exit(1); } + foreach($aAccountShares as $key => $aData) { + $iNewRoundShares += $aData['valid']; + } $log->logInfo('Adjusting round target to PPLNS target ' . $pplns_target); - $iRoundShares = $pplns_target; + $iRoundShares = $iNewRoundShares; } else { $log->logDebug("Not able to match PPLNS target of $pplns_target with $iRoundShares"); // We need to fill up with archived shares diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index d3b88759..85a005de 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -130,7 +130,7 @@ class Share { * return array data Returns an array with usernames as keys for easy access **/ function getArchiveShares($iCount) { - $iMinId = $this->getMaxArchiveShareId() - $iCount; + $iMinId = $this->getMinArchiveShareId($iCount); $iMaxId = $this->getMaxArchiveShareId(); $stmt = $this->mysqli->prepare(" SELECT @@ -307,6 +307,46 @@ class Share { return false; } + /** + * Fetch the lowest needed share ID from shares + **/ + function getMinimumShareId($iCount, $current_upstream) { + $stmt = $this->mysqli->prepare(" + SELECT MIN(b.id) AS id FROM + ( + SELECT id, @total := @total + IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS total + FROM $this->table, (SELECT @total := 0) AS a + WHERE our_result = 'Y' + AND id <= ? AND @total < ? + ORDER BY id DESC + ) AS b + WHERE total <= ? + "); + if ($this->checkStmt($stmt) && $stmt->bind_param('iii', $current_upstream, $iCount, $iCount) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + return false; + } + + /** + * Fetch the lowest needed share ID from archive + **/ + function getMinArchiveShareId($iCount) { + $stmt = $this->mysqli->prepare(" + SELECT MIN(b.share_id) AS share_id FROM + ( + SELECT share_id, @total := @total + IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS total + FROM $this->tableArchive, (SELECT @total := 0) AS a + WHERE our_result = 'Y' + AND @total < ? + ORDER BY share_id DESC + ) AS b + WHERE total <= ? + "); + if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $iCount, $iCount) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->share_id; + return false; + } + /** * Helper function **/