From 59c53a5d3dc1541e0c93b9209122c1ff0fd5b011 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sat, 11 May 2013 08:00:35 +0200 Subject: [PATCH] adding more output to findblocks --- cronjobs/findblock.php | 25 ++++++++++++++++++------- public/include/classes/block.class.php | 20 ++++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/cronjobs/findblock.php b/cronjobs/findblock.php index 9794ff26..a56cfb3e 100644 --- a/cronjobs/findblock.php +++ b/cronjobs/findblock.php @@ -22,25 +22,36 @@ limitations under the License. require_once('shared.inc.php'); // Fetch our last block found from the DB as a starting point -$strLastBlockHash = $block->getLast()->blockhash; +$strLastBlockHash = @$block->getLast()->blockhash; if (!$strLastBlockHash) { $strLastBlockHash = ''; } -try { +if ( $bitcoin->can_connect() === true ){ $aTransactions = $bitcoin->query('listsinceblock', $strLastBlockHash); -} catch (Exception $e) { - echo "Unable to connect to bitcoin RPC\n"; + $iDifficulty = $bitcoin->query('getdifficulty'); +} else { + echo "Aborted: " . $bitcoin->can_connect() . "\n"; exit(1); } +echo "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']; - if ( ! $block->addBlock($aData) ) { - echo "Failed to add block: " . $aData['height'] , "\n"; - echo "Error : " . $block->getError() . "\n"; + $aData['difficulty'] = $iDifficulty; + echo 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"; + if ( $block->addBlock($aData) ) { + echo "Added\n"; + } else { + echo "Failed" . "\n"; } } } diff --git a/public/include/classes/block.class.php b/public/include/classes/block.class.php index 6273af98..83b5907d 100644 --- a/public/include/classes/block.class.php +++ b/public/include/classes/block.class.php @@ -8,7 +8,7 @@ class Block { private $sError = ''; private $table = 'blocks'; // This defines each block - public $height, $blockhash, $confirmations, $difficulty, $time; + public $height, $blockhash, $confirmations, $time, $accounted; public function __construct($debug, $mysqli, $salt) { $this->debug = $debug; @@ -35,12 +35,24 @@ class Block { return false; } - public function addBlock($block) { - $stmt = $this->mysqli->prepare("INSERT INTO $this->table (height, blockhash, confirmations, amount, time) VALUES (?, ?, ?, ?, ?)"); + public function getAll($order='DESC') { + $stmt = $this->mysqli->prepare("SELECT * FROM $this->table ORDER BY height $order"); if ($this->checkStmt($stmt)) { - $stmt->bind_param('isidi', $block['height'], $block['blockhash'], $block['confirmations'], $block['amount'], $block['time']); + $stmt->execute(); + $result = $stmt->get_result(); + $stmt->close(); + return $result->fetch_all(MYSQLI_ASSOC); + } + return false; + } + + public function addBlock($block) { + $stmt = $this->mysqli->prepare("INSERT INTO $this->table (height, blockhash, confirmations, amount, difficulty, time) VALUES (?, ?, ?, ?, ?, ?)"); + if ($this->checkStmt($stmt)) { + $stmt->bind_param('isiddi', $block['height'], $block['blockhash'], $block['confirmations'], $block['amount'], $block['difficulty'], $block['time']); if (!$stmt->execute()) { $this->debug->append("Failed to execute statement: " . $stmt->error); + $this->setErrorMessage($stmt->error); $stmt->close(); return false; }