diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 18b9495a..d1016664 100644 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -28,25 +28,33 @@ if (empty($aAllBlocks)) { exit(0); } +$count = 0; foreach ($aAllBlocks as $iIndex => $aBlock) { if (!$aBlock['accounted']) { - $iPrevBlockTime = @$aAllBlocks[$iIndex - 1]['time']; - if (!$iPrevBlockTime) { - $iPrevBlockTime = 0; + if ($share->setUpstream(@$aAllBlocks[$iIndex - 1]['time'])) { + $share->setLastUpstreamId(); } - $aAccountShares = $share->getSharesForAccountsByTimeframe($aBlock['time'], $iPrevBlockTime); - if (empty($aAccountShares)) { - verbose("No shares found for this block\n"); + + if ($share->setUpstream($aBlock['time'])) { + $iCurrentUpstreamId = $share->getUpstreamId(); + } else { + verbose("Unable to fetch blocks upstream share\n"); + verbose($share->getError() . "\n"); + continue; + } + $aAccountShares = $share->getSharesForAccounts($share->getLastUpstreamId(), $iCurrentUpstreamId); + $iRoundShares = $share->getRoundShares($share->getLastUpstreamId(), $iCurrentUpstreamId); + verbose("ID\tHeight\tTime\t\tShares\tFinder\t\tShare ID\tPrevious Share\n"); + verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $share->getUpstreamFinder() . "\t" . $share->getUpstreamId() . "\t\t" . $share->getLastUpstreamId() . "\n\n"); + if (empty($aAccountShares)) { + verbose("\nNo shares found for this block\n\n"); + sleep(2); continue; } - $iRoundShares = $share->getRoundSharesByTimeframe($aBlock['time'], $iPrevBlockTime); - $strFinder = $share->getFinderByTimeframe($aBlock['time'], $iPrevBlockTime); - verbose("ID\tHeight\tTime\t\tShares\tFinder\n"); - verbose($aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $strFinder . "\n\n"); verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n"); foreach ($aAccountShares as $key => $aData) { - $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 10),10); - $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 10), 10); + $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8); + $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8); verbose($aData['id'] . "\t" . $aData['username'] . "\t" . $aData['valid'] . "\t" . @@ -58,14 +66,14 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { $strStatus = "OK"; if (!$statistics->updateShareStatistics($aData, $aBlock['id'])) $strStatus = "Stats Failed"; - if (!$transaction->addCredit($aData['id'], $aData['payout'], $aBlock['id'])) + if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit', $aBlock['id'])) $strStatus = "Transaction Failed"; verbose("$strStatus\n"); } verbose("------------------------------------------------------------------------\n\n"); - // Move counted shares to archive for this blockhash - $share->moveArchiveByTimeframe($aBlock['time'], $iPrevBlockTime, $aBlock['id']); + // Move counted shares to archive before this blockhash upstream share + $share->moveArchive($share->getLastUpstreamId(), $iCurrentUpstreamId, $aBlock['id']); $block->setAccounted($aBlock['blockhash']); } } diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index 6a79b452..8e64c769 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -7,6 +7,8 @@ if (!defined('SECURITY')) class Share { private $sError = ''; private $table = 'shares'; + private $oUpstream; + private $iLastUpstreamId; // This defines each share public $rem_host, $username, $our_result, $upstream_result, $reason, $solution, $time; @@ -24,7 +26,7 @@ class Share { return $this->sError; } - public function getSharesForAccountsByTimeframe($current='', $old='') { + public function getSharesForAccounts($previous_upstream=0, $current_upstream) { $stmt = $this->mysqli->prepare("SELECT a.id, validT.account AS username, @@ -36,12 +38,8 @@ class Share { SUBSTRING_INDEX( `username` , '.', 1 ) as account, COUNT(id) AS valid FROM $this->table - WHERE - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? - AND - our_result = 'Y' + WHERE id BETWEEN ? AND ? + AND our_result = 'Y' GROUP BY account ) validT LEFT JOIN @@ -50,19 +48,15 @@ class Share { SUBSTRING_INDEX( `username` , '.', 1 ) as account, COUNT(id) AS invalid FROM $this->table - WHERE - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? - AND - our_result = 'N' + WHERE id BETWEEN ? AND ? + AND our_result = 'N' GROUP BY account ) invalidT ON validT.account = invalidT.account INNER JOIN accounts a ON a.username = validT.account GROUP BY a.username DESC"); if ($this->checkStmt($stmt)) { - $stmt->bind_param('iiii', $old, $current, $old, $current); + $stmt->bind_param('iiii', $previous_upstream, $current_upstream, $previous_upstream, $current_upstream); $stmt->execute(); $result = $stmt->get_result(); $stmt->close(); @@ -71,18 +65,15 @@ class Share { return false; } - public function getRoundSharesByTimeframe($current='', $old='') { + public function getRoundShares($previous_upstream=0, $current_upstream) { $stmt = $this->mysqli->prepare("SELECT count(id) as total FROM $this->table WHERE our_result = 'Y' - AND - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? + AND id BETWEEN ? AND ? "); if ($this->checkStmt($stmt)) { - $stmt->bind_param('ii', $old, $current); + $stmt->bind_param('ii', $previous_upstream, $current_upstream); $stmt->execute(); $result = $stmt->get_result(); $stmt->close(); @@ -91,25 +82,16 @@ class Share { return false; } - public function moveArchiveByTimeframe($current='', $old='',$block_id) { + public function moveArchive($previous_upstream=0, $current_upstream,$block_id) { $archive_stmt = $this->mysqli->prepare("INSERT INTO shares_archive (share_id, username, our_result, upstream_result, block_id) SELECT id, username, our_result, upstream_result, ? FROM $this->table - WHERE - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? - "); - $delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table - WHERE - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? - "); + WHERE id BETWEEN ? AND ?"); + $delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id BETWEEN ? AND ?"); if ($this->checkStmt($archive_stmt) && $this->checkStmt($delete_stmt)) { - $archive_stmt->bind_param('iii', $block_id, $old, $current); + $archive_stmt->bind_param('iii', $block_id, $previous_upstream, $current_upstream); $archive_stmt->execute(); - $delete_stmt->bind_param('ii', $old, $current); + $delete_stmt->bind_param('ii', $previous_upstream, $current_upstream); $delete_stmt->execute(); $delete_stmt->close(); $archive_stmt->close(); @@ -118,22 +100,35 @@ class Share { return false; } - public function getFinderByTimeframe($current='', $old='') { + public function setLastUpstreamId() { + $this->iLastUpstreamId = @$this->oUpstream->id ? $this->oUpstream->id : 0; + } + public function getLastUpstreamId() { + return $this->iLastUpstreamId; + } + public function getUpstreamFinder() { + return $this->oUpstream->account; + } + public function getUpstreamId() { + return @$this->oUpstream->id; + } + public function setUpstream($time='') { $stmt = $this->mysqli->prepare("SELECT - SUBSTRING_INDEX( `username` , '.', 1 ) AS account - FROM $this->table - WHERE upstream_result = 'Y' - AND - UNIX_TIMESTAMP(time) > ? - AND - UNIX_TIMESTAMP(time) <= ? - ORDER BY id DESC"); + SUBSTRING_INDEX( `username` , '.', 1 ) AS account, id + FROM $this->table + WHERE upstream_result = 'Y' + AND UNIX_TIMESTAMP(time) BETWEEN ? AND (? + 1) + ORDER BY id ASC LIMIT 1"); if ($this->checkStmt($stmt)) { - $stmt->bind_param('ii', $old, $current); + $stmt->bind_param('ii', $time, $time); $stmt->execute(); - $result = $stmt->get_result(); + if (! $result = $stmt->get_result()) { + $this->setErrorMessage("No result returned from query"); + $stmt->close(); + } $stmt->close(); - return @$result->fetch_object()->account; + $this->oUpstream = $result->fetch_object(); + return true; } return false; } diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 3aba6586..b462834f 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -13,7 +13,7 @@ class Transaction { $this->debug = $debug; $this->mysqli = $mysqli; $this->config = $config; - $this->debug->append("Instantiated Ledger class", 2); + $this->debug->append("Instantiated Transaction class", 2); } // get and set methods @@ -24,13 +24,12 @@ class Transaction { return $this->sError; } - public function addCredit($account_id, $amount, $block_id) { - $strType = 'Credit'; - $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type) VALUES (?, ?, ?, ?)"); - echo $this->mysqli->error; + 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 (?, ?, ?, ?, ?, ?)"); if ($this->checkStmt($stmt)) { - $stmt->bind_param("idis", $account_id, $amount, $block_id, $strType); + $stmt->bind_param("idissd", $account_id, $amount, $block_id, $type, $coin_address, $fee); if ($stmt->execute()) { + $this->setErrorMessage("Failed to store transaction"); $stmt->close(); return true; } @@ -38,23 +37,7 @@ class Transaction { return false; } - public function confirmCredits() { - $stmt = $this->mysqli->prepare("UPDATE - ledger AS l - INNER JOIN blocks as b ON l.assocBlock = b.height - SET l.confirmed = 1 - WHERE b.confirmations > 120 - AND l.confirmed = 0"); - if ($this->checkStmt($stmt)) { - if (!$stmt->execute()) { - $this->debug->append("Failed to execute statement: " . $stmt->error); - $stmt->close(); - return false; - } - $stmt->close(); - return true; - } - return false; + public function addDebit($account_id, $amount, $type='AP') { } public function getTransactions($account_id, $start=0) { @@ -63,17 +46,16 @@ class Transaction { t.id AS id, t.type AS type, t.amount AS amount, - t.sendAddress AS sendAddress, + t.coin_address AS coin_address, t.timestamp AS timestamp, b.height AS height, b.confirmations AS confirmations FROM transactions AS t LEFT JOIN blocks AS b ON t.block_id = b.id WHERE t.account_id = ? - ORDER BY timestamp DESC - LIMIT ? , 30"); + ORDER BY id DESC"); if ($this->checkStmt($stmt)) { - if(!$stmt->bind_param('ii', $account_id, $start)) return false; + if(!$stmt->bind_param('i', $account_id)) return false; $stmt->execute(); $result = $stmt->get_result(); return $result->fetch_all(MYSQLI_ASSOC); @@ -93,7 +75,7 @@ class Transaction { public function getBalance($account_id) { $stmt = $this->mysqli->prepare(" - SELECT IFNULL(c.credit - d.debit, 0) AS balance + SELECT IFNULL(c.credit, 0) - IFNULL(d.debit,0) AS balance FROM ( SELECT account_id, sum(t.amount) AS credit FROM $this->table AS t