From ed769f9659616176c33101749b94e098474bce50 Mon Sep 17 00:00:00 2001 From: Frederick Behrends Date: Mon, 16 Dec 2013 21:51:31 +0100 Subject: [PATCH] Moved Calculation of "Expected Time Per Block (Network)" and "Next Difficulty" to Statistics Class and fixed Dashboard-API for Ajax Refresh --- public/include/classes/statistics.class.php | 36 +++++++++++++++++++ .../pages/api/getdashboarddata.inc.php | 6 ++-- public/include/pages/dashboard.inc.php | 4 +-- public/include/pages/statistics/pool.inc.php | 4 +-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index fc24e7f9..01982c2a 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -800,6 +800,41 @@ class Statistics extends Base { public function getEstimatedShares($dDiff) { return round((POW(2, (32 - $this->config['target_bits'])) * $dDiff) / pow(2, ($this->config['difficulty'] - 16))); } + + /** + * Get the Expected Time per Block in the whole Network in seconde + * @return seconds double Seconds per Block + */ + public function getNetworkExpectedTimePerBlock(){ + if ($data = $this->memcache->get(__FUNCTION__)) return $data; + + if ($this->bitcoin->can_connect() === true) { + $dNetworkHashrate = $this->bitcoin->getnetworkhashps(); + $dDifficulty = $this->bitcoin->getdifficulty(); + } else { + $dNetworkHashrate = 0; + $dDifficulty = 1; + } + + return pow(2, 32) * $dDifficulty / $dNetworkHashrate; + } + + /** + * Get the Expected next Difficulty + * @return difficulty double Next difficulty + */ + public function getExpectedNextDifficulty(){ + if ($data = $this->memcache->get(__FUNCTION__)) return $data; + + if ($this->bitcoin->can_connect() === true) { + $dDifficulty = $this->bitcoin->getdifficulty(); + } else { + $dDifficulty = 1; + } + + return round($dDifficulty * $this->config['cointarget'] / $this->getNetworkExpectedTimePerBlock(), 8); + } + } $statistics = new Statistics(); @@ -810,6 +845,7 @@ $statistics->setUser($user); $statistics->setBlock($block); $statistics->setMemcache($memcache); $statistics->setConfig($config); +$statistics->setBitcoin($bitcoin); $statistics->setErrorCodes($aErrorCodes); ?> diff --git a/public/include/pages/api/getdashboarddata.inc.php b/public/include/pages/api/getdashboarddata.inc.php index 78f5206a..5be24aee 100644 --- a/public/include/pages/api/getdashboarddata.inc.php +++ b/public/include/pages/api/getdashboarddata.inc.php @@ -104,8 +104,8 @@ if ($iEstShares > 0 && $aRoundShares['valid'] > 0) { $dEstPercent = 0; } -$dExpectedTimePerBlock = pow(2, $config['target_bits']) * $dDifficulty / $dNetworkHashrate; -$dEstNextDifficulty = round($dDifficulty * $config['cointarget'] / $dExpectedTimePerBlock, 8); +$dExpectedTimePerBlock = $statistics->getNetworkExpectedTimePerBlock(); +$dEstNextDifficulty = $statistics->getExpectedNextDifficulty(); // Output JSON format $data = array( @@ -126,7 +126,7 @@ $data = array( 'target_bits' => $config['difficulty'] ), 'system' => array( 'load' => sys_getloadavg() ), - 'network' => array( 'hashrate' => $dNetworkHashrateAdjusted, 'difficulty' => $dDifficulty, 'block' => $iBlock ), + 'network' => array( 'hashrate' => $dNetworkHashrateAdjusted, 'difficulty' => $dDifficulty, 'block' => $iBlock, 'esttimeperblock' => round($dExpectedTimePerBlock ,2), 'nextdifficulty' => $dEstNextDifficulty ), ); echo $api->get_json($data); diff --git a/public/include/pages/dashboard.inc.php b/public/include/pages/dashboard.inc.php index a5d1601b..86ccc331 100644 --- a/public/include/pages/dashboard.inc.php +++ b/public/include/pages/dashboard.inc.php @@ -35,8 +35,8 @@ if ($user->isAuthenticated()) { // Avoid confusion, ensure our nethash isn't higher than poolhash if ($iCurrentPoolHashrate > $dNetworkHashrate) $dNetworkHashrate = $iCurrentPoolHashrate; - $dExpectedTimePerBlock = pow(2, $config['target_bits']) * $dDifficulty / $dNetworkHashrate; - $dEstNextDifficulty = round($dDifficulty * $config['cointarget'] / $dExpectedTimePerBlock, 8); + $dExpectedTimePerBlock = $statistics->getNetworkExpectedTimePerBlock(); + $dEstNextDifficulty = $statistics->getExpectedNextDifficulty(); // Make it available in Smarty $smarty->assign('DISABLED_DASHBOARD', $setting->getValue('disable_dashboard')); diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 3ebf2418..1e3f65f3 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -52,8 +52,8 @@ if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $dEstPercent = 0; } - $dExpectedTimePerBlock = pow(2, $config['target_bits']) * $dDifficulty / $dNetworkHashrate; - $dEstNextDifficulty = round($dDifficulty * $config['cointarget'] / $dExpectedTimePerBlock, 8); + $dExpectedTimePerBlock = $statistics->getNetworkExpectedTimePerBlock(); + $dEstNextDifficulty = $statistics->getExpectedNextDifficulty(); // Propagate content our template $smarty->assign("ESTTIME", $iEstTime);