/** * Output the block detail information HTML/PHP code * * @param int $block_id * @param bool $hash */ function block_detail($block_id, $hash = FALSE) { if ($hash == TRUE) { $raw_block = getblock($block_id); } else { $block_hash = getblockhash(intval($block_id)); $raw_block = getblock($block_hash); } if (isset($raw_block["hash"])) { include 'views/block_detail.php'; } else { section_head("Error"); section_subhead("This block is not in the blockchain"); } }
function spectra_block_setformat() { if (!spectra_node_isalive()) { spectra_log_write(1, "Unable to connect to " . $GLOBALS["currency"]["name"] . " node."); } // Retrieve the first block from the chain without specifying // verbose mode $hash = getblockhash(1); $block = getblock($hash); // Determine the format of the returned block data if (is_array($block)) { system_flag_set("block_req_verbose", 0); spectra_log_write(0, "Block format set to ignore verbose mode."); } else { system_flag_set("block_req_verbose", 1); spectra_log_write(0, "Block format set to require verbose mode."); } }
function block_detail($block_id, $hash = FALSE) { if ($hash == TRUE) { $raw_block = getblock($block_id); } else { $block_hash = getblockhash(intval($block_id)); $raw_block = getblock($block_hash); } echo "\t<div class=\"block_banner\">\n"; echo "\n"; echo "\t\t<div class=\"blockbanner_left\">\n"; echo "\t\t\tBlock Height: " . $raw_block["height"] . "\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blockbanner_right\">\n"; echo "\t\t\tBlock Time: " . date("F j, Y, H:i:s", $raw_block["time"]) . "\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t</div>\n"; echo "\n"; echo "\t<div class=\"blockdetail\">\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\">Block Version</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["version"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\">Block Size</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["size"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\"># of Confirmations</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["confirmations"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t</div>\n"; echo "\n"; echo "\t<div class=\"blockdetail\">\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\">Block Bits</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["bits"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\">Block Nonce</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["nonce"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blockdetail_detail\">\n"; echo "\t\t\t<div class=\"blockdetail_header\">Block Difficulty</div>\n"; echo "\t\t\t<div class=\"blockdetail_content\">\n"; echo "\t\t\t\t" . $raw_block["difficulty"] . "\n"; echo "\t\t\t</div>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t</div>\n"; echo "\n"; detail_display("Merkle Root", $raw_block["merkleroot"]); detail_display("Block Hash", blockhash_link($raw_block["hash"])); echo "\t<div class=\"blocknav\">\n"; echo "\n"; echo "\t\t<div class=\"blocknav_prev\">\n"; echo "\t\t\t<a href=\"" . $_SERVER["PHP_SELF"] . "?block_hash=" . $raw_block["previousblockhash"] . "\" title=\"View Previous Block\"><- Previous Block</a>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blocknav_news\">\n"; echo "\t\t\tBlock Time: " . date("F j, Y, g:i a", $raw_block["time"]) . "\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t\t<div class=\"blocknav_next\">\n"; echo "\t\t\t<a href=\"" . $_SERVER["PHP_SELF"] . "?block_hash=" . $raw_block["nextblockhash"] . "\" title=\"View Next Block\">Next Block -></a>\n"; echo "\t\t</div>\n"; echo "\n"; echo "\t</div>\n"; echo "\n"; echo "\t<div class=\"txlist_header\">\n"; echo "\t\tTransactions In This Block\n"; echo "\t</div>\n"; echo "\n"; echo "\t<div class=\"txlist_wrap\">\n"; foreach ($raw_block["tx"] as $index => $tx) { echo "\t\t<div class=\"txlist_showtx\" id=\"showtx_" . $index . "\">\n"; echo "\t\t\t<a href=\"" . $_SERVER["PHP_SELF"] . "?transaction=" . $tx . "\" title=\"Transaction Details\">\n"; echo "\t\t\t\t" . $tx . "\n"; echo "\t\t\t</a>\n"; echo "\t\t</div>\n\n"; } echo "\t</div>\n"; echo "\n"; }
function spectra_network_statistics() { echo "\t<h1> Network Statistics </h1> \n\n"; echo "\t<div id=\"network_panel\">\n\n"; $network_info = getinfo(); if (isset($network_info["blocks"]) && $network_info["blocks"] != "") { echo "\t\t<div class=\"network_detail\">\n"; echo "\t\t\t<div class=\"network_detail_label\"> \n"; echo "\t\t\t\tBlock Count: \n"; echo "\t\t\t</div> \n"; echo "\t\t\t<div class=\"network_detail_value\"> \n"; echo "\t\t\t\t" . $network_info["blocks"] . "\n"; echo "\t\t\t</div> \n"; echo "\t\t</div> \n\n"; } if (isset($network_info["difficulty"]) && $network_info["difficulty"] != "") { echo "\t\t<div class=\"network_detail\">\n"; echo "\t\t\t<div class=\"network_detail_label\"> \n"; echo "\t\t\t\tDifficulty: \n"; echo "\t\t\t</div> \n"; echo "\t\t\t<div class=\"network_detail_value\"> \n"; echo "\t\t\t\t" . $network_info["difficulty"] . "\n"; echo "\t\t\t</div> \n"; echo "\t\t</div> \n\n"; } if (isset($network_info["connections"]) && $network_info["connections"] != "") { echo "\t\t<div class=\"network_detail\">\n"; echo "\t\t\t<div class=\"network_detail_label\"> \n"; echo "\t\t\t\tConnections: \n"; echo "\t\t\t</div> \n"; echo "\t\t\t<div class=\"network_detail_value\"> \n"; echo "\t\t\t\t" . $network_info["connections"] . "\n"; echo "\t\t\t</div> \n"; echo "\t\t</div> \n\n"; } $net_speed = getnetworkhashps(); if ($net_speed != "" && !is_array($net_speed)) { echo "\t\t<div class=\"network_detail\">\n"; echo "\t\t\t<div class=\"network_detail_label\"> \n"; echo "\t\t\t\tNetwork H/s: \n"; echo "\t\t\t</div> \n"; echo "\t\t\t<div class=\"network_detail_value\"> \n"; echo "\t\t\t\t" . $net_speed . "\n"; echo "\t\t\t</div> \n"; echo "\t\t</div>\n\n"; } // Sort out the time of the last block in the node $node_height = getblockcount(); $node_hash = getblockhash($node_height); $node_block = getblock($node_hash); if (!is_numeric($node_block["time"])) { $node_block["time"] = strtotime($node_block["time"]); } // Calculate the time since the last block $last_time = floor((time() - $node_block["time"]) / 60); echo "\t\t<div class=\"network_detail\">\n"; echo "\t\t\t<div class=\"network_detail_label\"> \n"; echo "\t\t\t\tLast Block: \n"; echo "\t\t\t</div> \n"; echo "\t\t\t<div class=\"network_detail_value\"> \n"; echo "\t\t\t\t" . $last_time . " Minutes\n"; echo "\t\t\t</div> \n"; echo "\t\t</div> \n\n"; echo "\t</div>\n\n"; }
} /****************************************************************************** Load New Blocks ******************************************************************************/ // Block heights are reloaded in case of a resync $start = spectra_block_height(); $top = getblockcount(); // If there is not a new block, end the checks if ($start >= $top) { // Make a note to the maintenance log spectra_log_write(0, "No new block, loading complete."); } else { // Log the new block height spectra_log_write(0, "Explorer Block: " . $start . " - Node Block: " . $top); // Get the data for the prior block from the node $hash_back = getblockhash((int) $start); $data_node = getblock($hash_back); // Update the prior block record $result = mysqli_setfield($GLOBALS["tables"]["block"], "nextblockhash", $data_node["nextblockhash"], "`hash` = '" . $data_node["hash"] . "'"); // Initialize a block counter for logging $count_loaded = 0; // Load any new blocks available from the node while ($start < $top) { // Increment the explorer height to indicate the next desired block $start++; spectra_log_write(0, "Loading Block: " . $start); // The next block is loaded spectra_block_load($start); // Increment the block count $count_loaded++; }
<?php // Enable the spectra functionality require_once "../lib/spectra_config.php"; // check for a block number in the command line if (!isset($argv[1]) or $argv[1] == "") { echo "You must provide a block height.\n\n"; exit; } // Verify that a block exists at this height $hash = getblockhash((int) $argv[1]); $block = getblock($hash); if (!isset($block["hash"])) { spectra_log_write(1, "Unable to retrieve block at height " . $argv[1]); } // Process the block with the block parser spectra_block_load((int) $argv[1]); // Some data for the log spectra_log_write(0, "Inserted block at height " . $argv[1]); /****************************************************************************** Developed By Jake Paysnoe - Copyright � 2015 SPEC Development Team SPEC Block Explorer is released under the MIT Software License. For additional details please read license.txt in this package. ******************************************************************************/
function spectra_block_load($block_height) { // Fetch the hash for the specified block height $hash = getblockhash($block_height); // Fetch the block data from the node $block = getblock($hash, TRUE); // The block-level data is sorted into an array to match the // formatting of the prepared statement $parsed_block = spectra_block_parse($block); // Parse and calculate each transaction in the block foreach ($block["tx"] as $txid) { // Parse the transaction by ID $tx_parsed = spectra_tx_parse($txid); // Process the transaction vins foreach ($tx_parsed["vin"] as $index => $vin) { // Parse the vin data together with the tx data $parsed_vin = spectra_vin_parse($tx_parsed, $vin); // Bind and insert the vin data $bound = $GLOBALS["db"]["prep"]["vin"]->bind_param("ssisdssiss", $parsed_vin["src_block"], $parsed_vin["src_tx"], $parsed_vin["src_vout"], $parsed_vin["src_address"], $parsed_vin["src_value"], $parsed_vin["in_block"], $parsed_vin["in_tx"], $parsed_vin["time"], $parsed_vin["coinbase"], $parsed_vin["sequence"]); if (!$bound) { spectra_log_write(1, "(prep_vin: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["vin"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_vin: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } // Update the transaction $tx_parsed["val_in"] = bcadd($tx_parsed["val_in"], $parsed_vin["src_value"], 8); // If this is not a generation, update the sending address if ($parsed_vin["src_address"] !== "Generated") { // decode the source address $addr_src = json_decode($parsed_vin["src_address"], 1); // Retrieve information about the source address $record = mysqli_getrow($GLOBALS["tables"]["ledger"], "`address` = '" . $addr_src[0] . "'"); // Increment the outgoing transaction counter for the source address $tx_outcount = $record["data"]["tx_out"] + 1; // Increment the outgoing transaction value for the source address $spent = bcadd($record["data"]["spent"], $parsed_vin["src_value"], 8); // Adjust the source account balance $balance = bcsub($record["data"]["balance"], $parsed_vin["src_value"], 8); // Bind and insert the updated values $bound = $GLOBALS["db"]["prep"]["addvin"]->bind_param("idds", $tx_outcount, $spent, $balance, $addr_src[0]); if (!$bound) { spectra_log_write(1, "(prep_addvin: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["addvin"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_addvin: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } } } // It is necessary to watch for a stake modifier in order // to separate coins generated in a block-split from any // tx fees in the block. $modifier = 0; // Process the transaction vouts foreach ($tx_parsed["vout"] as $index => $vout) { // Parse the vout $parsed_vout = spectra_vout_parse($parsed_block["flags"], $tx_parsed, $vout); // Set the modiier flag if this vout is a stake-modifier if (strcasecmp(substr($parsed_vout["addresses"], 2, 14), "Stake Modifier") == 0) { $modifier = 1; } // Insert the vout data $bound = $GLOBALS["db"]["prep"]["vout"]->bind_param("ssidiiss", $parsed_vout["in_block"], $parsed_vout["in_tx"], $parsed_vout["time"], $parsed_vout["value"], $parsed_vout["n"], $parsed_vout["reqsigs"], $parsed_vout["type"], $parsed_vout["addresses"]); if (!$bound) { spectra_log_write(1, "(prep_vout: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["vout"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_vout: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } // Update the transaction $tx_parsed["val_out"] = bcadd($tx_parsed["val_out"], $parsed_vout["value"], 8); // If this address is unknown a record is created foreach (json_decode($parsed_vout["addresses"], 1) as $address) { $exists = mysqli_isrow($GLOBALS["tables"]["ledger"], "`address` = '" . $address . "'"); if (!$exists["data"]) { // Format an empty address record $address_new = spectra_address_new(); // Populate the empty record with the supplied values $address_new["address"] = $address; $address_new["firstblock"] = $block["hash"]; // Bind the record for insertion $bound = $GLOBALS["db"]["prep"]["address"]->bind_param("ssididds", $address_new["address"], $address_new["firstblock"], $address_new["tx_in"], $address_new["received"], $address_new["tx_out"], $address_new["spent"], $address_new["balance"], $address_new["owner"]); if (!$bound) { spectra_log_write(1, "(prep_address: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } // Execute the prepared statement $response = $GLOBALS["db"]["prep"]["address"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_address: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } } } // For balancing purposes, in case of a multi-sig tx // Only the first address in the list is credited with // the balance from the transaction // Update address information with values from this tx // Decode the addresses field from the parsed vout $addresses = json_decode($parsed_vout["addresses"], 1); // Retrieve information about the receiving address $record = mysqli_getrow($GLOBALS["tables"]["ledger"], "`address` = '" . $addresses[0] . "'"); // Increment the incoming transaction counter for the receiving address $tx_incount = $record["data"]["tx_in"] + 1; // Increment the incoming transaction value for the receiving address $received = bcadd($record["data"]["received"], $parsed_vout["value"], 8); // Adjust the receiving account balance $balance = bcadd($record["data"]["balance"], $parsed_vout["value"], 8); // Bind and insert the updated values $bound = $GLOBALS["db"]["prep"]["addvout"]->bind_param("idds", $tx_incount, $received, $balance, $addresses[0]); if (!$bound) { spectra_log_write(1, "(prep_addvout: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["addvout"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_addvout: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } } // Calculate the fee value for this transaction if ($modifier == 0) { $tx_parsed["val_fee"] = bcsub($tx_parsed["val_in"], $tx_parsed["val_out"], 8); } else { $tx_parsed["val_fee"] = 0; } // Bind and insert the parsed tx $bound = $GLOBALS["db"]["prep"]["tx"]->bind_param("ssiiiisddd", $tx_parsed["in_block"], $tx_parsed["txid"], $tx_parsed["version"], $tx_parsed["time"], $tx_parsed["locktime"], $tx_parsed["blocktime"], $tx_parsed["tx-comment"], $tx_parsed["val_in"], $tx_parsed["val_out"], $tx_parsed["val_fee"]); if (!$bound) { spectra_log_write(1, "(prep_tx: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["tx"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_tx: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } // Update the block with the values $parsed_block["val_in"] = bcadd($parsed_block["val_in"], $tx_parsed["val_in"], 8); $parsed_block["val_out"] = bcadd($parsed_block["val_out"], $tx_parsed["val_out"], 8); $parsed_block["val_fee"] = bcadd($parsed_block["val_fee"], $tx_parsed["val_fee"], 8); } // Insert the block data into the database $bound = $GLOBALS["db"]["prep"]["block"]->bind_param("ssiiidssssiisdsssssddd", $parsed_block["hash"], $parsed_block["proofhash"], $parsed_block["size"], $parsed_block["height"], $parsed_block["version"], $parsed_block["mint"], $parsed_block["flags"], $parsed_block["entropybit"], $parsed_block["merkleroot"], $parsed_block["tx"], $parsed_block["time"], $parsed_block["nonce"], $parsed_block["bits"], $parsed_block["difficulty"], $parsed_block["modifier"], $parsed_block["modifierchecksum"], $parsed_block["signature"], $parsed_block["previousblockhash"], $parsed_block["nextblockhash"], $parsed_block["val_in"], $parsed_block["val_out"], $parsed_block["val_fee"]); if (!$bound) { spectra_log_write(1, "(prep_block: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } $response = $GLOBALS["db"]["prep"]["block"]->execute(); if ($GLOBALS["db"]["obj"]->errno > 0) { spectra_log_write(1, "(prep_block: " . __LINE__ . ") " . $GLOBALS["db"]["obj"]->error); } // Reset prepared statements for performance spectra_prep_reset(); }
function block_mint($block_id) { $block_hash = getblockhash($block_id); $raw_block = getblock($block_hash); $mint = $raw_block["mint"]; return $mint; }
/****************************************************************************** Node / Chain API ******************************************************************************/ if ($_REQUEST["method"] == "getblockcount") { $requested = getblockcount(); if (isset($requested["error"]) && $requested["error"] != "") { spectra_api_response("", 0, $requested["error"]["message"]); } else { spectra_api_response($requested); } } if ($_REQUEST["method"] == "getblockhash") { if (!isset($_REQUEST["height"])) { spectra_api_response("", 0, "Block Height (&height=xxxxx) Is Required For Method 'getblockhash'"); } $requested = getblockhash((int) $_REQUEST["height"]); if (isset($requested["error"]) && $requested["error"] != "") { spectra_api_response("", 0, $requested["error"]["message"]); } else { spectra_api_response($requested); } } if ($_REQUEST["method"] == "getbestblockhash") { $requested = getbestblockhash(); if (isset($requested["error"]) && $requested["error"] != "") { spectra_api_response("", 0, $requested["error"]["message"]); } else { spectra_api_response($requested); } } if ($_REQUEST["method"] == "getblock") {