From 3971ea797c44d49369e153d698d25427842529fe Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sat, 11 May 2013 09:15:19 +0200 Subject: [PATCH] further work on sharecounter cron, almost all features available now --- cronjobs/sharecounter.php | 26 +++++----- public/include/autoloader.inc.php | 1 + public/include/classes/block.class.php | 16 +++++++ public/include/classes/share.class.php | 45 ++++++++++++----- public/include/classes/statistics.class.php | 48 +++++++++++++++++++ public/include/classes/user.class.php | 2 +- public/include/pages/statistics/pool.inc.php | 9 ++-- .../mmcFE/statistics/blocks/blocks_found.tpl | 10 ++-- 8 files changed, 124 insertions(+), 33 deletions(-) create mode 100644 public/include/classes/statistics.class.php diff --git a/cronjobs/sharecounter.php b/cronjobs/sharecounter.php index adab09dc..77b1d6ca 100644 --- a/cronjobs/sharecounter.php +++ b/cronjobs/sharecounter.php @@ -23,32 +23,36 @@ require_once('shared.inc.php'); // Fetch our last block found from the DB as a starting point $aAllBlocks = $block->getAll('ASC'); - foreach ($aAllBlocks as $iIndex => $aBlock) { if (!$aBlock['accounted']) { - $iPrevBlockTime = $aAllBlocks[$iIndex - 1]['time']; + $iPrevBlockTime = @$aAllBlocks[$iIndex - 1]['time']; if (!$iPrevBlockTime) { $iPrevBlockTime = 0; } $aAccountShares = $share->getSharesForAccountsByTimeframe($aBlock['time'], $iPrevBlockTime); $iRoundShares = $share->getRoundSharesByTimeframe($aBlock['time'], $iPrevBlockTime); $strFinder = $share->getFinderByTimeframe($aBlock['time'], $iPrevBlockTime); - echo "Height\tTime\t\tShares\tFinder\n"; - echo $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $strFinder . "\n\n"; - echo "ID\tUsername\tValid\tInvalid\tPercentage\tPayout\n"; + echo "ID\tHeight\tTime\t\tShares\tFinder\n"; + echo $aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $strFinder . "\n\n"; + echo "ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n"; foreach ($aAccountShares as $key => $aData) { - $aData['percentage'] = ( 100 / $iRoundShares ) * $aData['valid']; - $aData['payout'] = ( $aData['percentage'] / 100 ) * $config['reward']; + $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 10),10); + $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 10), 10); echo $aData['id'] . "\t" . $aData['username'] . "\t" . $aData['valid'] . "\t" . $aData['invalid'] . "\t" . $aData['percentage'] . "\t" . - $aData['payout'] . "\t" . - "\n"; + $aData['payout'] . "\t"; + if (!$statistics->updateShareStatistics($aData, $aBlock['id'])) { + echo "Stats Failed" . "\n"; + } } echo "------------------------------------------------------------------------\n\n"; + + // Now that we have all shares counted internally let's update the tables + // Set shares as counted and mark block as accounted for + // $share->setCountedByTimeframe($aBlock['time'], $iPrevBlockTime); + // $block->setAccounted($aBlock['blockhash']); } - // TODO: We have accounted all shares for a block so mark it accounted - // and delete all the shares we just accounted for. } diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index 37b3d09a..015d5590 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -8,4 +8,5 @@ require_once(INCLUDE_DIR . '/smarty.inc.php'); require_once(CLASS_DIR . '/user.class.php'); require_once(CLASS_DIR . '/block.class.php'); require_once(CLASS_DIR . '/share.class.php'); +require_once(CLASS_DIR . '/statistics.class.php'); require_once(CLASS_DIR . '/settings.class.php'); diff --git a/public/include/classes/block.class.php b/public/include/classes/block.class.php index e24f82d4..20218853 100644 --- a/public/include/classes/block.class.php +++ b/public/include/classes/block.class.php @@ -61,6 +61,22 @@ class Block { return false; } + public function setAccounted($blockhash='') { + if ($blockhash == '') return false; + $stmt = $this->mysqli->prepare("UPDATE $this->table SET accounted = 1 WHERE blockhash = ?"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('s', $blockhash); + if (!$stmt->execute()) { + $this->debug->append("Failed to execute statement: " . $stmt->error); + $stmt->close(); + return false; + } + $stmt->close(); + return true; + } + return false; + } + private function checkStmt($bState) { if ($bState ===! true) { $this->debug->append("Failed to prepare statement: " . $this->mysqli->error); diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index 21772042..b7928dd0 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -40,6 +40,8 @@ class Share { UNIX_TIMESTAMP(time) BETWEEN ? AND ? AND our_result = 'Y' + AND + counted = 0 GROUP BY account ) validT LEFT JOIN @@ -52,12 +54,13 @@ class Share { UNIX_TIMESTAMP(time) BETWEEN ? AND ? AND our_result = 'N' + AND + counted = 0 GROUP BY account ) invalidT ON validT.account = invalidT.account INNER JOIN accounts a ON a.username = validT.account GROUP BY a.username DESC"); - echo $this->mysqli->error; if ($this->checkStmt($stmt)) { $stmt->bind_param('iiii', $old, $current, $old, $current); $stmt->execute(); @@ -73,9 +76,9 @@ class Share { count(id) as total FROM $this->table WHERE our_result = 'Y' - AND UNIX_TIMESTAMP(time) BETWEEN ? AND ? + AND UNIX_TIMESTAMP(time) BETWEEN ? AND ? + AND counted = 0 "); - echo $this->mysqli->error; if ($this->checkStmt($stmt)) { $stmt->bind_param('ii', $old, $current); $stmt->execute(); @@ -85,20 +88,38 @@ class Share { } return false; } - public function getFinderByTimeframe($current='', $old='') { - $stmt = $this->mysqli->prepare("SELECT - SUBSTRING_INDEX( `username` , '.', 1 ) AS account - FROM $this->table - WHERE upstream_result = 'Y' - AND UNIX_TIMESTAMP(time) BETWEEN ? AND ? - ORDER BY id DESC"); - echo $this->mysqli->error; + + public function setCountedByTimeframe($current='', $old='') { + $stmt = $this->mysqli->prepare("UPDATE $this->table + SET + counted = 1 + WHERE + UNIX_TIMESTAMP(time) BETWEEN ? AND ? + AND counted = 0 + "); if ($this->checkStmt($stmt)) { $stmt->bind_param('ii', $old, $current); $stmt->execute(); $result = $stmt->get_result(); $stmt->close(); - return $result->fetch_object()->account; + return true; + } + return false; + } + + public function getFinderByTimeframe($current='', $old='') { + $stmt = $this->mysqli->prepare("SELECT + SUBSTRING_INDEX( `username` , '.', 1 ) AS account + FROM $this->table + WHERE upstream_result = 'Y' + AND UNIX_TIMESTAMP(time) BETWEEN ? AND ? + ORDER BY id DESC"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('ii', $old, $current); + $stmt->execute(); + $result = $stmt->get_result(); + $stmt->close(); + return @$result->fetch_object()->account; } return false; } diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php new file mode 100644 index 00000000..27a85445 --- /dev/null +++ b/public/include/classes/statistics.class.php @@ -0,0 +1,48 @@ +debug = $debug; + $this->mysqli = $mysqli; + $this->debug->append("Instantiated Share class", 2); + } + + // get and set methods + private function setErrorMessage($msg) { + $this->sError = $msg; + } + public function getError() { + return $this->sError; + } + + public function updateShareStatistics($aStats, $iBlockId) { + $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?, ?)"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('iiiddi', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId); + if ($stmt->execute()) { + return true; + } + } + return false; + } + + private function checkStmt($bState) { + if ($bState ===! true) { + $this->debug->append("Failed to prepare statement: " . $this->mysqli->error); + $this->setErrorMessage('Failed to prepare statement'); + return false; + } + return true; + } +} + +$statistics = new Statistics($debug, $mysqli, SALT); diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index b64b1439..44ff027a 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -7,7 +7,7 @@ if (!defined('SECURITY')) class User { private $sError = ''; private $userID = false; - private $table = 'webUsers'; + private $table = 'accounts'; private $user = array(); private $tableAccountBalance = 'accountBalance'; private $tablePoolWorker = 'pool_worker'; diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 2434ad8d..add92cf4 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -14,16 +14,16 @@ if ($bitcoin->can_connect() === true){ $_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to pushpool service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg'); } +/** Disabled Stats // Top 15 hashrate list -$stmt = $mysqli->prepare("SELECT username, id, hashrate FROM webUsers WHERE hashrate != '0' ORDER BY hashrate DESC LIMIT 15"); +$stmt = $mysqli->prepare("SELECT username, id, hashrate FROM accounts WHERE hashrate != '0' ORDER BY hashrate DESC LIMIT 15"); $stmt->execute(); $hashrates= $stmt->get_result(); $aHashData = $hashrates->fetch_all(MYSQLI_ASSOC); $stmt->close(); // Top 15 Contributors -# SELECT id, shares_this_round AS shares FROM webUsers WHERE shares_this_round > 0 ORDER BY shares DESC LIMIT -$stmt = $mysqli->prepare("SELECT id, shares_this_round AS shares, username FROM webUsers WHERE shares_this_round > 0 ORDER BY shares DESC LIMIT 15"); +$stmt = $mysqli->prepare("SELECT id, shares_this_round AS shares, username FROM accounts WHERE shares_this_round > 0 ORDER BY shares DESC LIMIT 15"); $stmt->execute(); $contributors = $stmt->get_result(); $aContributorData = $contributors->fetch_all(MYSQLI_ASSOC); @@ -36,8 +36,9 @@ $blocks = $stmt->get_result(); $aBlockData = $blocks->fetch_array(); $stmt->close(); + */ // Grab the last 10 blocks found -$stmt = $mysqli->prepare("SELECT DISTINCT w.shareCount AS shares, w.username, n.blockNumber, n.confirms, n.timestamp FROM winning_shares w, networkBlocks n WHERE w.blockNumber = n.blockNumber ORDER BY w.blockNumber DESC LIMIT 10"); +$stmt = $mysqli->prepare("SELECT DISTINCT * FROM blocks ORDER BY height DESC LIMIT 10"); $stmt->execute(); $blocksfound = $stmt->get_result(); $aBlocksFoundData = $blocksfound->fetch_all(MYSQLI_ASSOC); diff --git a/public/templates/mmcFE/statistics/blocks/blocks_found.tpl b/public/templates/mmcFE/statistics/blocks/blocks_found.tpl index 0f67dae8..9fabfd93 100644 --- a/public/templates/mmcFE/statistics/blocks/blocks_found.tpl +++ b/public/templates/mmcFE/statistics/blocks/blocks_found.tpl @@ -15,11 +15,11 @@ {section block $BLOCKSFOUND} {assign var=user value="."|explode:$BLOCKSFOUND[block].username} - {$BLOCKSFOUND[block].blockNumber} - {if $BLOCKSFOUND[block].confirms >= 120}Confirmed{else}{$BLOCKSFOUND[block].confirms - 120} left{/if} - {$user.0} - {$BLOCKSFOUND[block].timestamp|date_format:"%d/%m/%Y %H:%M:%S"} - {$BLOCKSFOUND[block].shares|number_format} + {$BLOCKSFOUND[block].height} + {if $BLOCKSFOUND[block].confirmations >= 120}Confirmed{else}{$BLOCKSFOUND[block].confirms - 120} left{/if} + {$user.0|default:"unknown"} + {$BLOCKSFOUND[block].time|date_format:"%d/%m/%Y %H:%M:%S"} + {$BLOCKSFOUND[block].difficulty|number_format} {/section}