def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks') ) #D:\Courses\Capstone\PyBC\pybit\Blocks acc_btc_per_address = {} script_count = {} script_count["p2pk"] = 0 script_count["p2pkh"] = 0 script_count["p2ms"] = 0 script_count["p2sh"] = 0 for block in blockchain.get_unordered_blocks(): for tx in block.transactions: for output in tx.outputs: address = output.addresses if output.is_pubkey(): script_count["p2pk"] += 1 elif output.is_pubkeyhash(): script_count["p2pkh"] += 1 elif output.is_multisig(): script_count["p2ms"] += 1 elif output.is_p2sh(): script_count["p2sh"] += 1 else: continue if address[0] in acc_btc_per_address.keys(): acc_btc_per_address[address[0]] = acc_btc_per_address[ address[0]] + output.value else: acc_btc_per_address[address[0]] = output.value analyze = {} analyze["0-1"] = 0 analyze["1-10"] = 0 analyze["10-100"] = 0 analyze["100-1000"] = 0 analyze["1000-10000"] = 0 analyze["10000-100000"] = 0 analyze[">=100000"] = 0 for address in acc_btc_per_address.keys(): btc_value = acc_btc_per_address[address] / 100000000 if btc_value < 1: analyze["0-1"] += 1 elif btc_value >= 1 and btc_value < 10: analyze["1-10"] += 1 elif btc_value >= 10 and btc_value < 100: analyze["10-100"] += 1 elif btc_value >= 100 and btc_value < 1000: analyze["100-1000"] += 1 elif btc_value >= 1000 and btc_value < 10000: analyze["1000-10000"] += 1 elif btc_value >= 10000 and btc_value < 100000: analyze["10000-100000"] += 1 elif btc_value >= 100000: analyze[">=100000"] += 1 print(analyze) for script in script_count.keys(): print("Address type:", script, "count:", script_count[script])
def main(): blockchain = Blockchain(os.path.expanduser('./blocks')) for block in blockchain.get_unordered_blocks(): for tx in block.transactions: for no, output in enumerate(tx.outputs): print(tx.hash, output.addresses, output.value) sys.exit(0)
def main(): uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "n")) blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Bitcoin data/Blockdata_testing/1')) blocks = blockchain.get_unordered_blocks() with driver.session() as session: #session.write_transaction(create_addresses, blocks) create_inputs(session, blocks) create_outputs(session, blocks) # with driver.session() as session: # create_outputs(session, blocks) # session.write_transaction(create_blocks,blocks) # with driver.session() as session2: # session2.write_transaction(create_transaction, blocks) # tx_hash_handle = open("txhash_00.txt","w+") # tx_index = open("txindex_00.txt", "w+") # #D:\Courses\Capstone\PyBC\pybit\Blocks # for block in blockchain.get_unordered_blocks(): # for tx in block.transactions: # for input in tx.inputs: # tx_hash_handle.write(str(input.transaction_hash)+"\n") # tx_index.write(str(input.transaction_index)+"\n") # # tx_hash_handle.flush() # tx_index.flush() # tx_index.close() # tx_hash_handle.close() driver.close()
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')) for block in blockchain.get_unordered_blocks(): for transaction in block.transactions: for output in transaction.outputs: if output.is_unknown(): print(block.header.timestamp, output.script.value)
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks') ) #D:\Courses\Capstone\PyBC\pybit\Blocks for block in blockchain.get_unordered_blocks(): for tx in block.transactions: for no, output in enumerate(tx.outputs): print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value))
def process(self): count = 0 blockchain = Blockchain(os.path.expanduser('~/.bitcoin/blocks')) for block in blockchain.get_unordered_blocks(): timestamp = block.header.timestamp if (timestamp.year == 2017): for tx in block.transactions: self.output_queue.put((tx, block.header.timestamp)) self.output_queue.put(None) #ending condition
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks') ) #D:\Courses\Capstone\PyBC\pybit\Blocks trans_per_address = {} same_transaction_counter = 0 for block in blockchain.get_unordered_blocks(): for tx in block.transactions: for output in tx.outputs: address = output.addresses if len(address) != 0: if address[0] in trans_per_address.keys(): same_transaction_counter += 1 trans_per_address[address[0].hash] += 1 else: trans_per_address[address[0].hash] = 1 analyze = {} analyze["1-2"] = 0 analyze["2-4"] = 0 analyze["4-10"] = 0 analyze["10-100"] = 0 analyze["100-1000"] = 0 analyze["1000-10000"] = 0 analyze["10000-100000"] = 0 analyze[">=100000"] = 0 for address in trans_per_address.keys(): trans_value = trans_per_address[address] if trans_value < 2: analyze["1-2"] += 1 elif trans_value >= 2 and trans_value < 4: analyze["2-4"] += 1 elif trans_value >= 4 and trans_value < 10: analyze["4-10"] += 1 elif trans_value >= 10 and trans_value < 100: analyze["10-100"] += 1 elif trans_value >= 100 and trans_value < 1000: analyze["100-1000"] += 1 elif trans_value >= 1000 and trans_value < 10000: analyze["1000-10000"] += 1 elif trans_value >= 10000 and trans_value < 100000: analyze["10000-100000"] += 1 elif trans_value >= 100000: analyze[">=100000"] += 1 print(analyze) print(same_transaction_counter)
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks') ) #D:\Courses\Capstone\PyBC\pybit\Blocks timelist = [] n_transactions = {} n_transactions_count = 0 n_col_transactions = [] # transactions with the same txid block_counter = 0 tx_per_block = {} for block in blockchain.get_unordered_blocks(): block_counter += 1 timelist.append(block.header.timestamp) tx_counter = 0 for tx in block.transactions: n_transactions_count += 1 tx_counter += 1 if tx.txid in n_transactions.keys(): n_transactions[tx.txid].append( block.header.timestamp) #adding timestamp to the n_col_transactions.append(tx.txid) else: n_transactions[tx.txid] = [] n_transactions[tx.txid].append(block.header.timestamp) # for no, output in enumerate(tx.outputs): # print("tx=%s outputno=%d type=%s value=%s" % (tx.hash, no, output.type, output.value)) tx_per_block[block.hash] = tx_counter print("the start date: ", min(timelist), "the end date", max(timelist), "of the datset") print("Total number of Transactions:", n_transactions_count) for txid in n_col_transactions: print("Same hash collision hash:", txid, "at time", n_transactions[txid], "boolean segwit: ", tx.is_segwit, "boolean isCoinbase:", tx.is_coinbase()) print("total number of blocks in the datset are:", block_counter) max_transaction_inblock = max(tx_per_block, key=tx_per_block.get) print("The maximum transactions in a block are: ", tx_per_block[max_transaction_inblock], "in block", max_transaction_inblock) min_transaction_inblock = min(tx_per_block, key=tx_per_block.get) print("The minimum transactions in a block are: ", tx_per_block[min_transaction_inblock], "in block", min_transaction_inblock)
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks') ) #D:\Courses\Capstone\PyBC\pybit\Blocks max_btc_per_address = {} for block in blockchain.get_unordered_blocks(): for tx in block.transactions: for output in tx.outputs: address = output.addresses if len(address) != 0: if address[0] in max_btc_per_address.keys(): if max_btc_per_address[address[0]] < output.value: max_btc_per_address[address[0]] = output.value else: max_btc_per_address[address[0]] = output.value analyze = {} analyze["0-0.1"] = 0 analyze["0.1-10"] = 0 analyze["10-100"] = 0 analyze["100-1000"] = 0 analyze["1000-10000"] = 0 analyze["10000-100000"] = 0 analyze[">=100000"] = 0 for address in max_btc_per_address.keys(): btc_value = max_btc_per_address[address] / 100000000 if btc_value < 0.1: analyze["0-0.1"] += 1 elif btc_value >= 1 and btc_value < 10: analyze["0.1-10"] += 1 elif btc_value >= 10 and btc_value < 100: analyze["10-100"] += 1 elif btc_value >= 100 and btc_value < 1000: analyze["100-1000"] += 1 elif btc_value >= 1000 and btc_value < 10000: analyze["1000-10000"] += 1 elif btc_value >= 10000 and btc_value < 100000: analyze["10000-100000"] += 1 elif btc_value >= 100000: analyze[">=100000"] += 1 print(analyze)
def main(): blockchain = Blockchain( os.path.expanduser( '/media/varun/DATA/Courses/Capstone/PyBC/pybit/Blocks')) for block in blockchain.get_unordered_blocks(): for transaction in block.transactions: coinbase = transaction.inputs[0] # Some coinbase scripts are not valid scripts try: script_operations = coinbase.script.operations except CScriptInvalidError: break # An operation is a CScriptOP or pushed bytes for operation in script_operations: if type(operation) == bytes and len(operation) > 3 \ and is_ascii_text(operation): print(block.header.timestamp, operation.decode("ascii")) break
def parse(filepath, filename): target_path = "/data/result/" j = filename.split(".") add_str = j[0] file_blk = open(target_path + add_str + ".blk.txt",'a') file_trans = open(target_path + add_str + ".trans.txt",'a') file_input = open(target_path + add_str + ".input.txt",'a') file_output = open(target_path + add_str + ".output.txt",'a') blockchain = Blockchain(os.path.expanduser(filepath + filename)) for block in blockchain.get_unordered_blocks(): file_blk.write(block.hash +"\t"+ str(block.size) +"\t"+ str(block.header.version) +"\t"+ block.header.previous_block_hash +"\t"+ block.header.merkle_root + "\t"+ str(block.header.timestamp) +"\t"+ str(block.header.bits) +"\t"+ str(block.header.nonce) +"\t"+ str(block.n_transactions) + "\t" + add_str + "\n") for tx_index,tx in enumerate(block.transactions): try: file_trans.write(block.hash + "\t" + str(tx_index) + "\t" + tx.hash + "\t" + str(tx.version) + "\t" + str(tx.locktime) + "\t" + str(tx.n_inputs) + "\t" + str(tx.n_outputs) + "\t" + str(tx.is_segwit) + "\t" + add_str + "\n") for input_index, input in enumerate(tx.inputs): transaction_hash = input.transaction_hash transaction_index = input.transaction_index if tx.is_coinbase(): transaction_hash = "coinbase" transaction_index = 0 file_input.write(tx.hash + "\t" + str(input.sequence_number) + "\t" + transaction_hash+ "\t" + str(transaction_index) + "\t" + add_str + "\n") for output_index, output in enumerate(tx.outputs): address_list = [] for address in output.addresses: address_list.append(address.address) file_output.write(tx.hash + "\t" + str(output_index) + "\t" + output.type+ "\t" + ",".join(address_list)+ "\t" + str(output.value) + "\t" + add_str + "\n") except Exception as e: print(e) file_blk.close() file_trans.close() file_input.close() file_output.close()
writetoinput(transaction_id, output_id, number, type, seq_no) conn.commit() cur.execute("SELECT value FROM transaction WHERE id = %s", (transaction_id,)) output_value = int(cur.fetchone()[0]) fee = output_value - inputvalue if output_number == None: fee = 0 cur.execute("UPDATE transaction SET fee = %s WHERE id = %s ", (fee, transaction_id)) conn.commit() blockchain = Blockchain(sys.argv[1]) counter = 0 for block in blockchain.get_unordered_blocks(): saveblockinfo(block) savetransactions(block) print('___') print (counter) counter +=1 try: cur.close() except: print('no cursor to close') try: conn.close() except: print('no connection to close')
import hashlib import binascii from blockchain_parser.blockchain import Blockchain from blockchain_parser.script import CScriptInvalidError def is_ascii_text(op): return all(32 <= x <= 127 for x in op) def hexify(value,zeros): try: return hex(value).replace('0x','').replace('L','').zfill(zeros) except: return value.encode('hex').zfill(zeros) blockchain = Blockchain(sys.argv[1]) h = 0 print("height,version, h0, h1, h2, h3, h4, h5, h6, h7, r0, r1, r2, r3, r4, r5, r6, r7, timestamp, bits, nonce") for block in blockchain.get_unordered_blocks(): header = block.header r = [header.version] r += struct.unpack("<IIIIIIII",binascii.unhexlify(header.previous_block_hash)[::-1]) r += struct.unpack("<IIIIIIII",binascii.unhexlify(header.merkle_root)[::-1]) r += struct.unpack("<I",bytes(header.hex[68:72])) r += [header.bits] r += [header.nonce] print(str(h)+","+str(r).replace("[","").replace("]","")) h += 1
def get_block(blockhash): payload = { 'method': 'getblock', 'params': [blockhash], } response = requests.post(URL, json=payload).json() return(response['result']) ONE_DAY_IN_BLOCKS = 6 * 24 ONE_MONTH_IN_BLOCKS = ONE_DAY_IN_BLOCKS * 30 ONE_YEAR_IN_BLOCKS = ONE_DAY_IN_BLOCKS * 365 print('txid;index;created_block;value;fanin;fanout;fanout_share;total_value;spent_block;life;label') for i, block in enumerate(blockchain.get_unordered_blocks()): if 1 == 100: break spent_block_hash = block.hash spent_block = get_block(spent_block_hash)['height'] for tx in block.transactions: if tx.is_coinbase(): continue raw_tx = get_raw_transaction(tx.hash) # Not sure why this happens. Needs more debugging. if raw_tx == None: continue for input in raw_tx['vin']: # Coinbase if 'txid' not in input: continue
utxos.append(document) ''' except: skipped_count += 1 pass itemlist = [ str(timestamp), str(utxo_count), str(tx_count), str(skipped_count), str(inserted_count) ] text = (',').join(itemlist) # print(text,type(text)) # print('Processing') # print (timestamp,itemlist) # return itemlist with open(str(timestamp), "w") as outfile: outfile.write(",".join(itemlist)) blockchain = Blockchain(os.path.expanduser('/home/shared/bitcoin/blocks')) count = 0 encountered = False for block in tqdm(blockchain.get_unordered_blocks()): # count += 1 # if count > 20: # break executor.submit(process_block, block) # process_block(block)