diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index cea1a7a3..bf849769 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -20,10 +20,20 @@ require_once(INCLUDE_DIR . '/database.inc.php'); require_once(INCLUDE_DIR . '/config/memcache_keys.inc.php'); require_once(INCLUDE_DIR . '/config/error_codes.inc.php'); -// We need to load these two first +// We need to load these first require_once(CLASS_DIR . '/base.class.php'); +require_once(CLASS_DIR . '/coin_base.class.php'); require_once(CLASS_DIR . '/setting.class.php'); +// Now decide on which coin class to load and instantiate +if (file_exists(CLASS_DIR . '/coin_' . $config['algorithm'] . '.class.php')) { + require_once(CLASS_DIR . '/coin_' . $config['algorithm'] . '.class.php'); + $coin = new Coin(); + $coin->setConfig($config); +} else { + die('Unable to load your coins class definition for ' . $config['algorithm']); +} + // We need this one in here to properly set our theme require_once(INCLUDE_DIR . '/lib/Mobile_Detect.php'); @@ -46,6 +56,9 @@ require_once(CLASS_DIR . '/template.class.php'); // Load smarty now that we have our theme defined require_once(INCLUDE_DIR . '/smarty.inc.php'); +// Load our base coin class +// require_once(CLASS_DIR . '/coin_base.class.php'); + // Load everything else in proper order require_once(CLASS_DIR . '/mail.class.php'); require_once(CLASS_DIR . '/tokentype.class.php'); diff --git a/public/include/classes/base.class.php b/public/include/classes/base.class.php index 34aa33cd..1bb27e3a 100644 --- a/public/include/classes/base.class.php +++ b/public/include/classes/base.class.php @@ -19,6 +19,9 @@ class Base { public function setDebug($debug) { $this->debug = $debug; } + public function setCoin($coin) { + $this->coin = $coin; + } public function setLog($log) { $this->log = $log; } diff --git a/public/include/classes/coin_base.class.php b/public/include/classes/coin_base.class.php new file mode 100644 index 00000000..625d98a6 --- /dev/null +++ b/public/include/classes/coin_base.class.php @@ -0,0 +1,42 @@ +config['target_bits']) / $interval / 1000; + } + + /** + * Calculate estimated shares of this coin, this is using baseline + * according to our configuration difficulty + **/ + public function calcEstaimtedShares($dDifficulty) { + return (int)round((pow(2, (32 - $this->config['target_bits'])) * $dDifficulty) / pow(2, ($this->config['difficulty'] - 16))); + } + + /** + * Calculate our networks expected time per block + **/ + public function calcNetworkExpectedTimePerBlock($dDifficulty, $dNetworkHashrate) { + return pow(2, 32) * $dDifficulty / $dNetworkHashrate; + } + /** + * Calculate next expected difficulty based on current difficulty + **/ + public function calcExpectedNextDifficulty($dDifficulty, $dNetworkHashrate) { + return round($dDifficulty * $this->config['cointarget'] / $this->calcNetworkExpectedTimePerBlock($dDifficulty, $dNetworkHashrate), 8); + } +} + +?> diff --git a/public/include/classes/coin_scrypt.class.php b/public/include/classes/coin_scrypt.class.php new file mode 100644 index 00000000..cf23476e --- /dev/null +++ b/public/include/classes/coin_scrypt.class.php @@ -0,0 +1,12 @@ + diff --git a/public/include/classes/coin_sha256d.class.php b/public/include/classes/coin_sha256d.class.php new file mode 100644 index 00000000..cf23476e --- /dev/null +++ b/public/include/classes/coin_sha256d.class.php @@ -0,0 +1,12 @@ + diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 1a9e1b3c..172a0530 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -221,19 +221,22 @@ class Statistics extends Base { SELECT ( ( - SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0) AS hashrate + SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty))), 0) AS shares FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' ) + ( - SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0) AS hashrate + SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty))), 0) AS shares FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' ) - ) AS hashrate + ) AS shares FROM DUAL"); - if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setStaticCache(__FUNCTION__, $result->fetch_object()->hashrate); + if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) { + $hashrate = $this->coin->calcHashrate($result->fetch_object()->shares, $interval); + return $this->memcache->setStaticCache(__FUNCTION__, $hashrate); + } return $this->sqlError(); } @@ -446,7 +449,7 @@ class Statistics extends Base { /** * Fetch all user hashrates based on shares and archived shares - * @return data integer Current Hashrate in khash/s + * @return data array Set of all user stats **/ public function getAllUserMiningStats($interval=180) { $this->debug->append("STA " . __METHOD__, 4); @@ -454,7 +457,7 @@ class Statistics extends Base { SELECT a.id AS id, a.username AS account, - IFNULL(ROUND(SUM(t1.difficulty) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000, 2), 0) AS hashrate, + IFNULL(SUM(t1.difficulty), 0) AS shares, ROUND(COUNT(t1.id) / ?, 2) AS sharerate, IFNULL(AVG(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS avgsharediff FROM ( @@ -476,12 +479,13 @@ class Statistics extends Base { ON SUBSTRING_INDEX( t1.username, '.', 1 ) = a.username WHERE a.id IS NOT NULL GROUP BY account - ORDER BY hashrate DESC + ORDER BY shares DESC "); - if ($this->checkStmt($stmt) && $stmt->bind_param("iiii", $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) { + if ($this->checkStmt($stmt) && $stmt->bind_param("iii", $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) { $aData = array(); while ($row = $result->fetch_assoc()) { $aData['data'][$row['id']] = $row; + $aData['data'][$row['id']]['hashrate'] = $this->coin->calcHashrate($row['shares'], $interval); } return $this->memcache->setStaticCache(STATISTICS_ALL_USER_HASHRATES, $aData, 600); } else { @@ -527,7 +531,7 @@ class Statistics extends Base { $stmt = $this->mysqli->prepare(" SELECT IFNULL(COUNT(*) / ?, 0) AS sharerate, - IFNULL(ROUND(SUM(difficulty) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000, 2), 0) AS hashrate, + IFNULL(SUM(difficulty), 0) AS shares, IFNULL(AVG(difficulty), 0) AS avgsharediff FROM ( SELECT @@ -547,27 +551,11 @@ class Statistics extends Base { AND our_result = 'Y' ) AS temp"); $username = $username . ".%"; - if ($this->checkStmt($stmt) && $stmt->bind_param("iisisi", $interval, $interval, $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) - return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_assoc()); - return $this->sqlError(); - } - - /** - * Get hashrate for a specific worker - * @param username string username - * @return data int Current hashrate in khash/s - **/ - public function getWorkerHashrate($workername, $worker_id=NULL, $interval=180) { - $this->debug->append("STA " . __METHOD__, 4); - if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data; - $stmt = $this->mysqli->prepare(" - SELECT IFNULL(ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 600 / 1000), 0) AS hashrate - FROM " . $this->share->getTableName() . " AS - WHERE username = '?' - AND our_result = 'Y' - AND time > DATE_SUB(now(), INTERVAL ? SECOND)"); - if ($this->checkStmt($stmt) && $stmt->bind_param("si", $workername, $interval) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__ . $worker_id, (float)$result->fetch_object()->hashrate); + if ($this->checkStmt($stmt) && $stmt->bind_param("isisi", $interval, $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) { + $aData = $result->fetch_assoc(); + $aData['hashrate'] = $this->coin->calcHashrate($aData['shares'], $interval); + return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData); + } return $this->sqlError(); } @@ -633,7 +621,7 @@ class Statistics extends Base { a.username AS account, a.donate_percent AS donate_percent, a.is_anonymous AS is_anonymous, - IFNULL(ROUND(SUM(t1.difficulty) * POW(2, " . $this->config['target_bits'] . ") / 600 / 1000, 2), 0) AS hashrate + IFNULL(SUM(t1.difficulty), 0) AS shares FROM ( SELECT id, IFNULL(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0) AS difficulty, username FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND our_result = 'Y' @@ -643,9 +631,17 @@ class Statistics extends Base { LEFT JOIN " . $this->user->getTableName() . " AS a ON SUBSTRING_INDEX( t1.username, '.', 1 ) = a.username GROUP BY account - ORDER BY hashrate DESC LIMIT ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setStaticCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); + ORDER BY shares DESC LIMIT ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) { + $aData = array(); + $count = 0; + while ($row = $result->fetch_assoc()) { + $aData[$count] = $row; + $aData[$count]['hashrate'] = $this->coin->calcHashrate($row['shares'], 600); + $count++; + } + return $this->memcache->setStaticCache(__FUNCTION__ . $type . $limit, $aData); + } return $this->sqlError(); break; } @@ -663,7 +659,7 @@ class Statistics extends Base { $stmt = $this->mysqli->prepare(" SELECT id, - IFNULL(ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 3600 / 1000), 0) AS hashrate, + IFNULL(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS shares, HOUR(time) AS hour FROM " . $this->share->getTableName() . " WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) @@ -674,7 +670,7 @@ class Statistics extends Base { UNION SELECT share_id, - IFNULL(ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 3600 / 1000), 0) AS hashrate, + IFNULL(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS shares, HOUR(time) AS hour FROM " . $this->share->getArchiveTableName() . " WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) @@ -688,7 +684,7 @@ class Statistics extends Base { // Initilize array for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; // Fill data - while ($row = $result->fetch_assoc()) $aData[$row['hour']] += $row['hashrate']; + while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int) $this->coin->calcHashrate($row['shares'], 3600); return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData); } return $this->sqlError(); @@ -705,7 +701,7 @@ class Statistics extends Base { $stmt = $this->mysqli->prepare(" SELECT id, - IFNULL(ROUND(SUM(IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 3600 / 1000), 0) AS hashrate, + IFNULL(SUM(IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) AS shares, HOUR(s.time) AS hour FROM " . $this->share->getTableName() . " AS s WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) @@ -715,7 +711,7 @@ class Statistics extends Base { UNION SELECT share_id, - IFNULL(ROUND(SUM(IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)) * POW(2, " . $this->config['target_bits'] . ") / 3600 / 1000), 0) AS hashrate, + IFNULL(SUM(IF(s.difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) AS shares, HOUR(s.time) AS hour FROM " . $this->share->getArchiveTableName() . " AS s WHERE time <= FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(NOW())/(60*60))*(60*60)) @@ -727,7 +723,7 @@ class Statistics extends Base { // Initilize array for ($i = 0; $i < 24; $i++) $aData[($iStartHour + $i) % 24] = 0; // Fill data - while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int) $row['hashrate']; + while ($row = $result->fetch_assoc()) $aData[$row['hour']] += (int) $this->coin->calcHashrate($row['shares'], 3600); return $this->memcache->setCache(__FUNCTION__, $aData); } return $this->sqlError(); @@ -788,17 +784,19 @@ class Statistics extends Base { $this->debug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__ . $hour)) return $data; $stmt = $this->mysqli->prepare(" - SELECT - IFNULL(COUNT(id), 0) as count, + SELECT + IFNULL(COUNT(id), 0) as count, IFNULL(AVG(difficulty), 0) as average, - IFNULL(ROUND(SUM((POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty) / POW(2, (" . $this->config['difficulty'] . " -16))), 0), 0) AS expected, IFNULL(ROUND(SUM(shares)), 0) as shares, - IFNULL(SUM(amount), 0) as rewards + IFNULL(SUM(amount), 0) as rewards FROM " . $this->block->getTableName() . " WHERE FROM_UNIXTIME(time) > DATE_SUB(now(), INTERVAL ? HOUR) AND confirmations >= 1"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $hour) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__ . $hour, $result->fetch_assoc()); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $hour) && $stmt->execute() && $result = $stmt->get_result()) { + $aData = $result->fetch_assoc(); + $aData['expected'] = $this->coin->calcEstaimtedShares($aData['average']); + return $this->memcache->setCache(__FUNCTION__ . $hour, $aData); + } return $this->sqlError(); } @@ -808,7 +806,7 @@ class Statistics extends Base { * @return shares integer Share count **/ public function getEstimatedShares($dDiff) { - return round((POW(2, (32 - $this->config['target_bits'])) * $dDiff) / pow(2, ($this->config['difficulty'] - 16))); + return $this->coin->calcEstaimtedShares($dDiff); } /** @@ -829,7 +827,7 @@ class Statistics extends Base { return $this->memcache->setCache(__FUNCTION__, $this->config['cointarget']); } - return $this->memcache->setCache(__FUNCTION__, pow(2, 32) * $dDifficulty / $dNetworkHashrate); + return $this->memcache->setCache(__FUNCTION__, $this->coin->calcNetworkExpectedTimePerBlock($dDifficulty, $dNetworkHashrate)); } /** @@ -841,11 +839,13 @@ class Statistics extends Base { if ($this->bitcoin->can_connect() === true) { $dDifficulty = $this->bitcoin->getdifficulty(); + $dNetworkHashrate = $this->bitcoin->getnetworkhashps(); } else { $dDifficulty = 1; + $dNetworkHashrate = 1; } - return $this->memcache->setCache(__FUNCTION__, round($dDifficulty * $this->config['cointarget'] / $this->getNetworkExpectedTimePerBlock(), 8)); + return $this->memcache->setCache(__FUNCTION__, $this->coin->calcExpectedNextDifficulty($dDifficulty, $dNetworkHashrate)); } /** @@ -922,5 +922,5 @@ $statistics->setMemcache($memcache); $statistics->setConfig($config); $statistics->setBitcoin($bitcoin); $statistics->setErrorCodes($aErrorCodes); - +$statistics->setCoin($coin); ?>