From bbe39228e1fb528f7f3c086d81f154fdf76ab221 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:36:08 +0200 Subject: [PATCH 1/3] Bitcoin Wrapper improvements * [FEATURE] Allow for PoS/PoW Detecion in getdifficulty * [FEATURE] Allow for SHA detecion in getnetworkhashps * [IMRPOVEMENT] Added caching for bitcoin values (30s only) * Will reduce the amount of RPC calls for high load pages --- .../include/classes/bitcoinwrapper.class.php | 21 +++++++++++++++---- public/include/smarty_globals.inc.php | 13 +++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/public/include/classes/bitcoinwrapper.class.php b/public/include/classes/bitcoinwrapper.class.php index eb6d55e0..7b4372b4 100644 --- a/public/include/classes/bitcoinwrapper.class.php +++ b/public/include/classes/bitcoinwrapper.class.php @@ -25,19 +25,32 @@ class BitcoinWrapper extends BitcoinClient { public function getblockcount() { $this->oDebug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__)) return $data; - return $this->memcache->setCache(__FUNCTION__, parent::getblockcount()); + return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30); } public function getdifficulty() { $this->oDebug->append("STA " . __METHOD__, 4); if ($data = $this->memcache->get(__FUNCTION__)) return $data; - return $this->memcache->setCache(__FUNCTION__, parent::getdifficulty()); + $data = parent::getdifficulty(); + // Check for PoS/PoW coins + if (is_array($data) && array_key_exists('proof-of-work', $data)) + $data = $data['proof-of-work']; + return $this->memcache->setCache(__FUNCTION__, $data, 30); } public function getestimatedtime($iCurrentPoolHashrate) { $this->oDebug->append("STA " . __METHOD__, 4); if ($iCurrentPoolHashrate == 0) return 0; if ($data = $this->memcache->get(__FUNCTION__)) return $data; - $dDifficulty = parent::getdifficulty(); - return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate); + $dDifficulty = $this->getdifficulty(); + return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30); + } + public function getnetworkhashps() { + $this->oDebug->append("STA " . __METHOD__, 4); + if ($data = $this->memcache->get(__FUNCTION__)) return $data; + try { $dNetworkHashrate = $this->query('getnetworkhashps') / 1000; } catch (Exception $e) { + // Maybe we are SHA + try { $dNetworkHashrate = $this->query('gethashespersec') / 1000; } catch (Exception $e) { return false; } + } + return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30); } } diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index acce6725..d068b494 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -14,17 +14,10 @@ $aRoundShares = 1; // Only run these if the user is logged in $aRoundShares = $statistics->getRoundShares(); if ($bitcoin->can_connect() === true) { - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; - try { $dNetworkHashrate = $bitcoin->query('getnetworkhashps') / 1000; } catch (Exception $e) { - // Maybe we are SHA - try { $dNetworkHashrate = $bitcoin->query('gethashespersec') / 1000; } catch (Exception $e) { - $dNetworkHashrate = 0; - } - $dNetworkHashrate = 0; - } + $dDifficulty = $bitcoin->getdifficulty(); + $dNetworkHashrate = $bitcoin->getnetworkhashps(); } else { + $dDifficulty = 0; $dNetworkHashrate = 0; } From af21cafea5924d7b13cc3bdba38d2c5e107e94f8 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:39:37 +0200 Subject: [PATCH 2/3] use new bitcoin method wrapper for public API --- public/include/pages/api/public.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/pages/api/public.inc.php b/public/include/pages/api/public.inc.php index 7e7c1f6e..e8be4181 100644 --- a/public/include/pages/api/public.inc.php +++ b/public/include/pages/api/public.inc.php @@ -11,7 +11,7 @@ $aLastBlock = $block->getLast(); $aShares = $statistics->getRoundShares(); // RPC Calls -$bitcoin->can_connect() === true ? $dNetworkHashrate = $bitcoin->query('getnetworkhashps') : $dNetworkHashrate = 0; +$bitcoin->can_connect() === true ? $dNetworkHashrate = $bitcoin->getnetworkhashps() : $dNetworkHashrate = 0; echo json_encode( array( From baaf74321805245726388fe212a4197cfa7762c0 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 17 Sep 2013 09:42:41 +0200 Subject: [PATCH 3/3] use bitcoin wrapper method for getdiff calls --- public/include/pages/api/getdifficulty.inc.php | 7 +------ public/include/pages/api/getpoolstatus.inc.php | 2 -- public/include/pages/statistics.inc.php | 4 +--- public/include/pages/statistics/pool.inc.php | 2 -- 4 files changed, 2 insertions(+), 13 deletions(-) diff --git a/public/include/pages/api/getdifficulty.inc.php b/public/include/pages/api/getdifficulty.inc.php index e84d4045..54cc7ac9 100644 --- a/public/include/pages/api/getdifficulty.inc.php +++ b/public/include/pages/api/getdifficulty.inc.php @@ -11,12 +11,7 @@ $id = $user->checkApiKey($_REQUEST['api_key']); // Fetch data from wallet if ($bitcoin->can_connect() === true){ - if (!$dDifficulty = $memcache->get('dDifficulty')) { - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; - $memcache->set('dDifficulty', $dDifficulty); - } + $dDifficulty = $bitcoin->getdifficulty(); } else { $iDifficulty = 1; } diff --git a/public/include/pages/api/getpoolstatus.inc.php b/public/include/pages/api/getpoolstatus.inc.php index 4cf0d5ef..bd6375db 100644 --- a/public/include/pages/api/getpoolstatus.inc.php +++ b/public/include/pages/api/getpoolstatus.inc.php @@ -19,8 +19,6 @@ $aShares['valid'] > 0 ? $dEfficiency = round((100 - (100 / $aShares['valid'] * $ // Fetch RPC data if ($bitcoin->can_connect() === true){ $dDifficulty = $bitcoin->getdifficulty(); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; $iBlock = $bitcoin->getblockcount(); } else { $dDifficulty = 1; diff --git a/public/include/pages/statistics.inc.php b/public/include/pages/statistics.inc.php index 076ce66f..be328545 100644 --- a/public/include/pages/statistics.inc.php +++ b/public/include/pages/statistics.inc.php @@ -7,9 +7,7 @@ if (!defined('SECURITY')) if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $debug->append('No cached version available, fetching from backend', 3); if ($bitcoin->can_connect() === true){ - $dDifficulty = $bitcoin->query('getdifficulty'); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; + $dDifficulty = $bitcoin->getdifficulty(); $iBlock = $bitcoin->query('getblockcount'); } else { $dDifficulty = 1; diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 7937029d..f6eeedfb 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -8,8 +8,6 @@ if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { // Fetch data from wallet if ($bitcoin->can_connect() === true){ $dDifficulty = $bitcoin->getdifficulty(); - if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty)) - $dDifficulty = $dDifficulty['proof-of-work']; $iBlock = $bitcoin->getblockcount(); is_int($iBlock) && $iBlock > 0 ? $sBlockHash = $bitcoin->query('getblockhash', $iBlock) : $sBlockHash = ''; } else {