Bitcoin's Blockchain


Blockhain is type of distributed database that store number of blocks to form a chain.
  • Distributed database.
  • Consists number of blocks, each block stores number of data.
  • Blocks linked up each other by hash.

Bitcoin-CLI Find Blockchain Stats

<?php 
include_once "../common.php"; //include this just to derive $_BITCOINCLI_CONFIG[...] variable
$supportCoins = ['btc/main'=>"Bitcoin Mainnet"];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	try { 
	
		$postFields = [
			'jsonrpc' => '1.0',
			'id'=>'curltest',
			'method'=>'getblockchaininfo',
			'params'=> []
		];
		
		$bitcoincliRpcProtocol = "http";
		$bitcoincliRpcHost = $_BITCOINCLI_CONFIG['bitcoincli_rpc_host'];

		$bitcoincliRpcPort = "8332";
		
		$bitcoincliRpcUser = $_BITCOINCLI_CONFIG['bitcoincli_rpc_user'];
		$bitcoincliRpcPwd = $_BITCOINCLI_CONFIG['bitcoincli_rpc_pwd'];
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url = "{$bitcoincliRpcProtocol}://{$bitcoincliRpcHost}:{$bitcoincliRpcPort}");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_POST , 1);
		curl_setopt($ch, CURLOPT_USERPWD , "{$bitcoincliRpcUser}:{$bitcoincliRpcPwd}");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $CURLOPT_POSTFIELDS = json_encode($postFields));

		$rawResult = curl_exec($ch);
		
		$displayRawResults[] = [$CURLOPT_POSTFIELDS, $rawResult];
		
		if (curl_errno($ch)) { 
			throw new Exception('CURL Error: ' . curl_error($ch). "#" . curl_errno($ch));
		} else if (!($result = json_decode($rawResult,true))) {
			throw new Exception('Invalid JSON format.');
		} else if (is_array($result['error']) AND $result['error'] !='null') {
			throw new Exception('Bitcoin CLI Error: ' . $result['error']['message'] . "#" . $result['error']['code']);
		}  else {
						
			curl_close($ch);
		}
		
	} catch (Exception $e) {
		$errmsg .= "Problem found. " . $e->getMessage();
	}
}

include_once("html_iframe_header.php");
if ($errmsg) {
?>
	<div class="alert alert-danger">
		<strong>Error!</strong> <?php echo $errmsg?>
	</div>
<?php
}

if (@count($displayRawResults) > 0) {
	$json_string = json_encode($result['result'], JSON_PRETTY_PRINT);
?>
	<div class="row">
		<div class="col-sm-6">
			<pre style='border-radius:none;'><?php echo $json_string;?></pre>
		</div>
		<div class="col-sm-6">
			<table border=0 class='table table-bordered'>		
				<tr>
					<th style='width:15%'>Params</th><th style='width:15%'>Data Type</th><th style='width:70%'>Explaination</th>
				</tr>
				<tr>
					<td>chain</td><td>String</td><td>Current network name as defined in BIP70 (main, test, regtest).</td>
				</tr>
				
				<tr>
					<td>blocks</td><td>Numeric</td><td>The current number of blocks processed in the server.</td>
				</tr>
				
				<tr>
					<td>headers</td><td>Numeric</td><td>The current number of headers we have validated.</td>
				</tr>
				
				<tr>
					<td>bestblockhash</td><td>String</td><td>The hash of the currently best block.</td>
				</tr>
				
				<tr>
					<td>difficulty</td><td>Numeric</td><td>The current difficulty.</td>
				</tr>
				
				<tr>
					<td>mediantime</td><td>Numeric</td><td>Median time for the current best block.</td>
				</tr>
				
				<tr>
					<td>verificationprogress</td><td>Numeric</td><td>estimate of verification progress [0 - 1].</td>
				</tr>
				
				<tr>
					<td>initialblockdownload</td><td>Boolean</td><td>Estimate of whether this node is in Initial Block Download mode. (debug information)</td>
				</tr>
				
				<tr>
					<td>chainwork</td><td>String</td><td>Total amount of work in active chain, in hexadecimal.</td>
				</tr>
				
				<tr>
					<td>size_on_disk</td><td>Numeric</td><td>The estimated size of the block and undo files on disk.</td>
				</tr>
				
				<tr>
					<td>pruned</td><td>Boolean</td><td>If the blocks are subject to pruning.</td>
				</tr>
				
				<tr>
					<td>pruneheight</td><td>Numeric</td><td>Lowest-height complete block stored (only present if pruning is enabled).</td>
				</tr>
				
				<tr>
					<td>automatic_pruning</td><td>Boolean</td><td>Whether automatic pruning is enabled (only present if pruning is enabled).</td>
				</tr>
				
				<tr>
					<td>prune_target_size</td><td>Numeric</td><td>The target size used by pruning (only present if automatic pruning is.</td>
				</tr>
				
				<tr>
					<td>pruneheight</td><td>Numeric</td><td>Lowest-height complete block stored (only present if pruning is enabled).</td>
				</tr>
				
				<tr>
					<td>softforks</td><td>Array</td><td>Status of softforks in progress.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;id</td><td>String</td><td>Name of softfork.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;version</td><td>Numeric</td><td>Block version.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;reject</td><td>Object</td><td>Progress toward rejecting pre-softfork blocks.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status</td><td>Boolean</td><td>True if threshold reached.</td>
				</tr>
				
				<tr>
					<td>bip9_softforks</td><td>Array</td><td>Status of BIP9 softforks in progress.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;$softfork_name</td><td>String (Variable)</td><td>Name of softfork.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status</td><td>String</td><td>One of "defined", "started", "locked_in", "active", "failed".</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bit</td><td>Numeric</td><td>The bit (0-28) in the block version field used to signal this softfork (only for "started" status).</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startTime</td><td>Numeric</td><td>The minimum median time past of a block at which the bit gains its meaning.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout</td><td>Numeric</td><td>The median time past of a block at which the deployment is considered failed if not yet locked in.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;since</td><td>Numeric</td><td>Height of the first block to which the status applies.</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statistics</td><td>Object</td><td>Numeric statistics about BIP9 signalling for a softfork (only for "started" status).</td>
				</tr>
				
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;period</td><td>Numeric</td><td>The length in blocks of the BIP9 signalling period.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;threshold</td><td>Numeric</td><td>The number of blocks with the version bit set required to activate the feature.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elapsed</td><td>Numeric</td><td>The number of blocks elapsed since the beginning of the current period.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count</td><td>Numeric</td><td>The number of blocks with the version bit set in the current period.</td>
				</tr>
				<tr>
					<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;possible</td><td>Boolean</td><td>Returns false if there are not enough blocks left in this period to pass activation threshold.</td>
				</tr>
				
				<tr>
					<td>warnings</td><td>String</td><td>Any network and blockchain warnings.</td>
				</tr>
			</table>
		</div>		
	</div>
	
	<div class="table-responsive">
		<table border=0 class='table'>		
			<tr>
				<th style='width:30%;'>Params</th>
				<th style='width:70%;'>Raw Result</th>
			</tr>
			<?php 
			foreach($displayRawResults as $thisResult) {
			?>
			<tr>
				<td style='width:30%;'><?php echo htmlentities($thisResult[0])?></td>
				<td style='width:70%;'>
					<textarea class="form-control" readonly rows=4><?php echo htmlentities($thisResult[1]);?></textarea>
				</td>
			</tr>
			<?php
			}
			?>
		</table>
	</div>
<?php
}
?>
<form id='this_form' action='' method='post'>
	<div class="form-group">
		<label for="network">Network:</label>
		<select id="network" name="network" class="form-control" >
			<?php
			foreach($supportCoins as $k=>$v) {
				echo "<option value='{$k}'".($k == $_POST['network'] ? " selected": "").">{$v}</option>";
			}
			?>
		</select>
	</div>
	
	<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php 
include_once("html_iframe_footer.php");		

Bitcoin-CLI Find Blockchain

<?php 
include_once "../common.php"; //include this just to derive $_BITCOINCLI_CONFIG[...] variable
$supportCoins = ['btc/main'=>"Bitcoin Mainnet"];

$load_blocks = 10;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	try { 
	
		if (!in_array($_POST['way'], ['after','before'])) {
			throw new Exception("Please select one of 'Block Generation Way'.");
		}
		
		$displayRawResults = $blocks = [];
		if (!ctype_digit($_POST['height'])) {
			$postFields = [
				'jsonrpc' => '1.0',
				'id'=>'curltest',
				'method'=>'getbestblockhash',
				'params'=> []
			];
		} else {
			$postFields = [
				'jsonrpc' => '1.0',
				'id'=>'curltest',
				'method'=>'getblockhash',
				'params'=> [(int)$_POST['height']]
			];
		}
		
		$bitcoincliRpcProtocol = "http";
		$bitcoincliRpcHost = $_BITCOINCLI_CONFIG['bitcoincli_rpc_host'];

		$bitcoincliRpcPort = "8332";
		
		$bitcoincliRpcUser = $_BITCOINCLI_CONFIG['bitcoincli_rpc_user'];
		$bitcoincliRpcPwd = $_BITCOINCLI_CONFIG['bitcoincli_rpc_pwd'];
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url = "{$bitcoincliRpcProtocol}://{$bitcoincliRpcHost}:{$bitcoincliRpcPort}");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_POST , 1);
		curl_setopt($ch, CURLOPT_USERPWD , "{$bitcoincliRpcUser}:{$bitcoincliRpcPwd}");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $CURLOPT_POSTFIELDS = json_encode($postFields));

		$rawResult = curl_exec($ch);
		
		$displayRawResults[] = [$CURLOPT_POSTFIELDS, $rawResult];
		
		if (curl_errno($ch)) { 
			throw new Exception('CURL Error: ' . curl_error($ch). "#" . curl_errno($ch));
		} else if (!($result = json_decode($rawResult,true))) {
			throw new Exception('Invalid JSON format.');
		} else if (is_array($result['error']) AND $result['error'] !='null') {
			throw new Exception('Bitcoin CLI Error: ' . $result['error']['message'] . "#" . $result['error']['code']);
		}  else {
			$info = curl_getinfo($ch);
			
			curl_close($ch);
			
			$block_hash = $result['result'];
			
			/*
			verbosity = 0, returns a string that is serialized, hex-encoded data for block 'hash'.
			verbosity = 1, returns an Object with information about block .
			verbosity = 2, returns an Object with information about block  and information about each transaction. 
			*/
			$verbosity = 1;
			while($load_blocks > 0 AND $block_hash) {
				
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url = "{$bitcoincliRpcProtocol}://{$bitcoincliRpcHost}:{$bitcoincliRpcPort}");
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
				curl_setopt($ch, CURLOPT_POST , 1);
				curl_setopt($ch, CURLOPT_USERPWD , "{$bitcoincliRpcUser}:{$bitcoincliRpcPwd}");
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $CURLOPT_POSTFIELDS = json_encode(['jsonrpc' => '1.0','id'=>'curltest','method'=>'getblock','params'=> [$block_hash,$verbosity ]]));
				
				$rawResult = curl_exec($ch);
				if (curl_errno($ch)) { 
					throw new Exception('CURL Error: ' . curl_error($ch). "#" . curl_errno($ch));
				} else if (!($result = json_decode($rawResult,true))) {
					throw new Exception('Invalid JSON format.');
				} else if (is_array($result['error']) AND $result['error'] !='null') {
					throw new Exception('Bitcoin CLI Error: ' . $result['error']['message'] . "#" . $result['error']['code']);
				} else {
					
					$displayRawResults[] = [$CURLOPT_POSTFIELDS, $rawResult];
				
					$block = $result['result'];
					$blocks[] = $block;
					if ($_POST['way'] == 'before') {
						$block_hash = $block['previousblockhash'];
					} else {
						$block_hash = $block['nextblockhash'];
						
					}
					
					$load_blocks--;
				}
			}
			
			krsort($blocks);
			$blocks = array_values($blocks);
		}
		
	} catch (Exception $e) {
		$errmsg .= "Problem found. " . $e->getMessage();
	}
}

include_once("html_iframe_header.php");
if ($errmsg) {
?>
	<div class="alert alert-danger">
		<strong>Error!</strong> <?php echo $errmsg?>
	</div>
<?php
}

if ($total_blocks = @count($displayRawResults) > 0) {
	
	echo "<center>";
	?>
		<p><a href='#this_form' onclick="
		var self = $(this);
		$('#height', $('#this_form')).val('<?php echo ($blocks[0]['height']) + 1?>');
		$('input[name=way][value=after]').prop('checked',true);
		$('#height', $('#this_form')).focus();
		">Load More ...</a></p>
		<img src='../media/images/link.png'/>
	<?php
	echo "<div id='blockchain'>";
	
	
	foreach($blocks as $k=>$block) {
		$txs = $block['tx'];
		unset($block['tx']);
?>
		<div class='card' style='margin-top:10px;width:300px;'>
			<div class='card-header'>
				<?php echo $block['hash']?>
				<table border=0 class='table-borderless'>	
					<tr>
						<td>Height</td>
						<td><?php echo $block['height']?></td>
					</tr>
				</table>
			</div>
			<div class='card-body'>
				<div class='table-responsive'>
					<h6>Header:</h6>
					<table border=0 class='table-borderless'>	
						<tr>
							<td>Version</td>
							<td><?php echo $block['version']?></td>
						</tr>
						
						<tr>
							<td>HashPrevBlock</td>
							<td><?php echo $block['previousblockhash']?></td>
						</tr>
						
						<tr>
							<td>MerkleRoot</td>
							<td><?php echo $block['merkleroot']?></td>
						</tr>
						
						<tr>
							<td>Time</td>
							<td><?php echo $block['time']?></td>
						</tr>
						
						<tr>
							<td>Bits</td>
							<td><?php echo $block['bits']?></td>
						</tr>
						
						<tr>
							<td>Nonce</td>
							<td><?php echo $block['nonce']?></td>
						</tr>
					</table>
				</div>
			</div> 
			
			<div class='card-footer'>
				<h6>Body:</h6>
				Total <a href='bitcoin_start_block.php?block_hash=<?php echo $block['hash']?>'><?php echo $block['nTx']?></a> Transaction(s)
			</div>
		</div>
		<img src='../media/images/link.png'/>
<?php
	}
?>
	</div>
	<p><a href='#this_form' onclick="
		var self = $(this);
		$('#height', $('#this_form')).val('<?php echo ($blocks[count($blocks)-1]['height']) - 1?>');
		$('input[name=way][value=before]').prop('checked',true);
		$('#height', $('#this_form')).focus();
		$('input[type=submit]', $('#this_form')).val('Submit To Load More');
		
		">Load More ...</a></p>
	</center>
	<div class="table-responsive">
		<table border=0 class='table'>		
			<tr>
				<th style='width:30%;'>Params</th>
				<th style='width:70%;'>Raw Result</th>
			</tr>
			<?php 
			foreach($displayRawResults as $thisResult) {
			?>
			<tr>
				<td style='width:30%;'><?php echo htmlentities($thisResult[0])?></td>
				<td style='width:70%;'>
					<textarea class="form-control" readonly rows=4><?php echo htmlentities($thisResult[1]);?></textarea>
				</td>
			</tr>
			<?php
			}
			?>
		</table>
	</div>
<?php
}
?>
<form id='this_form' action='' method='post'>
	<div class="form-group">
		<label for="network">Network:</label>
		<select id="network" name="network" class="form-control" >
			<?php
			foreach($supportCoins as $k=>$v) {
				echo "<option value='{$k}'".($k == $_POST['network'] ? " selected": "").">{$v}</option>";
			}
			?>
		</select>
	</div>
	<div class="form-group">
		<label for="height">Block Height:</label>
		<input class="form-control" type='text' name='height' id='height' value='<?php echo $_POST['height']?>'>
		
		 * Put empty if you want latest block height.
	</div>
	
	<div class="form-group">
		<label for="height">Block Generation Way:</label>
		<div class="form-check-inline">
			<label class="form-check-label">
			<input type="radio" class="form-check-input" name="way" value="after"<?php echo $_POST['way']=='after' ? ' checked': ''?>>After Block Height
			</label>
		</div>
		<div class="form-check-inline">
			<label class="form-check-label">
				<input type="radio" class="form-check-input" name="way" value="before"<?php echo $_POST['way']=='before' ? ' checked': ''?>>Before Block Height
			</label>
		</div>
	</div>

	<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php 
include_once("html_iframe_footer.php");		








Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.