cleaned up crons, changed timestamp range to properly find shares, added verbose parameter

This commit is contained in:
Sebastian Grewe 2013-05-11 18:51:10 +02:00
parent 365a91e407
commit 47b3816d27
4 changed files with 67 additions and 40 deletions

View File

@ -31,27 +31,27 @@ if ( $bitcoin->can_connect() === true ){
$aTransactions = $bitcoin->query('listsinceblock', $strLastBlockHash);
$iDifficulty = $bitcoin->query('getdifficulty');
} else {
echo "Aborted: " . $bitcoin->can_connect() . "\n";
verbose("Aborted: " . $bitcoin->can_connect() . "\n");
exit(1);
}
echo "Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n";
verbose("Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n");
foreach ($aTransactions['transactions'] as $iIndex => $aData) {
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
$aData['height'] = $aBlockInfo['height'];
$aData['difficulty'] = $iDifficulty;
echo substr($aData['blockhash'], 0, 15) . "...\t" .
verbose(substr($aData['blockhash'], 0, 15) . "...\t" .
$aData['height'] . "\t" .
$aData['amount'] . "\t" .
$aData['confirmations'] . "\t\t" .
$aData['difficulty'] . "\t" .
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t";
strftime("%Y-%m-%d %H:%M:%S", $aData['time']) . "\t");
if ( $block->addBlock($aData) ) {
echo "Added\n";
verbose("Added\n");
} else {
echo "Failed" . "\n";
verbose("Failed" . "\n");
}
}
}

View File

@ -21,7 +21,7 @@ limitations under the License.
// Include all settings and classes
require_once('shared.inc.php');
// Fetch our last block found from the DB as a starting point
// Fetch all accounted blocks
$aAllBlocks = $block->getAllUnaccounted('ASC');
foreach ($aAllBlocks as $iIndex => $aBlock) {
if (!$aBlock['accounted']) {
@ -32,32 +32,31 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
$aAccountShares = $share->getSharesForAccountsByTimeframe($aBlock['time'], $iPrevBlockTime);
$iRoundShares = $share->getRoundSharesByTimeframe($aBlock['time'], $iPrevBlockTime);
$strFinder = $share->getFinderByTimeframe($aBlock['time'], $iPrevBlockTime);
echo "ID\tHeight\tTime\t\tShares\tFinder\n";
echo $aBlock['id'] . "\t" . $aBlock['height'] . "\t" . $aBlock['time'] . "\t" . $iRoundShares . "\t" . $strFinder . "\n\n";
echo "ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tStatus\n";
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);
echo $aData['id'] . "\t" .
verbose($aData['id'] . "\t" .
$aData['username'] . "\t" .
$aData['valid'] . "\t" .
$aData['invalid'] . "\t" .
$aData['percentage'] . "\t" .
$aData['payout'] . "\t";
$aData['payout'] . "\t");
// Do all database updates for statistics and payouts
$strStatus = "OK";
// if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
// $strStatus = "Stats Failed";
if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
$strStatus = "Stats Failed";
if (!$transaction->addCredit($aData['id'], $aData['payout'], $aBlock['id']))
$strStatus = "Transaction Failed";
echo "$strStatus\n";
verbose("$strStatus\n");
}
echo "------------------------------------------------------------------------\n\n";
verbose("------------------------------------------------------------------------\n\n");
// Now that we have all shares counted internally let's update the tables
// Set shares as counted and mark block as accounted for
$share->setCountedByTimeframe($aBlock['time'], $iPrevBlockTime);
// Move counted shares to archive for this blockhash
$share->moveArchiveByTimeframe($aBlock['time'], $iPrevBlockTime, $aBlock['id']);
$block->setAccounted($aBlock['blockhash']);
}
}

View File

@ -30,3 +30,17 @@ require_once(BASEPATH . '/include/config/global.inc.php');
// We include all needed files here, even though our templates could load them themself
require_once(BASEPATH . INCLUDE_DIR . '/autoloader.inc.php');
// Parse command line
$options = getopt("v");
if (array_key_exists('v', $options)) {
define("VERBOSE", true);
} else {
define("VERBOSE", false);
}
// Command line cron functions only
function verbose($msg) {
if (VERBOSE) echo $msg;
}

View File

@ -37,11 +37,11 @@ class Share {
COUNT(id) AS valid
FROM $this->table
WHERE
UNIX_TIMESTAMP(time) BETWEEN ? AND ?
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
AND
our_result = 'Y'
AND
counted = 0
GROUP BY account
) validT
LEFT JOIN
@ -51,11 +51,11 @@ class Share {
COUNT(id) AS invalid
FROM $this->table
WHERE
UNIX_TIMESTAMP(time) BETWEEN ? AND ?
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
AND
our_result = 'N'
AND
counted = 0
GROUP BY account
) invalidT
ON validT.account = invalidT.account
@ -76,8 +76,10 @@ class Share {
count(id) as total
FROM $this->table
WHERE our_result = 'Y'
AND UNIX_TIMESTAMP(time) BETWEEN ? AND ?
AND counted = 0
AND
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $old, $current);
@ -89,19 +91,28 @@ class Share {
return false;
}
public function setCountedByTimeframe($current='', $old='') {
$stmt = $this->mysqli->prepare("UPDATE $this->table
SET
counted = 1
public function moveArchiveByTimeframe($current='', $old='',$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) BETWEEN ? AND ?
AND counted = 0
");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $old, $current);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
UNIX_TIMESTAMP(time) > ?
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)) {
$archive_stmt->bind_param('iii', $block_id, $old, $current);
$archive_stmt->execute();
$delete_stmt->bind_param('ii', $old, $current);
$delete_stmt->execute();
$delete_stmt->close();
$archive_stmt->close();
return true;
}
return false;
@ -112,7 +123,10 @@ class Share {
SUBSTRING_INDEX( `username` , '.', 1 ) AS account
FROM $this->table
WHERE upstream_result = 'Y'
AND UNIX_TIMESTAMP(time) BETWEEN ? AND ?
AND
UNIX_TIMESTAMP(time) > ?
AND
UNIX_TIMESTAMP(time) <= ?
ORDER BY id DESC");
if ($this->checkStmt($stmt)) {
$stmt->bind_param('ii', $old, $current);