search shares by IDs not timestamps, cleaned up transaction class in the process

This commit is contained in:
Sebastian Grewe 2013-05-12 16:58:33 +02:00
parent c3964a59da
commit 91144afa4e
3 changed files with 74 additions and 89 deletions

View File

@ -28,25 +28,33 @@ if (empty($aAllBlocks)) {
exit(0); exit(0);
} }
$count = 0;
foreach ($aAllBlocks as $iIndex => $aBlock) { foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$aBlock['accounted']) { if (!$aBlock['accounted']) {
$iPrevBlockTime = @$aAllBlocks[$iIndex - 1]['time']; if ($share->setUpstream(@$aAllBlocks[$iIndex - 1]['time'])) {
if (!$iPrevBlockTime) { $share->setLastUpstreamId();
$iPrevBlockTime = 0;
} }
$aAccountShares = $share->getSharesForAccountsByTimeframe($aBlock['time'], $iPrevBlockTime);
if (empty($aAccountShares)) { if ($share->setUpstream($aBlock['time'])) {
verbose("No shares found for this block\n"); $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; 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"); verbose("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n");
foreach ($aAccountShares as $key => $aData) { foreach ($aAccountShares as $key => $aData) {
$aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 10),10); $aData['percentage'] = number_format(round(( 100 / $iRoundShares ) * $aData['valid'], 8), 8);
$aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 10), 10); $aData['payout'] = number_format(round(( $aData['percentage'] / 100 ) * $config['reward'], 8), 8);
verbose($aData['id'] . "\t" . verbose($aData['id'] . "\t" .
$aData['username'] . "\t" . $aData['username'] . "\t" .
$aData['valid'] . "\t" . $aData['valid'] . "\t" .
@ -58,14 +66,14 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
$strStatus = "OK"; $strStatus = "OK";
if (!$statistics->updateShareStatistics($aData, $aBlock['id'])) if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
$strStatus = "Stats Failed"; $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"; $strStatus = "Transaction Failed";
verbose("$strStatus\n"); verbose("$strStatus\n");
} }
verbose("------------------------------------------------------------------------\n\n"); verbose("------------------------------------------------------------------------\n\n");
// Move counted shares to archive for this blockhash // Move counted shares to archive before this blockhash upstream share
$share->moveArchiveByTimeframe($aBlock['time'], $iPrevBlockTime, $aBlock['id']); $share->moveArchive($share->getLastUpstreamId(), $iCurrentUpstreamId, $aBlock['id']);
$block->setAccounted($aBlock['blockhash']); $block->setAccounted($aBlock['blockhash']);
} }
} }

View File

@ -7,6 +7,8 @@ if (!defined('SECURITY'))
class Share { class Share {
private $sError = ''; private $sError = '';
private $table = 'shares'; private $table = 'shares';
private $oUpstream;
private $iLastUpstreamId;
// This defines each share // This defines each share
public $rem_host, $username, $our_result, $upstream_result, $reason, $solution, $time; public $rem_host, $username, $our_result, $upstream_result, $reason, $solution, $time;
@ -24,7 +26,7 @@ class Share {
return $this->sError; return $this->sError;
} }
public function getSharesForAccountsByTimeframe($current='', $old='') { public function getSharesForAccounts($previous_upstream=0, $current_upstream) {
$stmt = $this->mysqli->prepare("SELECT $stmt = $this->mysqli->prepare("SELECT
a.id, a.id,
validT.account AS username, validT.account AS username,
@ -36,12 +38,8 @@ class Share {
SUBSTRING_INDEX( `username` , '.', 1 ) as account, SUBSTRING_INDEX( `username` , '.', 1 ) as account,
COUNT(id) AS valid COUNT(id) AS valid
FROM $this->table FROM $this->table
WHERE WHERE id BETWEEN ? AND ?
UNIX_TIMESTAMP(time) > ? AND our_result = 'Y'
AND
UNIX_TIMESTAMP(time) <= ?
AND
our_result = 'Y'
GROUP BY account GROUP BY account
) validT ) validT
LEFT JOIN LEFT JOIN
@ -50,19 +48,15 @@ class Share {
SUBSTRING_INDEX( `username` , '.', 1 ) as account, SUBSTRING_INDEX( `username` , '.', 1 ) as account,
COUNT(id) AS invalid COUNT(id) AS invalid
FROM $this->table FROM $this->table
WHERE WHERE id BETWEEN ? AND ?
UNIX_TIMESTAMP(time) > ? AND our_result = 'N'
AND
UNIX_TIMESTAMP(time) <= ?
AND
our_result = 'N'
GROUP BY account GROUP BY account
) invalidT ) invalidT
ON validT.account = invalidT.account ON validT.account = invalidT.account
INNER JOIN accounts a ON a.username = validT.account INNER JOIN accounts a ON a.username = validT.account
GROUP BY a.username DESC"); GROUP BY a.username DESC");
if ($this->checkStmt($stmt)) { 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(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
$stmt->close(); $stmt->close();
@ -71,18 +65,15 @@ class Share {
return false; return false;
} }
public function getRoundSharesByTimeframe($current='', $old='') { public function getRoundShares($previous_upstream=0, $current_upstream) {
$stmt = $this->mysqli->prepare("SELECT $stmt = $this->mysqli->prepare("SELECT
count(id) as total count(id) as total
FROM $this->table FROM $this->table
WHERE our_result = 'Y' WHERE our_result = 'Y'
AND AND id BETWEEN ? AND ?
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
"); ");
if ($this->checkStmt($stmt)) { if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $old, $current); $stmt->bind_param('ii', $previous_upstream, $current_upstream);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
$stmt->close(); $stmt->close();
@ -91,25 +82,16 @@ class Share {
return false; 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) $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, ? SELECT id, username, our_result, upstream_result, ?
FROM $this->table FROM $this->table
WHERE WHERE id BETWEEN ? AND ?");
UNIX_TIMESTAMP(time) > ? $delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id BETWEEN ? AND ?");
AND
UNIX_TIMESTAMP(time) <= ?
");
$delete_stmt = $this->mysqli->prepare("DELETE FROM $this->table
WHERE
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
");
if ($this->checkStmt($archive_stmt) && $this->checkStmt($delete_stmt)) { 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(); $archive_stmt->execute();
$delete_stmt->bind_param('ii', $old, $current); $delete_stmt->bind_param('ii', $previous_upstream, $current_upstream);
$delete_stmt->execute(); $delete_stmt->execute();
$delete_stmt->close(); $delete_stmt->close();
$archive_stmt->close(); $archive_stmt->close();
@ -118,22 +100,35 @@ class Share {
return false; 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 $stmt = $this->mysqli->prepare("SELECT
SUBSTRING_INDEX( `username` , '.', 1 ) AS account SUBSTRING_INDEX( `username` , '.', 1 ) AS account, id
FROM $this->table FROM $this->table
WHERE upstream_result = 'Y' WHERE upstream_result = 'Y'
AND AND UNIX_TIMESTAMP(time) BETWEEN ? AND (? + 1)
UNIX_TIMESTAMP(time) > ? ORDER BY id ASC LIMIT 1");
AND
UNIX_TIMESTAMP(time) <= ?
ORDER BY id DESC");
if ($this->checkStmt($stmt)) { if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $old, $current); $stmt->bind_param('ii', $time, $time);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); if (! $result = $stmt->get_result()) {
$this->setErrorMessage("No result returned from query");
$stmt->close();
}
$stmt->close(); $stmt->close();
return @$result->fetch_object()->account; $this->oUpstream = $result->fetch_object();
return true;
} }
return false; return false;
} }

View File

@ -13,7 +13,7 @@ class Transaction {
$this->debug = $debug; $this->debug = $debug;
$this->mysqli = $mysqli; $this->mysqli = $mysqli;
$this->config = $config; $this->config = $config;
$this->debug->append("Instantiated Ledger class", 2); $this->debug->append("Instantiated Transaction class", 2);
} }
// get and set methods // get and set methods
@ -24,13 +24,12 @@ class Transaction {
return $this->sError; return $this->sError;
} }
public function addCredit($account_id, $amount, $block_id) { public function addTransaction($account_id, $amount, $type='Credit', $block_id=NULL, $coin_address=NULL, $fee=0) {
$strType = 'Credit'; $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type, coin_address, fee_amount) VALUES (?, ?, ?, ?, ?, ?)");
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, amount, block_id, type) VALUES (?, ?, ?, ?)");
echo $this->mysqli->error;
if ($this->checkStmt($stmt)) { 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()) { if ($stmt->execute()) {
$this->setErrorMessage("Failed to store transaction");
$stmt->close(); $stmt->close();
return true; return true;
} }
@ -38,23 +37,7 @@ class Transaction {
return false; return false;
} }
public function confirmCredits() { public function addDebit($account_id, $amount, $type='AP') {
$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 getTransactions($account_id, $start=0) { public function getTransactions($account_id, $start=0) {
@ -63,17 +46,16 @@ class Transaction {
t.id AS id, t.id AS id,
t.type AS type, t.type AS type,
t.amount AS amount, t.amount AS amount,
t.sendAddress AS sendAddress, t.coin_address AS coin_address,
t.timestamp AS timestamp, t.timestamp AS timestamp,
b.height AS height, b.height AS height,
b.confirmations AS confirmations b.confirmations AS confirmations
FROM transactions AS t FROM transactions AS t
LEFT JOIN blocks AS b ON t.block_id = b.id LEFT JOIN blocks AS b ON t.block_id = b.id
WHERE t.account_id = ? WHERE t.account_id = ?
ORDER BY timestamp DESC ORDER BY id DESC");
LIMIT ? , 30");
if ($this->checkStmt($stmt)) { 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(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC); return $result->fetch_all(MYSQLI_ASSOC);
@ -93,7 +75,7 @@ class Transaction {
public function getBalance($account_id) { public function getBalance($account_id) {
$stmt = $this->mysqli->prepare(" $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 ( FROM (
SELECT account_id, sum(t.amount) AS credit SELECT account_id, sum(t.amount) AS credit
FROM $this->table AS t FROM $this->table AS t