From ccf322bde1d4d8a571b799823313d5c481a0d2f0 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 13:40:22 +0200 Subject: [PATCH 01/21] re-adding seperate tables for shares and hashes due to SQL load when combining them --- public/include/classes/statistics.class.php | 52 +++++++++++-------- public/include/pages/statistics/pool.inc.php | 22 +++++--- .../mmcFE/statistics/pool/authenticated.tpl | 34 ++---------- .../statistics/pool/contributors_hashrate.tpl | 26 ++++++++++ .../statistics/pool/contributors_shares.tpl | 24 +++++++++ 5 files changed, 99 insertions(+), 59 deletions(-) create mode 100644 public/templates/mmcFE/statistics/pool/contributors_hashrate.tpl create mode 100644 public/templates/mmcFE/statistics/pool/contributors_shares.tpl diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 0681fcce..7629cd48 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -156,31 +156,37 @@ class Statistics { return false; } - public function getTopContributors($limit=15) { - $stmt = $this->mysqli->prepare(" - SELECT - ROUND(COUNT(id) / 60 / 10, 2) AS sharesps, - ROUND(COUNT(id) * POW(2," . $this->config['difficulty'] . ")/600/1000,2) AS hashrate, - SUBSTRING_INDEX( username, '.', 1 ) AS account - FROM " . $this->share->getTableName() . " - WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) - GROUP BY account - ORDER BY hashrate DESC LIMIT ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $hashrates= $stmt->get_result()) { - $aHashData = $hashrates->fetch_all(MYSQLI_ASSOC); - $stmt->close(); - } else { + public function getTopContributors($type='shares', $limit=15) { + switch ($type) { + case 'shares': + $stmt = $this->mysqli->prepare(" + SELECT + COUNT(id) AS shares, + SUBSTRING_INDEX( username, '.', 1 ) AS account + FROM " . $this->share->getTableName() . " + GROUP BY account + LIMIT ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_all(MYSQLI_ASSOC); + $this->debug->append("Fetching shares failed: "); return false; + break; + + case 'hashes': + $stmt = $this->mysqli->prepare(" + SELECT + ROUND(COUNT(id) * POW(2," . $this->config['difficulty'] . ")/600/1000,2) AS hashrate, + SUBSTRING_INDEX( username, '.', 1 ) AS account + FROM " . $this->share->getTableName() . " + WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) + GROUP BY account + ORDER BY hashrate DESC LIMIT ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_all(MYSQLI_ASSOC); + $this->debug->append("Fetching shares failed: "); + return false; + break; } - foreach ($aHashData as $key => $aData) { - $stmt = $this->mysqli->prepare("SELECT COUNT(id) FROM " . $this->share->getTableName() . " WHERE SUBSTRING_INDEX( username , '.', 1 ) = ?"); - if ($stmt->bind_param("s", $aData['username']) && $stmt->execute() && $result = $stmt->get_result()) { - $aHashData[$key]['shares'] = $this->getUserShares($this->user->getUserId($aData['account']))['valid']; - } else { - continue; - } - } - return $aHashData; } public function getHourlyHashrateByAccount($account_id) { diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 005e167d..74c1d171 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -20,11 +20,20 @@ if ($bitcoin->can_connect() === true){ $_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to pushpool service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg'); } -if (!$aHashData = $memcache->get('aHashData')) { - $debug->append('STA Fetching Hashrates from database'); - $aHashData = $statistics->getTopContributors(); - $memcache->set('aHashData', $aHashData, 60); - $debug->append('END Fetching Hashrates from database'); +// Top share contributors +if (!$aContributorsShares = $memcache->get('aContributorsShares')) { + $debug->append('STA Fetching contributor shares from database'); + $aContributorsShares = $statistics->getTopContributors('shares', 15); + $memcache->set('aContributorsShares', $aContributorsShares, 60); + $debug->append('END Fetching contributor shares from database'); +} + +// Top hash contributors +if (!$aContributorsHashes = $memcache->get('aContributorsHashes')) { + $debug->append('STA Fetching contributor hashes from database'); + $aContributorsHashes = $statistics->getTopContributors('hashes', 15); + $memcache->set('aContributorsHashes', $aContributorsHashes, 60); + $debug->append('END Fetching contributor hashes from database'); } // Grab the last 10 blocks found @@ -52,7 +61,8 @@ if (!empty($aBlockData)) { $smarty->assign("ESTTIME", $iEstTime); $smarty->assign("TIMESINCELAST", $dTimeSinceLast); $smarty->assign("BLOCKSFOUND", $aBlocksFoundData); -$smarty->assign("TOPHASHRATES", $aHashData); +$smarty->assign("CONTRIBSHARES", $aContributorsShares); +$smarty->assign("CONTRIBHASHES", $aContributorsHashes); $smarty->assign("CURRENTBLOCK", $iBlock); $smarty->assign("LASTBLOCK", $aBlockData['height']); $smarty->assign("DIFFICULTY", $dDifficulty); diff --git a/public/templates/mmcFE/statistics/pool/authenticated.tpl b/public/templates/mmcFE/statistics/pool/authenticated.tpl index 094493a1..7bddd421 100644 --- a/public/templates/mmcFE/statistics/pool/authenticated.tpl +++ b/public/templates/mmcFE/statistics/pool/authenticated.tpl @@ -1,34 +1,8 @@ {include file="global/block_header.tpl" BLOCK_HEADER="Pool Statistics" BLOCK_STYLE="clear:none;"} -{include file="global/block_header.tpl" BLOCK_HEADER="Top Contributers"} -
- - - - - - - - - - - - -{assign var=rank value=1} -{section hashrate $TOPHASHRATES} - - - - - - - - -{/section} - -
RankUser NameKH/sSharesShares/sŁ/Day (est)
{$rank++}{$TOPHASHRATES[hashrate].account}{$TOPHASHRATES[hashrate].hashrate|number_format}{$TOPHASHRATES[hashrate].shares|number_format}{$TOPHASHRATES[hashrate].sharesps}{math equation="round(reward / ( diff * pow(2,32) / ( hashrate * 1000 ) / 3600 / 24),3)" diff=$DIFFICULTY reward=$REWARD hashrate=$TOPHASHRATES[hashrate].hashrate}
- -
-{include file="global/block_footer.tpl"} + +{include file="statistics/pool/contributors_shares.tpl"} + +{include file="statistics/pool/contributors_hashrate.tpl"} {include file="global/block_header.tpl" BLOCK_HEADER="Server Stats" BLOCK_STYLE="clear:all;" STYLE="padding-left:5px;padding-right:5px;"} diff --git a/public/templates/mmcFE/statistics/pool/contributors_hashrate.tpl b/public/templates/mmcFE/statistics/pool/contributors_hashrate.tpl new file mode 100644 index 00000000..f9f9c27f --- /dev/null +++ b/public/templates/mmcFE/statistics/pool/contributors_hashrate.tpl @@ -0,0 +1,26 @@ +{include file="global/block_header.tpl" ALIGN="left" BLOCK_HEADER="Top Hashrate Contributers"} +
+
+ + + + + + + + + +{assign var=rank value=1} +{section contrib $CONTRIBHASHES} + + + + + + +{/section} + +
RankUser NameKH/sŁ/Day (est)
{$rank++}{$CONTRIBHASHES[contrib].account}{$CONTRIBHASHES[contrib].hashrate|number_format}{math equation="round(reward / ( diff * pow(2,32) / ( hashrate * 1000 ) / 3600 / 24),3)" diff=$DIFFICULTY reward=$REWARD hashrate=$CONTRIBHASHES[contrib].hashrate}
+ + +{include file="global/block_footer.tpl"} diff --git a/public/templates/mmcFE/statistics/pool/contributors_shares.tpl b/public/templates/mmcFE/statistics/pool/contributors_shares.tpl new file mode 100644 index 00000000..1214ef44 --- /dev/null +++ b/public/templates/mmcFE/statistics/pool/contributors_shares.tpl @@ -0,0 +1,24 @@ +{include file="global/block_header.tpl" ALIGN="right" BLOCK_HEADER="Top Share Contributers"} +
+ + + + + + + + + +{assign var=rank value=1} +{section hashrate $CONTRIBSHARES} + + + + + +{/section} + +
RankUser NameShares
{$rank++}{$CONTRIBSHARES[hashrate].account}{$CONTRIBSHARES[hashrate].shares|number_format}
+ +
+{include file="global/block_footer.tpl"} From f14f0ff981b86f3f42f46b56d8ded887bba77186 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 13:52:44 +0200 Subject: [PATCH 02/21] adding proper confirmations from global config --- public/templates/mmcFE/statistics/blocks/blocks_found.tpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/templates/mmcFE/statistics/blocks/blocks_found.tpl b/public/templates/mmcFE/statistics/blocks/blocks_found.tpl index ea0b251f..a8f9686f 100644 --- a/public/templates/mmcFE/statistics/blocks/blocks_found.tpl +++ b/public/templates/mmcFE/statistics/blocks/blocks_found.tpl @@ -16,7 +16,7 @@ {section block $BLOCKSFOUND} {$BLOCKSFOUND[block].height} - {if $BLOCKSFOUND[block].confirmations >= 120}Confirmed{else}{120 - $BLOCKSFOUND[block].confirmations} left{/if} + {if $BLOCKSFOUND[block].confirmations >= $GLOBAL.confirmations}Confirmed{else}{$GLOBAL.confirmations - $BLOCKSFOUND[block].confirmations} left{/if} {$BLOCKSFOUND[block].finder|default:"unknown"} {$BLOCKSFOUND[block].time|date_format:"%d/%m/%Y %H:%M:%S"} {$BLOCKSFOUND[block].difficulty|number_format:"8"} @@ -27,6 +27,6 @@ {include file="global/block_footer.tpl"} From cf9e3928678452b0247a748a7c644055f029a4ec Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 13:56:11 +0200 Subject: [PATCH 03/21] fix ordering for share contributors --- public/include/classes/statistics.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 7629cd48..32fda095 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -165,6 +165,7 @@ class Statistics { SUBSTRING_INDEX( username, '.', 1 ) AS account FROM " . $this->share->getTableName() . " GROUP BY account + ORDER BY shares LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_all(MYSQLI_ASSOC); From f3da885137a6257ee94e7dd499f6a656db150fa2 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 13:57:32 +0200 Subject: [PATCH 04/21] fix ordering for share contributors, for real --- public/include/classes/statistics.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 32fda095..8a277891 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -165,7 +165,7 @@ class Statistics { SUBSTRING_INDEX( username, '.', 1 ) AS account FROM " . $this->share->getTableName() . " GROUP BY account - ORDER BY shares + ORDER BY shares DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_all(MYSQLI_ASSOC); From 6ffaece83798f01a2b0559c2cfc0ca15c9e4e757 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:25:45 +0200 Subject: [PATCH 05/21] allow caching to be more transparent and configurable --- public/include/autoloader.inc.php | 5 +- public/include/classes/debug.class.php | 2 +- public/include/classes/statistics.class.php | 123 ++++++++++++++++--- public/include/classes/statscache.class.php | 49 ++++++++ public/include/config/global.inc.dist.php | 7 ++ public/include/pages/statistics/pool.inc.php | 22 +--- public/include/pages/statistics/user.inc.php | 11 +- public/include/smarty_globals.inc.php | 52 ++------ public/templates/cache/README.md | 2 + 9 files changed, 182 insertions(+), 91 deletions(-) create mode 100644 public/include/classes/statscache.class.php create mode 100644 public/templates/cache/README.md diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index 4aeb78ef..0da83b59 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -2,6 +2,7 @@ require_once(CLASS_DIR . '/debug.class.php'); require_once(CLASS_DIR . '/bitcoin.class.php'); +require_once(CLASS_DIR . '/statscache.class.php'); require_once(INCLUDE_DIR . '/database.inc.php'); require_once(INCLUDE_DIR . '/smarty.inc.php'); // Load classes that need the above as dependencies @@ -12,7 +13,3 @@ require_once(CLASS_DIR . '/worker.class.php'); require_once(CLASS_DIR . '/statistics.class.php'); require_once(CLASS_DIR . '/transaction.class.php'); require_once(CLASS_DIR . '/settings.class.php'); - -// Use Memcache to store our data -$memcache = new Memcached(); -$memcache->addServer('localhost', 11211); diff --git a/public/include/classes/debug.class.php b/public/include/classes/debug.class.php index f0c549fc..362a926d 100644 --- a/public/include/classes/debug.class.php +++ b/public/include/classes/debug.class.php @@ -66,7 +66,7 @@ class Debug { } return $backtrace; } - + /** * We fill our data array here * @param string $msg Debug Message diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 32fda095..1761ba39 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -4,21 +4,29 @@ if (!defined('SECURITY')) die('Hacking attempt'); + +/* + * We give access to plenty of statistics through this class + * Statistics should be non-intrusive and not change any + * rows in our database to ensure data integrity for the backend + **/ class Statistics { private $sError = ''; private $table = 'statistics_shares'; - public function __construct($debug, $mysqli, $config, $share, $user, $block) { + public function __construct($debug, $mysqli, $config, $share, $user, $block, $memcache) { $this->debug = $debug; $this->mysqli = $mysqli; $this->share = $share; $this->config = $config; $this->user = $user; $this->block = $block; + $this->memcache = $memcache; $this->debug->append("Instantiated Share class", 2); } - // get and set methods + /* Some basic get and set methods + **/ private function setErrorMessage($msg) { $this->sError = $msg; } @@ -35,7 +43,26 @@ class Statistics { return true; } + /** + * Another wrapper, we want to store data in memcache and return the actual data + * for further processing + * @param key string Our memcache key + * @param data mixed Our data to store in Memcache + * @param expiration time Our expiration time, see Memcached documentation + * @return data mixed Return our stored data unchanged + **/ + public function setCache($key, $data, $expiration=NULL) { + if ($this->config['memcache']['enabled']) $this->memcache->set($key, $data, $expiration); + return $data; + } + + /** + * Get our last $limit blocks found + * @param limit int Last limit blocks + * @return array + **/ public function getBlocksFound($limit=10) { + if ($data = $this->memcache->get(__FUNCTION__ . $limit)) return $data; $stmt = $this->mysqli->prepare(" SELECT b.*, a.username as finder FROM " . $this->block->getTableName() . " AS b @@ -43,11 +70,19 @@ class Statistics { ON b.account_id = a.id ORDER BY height DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $limit, $result->fetch_all(MYSQLI_ASSOC), 5); // Catchall $this->debug->append("Failed to find blocks:" . $this->mysqli->error); return false; } + + /** + * Currently the only function writing to the database + * Stored per block user statistics of valid and invalid shares + * @param aStats array Array with user id, valid and invalid shares + * @param iBlockId int Block ID as store in the Block table + * @return bool + **/ 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('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true; @@ -56,7 +91,14 @@ class Statistics { return false; } + /** + * Get our current pool hashrate for the past 10 minutes across both + * shares and shares_archive table + * @param none + * @return data object Return our hashrateas an object + **/ public function getCurrentHashrate() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT SUM(hashrate) AS hashrate FROM ( @@ -65,12 +107,18 @@ class Statistics { SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) ) AS sum"); // Catchall - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->setCache(__FUNCTION__, $result->fetch_object()->hashrate); $this->debug->append("Failed to get hashrate: " . $this->mysqli->error); return false; } + /** + * Same as getCurrentHashrate but for Shares + * @param none + * @return data object Our share rate in shares per second + **/ public function getCurrentShareRate() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(SUM(sharerate) / 600, 2) AS sharerate FROM ( @@ -78,13 +126,19 @@ class Statistics { UNION ALL SELECT COUNT(id) AS sharerate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) ) AS sum"); - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->sharerate; + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->setCache(__FUNCTION__, $result->fetch_object()->sharerate); // Catchall $this->debug->append("Failed to fetch share rate: " . $this->mysqli->error); return false; } + /** + * Get total shares for this round, since last block found + * @param none + * @return data array invalid and valid shares + **/ public function getRoundShares() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( SELECT IFNULL(count(id), 0) @@ -95,13 +149,20 @@ class Statistics { FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) AND our_result = 'N' ) as invalid"); - if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_assoc(); + if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__, $result->fetch_assoc()); // Catchall $this->debug->append("Failed to fetch round shares: " . $this->mysqli->error); return false; } + /** + * Get amount of shares for a specific user + * @param account_id int User ID + * @return data array invalid and valid share counts + **/ public function getUserShares($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( @@ -109,7 +170,7 @@ class Statistics { FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND our_result = 'Y' AND u.id = ? ) AS valid, @@ -118,17 +179,24 @@ class Statistics { FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND our_result = 'N' AND u.id = ? ) AS invalid"); - if ($stmt && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_assoc(); + if ($stmt && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_assoc()); // Catchall $this->debug->append("Unable to fetch user round shares: " . $this->mysqli->error); return false; } + /** + * Same as getUserShares for Hashrate + * @param account_id integer User ID + * @return data integer Current Hashrate in khash/s + **/ public function getUserHashrate($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, @@ -136,13 +204,20 @@ class Statistics { WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->hashrate); // Catchall $this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error); return false; } + /** + * Get hashrate for a specific worker + * @param worker_id int Worker ID to fetch hashrate for + * @return data int Current hashrate in khash/s + **/ public function getWorkerHashrate($worker_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, @@ -150,13 +225,21 @@ class Statistics { WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__ . $worker_id, $result->fetch_object()->hashrate); // Catchall $this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error); return false; } + /** + * get our top contributors for either shares or hashrate + * @param type string shares or hashes + * @param limit int Limit result to $limit + * @return data array Users with shares, account or hashrate, account + **/ public function getTopContributors($type='shares', $limit=15) { + if ($data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data; switch ($type) { case 'shares': $stmt = $this->mysqli->prepare(" @@ -168,7 +251,7 @@ class Statistics { ORDER BY shares LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break; @@ -183,14 +266,21 @@ class Statistics { GROUP BY account ORDER BY hashrate DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break; } } + /** + * get Hourly hashrate for a user + * Not working yet since I was not able to solve this via SQL queries + * @param account_id int User ID + * @return data array NOT FINISHED YET + **/ public function getHourlyHashrateByAccount($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2, 12)/600/1000) AS hashrate, @@ -209,11 +299,12 @@ class Statistics { AND a.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND a.id = ? GROUP BY HOUR(time)"); - if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $hourlyhashrates = $stmt->get_result()) - return $hourlyhashrates->fetch_all(MYSQLI_ASSOC); + if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_all(MYSQLI_ASSOC), 3600); // Catchall $this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error); return false; } } -$statistics = new Statistics($debug, $mysqli, $config, $share, $user, $block); + +$statistics = new Statistics($debug, $mysqli, $config, $share, $user, $block, $memcache); diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php new file mode 100644 index 00000000..7931da99 --- /dev/null +++ b/public/include/classes/statscache.class.php @@ -0,0 +1,49 @@ +config = $config; + $this->debug = $debug; + if (! $config['memcache']['enabled'] ) $this->debug->append("Not storing any values in memcache"); + return parent::__construct(); + } + + /** + * Wrapper around memcache->set + * Do not store values if memcache is disabled + **/ + public function set($key, $value, $expiration=NULL) { + if (! $this->config['memcache']['enabled']) return false; + if (empty($expiration)) + $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); + $this->debug->append("Stroing " . $this->config['memcache']['keyprefix'] . "$key with expiration of $expiration"); + return parent::set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); + } + + /** + * Wrapper around memcache->get + * Always return false if memcache is disabled + **/ + public function get($key, $cache_cb = NULL, &$cas_token = NULL) { + if (! $this->config['memcache']['enabled']) return false; + $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3); + if ($data = parent::get($this->config['memcache']['keyprefix'].$key)) { + $this->debug->append("Found key in cache", 3); + return $data; + } else { + $this->debug->append("Key not found", 3); + } + } +} + +$memcache = new StatsCache($config, $debug); +$memcache->addServer($config['memcache']['host'], $config['memcache']['port']); diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index fead5b6c..88430965 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -28,6 +28,13 @@ $config = array( 'difficulty' => '31', // Target difficulty for this pool 'reward' => '50', // Reward for finding blocks 'confirmations' => '120', // Confirmations per block found to credit transactions + 'memcache' => array( + 'host' => 'localhost', // Memcache Host + 'post' => 11211, // Memcache Port + 'keyprefix' => 'mmcfe_ng_', // Prefix for all keys + 'expiration'=> '90', // Cache time + 'splay' => '15' // Splay time + ), 'wallet' => array( 'type' => 'http', // http or https are supported 'host' => 'localhost:9332', diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 74c1d171..af0a4ebf 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -8,11 +8,11 @@ if (!defined('SECURITY')) if ($bitcoin->can_connect() === true){ if (!$dDifficulty = $memcache->get('dDifficulty')) { $dDifficulty = $bitcoin->query('getdifficulty'); - $memcache->set('dDifficulty', $dDifficulty, 60); + $memcache->set('dDifficulty', $dDifficulty); } if (!$iBlock = $memcache->get('iBlock')) { $iBlock = $bitcoin->query('getblockcount'); - $memcache->set('iBlock', $iBlock, 60); + $memcache->set('iBlock', $iBlock); } } else { $iDifficulty = 1; @@ -21,31 +21,17 @@ if ($bitcoin->can_connect() === true){ } // Top share contributors -if (!$aContributorsShares = $memcache->get('aContributorsShares')) { - $debug->append('STA Fetching contributor shares from database'); - $aContributorsShares = $statistics->getTopContributors('shares', 15); - $memcache->set('aContributorsShares', $aContributorsShares, 60); - $debug->append('END Fetching contributor shares from database'); -} +$aContributorsShares = $statistics->getTopContributors('shares', 15); // Top hash contributors -if (!$aContributorsHashes = $memcache->get('aContributorsHashes')) { - $debug->append('STA Fetching contributor hashes from database'); $aContributorsHashes = $statistics->getTopContributors('hashes', 15); - $memcache->set('aContributorsHashes', $aContributorsHashes, 60); - $debug->append('END Fetching contributor hashes from database'); -} // Grab the last 10 blocks found $aBlocksFoundData = $statistics->getBlocksFound(10); $aBlockData = $aBlocksFoundData[0]; // Estimated time to find the next block -if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { - $debug->append('Fetching iCurrentPoolHashrate from database'); - $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); - $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 60); -} +$iCurrentPoolHashrate = $statistics->getCurrentHashrate(); // Time in seconds, not hours, using modifier in smarty to translate $iEstTime = $dDifficulty * pow(2,32) / ($iCurrentPoolHashrate * 1000); diff --git a/public/include/pages/statistics/user.inc.php b/public/include/pages/statistics/user.inc.php index e690fc80..41316beb 100644 --- a/public/include/pages/statistics/user.inc.php +++ b/public/include/pages/statistics/user.inc.php @@ -8,11 +8,11 @@ if (!defined('SECURITY')) if ($bitcoin->can_connect() === true){ if (!$dDifficulty = $memcache->get('dDifficulty')) { $dDifficulty = $bitcoin->query('getdifficulty'); - $memcache->set('dDifficulty', $dDifficulty, 60); + $memcache->set('dDifficulty', $dDifficulty); } if (!$iBlock = $memcache->get('iBlock')) { $iBlock = $bitcoin->query('getblockcount'); - $memcache->set('iBlock', $iBlock, 60); + $memcache->set('iBlock', $iBlock); } } else { $iDifficulty = 1; @@ -20,12 +20,7 @@ if ($bitcoin->can_connect() === true){ $_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to pushpool service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg'); } -if (!$aHourlyHashRates = $memcache->get('mmcfe_' . $_SESSION['USERDATA']['id'] . '_hourlyhashrate')) { - $debug->append('STA Fetching hourly hashrates from database'); - $aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); - $memcache->set('mmcfe_' . $_SESSION['USERDATA']['id'] . '_hourlyhashrate', $aHourlyHashRates, 600); - $debug->append('END Fetching hourly hashrates from database'); -} +$aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); // Propagate content our template $smarty->assign("YOURHASHRATES", $aHourlyHashRates); diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index 060aba83..a6cbcb3e 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -7,34 +7,11 @@ if (!defined('SECURITY')) // Globally available variables $debug->append('Global smarty variables', 3); -// Store some stuff in memcache prior to assigning it to Smarty -if (!$aRoundShares = $memcache->get('aRoundShares')) { - $debug->append('STA Fetching aRoundShares from database'); - $aRoundShares = $statistics->getRoundShares(); - $debug->append('END Fetching aRoundShares from database'); - $memcache->set('aRoundShares', $aRoundShares, 90); -} - -if (!$iCurrentActiveWorkers = $memcache->get('iCurrentActiveWorkers')) { - $debug->append('STA Fetching iCurrentActiveWorkers from database'); - $iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); - $debug->append('END Fetching iCurrentActiveWorkers from database'); - $memcache->set('iCurrentActiveWorkers', $iCurrentActiveWorkers, 80); -} - -if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { - $debug->append('STA Fetching iCurrentPoolHashrate from database'); - $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); - $debug->append('END Fetching iCurrentPoolHashrate from database'); - $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 90); -} - -if (!$iCurrentPoolShareRate = $memcache->get('iCurrentPoolShareRate')) { - $debug->append('STA Fetching iCurrentPoolShareRate from database'); - $iCurrentPoolShareRate = $statistics->getCurrentShareRate(); - $debug->append('END Fetching iCurrentPoolShareRate from database'); - $memcache->set('iCurrentPoolShareRate', $iCurrentPoolShareRate, 90); -} +// Fetch some data +$aRoundShares = $statistics->getRoundShares(); +$iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); +$iCurrentPoolHashrate = $statistics->getCurrentHashrate(); +$iCurrentPoolShareRate = $statistics->getCurrentShareRate(); $aGlobal = array( 'slogan' => $config['website']['slogan'], @@ -47,26 +24,13 @@ $aGlobal = array( 'reward' => $config['reward'] ); -// We don't want the session infos cached +// We don't want these session infos cached $aGlobal['userdata'] = $_SESSION['USERDATA']['id'] ? $user->getUserData($_SESSION['USERDATA']['id']) : array(); - -// Balance should also not be cached $aGlobal['userdata']['balance'] = $transaction->getBalance($_SESSION['USERDATA']['id']); // Other userdata that we can cache savely -if (!$aGlobal['userdata']['shares'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_shares')) { - $debug->append('STA Loading user shares from database'); - $aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); - $debug->append('END Loading user shares from database'); - $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_shares', $aGlobal['userdata']['shares'], 80); -} - -if (!$aGlobal['userdata']['hashrate'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_hashrate') ) { - $debug->append('STA Loading user hashrate from database'); - $aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); - $debug->append('END Loading user hashrate from database'); - $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_hashrate', $aGlobal['userdata']['hashrate'], 70); -} +$aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); +$aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); // Make it available in Smarty $smarty->assign('PATH', 'site_assets/' . THEME); diff --git a/public/templates/cache/README.md b/public/templates/cache/README.md new file mode 100644 index 00000000..8b5701aa --- /dev/null +++ b/public/templates/cache/README.md @@ -0,0 +1,2 @@ +Please ensure the webserver has access to this folder to write the +caching templates. From 6102357cb926e195c365bdc7fdec51d573cebe49 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:26:30 +0200 Subject: [PATCH 06/21] adding memcache switch to dist config --- public/include/config/global.inc.dist.php | 1 + 1 file changed, 1 insertion(+) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 88430965..4da05f0f 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -29,6 +29,7 @@ $config = array( 'reward' => '50', // Reward for finding blocks 'confirmations' => '120', // Confirmations per block found to credit transactions 'memcache' => array( + 'enabled' => true, 'host' => 'localhost', // Memcache Host 'post' => 11211, // Memcache Port 'keyprefix' => 'mmcfe_ng_', // Prefix for all keys From 6a2173d504aa144b9867c4f58bf495d596b0076c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:38:58 +0200 Subject: [PATCH 07/21] fixed typo, added debug level to key store --- public/include/classes/statscache.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php index 7931da99..b6884d61 100644 --- a/public/include/classes/statscache.class.php +++ b/public/include/classes/statscache.class.php @@ -25,7 +25,7 @@ class StatsCache extends Memcached { if (! $this->config['memcache']['enabled']) return false; if (empty($expiration)) $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); - $this->debug->append("Stroing " . $this->config['memcache']['keyprefix'] . "$key with expiration of $expiration"); + $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3); return parent::set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); } From 7197a318b7b48612c773c47fbc360378149a2c9b Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:41:28 +0200 Subject: [PATCH 08/21] adding more documentation --- README.md | 3 ++- public/include/config/global.inc.dist.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f6f60726..71daca7e 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ Memcache Please install and start a default memcache instance. Not only would you need one for `pushpoold` but the statistics page is storing data in -`memcache` as well to improve performance. +`memcache` as well to improve performance. Your memcache can be +configured in the global configuration file (see below). Configuration ------------- diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 4da05f0f..2e311300 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -25,9 +25,9 @@ $config = array( 'name' => 'The Pool', 'slogan' => 'Resistance is futile', ), - 'difficulty' => '31', // Target difficulty for this pool - 'reward' => '50', // Reward for finding blocks - 'confirmations' => '120', // Confirmations per block found to credit transactions + 'difficulty' => '31', // Target difficulty for this pool as set in pushpoold json + 'reward' => '50', // Reward for finding blocks, fixed value but changes someday + 'confirmations' => '120', // Confirmations per block needed to credit transactions 'memcache' => array( 'enabled' => true, 'host' => 'localhost', // Memcache Host From 0e3ebe6bdef0c78db72189035c49097a47de0798 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 16:29:11 +0200 Subject: [PATCH 09/21] adding auto-payout cronjob --- cronjobs/auto_payout.php | 69 +++++++++++++++++++++++++++ public/include/classes/user.class.php | 55 ++++++++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 cronjobs/auto_payout.php diff --git a/cronjobs/auto_payout.php b/cronjobs/auto_payout.php new file mode 100644 index 00000000..fef0d484 --- /dev/null +++ b/cronjobs/auto_payout.php @@ -0,0 +1,69 @@ +#!/usr/bin/php +can_connect() !== true) { + verbose("Unable to connect to RPC server, exiting"); + exit(1); +} + +// Fetch all users with setup AP +$users = $user->getAllAutoPayout(); + +// Go through users and run transactions +if (! empty($users)) { + verbose("UserID\tUsername\tBalance\tThreshold\tAddress\t\t\t\t\tStatus\n\n"); + + foreach ($users as $aUserData) { + $dBalance = $transaction->getBalance($aUserData['id']); + verbose($aUserData['id'] . "\t" . $aUserData['username'] . "\t" . $dBalance . "\t" . $aUserData['ap_threshold'] . "\t\t" . $aUserData['coin_address'] . "\t"); + + // Only run if balance meets threshold and can pay the transaction fee + if ($dBalance > $aUserData['ap_threshold'] && $dBalance > 0.1) { + // Validate address against RPC + try { + $bitcoin->validateaddress($aUserData['coin_address']); + } catch (BitcoinClientException $e) { + verbose("VERIFY FAILED\n"); + continue; + } + + // Send balance - 0.1 Fee to address + try { + $bitcoin->sendtoaddress($aUserData['coin_address'], $dBalance - 0.1); + } catch (BitcoinClientException $e) { + verbose("SEND FAILED\n"); + continue; + } + + // Create transaction record + if ($transaction->addTransaction($aUserData['id'], $dBalance, 'Debit_AP', NULL, $aUserData['coin_address'], 0.1)) { + verbose("OK\n"); + } else { + verbose("FAILED\n"); + } + } + } +} else { + verbose("No user has configured their AP > 0\n"); +} diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index f9b8e5e3..0a1dcd20 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -34,6 +34,12 @@ class User { return $this->getSingle($username, 'id', 'username', 's'); } + /** + * Check user login + * @param username string Username + * @param password string Password + * @return bool + **/ public function checkLogin($username, $password) { $this->debug->append("Checking login for $username with password $password", 2); if ( $this->checkUserPassword($username, $password) ) { @@ -43,6 +49,12 @@ class User { return false; } + /** + * Check the users PIN for confirmation + * @param userID int User ID + * @param pin int PIN to check + * @return bool + **/ public function checkPin($userId, $pin=false) { $this->debug->append("Confirming PIN for $userId and pin $pin", 2); $stmt = $this->mysqli->prepare("SELECT pin FROM $this->table WHERE id=? AND pin=? LIMIT 1"); @@ -55,6 +67,14 @@ class User { return $pin_hash === $row_pin; } + /** + * Get a single row from the table + * @param value string Value to search for + * @param search Return column to search for + * @param field string Search column + * @param type string Type of value + * @return array Return result + **/ private function getSingle($value, $search='id', $field='id', $type="i") { $stmt = $this->mysqli->prepare("SELECT $search FROM $this->table WHERE $field = ? LIMIT 1"); if ($this->checkStmt($stmt)) { @@ -68,12 +88,45 @@ class User { return false; } + /** + * Get all users that have auto payout setup + * @param none + * @return data array All users with payout setup + **/ + public function getAllAutoPayout() { + $stmt = $this->mysqli->prepare(" + SELECT + id, username, coin_address, ap_threshold + FROM " . $this->getTableName() . " + WHERE ap_threshold > 0 + AND coin_address IS NOT NULL + "); + if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { + return $result->fetch_all(MYSQLI_ASSOC); + } + $this->debug->append("Unable to fetch users with AP set"); + echo $this->mysqli->error; + return false; + } + + /** + * Fetch users coin address + * @param userID int UserID + * @return data string Coin Address + **/ public function getCoinAddress($userID) { return $this->getSingle($userID, 'coin_address', 'id', 's'); } + /** + * Update a single row in a table + * @param userID int Account ID + * @param field string Field to update + * @param table string Table to update + * @return bool + **/ private function updateSingle($userID, $field, $table) { - $stmt = $this->mysqli->prepare("UPDATE $table SET " . $field['name'] . " = ? WHERE userId = ? LIMIT 1"); + $stmt = $this->mysqli->prepare("UPDATE $this->table SET " . $field['name'] . " = ? WHERE userId = ? LIMIT 1"); if ($this->checkStmt($stmt)) { $stmt->bind_param($field['type'].'i', $field['value'], $userID); $stmt->execute(); From 65bcabbd28ccf8df4f6f2bd90fd6291f2c5082e3 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 16:31:37 +0200 Subject: [PATCH 10/21] removing argument table --- public/include/classes/user.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index 0a1dcd20..cb7ff1c7 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -122,10 +122,9 @@ class User { * Update a single row in a table * @param userID int Account ID * @param field string Field to update - * @param table string Table to update * @return bool **/ - private function updateSingle($userID, $field, $table) { + private function updateSingle($userID, $field) { $stmt = $this->mysqli->prepare("UPDATE $this->table SET " . $field['name'] . " = ? WHERE userId = ? LIMIT 1"); if ($this->checkStmt($stmt)) { $stmt->bind_param($field['type'].'i', $field['value'], $userID); From f84fd9b4240000ff6477cbcef751def303eae618 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 16:37:30 +0200 Subject: [PATCH 11/21] made auto payout executable --- cronjobs/auto_payout.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 cronjobs/auto_payout.php diff --git a/cronjobs/auto_payout.php b/cronjobs/auto_payout.php old mode 100644 new mode 100755 From 6ae51aa63d23a5e4608dc2ce208ba81e0d428a4d Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 16:44:03 +0200 Subject: [PATCH 12/21] fixing typo in dist config --- public/include/config/global.inc.dist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 2e311300..ce001963 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -31,7 +31,7 @@ $config = array( 'memcache' => array( 'enabled' => true, 'host' => 'localhost', // Memcache Host - 'post' => 11211, // Memcache Port + 'port' => 11211, // Memcache Port 'keyprefix' => 'mmcfe_ng_', // Prefix for all keys 'expiration'=> '90', // Cache time 'splay' => '15' // Splay time From e4732f55ca04d1ceba56673ccc4b9ffe83bfe209 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 19:38:21 +0200 Subject: [PATCH 13/21] adding support for user donations, added all required changes into this commit --- cronjobs/pps_payout.php | 23 +++-- public/include/classes/transaction.class.php | 41 +++++---- public/include/classes/user.class.php | 14 +++- .../mmcFE/account/transactions/default.tpl | 32 ++++--- public/templates/mmcFE/global/sidebar.tpl | 84 +++++++++++++------ sql/mmcfe_ng_structure.sql | 20 ++--- 6 files changed, 149 insertions(+), 65 deletions(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 72ca7240..8f7a08d7 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -45,7 +45,7 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { } $aAccountShares = $share->getSharesForAccounts($share->getLastUpstreamId(), $iCurrentUpstreamId); $iRoundShares = $share->getRoundShares($share->getLastUpstreamId(), $iCurrentUpstreamId); - verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\tStatus\n"); + verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrev Share\t\tStatus\n"); verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $share->getUpstreamFinder() . "\t" . $share->getUpstreamId() . "\t\t" . $share->getLastUpstreamId()); if (empty($aAccountShares)) { verbose("\nNo shares found for this block\n\n"); @@ -58,24 +58,37 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { if (!$block->setShares($aBlock['id'], $iRoundShares)) $strStatus = "Shares Failed"; verbose("\t\t$strStatus\n\n"); - verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n"); + verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\t\tStatus\n"); foreach ($aAccountShares as $key => $aData) { + // Payout based on shares, PPS system $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8); $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8); + + // Calculate donation amount for Donation transaction + $aData['donation'] = $user->getDonatePercent($user->getUserId($aData['username'])) / 100 * $aData['payout']; + + // Verbose output of this users calculations verbose($aData['id'] . "\t" . $aData['username'] . "\t" . $aData['valid'] . "\t" . $aData['invalid'] . "\t" . $aData['percentage'] . "\t" . - $aData['payout'] . "\t"); + $aData['payout'] . "\t" . + $aData['donation'] . "\t"); - // Do all database updates for block, statistics and payouts $strStatus = "OK"; + // Update user share statistics if (!$statistics->updateShareStatistics($aData, $aBlock['id'])) $strStatus = "Stats Failed"; + // Add new credit transaction if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit', $aBlock['id'])) $strStatus = "Transaction Failed"; - verbose("$strStatus\n"); + // Add new donation debit + if ($aData['donation'] > 0) { + if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation', $aBlock['id'])) + $strStatus = "Donation Failed"; + } + verbose("\t\t$strStatus\n"); } verbose("------------------------------------------------------------------------\n\n"); diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 4f443acb..465e9a0f 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -9,10 +9,11 @@ class Transaction { private $table = 'transactions'; private $tableBlocks = 'blocks'; - public function __construct($debug, $mysqli, $config) { + public function __construct($debug, $mysqli, $config, $block) { $this->debug = $debug; $this->mysqli = $mysqli; $this->config = $config; + $this->block = $block; $this->debug->append("Instantiated Transaction class", 2); } @@ -75,23 +76,33 @@ class Transaction { public function getBalance($account_id) { $stmt = $this->mysqli->prepare(" - SELECT IFNULL(c.credit, 0) - IFNULL(d.debit,0) AS balance - FROM ( - SELECT t.account_id, sum(t.amount) AS credit + SELECT IFNULL(t1.credit, 0) - IFNULL(t2.debit, 0) - IFNULL(t3.other, 0) AS balance + FROM + ( + SELECT sum(t.amount) AS credit FROM $this->table AS t - LEFT JOIN $this->tableBlocks AS b ON t.block_id = b.id - WHERE type = 'Credit' - AND b.confirmations > ? - AND t.account_id = ? ) AS c - LEFT JOIN ( - SELECT t.account_id, sum(amount) AS debit + LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id + WHERE t.type = 'Credit' + AND b.confirmations >= ? + AND t.account_id = ? + ) AS t1, + ( + SELECT sum(t.amount) AS debit FROM $this->table AS t - WHERE type IN ('Debit_MP','Debit_AP') - AND t.account_id = ? ) AS d - ON c.account_id = d.account_id + WHERE t.type IN ('Debit_MP', 'Debit_AP') + AND t.account_id = ? + ) AS t2, + ( + SELECT sum(t.amount) AS other + FROM $this->table AS t + LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id + WHERE t.type IN ('Donation') + AND b.confirmations >= ? + AND t.account_id = ? + ) AS t3 "); if ($this->checkStmt($stmt)) { - $stmt->bind_param("iii", $this->config['confirmations'], $account_id, $account_id); + $stmt->bind_param("iiiii", $this->config['confirmations'], $account_id, $account_id, $this->config['confirmations'], $account_id); if (!$stmt->execute()) { $this->debug->append("Unable to execute statement: " . $stmt->error); $this->setErrorMessage("Fetching balance failed"); @@ -104,4 +115,4 @@ class Transaction { } } -$transaction = new Transaction($debug, $mysqli, $config); +$transaction = new Transaction($debug, $mysqli, $config, $block); diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index cb7ff1c7..a2b7c07e 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -115,7 +115,19 @@ class User { * @return data string Coin Address **/ public function getCoinAddress($userID) { - return $this->getSingle($userID, 'coin_address', 'id', 's'); + return $this->getSingle($userID, 'coin_address', 'id'); + } + + /** + * Fetch users donation value + * @param userID int UserID + * @return data string Coin Address + **/ + public function getDonatePercent($userID) { + $dPercent = $this->getSingle($userID, 'donate_percent', 'id'); + if ($dPercent > 100) $dPercent = 100; + if ($dPercent < 0) $dPercent = 0; + return $dPercent; } /** diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 9ec3aad6..3615a3dc 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -1,11 +1,6 @@ {include file="global/block_header.tpl" BLOCK_HEADER="Transaction Log" BUTTONS=array(Confirmed,Unconfirmed)}
-

- - ATP = Auto Threshold Payment, MP = Manual Payment, Don_Fee = donation amount + pool fees (if applicable) - -

@@ -19,7 +14,12 @@ {section transaction $TRANSACTIONS} - {if (($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or $TRANSACTIONS[transaction].type != 'Credit')} + {if ( + ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or $TRANSACTIONS[transaction].type == 'Debit_AP' + or $TRANSACTIONS[transaction].type == 'Debit_MP' + )} @@ -32,11 +32,15 @@ {/section}
{$TRANSACTIONS[transaction].id} {$TRANSACTIONS[transaction].timestamp}
+

+ + Credit_AP = Auto Threshold Payment, Credit_MP = Manual Payment, Donation = Donation, Fee = Pool Fees (if applicable) + +

-

Listed below are your estimated rewards and donations/fees for all blocks awaiting 120 confirmations.

@@ -50,7 +54,10 @@ {section transaction $TRANSACTIONS} - {if $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations} + {if ( + $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations + or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations) + )} @@ -59,15 +66,20 @@ - {assign var="sum" value="`$sum+$TRANSACTIONS[transaction].amount`"} + {if $TRANSACTIONS[transaction].type == Credit} + {assign var="credits" value="`$credits+$TRANSACTIONS[transaction].amount`"} + {else} + {assign var="debits" value="`$debits+$TRANSACTIONS[transaction].amount`"} + {/if} {/if} {/section} - +
{$TRANSACTIONS[transaction].id} {$TRANSACTIONS[transaction].timestamp}{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} {$TRANSACTIONS[transaction].amount}
Unconfirmed Totals:{$sum}{$credits - $debits}
+

Listed are your estimated rewards and donations/fees for all blocks awaiting {$GLOBAL.confirmations} confirmations.

{include file="global/block_footer.tpl"} diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 52e21a94..4c4f4061 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -1,25 +1,61 @@ -
-
-
-
-

Dashboard

-
-
-

- Your Current Hashrate
- {$GLOBAL.userdata.hashrate} KH/s

- Unpaid Shares
- Your Valid: {$GLOBAL.userdata.shares.valid}
- Pool Valid: {$GLOBAL.roundshares.valid}

- Round Shares
- Pool Valid: {$GLOBAL.roundshares.valid}
- Pool Inalid: {$GLOBAL.roundshares.invalid}
- Your Invalid: {$GLOBAL.userdata.shares.invalid}

- Round Estimate
- {math equation="round(( x / y ) * z, 8)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward} LTC

- Account Balance
{$GLOBAL.userdata.balance|default:"0"} LTC

-

-
-
-
+
+
+
+
+

Dashboard

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + {math assign="block" equation="round(( x / y ) * z, 3)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward} + {math assign="donation" equation="round(((d / 100) * est), 3)" d=$GLOBAL.userdata.donate_percent est=$block} + + + + + + + + + + + + + + + + + + +
+
+
+
diff --git a/sql/mmcfe_ng_structure.sql b/sql/mmcfe_ng_structure.sql index 69b0ea6a..780d00e1 100644 --- a/sql/mmcfe_ng_structure.sql +++ b/sql/mmcfe_ng_structure.sql @@ -1,11 +1,11 @@ -- phpMyAdmin SQL Dump --- version 3.5.1 +-- version 3.5.8.1deb1 -- http://www.phpmyadmin.net -- -- Host: localhost --- Generation Time: May 16, 2013 at 09:25 PM --- Server version: 5.5.31-log --- PHP Version: 5.4.15 +-- Generation Time: May 20, 2013 at 07:35 PM +-- Server version: 5.5.31-0ubuntu0.13.04.1 +-- PHP Version: 5.4.9-4ubuntu2 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; @@ -17,7 +17,7 @@ SET time_zone = "+00:00"; /*!40101 SET NAMES utf8 */; -- --- Database: `mmcfe_ng_db` +-- Database: `mmcfe_ng` -- -- -------------------------------------------------------- @@ -63,7 +63,7 @@ CREATE TABLE IF NOT EXISTS `blocks` ( PRIMARY KEY (`id`), UNIQUE KEY `height` (`height`,`blockhash`), KEY `time` (`time`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service'; -- -------------------------------------------------------- @@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS `shares_archive` ( `time` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `share_id` (`share_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes'; -- -------------------------------------------------------- @@ -132,7 +132,7 @@ CREATE TABLE IF NOT EXISTS `statistics_shares` ( PRIMARY KEY (`id`), KEY `account_id` (`account_id`), KEY `block_id` (`block_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- @@ -143,7 +143,7 @@ CREATE TABLE IF NOT EXISTS `statistics_shares` ( CREATE TABLE IF NOT EXISTS `transactions` ( `id` int(255) NOT NULL AUTO_INCREMENT, `account_id` int(255) unsigned NOT NULL, - `type` enum('Credit','Debit_MP','Debit_AP') DEFAULT NULL, + `type` enum('Credit','Debit_MP','Debit_AP','Fee','Donation') DEFAULT NULL, `coin_address` varchar(255) DEFAULT NULL, `amount` double DEFAULT '0', `fee_amount` float DEFAULT '0', @@ -153,7 +153,7 @@ CREATE TABLE IF NOT EXISTS `transactions` ( KEY `block_id` (`block_id`), KEY `account_id` (`account_id`), KEY `type` (`type`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- From a120d41612d948840d660f4582dbc3ce4ef9fb6d Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 20:01:45 +0200 Subject: [PATCH 14/21] lets try to get around some rounding issue for balance and transactions --- cronjobs/auto_payout.php | 2 ++ cronjobs/pps_payout.php | 2 +- public/include/classes/transaction.class.php | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cronjobs/auto_payout.php b/cronjobs/auto_payout.php index fef0d484..3454ad9c 100755 --- a/cronjobs/auto_payout.php +++ b/cronjobs/auto_payout.php @@ -62,6 +62,8 @@ if (! empty($users)) { } else { verbose("FAILED\n"); } + } else { + verbose("SKIPPED\n"); } } } else { diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 8f7a08d7..329104c5 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -65,7 +65,7 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8); // Calculate donation amount for Donation transaction - $aData['donation'] = $user->getDonatePercent($user->getUserId($aData['username'])) / 100 * $aData['payout']; + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * $aData['payout'], 8), 8); // Verbose output of this users calculations verbose($aData['id'] . "\t" . diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 465e9a0f..5fff814b 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -76,7 +76,7 @@ class Transaction { public function getBalance($account_id) { $stmt = $this->mysqli->prepare(" - SELECT IFNULL(t1.credit, 0) - IFNULL(t2.debit, 0) - IFNULL(t3.other, 0) AS balance + SELECT ROUND(IFNULL(t1.credit, 0) - IFNULL(t2.debit, 0) - IFNULL(t3.other, 0), 8) AS balance FROM ( SELECT sum(t.amount) AS credit From 09671131e6db231aa50e4b551ad956ae5ebda3e1 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 20 May 2013 21:01:55 +0200 Subject: [PATCH 15/21] properly display payment address in transactions log --- public/templates/mmcFE/account/transactions/default.tpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 3615a3dc..4e490ad2 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -24,7 +24,7 @@ {$TRANSACTIONS[transaction].id} {$TRANSACTIONS[transaction].timestamp} {$TRANSACTIONS[transaction].type} - {$TRANSACTIONS[transaction].sendAddress} + {$TRANSACTIONS[transaction].coin_address} {if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} {$TRANSACTIONS[transaction].amount} @@ -62,7 +62,7 @@ {$TRANSACTIONS[transaction].id} {$TRANSACTIONS[transaction].timestamp} {$TRANSACTIONS[transaction].type} - {$TRANSACTIONS[transaction].sendAddress} + {$TRANSACTIONS[transaction].coin_address} {if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} {$TRANSACTIONS[transaction].amount} From 97eda88e2f3bbe47b69d83d92321622f5d94ce67 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 11:30:46 +0200 Subject: [PATCH 16/21] fixing worker updates and deletion --- public/include/classes/worker.class.php | 49 ++++++++++++++++++++----- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/public/include/classes/worker.class.php b/public/include/classes/worker.class.php index 71a13f28..b2039dd4 100644 --- a/public/include/classes/worker.class.php +++ b/public/include/classes/worker.class.php @@ -32,7 +32,13 @@ class Worker { } return true; } - // Worker code, could possibly be moved to it's own class someday + + /** + * Update worker list for a user + * @param account_id int User ID + * @param data array All workers and their settings + * @return bool + **/ public function updateWorkers($account_id, $data) { $username = $this->user->getUserName($account_id); foreach ($data as $key => $value) { @@ -47,23 +53,32 @@ class Worker { } return true; } + + /** + * Fetch all workers for an account + * @param account_id int User ID + * @return mixed array Workers and their settings or false + **/ public function getWorkers($account_id) { $stmt = $this->mysqli->prepare(" - SELECT $this->table.username, $this->table.password, - ( SELECT SIGN(count(id)) FROM " . $this->share->getTableName() . " WHERE username = $this->table.username AND time > DATE_SUB(now(), INTERVAL 10 MINUTE)) AS active, + SELECT id, username, password, + ( SELECT SIGN(COUNT(id)) FROM " . $this->share->getTableName() . " WHERE username = $this->table.username AND time > DATE_SUB(now(), INTERVAL 10 MINUTE)) AS active, ( SELECT ROUND(COUNT(id) * POW(2,21)/600/1000) FROM " . $this->share->getTableName() . " WHERE username = $this->table.username AND time > DATE_SUB(now(), INTERVAL 10 MINUTE)) AS hashrate FROM $this->table WHERE account_id = ?"); - if ($this->checkStmt($stmt)) { - if (!$stmt->bind_param('i', $account_id)) return false; - if (!$stmt->execute()) return false; - $result = $stmt->get_result(); - $stmt->close(); + if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_all(MYSQLI_ASSOC); - } + // Catchall + $this->setErrorMessage('Failed to fetch workers for your account'); + $this->debug->append('Fetching workers failed: ' . $this->mysqli->error); return false; } + /** + * Get all currently active workers in the past 10 minutes + * @param none + * @return data mixed int count if any workers are active, false otherwise + **/ public function getCountAllActiveWorkers() { $stmt = $this->mysqli->prepare("SELECT COUNT(DISTINCT username) AS total FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)"); if ($this->checkStmt($stmt)) { @@ -77,6 +92,15 @@ class Worker { return false; } + /** + * Add new worker to an existing web account + * The webuser name is prefixed to the worker name + * Passwords are plain text for pushpoold + * @param account_id int User ID + * @param workerName string Worker name + * @param workerPassword string Worker password + * @return bool + **/ public function addWorker($account_id, $workerName, $workerPassword) { $username = $this->user->getUserName($account_id); $workerName = "$username.$workerName"; @@ -92,6 +116,13 @@ class Worker { } return false; } + + /** + * Delete existing worker from account + * @param account_id int User ID + * @param id int Worker ID + * @return bool + **/ public function deleteWorker($account_id, $id) { $stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE account_id = ? AND id = ?"); if ($this->checkStmt($stmt)) { From 6429b2b442532c6403926c715dd3d683a1654589 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 13:57:49 +0200 Subject: [PATCH 17/21] adding support for pool fees --- cronjobs/pps_payout.php | 22 +++++++++++++------ public/include/classes/transaction.class.php | 8 +++---- public/include/smarty_globals.inc.php | 7 ++++++ .../mmcFE/account/transactions/default.tpl | 2 ++ public/templates/mmcFE/global/sidebar.tpl | 12 +++++----- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 329104c5..848b9d5c 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -58,14 +58,19 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { if (!$block->setShares($aBlock['id'], $iRoundShares)) $strStatus = "Shares Failed"; verbose("\t\t$strStatus\n\n"); - verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\t\tStatus\n"); + verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\tFee\t\tStatus\n"); foreach ($aAccountShares as $key => $aData) { // Payout based on shares, PPS system $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8); $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8); + // Defaults + $aData['fee' ] = 0; + $aData['donation'] = 0; - // Calculate donation amount for Donation transaction - $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * $aData['payout'], 8), 8); + if ($config['fees'] > 0) + $aData['fee'] = number_format(round($config['fees'] / 100 * $aData['payout'], 8), 8); + // Calculate donation amount, fees not included + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 8), 8); // Verbose output of this users calculations verbose($aData['id'] . "\t" . @@ -74,7 +79,8 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { $aData['invalid'] . "\t" . $aData['percentage'] . "\t" . $aData['payout'] . "\t" . - $aData['donation'] . "\t"); + $aData['donation'] . "\t" . + $aData['fee'] . "\t"); $strStatus = "OK"; // Update user share statistics @@ -84,11 +90,13 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit', $aBlock['id'])) $strStatus = "Transaction Failed"; // Add new donation debit - if ($aData['donation'] > 0) { + if ($aData['donation'] > 0) if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation', $aBlock['id'])) $strStatus = "Donation Failed"; - } - verbose("\t\t$strStatus\n"); + if ($aData['fee'] > 0 && $config['fees'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['fee'], 'Fee', $aBlock['id'])) + $strStatus = "Fee Failed"; + verbose("\t$strStatus\n"); } verbose("------------------------------------------------------------------------\n\n"); diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 5fff814b..7f8cd76d 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -25,10 +25,10 @@ class Transaction { return $this->sError; } - public function addTransaction($account_id, $amount, $type='Credit', $block_id=NULL, $coin_address=NULL, $fee=0) { - $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type, coin_address, fee_amount) VALUES (?, ?, ?, ?, ?, ?)"); + public function addTransaction($account_id, $amount, $type='Credit', $block_id=NULL, $coin_address=NULL) { + $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type, coin_address) VALUES (?, ?, ?, ?, ?)"); if ($this->checkStmt($stmt)) { - $stmt->bind_param("idissd", $account_id, $amount, $block_id, $type, $coin_address, $fee); + $stmt->bind_param("idiss", $account_id, $amount, $block_id, $type, $coin_address); if ($stmt->execute()) { $this->setErrorMessage("Failed to store transaction"); $stmt->close(); @@ -96,7 +96,7 @@ class Transaction { SELECT sum(t.amount) AS other FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation') + WHERE t.type IN ('Donation','Fee') AND b.confirmations >= ? AND t.account_id = ? ) AS t3 diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index a6cbcb3e..4d55829e 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -20,6 +20,7 @@ $aGlobal = array( 'sharerate' => $iCurrentPoolShareRate, 'workers' => $iCurrentActiveWorkers, 'roundshares' => $aRoundShares, + 'fees' => $config['fees'], 'confirmations' => $config['confirmations'], 'reward' => $config['reward'] ); @@ -32,6 +33,12 @@ $aGlobal['userdata']['balance'] = $transaction->getBalance($_SESSION['USERDATA'] $aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); $aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); +// Some estimations +$aGlobal['userdata']['est_block'] = round(( (int)$aGlobal['userdata']['shares']['valid'] / (int)$aRoundShares['valid'] ) * (int)$config['reward'], 3); +$aGlobal['userdata']['est_donation'] = round((( $aGlobal['userdata']['donate_percent'] / 100) * $aGlobal['userdata']['est_block']), 3); +$aGlobal['userdata']['est_fee'] = round((($config['fees'] / 100) * ($aGlobal['userdata']['est_block'] - $aGlobal['userdata']['est_donation'])), 3); +$aGlobal['userdata']['est_payout'] = round($aGlobal['userdata']['est_block'] - $aGlobal['userdata']['est_donation'] - $aGlobal['userdata']['est_fee'], 3); + // Make it available in Smarty $smarty->assign('PATH', 'site_assets/' . THEME); $smarty->assign('GLOBAL', $aGlobal); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 4e490ad2..35ec2cc4 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -17,6 +17,7 @@ {if ( ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' )} @@ -57,6 +58,7 @@ {if ( $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations) + or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations) )} {$TRANSACTIONS[transaction].id} diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 4c4f4061..3845d42d 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -34,22 +34,24 @@ Your Invalid {$GLOBAL.userdata.shares.invalid} - {math assign="block" equation="round(( x / y ) * z, 3)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward} - {math assign="donation" equation="round(((d / 100) * est), 3)" d=$GLOBAL.userdata.donate_percent est=$block} Round Estimate Block - {$block} LTC + {$GLOBAL.userdata.est_block} LTC Donation - {$donation} LTC + {$GLOBAL.userdata.est_donation} LTC + + + Fees + {$GLOBAL.userdata.est_fee} LTC Payout - {math equation="block - donation" block=$block donation=$donation} LTC + {$GLOBAL.userdata.est_payout} LTC   Account Balance From edef3a5b111ad819270f9c359071b161258b09a2 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 14:00:54 +0200 Subject: [PATCH 18/21] adding new SQL structure, removed a column from transactions --- sql/mmcfe_ng_structure.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/sql/mmcfe_ng_structure.sql b/sql/mmcfe_ng_structure.sql index 780d00e1..52639bbc 100644 --- a/sql/mmcfe_ng_structure.sql +++ b/sql/mmcfe_ng_structure.sql @@ -146,7 +146,6 @@ CREATE TABLE IF NOT EXISTS `transactions` ( `type` enum('Credit','Debit_MP','Debit_AP','Fee','Donation') DEFAULT NULL, `coin_address` varchar(255) DEFAULT NULL, `amount` double DEFAULT '0', - `fee_amount` float DEFAULT '0', `block_id` int(255) DEFAULT NULL, `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), From 0aab1dff9a04bfd6b297d5f17c41a99a3ab58928 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 14:08:39 +0200 Subject: [PATCH 19/21] adding new fees variable to dist config --- public/include/config/global.inc.dist.php | 1 + 1 file changed, 1 insertion(+) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index ce001963..37d39c3f 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -25,6 +25,7 @@ $config = array( 'name' => 'The Pool', 'slogan' => 'Resistance is futile', ), + 'fees' => 0, 'difficulty' => '31', // Target difficulty for this pool as set in pushpoold json 'reward' => '50', // Reward for finding blocks, fixed value but changes someday 'confirmations' => '120', // Confirmations per block needed to credit transactions From fb56bde2e4ba5ab35902f6cdc9ab09d871bd1696 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 15:42:44 +0300 Subject: [PATCH 20/21] Update README.md --- README.md | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 71daca7e..e7bd3247 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Description mmcFE-ng is a web frontend for Pooled LTC Mining. -This is based on mmcFE, the original work by Greedi: +The web frontend layout is based on mmcFE, the original work by Greedi: https://github.com/Greedi/mmcFE After working a few days trying to learn to run my own pool and the @@ -12,6 +12,10 @@ understand how it works. While doing so I also migrated the existing code over to my own framework so maintenance would be easier in the future. +**NOTE**: This project is still under development and commits are happening on a daily basis. +I do not recommend using this for a live setup as of yet. Wait for the later Release Candidate +if you wish to run your pool with it. Testing pools are much appreciated though! + Requirements ============ @@ -31,6 +35,24 @@ in the appropriate forums. * pushpoold * litecoind +Features +======== + +The following feature have been implemented so far: + +* Use of memcache for statistics instead of a cronjob +* Web User accounts +* Worker accounts + * Worker activity (live, past 10 minutes) + * Worker hashrates (live, past 10 minutes) +* Pool statistics +* Minimal Block statistics +* Pool donations +* Pool fees +* Manual payout with 0.1 LTC fee +* Auto payout with 0.1 LTC fee +* Transaction list (confirmed and unconfirmed) + Installation ============ @@ -124,8 +146,9 @@ me know by creating an [Issue][1] marked as `Feature Request`. Disclaimer ========== -This is a *WIP Project*. Most functionality is now added, the core -features are available and the backend cronjobs are working. If you -encounter any problems related to the code please create a new [Issue][1] +This is a **WIP Project**. Most functionality is now added, the core +features are available and the backend cronjobs are working. I would not recommend +running this on a live pool yet. You can play around and test basic functionality but +wait for any live deployment for at least a stable Release Candidate. [1]: https://github.com/TheSerapher/php-mmcfe-ng/issues "Issue" From bfff517bc5cd2d6b089daae6ce01a1e3d62e5aa5 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 May 2013 22:09:07 +0300 Subject: [PATCH 21/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e7bd3247..6d3dd879 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ in the appropriate forums. * Apache2 * libapache2-mod-php5 -* PHP 5.4+ (5.3 might work too) +* PHP 5.4+ * php5-mysqlnd * php5-memcached * MySQL Server