function crawl_blockchain($coin_name, $block_coll, $transaction_coll, $start) { $client = get_rpc_client(); while (1) { try { $hash = get_block_hash($client, $start); $data = get_block_data($client, $hash); $txs = $data['tx']; $num_txs = count($txs); print "Crawling block {$start} ... (num_tx = {$num_txs})\n"; $tx_array = array(); for ($i = 0; $i < $num_txs; $i++) { try { $cur_tx = $txs[$i]; if ($start > 0) { $raw_tx = $client->getrawtransaction($cur_tx); $decoded_tx = $client->decoderawtransaction($raw_tx); } else { $raw_tx = null; $decoded_tx = null; } array_push($tx_array, $cur_tx); $vin_array = array(); $num_vin = count($decoded_tx['vin']); for ($j = 0; $j < $num_vin; $j++) { if (isset($decoded_tx['vin'][$j]['coinbase'])) { $cur_vin_array = array('coinbase' => $decoded_tx['vin'][$j]['coinbase'], 'sequence' => $decoded_tx['vin'][$j]['sequence']); array_push($vin_array, $cur_vin_array); } else { if (isset($decoded_tx['vin'][$j]['scriptSig'])) { $script_sig_array = array('asm' => $decoded_tx['vin'][$j]['scriptSig']['asm'], 'hex' => $decoded_tx['vin'][$j]['scriptSig']['hex']); $cur_vin_array = array('txid' => $decoded_tx['vin'][$j]['txid'], 'vout' => $decoded_tx['vin'][$j]['vout'], 'scriptSig' => $script_sig_array, 'sequence' => $decoded_tx['vin'][$j]['sequence']); array_push($vin_array, $cur_vin_array); } } } $vout_array = array(); $num_vout = count($decoded_tx['vout']); for ($j = 0; $j < $num_vout; $j++) { $cur_address_array = $decoded_tx['vout'][$j]['scriptPubKey']['addresses']; $cur_script_array = array('asm' => $decoded_tx['vout'][$j]['scriptPubKey']['asm'], 'hex' => $decoded_tx['vout'][$j]['scriptPubKey']['hex'], 'reqSigs' => $decoded_tx['vout'][$j]['scriptPubKey']['reqSigs'], 'type' => $decoded_tx['vout'][$j]['scriptPubKey']['type'], 'addresses' => $cur_address_array); $cur_vout_array = array('value' => $decoded_tx['vout'][$j]['value'], 'n' => $decoded_tx['vout'][$j]['n'], 'scriptPubKey' => $cur_script_array); array_push($vout_array, $cur_vout_array); } $tx_data_array = array('txid' => $decoded_tx['txid'], 'version' => $decoded_tx['version'], 'locktime' => $decoded_tx['locktime'], 'vin' => $vin_array, 'vout' => $vout_array); $tx_data = array('index' => $i, 'block_index' => $start, 'hash' => $cur_tx, 'data' => $tx_data_array); $transaction_coll->insert($tx_data); } catch (Exception $tx_e) { print "Skipping transaction: No info: {$tx_e}\n"; } } $block_data = array('index' => $start, 'data' => array('hash' => $hash, "size" => $data['size'], "height" => $data['height'], "version" => $data['version'], "merkleroot" => $data['merkleroot'], "tx" => $tx_array, "time" => $data['time'], "nonce" => $data['nonce'], "bits" => $data['bits'], "difficulty" => $data['difficulty'], "previousblockhash" => $start == 0 ? '' : $data['previousblockhash'], "nextblockhash" => $data['nextblockhash'])); $block_coll->insert($block_data); $start++; set_last_block($block_coll, $start); } catch (Exception $e) { print "Failed to retrieve block info at index {$start}: {$e}\n"; break; } } }
if (empty($block)) { return "error: could not get block {$block_hash}"; } foreach ($block['tx'] as $key => $txid) { $tx = $daemon->getrawtransaction($txid, 1); if (empty($tx)) { return "error: could not get tx {$txid}"; } else { scan_tx($tx); } } return $block; } while ($l_blk <= $block_height) { $block_hash = $daemon->getblockhash($l_blk); $last_hash = get_block_hash($l_blk); if (empty($block_hash)) { $error = "error: could not get block {$l_blk}"; break; } if (empty($last_hash) || $last_hash === $null_64bstr) { $block = process_block($block_hash); if (isset($block['hash'])) { update_astats(false); put_block_hash($l_blk, $block_hash); $l_txn += count($block['tx']); $l_blk++; } else { $error = $block; break; }