diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 4b5a0e9e..d7036ee0 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -63,7 +63,7 @@ if ($config['pps']['reward']['type'] == 'blockavg' && $block->getBlockCount() > } // Per-share value to be paid out to users -$pps_value = round($pps_reward / (pow(2, $config['target_bits']) * $dDifficulty), 12); +$pps_value = round($pps_reward / (pow(2, $coin->getTargetBits()) * $dDifficulty), 12); // Find our last share accounted and last inserted share for PPS calculations if (!$iPreviousShareId = $setting->getValue('pps_last_share_id')) { diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index 7813b5d6..3091035c 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -2,13 +2,6 @@ (SECURITY == "*)WT#&YHfd" && SECHASH_CHECK) ? die("public/index.php -> Set a new SECURITY value to continue") : 0; $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; -// SHA/Scrypt check -if (empty($config['algorithm']) || $config['algorithm'] == 'scrypt') { - $config['target_bits'] = 16; -} else { - $config['target_bits'] = 32; -} - // Default classes require_once(INCLUDE_DIR . '/lib/KLogger.php'); require_once(CLASS_DIR . '/logger.class.php'); @@ -20,10 +13,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 . '/coins/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 . '/coins/coin_' . $config['algorithm'] . '.class.php')) { + require_once(CLASS_DIR . '/coins/coin_' . $config['algorithm'] . '.class.php'); + $coin = new Coin(); + $coin->setConfig($config); +} else { + die('Unable to load your coins class definition for ' . $config['algorithm']); +} + // Detect device if ( PHP_SAPI == 'cli') { // Create a new compile folder just for crons 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/coins/coin_base.class.php b/public/include/classes/coins/coin_base.class.php new file mode 100644 index 00000000..79e74c58 --- /dev/null +++ b/public/include/classes/coins/coin_base.class.php @@ -0,0 +1,52 @@ +target_bits; + } + + /** + * Calculate our hashrate based on shares inserted to DB + * We use diff1 share values, not a baseline one + **/ + public function calcHashrate($shares, $interval) { + return $shares * pow(2, $this->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->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/coins/coin_scrypt.class.php b/public/include/classes/coins/coin_scrypt.class.php new file mode 100644 index 00000000..bf829ab1 --- /dev/null +++ b/public/include/classes/coins/coin_scrypt.class.php @@ -0,0 +1,13 @@ + diff --git a/public/include/classes/coins/coin_sha256d.class.php b/public/include/classes/coins/coin_sha256d.class.php new file mode 100644 index 00000000..1147770e --- /dev/null +++ b/public/include/classes/coins/coin_sha256d.class.php @@ -0,0 +1,13 @@ + diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 786c00d1..b04bff4a 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -46,46 +46,48 @@ class Statistics extends Base { IFNULL(SUM(IF(confirmations = -1, 1, 0)), 0) AS TotalOrphan, IFNULL(SUM(IF(confirmations > 0, difficulty, 0)), 0) AS TotalDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1, shares, 0))), 0) AS TotalShares, - IFNULL(ROUND(SUM(IF(confirmations > -1, POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS TotalEstimatedShares, IFNULL(SUM(IF(confirmations > -1, amount, 0)), 0) AS TotalAmount, IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourTotal, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourValid, IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourOrphan, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), difficulty, 0)), 0) AS 1HourDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), shares, 0))), 0) AS 1HourShares, - IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS 1HourEstimatedShares, IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), amount, 0)), 0) AS 1HourAmount, IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourTotal, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourValid, IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourOrphan, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), difficulty, 0)), 0) AS 24HourDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), shares, 0))), 0) AS 24HourShares, - IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS 24HourEstimatedShares, IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), amount, 0)), 0) AS 24HourAmount, IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysTotal, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysValid, IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysOrphan, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), difficulty, 0)), 0) AS 7DaysDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), shares, 0))), 0) AS 7DaysShares, - IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS 7DaysEstimatedShares, IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), amount, 0)), 0) AS 7DaysAmount, IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksTotal, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksValid, IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksOrphan, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), difficulty, 0)), 0) AS 4WeeksDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), shares, 0))), 0) AS 4WeeksShares, - IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS 4WeeksEstimatedShares, IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), amount, 0)), 0) AS 4WeeksAmount, IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthTotal, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthValid, IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthOrphan, IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), difficulty, 0)), 0) AS 12MonthDifficulty, IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), shares, 0))), 0) AS 12MonthShares, - IFNULL(ROUND(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), POW(2, ( 32 - " . $this->config['target_bits'] . " )) * difficulty / POW(2, (" . $this->config['difficulty'] . " -16)), 0))), 0) AS 12MonthEstimatedShares, IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), amount, 0)), 0) AS 12MonthAmount FROM " . $this->block->getTableName()); - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__, $result->fetch_assoc()); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { + $aData = $result->fetch_assoc(); + $aData['TotalEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['TotalDifficulty']); + $aData['1HourEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['1HourDifficulty']); + $aData['24HourEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['24HourDifficulty']); + $aData['7DaysEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['7DaysDifficulty']); + $aData['4WeeksEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['4WeeksDifficulty']); + $aData['12MonthEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['12MonthDifficulty']); + return $this->memcache->setCache(__FUNCTION__, $aData); + } return $this->sqlError(); } @@ -102,7 +104,7 @@ class Statistics extends Base { b.*, a.username AS finder, a.is_anonymous AS is_anonymous, - ROUND((difficulty * POW(2, 32 - " . $this->config['target_bits'] . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares + ROUND((difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares FROM " . $this->block->getTableName() . " AS b LEFT JOIN " . $this->user->getTableName() . " AS a ON b.account_id = a.id @@ -125,7 +127,7 @@ class Statistics extends Base { b.*, a.username AS finder, a.is_anonymous AS is_anonymous, - ROUND((difficulty * POW(2, 32 - " . $this->config['target_bits'] . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares + ROUND((difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . ")) / POW(2, (" . $this->config['difficulty'] . " -16)), 0) AS estshares FROM " . $this->block->getTableName() . " AS b LEFT JOIN " . $this->user->getTableName() . " AS a ON b.account_id = a.id @@ -221,19 +223,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 +451,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 +459,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 +481,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 +533,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 +553,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 +623,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 +633,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 +661,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 +672,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 +686,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 +703,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 +713,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 +725,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 +786,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 +808,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 +829,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 +841,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)); } /** @@ -890,7 +892,7 @@ class Statistics extends Base { $pps_reward = $this->config['pps']['reward']['default']; } } - return round($pps_reward / (pow(2, $this->config['target_bits']) * $dDifficulty), 12); + return round($pps_reward / (pow(2, $this->coin->getTargetBits()) * $dDifficulty), 12); } /** @@ -922,5 +924,5 @@ $statistics->setMemcache($memcache); $statistics->setConfig($config); $statistics->setBitcoin($bitcoin); $statistics->setErrorCodes($aErrorCodes); - +$statistics->setCoin($coin); ?> diff --git a/public/include/classes/worker.class.php b/public/include/classes/worker.class.php index b661bd22..9b7344ac 100644 --- a/public/include/classes/worker.class.php +++ b/public/include/classes/worker.class.php @@ -74,14 +74,14 @@ class Worker extends Base { ( SELECT COUNT(id) FROM " . $this->share->getArchiveTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL ? SECOND)) AS count_all_archive, ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL ? SECOND) ) + ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getArchiveTableName() . " WHERE username = w.username @@ -117,14 +117,14 @@ class Worker extends Base { ( SELECT COUNT(id) FROM " . $this->share->getArchiveTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL ? SECOND)) AS count_all_archive, ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL ? SECOND) ) + ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getArchiveTableName() . " WHERE username = w.username @@ -158,14 +158,14 @@ class Worker extends Base { IFNULL(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0) AS difficulty, ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL ? SECOND) ) + ( SELECT - IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000), 0), 0) AS hashrate + IFNULL(IF(our_result='Y', ROUND(SUM(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) * POW(2, " . $this->coin->getTargetBits() . ") / ? / 1000), 0), 0) AS hashrate FROM " . $this->share->getArchiveTableName() . " WHERE username = w.username @@ -272,5 +272,5 @@ $worker->setShare($share); $worker->setConfig($config); $worker->setUser($user); $worker->setErrorCodes($aErrorCodes); - +$worker->setCoin($coin); ?> diff --git a/public/include/pages/api/getdashboarddata.inc.php b/public/include/pages/api/getdashboarddata.inc.php index db70772a..b4b3575b 100644 --- a/public/include/pages/api/getdashboarddata.inc.php +++ b/public/include/pages/api/getdashboarddata.inc.php @@ -98,7 +98,7 @@ $data = array( 'shares' => array( 'valid' => $aRoundShares['valid'], 'invalid' => $aRoundShares['invalid'], 'invalid_percent' => $dPoolInvalidPercent, 'estimated' => $iEstShares, 'progress' => $dEstPercent ), 'price' => $aPrice, 'difficulty' => pow(2, $config['difficulty'] - 16), - 'target_bits' => $config['difficulty'] + 'target_bits' => $coin->getTargetBits() ), 'system' => array( 'load' => sys_getloadavg() ), 'network' => array( 'hashrate' => $dNetworkHashrateAdjusted, 'difficulty' => $dDifficulty, 'block' => $iBlock, 'esttimeperblock' => round($dExpectedTimePerBlock ,2), 'nextdifficulty' => $dEstNextDifficulty, 'blocksuntildiffchange' => $iBlocksUntilDiffChange ), diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index 3befa0e5..e11fdea4 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -75,7 +75,7 @@ $aGlobal = array( 'disable_contactform' => $setting->getValue('disable_contactform'), 'disable_contactform_guest' => $setting->getValue('disable_contactform_guest'), 'algorithm' => $config['algorithm'], - 'target_bits' => $config['target_bits'], + 'target_bits' => $coin->getTargetBits(), 'accounts' => $config['accounts'], 'disable_invitations' => $setting->getValue('disable_invitations'), 'disable_notifications' => $setting->getValue('disable_notifications'),