def get_latest_blocks(): p = RawProxy() blocks_to_take = 10 if request.args.get('count'): blocks_to_take = int(blocks_to_take) blocks_count = p.getblockcount() block_heights = list(range(blocks_count - blocks_to_take, blocks_count)) block_hashes = list(map(p.getblockhash, block_heights)) block_hashes.reverse() # So latest hashes would go first return jsonify(block_hashes)
#!/usr/bin/env python3 import codecs from bitcoin.rpc import RawProxy if __name__ == '__main__': conf = input('Please enter path to the configuration file: ') proxy = RawProxy(btc_conf_file=conf) numblocks = proxy.getblockcount() + 1 fp = codecs.open('out.csv', 'w', 'utf-8') for idx in range(numblocks): blockinfo = proxy.getblock(proxy.getblockhash(idx)) fp.write(','.join( map(str, [ blockinfo['height'], blockinfo['time'], blockinfo['difficulty'], ])) + '\n') fp.close() # End of File
from bitcoin.rpc import RawProxy p = RawProxy() lastblockheight = p.getblockcount() total_value = 0 # Skip the block of height 0 since the genesis block coinbase is not considered # an ordinary transaction and cannot be retrieved for blockheight in range(1, lastblockheight + 1): blockhash = p.getblockhash(blockheight) block = p.getblock(blockhash) transactions = block['tx'] block_value = 0 for txid in transactions: tx_value = 0 raw_tx = p.getrawtransaction(txid) decoded_tx = p.decoderawtransaction(raw_tx) for output in decoded_tx['vout']: tx_value += output['value'] block_value += tx_value total_value += block_value print(f"blockheight = {blockheight}, total_value = {total_value}") print("Total value of BTC moved in blockchain: ", total_value)
# 파이썬 실습 파일: 7-4.MiningTimeDistribution.py # RPC 패키지 : https://github.com/petertodd/python-bitcoinlib from bitcoin.rpc import RawProxy import pandas as pd import numpy as np import matplotlib.pyplot as plt # Bitcoin Core에 접속한다. p = RawProxy() # 블록체인의 블록 개수을 읽어온다 n = p.getblockcount() # 최근 1000개 블록의 헤더를 읽어서 생성 시간을 조회한다. header = [] for i in range(n - 999, n + 1): bHash = p.getblockhash(i) hdr = p.getblockheader(bHash) height = hdr['height'] btime = hdr['time'] bhash = hdr['hash'] header.append([height, btime, bhash]) df = pd.DataFrame(header, columns=['Height', 'Time', 'Hash']) sdf = df.sort_values('Time') sdf = sdf.reset_index() print(df.to_string()) print('총 %d 개 블록 헤더를 읽어왔습니다.' % len(df)) # 블록 생성 소요 시간 분포 관찰 mtime = sdf['Time'].diff().values