Merge pull request #6 from TheSerapher/findblock-update

adding more output to findblocks
This commit is contained in:
Sebastian Grewe 2013-05-10 23:01:08 -07:00
commit 0eab903670
2 changed files with 34 additions and 11 deletions

View File

@ -22,25 +22,36 @@ limitations under the License.
require_once('shared.inc.php'); require_once('shared.inc.php');
// Fetch our last block found from the DB as a starting point // Fetch our last block found from the DB as a starting point
$strLastBlockHash = $block->getLast()->blockhash; $strLastBlockHash = @$block->getLast()->blockhash;
if (!$strLastBlockHash) { if (!$strLastBlockHash) {
$strLastBlockHash = ''; $strLastBlockHash = '';
} }
try { if ( $bitcoin->can_connect() === true ){
$aTransactions = $bitcoin->query('listsinceblock', $strLastBlockHash); $aTransactions = $bitcoin->query('listsinceblock', $strLastBlockHash);
} catch (Exception $e) { $iDifficulty = $bitcoin->query('getdifficulty');
echo "Unable to connect to bitcoin RPC\n"; } else {
echo "Aborted: " . $bitcoin->can_connect() . "\n";
exit(1); exit(1);
} }
echo "Blockhash\t\tHeight\tAmount\tConfirmations\tDiff\t\tTime\t\t\tStatus\n";
foreach ($aTransactions['transactions'] as $iIndex => $aData) { foreach ($aTransactions['transactions'] as $iIndex => $aData) {
if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) { if ( $aData['category'] == 'generate' || $aData['category'] == 'immature' ) {
$aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']); $aBlockInfo = $bitcoin->query('getblock', $aData['blockhash']);
$aData['height'] = $aBlockInfo['height']; $aData['height'] = $aBlockInfo['height'];
if ( ! $block->addBlock($aData) ) { $aData['difficulty'] = $iDifficulty;
echo "Failed to add block: " . $aData['height'] , "\n"; echo substr($aData['blockhash'], 0, 15) . "...\t" .
echo "Error : " . $block->getError() . "\n"; $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";
} }
} }
} }

View File

@ -8,7 +8,7 @@ class Block {
private $sError = ''; private $sError = '';
private $table = 'blocks'; private $table = 'blocks';
// This defines each block // This defines each block
public $height, $blockhash, $confirmations, $difficulty, $time; public $height, $blockhash, $confirmations, $time, $accounted;
public function __construct($debug, $mysqli, $salt) { public function __construct($debug, $mysqli, $salt) {
$this->debug = $debug; $this->debug = $debug;
@ -35,12 +35,24 @@ class Block {
return false; return false;
} }
public function addBlock($block) { public function getAll($order='DESC') {
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (height, blockhash, confirmations, amount, time) VALUES (?, ?, ?, ?, ?)"); $stmt = $this->mysqli->prepare("SELECT * FROM $this->table ORDER BY height $order");
if ($this->checkStmt($stmt)) { 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()) { if (!$stmt->execute()) {
$this->debug->append("Failed to execute statement: " . $stmt->error); $this->debug->append("Failed to execute statement: " . $stmt->error);
$this->setErrorMessage($stmt->error);
$stmt->close(); $stmt->close();
return false; return false;
} }