# 파이썬 실습 파일: 7-9.TxFee.py # RPC 패키지 : https://github.com/petertodd/python-bitcoinlib from bitcoin.rpc import RawProxy import matplotlib.pyplot as plt # Bitcoin Core에 접속한다. p = RawProxy() def getUtxo(x, n): tt = p.getrawtransaction(x, True) return tt['vout'][n]['value'] # 블록체인 tip (끝 부분)의 hash, height를 읽어온다 tip = p.getchaintips() # 마지막 블록을 읽어온다 height = tip[0]['height'] bHash = tip[0]['hash'] block = p.getblock(bHash) # 마지막 블록의 Tx를 읽는다. nTx = len(block['tx']) # Tx를 차례대로 읽어가면서 Fee를 계산한다. (Coinbase Tx는 제외) txFee = [] byteFee = [] accByteFee = [] cnt = 0 for i in range(1, nTx): txid = block['tx'][i] tx = p.getrawtransaction(txid, True)
class Process: """ TODO startsignal """ #def __init__(self): # DONE run bitcoind as subprocess # TODO do something with bitcoind's output, e.g. input/output tape emulation # TODO run "strategy" # self.input_i*d_tape = file.read("/id") def __del__(self): # TODO stop bitcoind return True def run(self, id, nodes, start_at_timestamp): # all times are in seconds s = Scheduler2(int(start_at_timestamp)) blocktime = 0.2 # seconds self.set_network_delay() s.wait() self.startd() self.rpc = Proxy(service_url="http://*****:*****@localhost:18332") self.rpc_a = RawProxy(service_url="http://*****:*****@localhost:18332") generateBlock = lambda: self.rpc.generate(1) getChainTips = lambda: self.rpc_a.getchaintips() s.save_sleep(15) logger.error(f"[{id}] setup phase complete ") # generate first round blocks s.save_sleep(id * blocktime) generateBlock() logger.error(f"[{id}] generated initial blocks") s.save_sleep((nodes - id) * blocktime + 0.5) # generate doublespends, synchronous block creation for _ in range(10): if id % 2 == 1: s.save_sleep(blocktime + 0.2) generateBlock() s.save_sleep(0.2) logger.error(f"[{id}] Tick") else: s.save_sleep(0.2) generateBlock() logger.error(f"[{id}] Tock") s.save_sleep(blocktime + 0.2) # generate last round of blocks logger.error(f"[{id}] Last round") s.save_sleep(id * blocktime) generateBlock() s.save_sleep((nodes - id) * blocktime + 0.2) logger.error(f"[{id}] Done") # the following part uses cog as code generation tool # python -mpip install cogapp # python -mcogapp -r p.py # [[[cog # import cog # cog.out("print('hello')") # ]]] print('hello') tips = getChainTips() # [[[end]]] self.stopd() print(tips) with open("output.tape","w") as out: out.write(str(tips)) exit() daemon = '/usr/local/bin/bitcoind' args = { 'regtest': '-regtest', 'datadir': '-datadir=/tmp', # disable security checks for better performance 'whitelist': '-whitelist=240.0.0.0/4', # log all events relevant for parsing 'debug': '-debug=cmpctblock -debug=net -debug=mempool', 'logips': '-logips', 'logtimemicros': '-logtimemicros', # activate listen even though explicit -connect will be set 'connect': '-connect=240.0.0.2', #bootstrap 'listen': '-listen=1', 'listenonion': '-listenonion=0', 'onlynet': '-onlynet=ipv4', 'dnsseed': '-dnsseed=0', 'reindex': '-reindex', 'checkmempool': '-checkmempool=0', 'keypool': '-keypool=1', # RPC configuration 'rpcuser': '******', 'rpcpassword': '******', 'rpcallowip': '-rpcallowip=1.1.1.1/0.0.0.0', 'rpcservertimeout': '-rpcservertimeout=3600', 'rpcport': '-rpcport=18332' } def startd(self): with open("output_daemon.log","wb") as out: self.daemon = subprocess.Popen([self.daemon] + list(self.args.values()),stdout=out) def stopd(self): self.daemon.terminate() def set_network_delay(self): cmds = [ 'tc qdisc add ' # Traffic_Control Queue_DISCipline add ' dev eth0 ' # DEVice eth0 ' root ' # ROOT_egress_outbound_location ' netem delay 10700ms ' ] subprocess.Popen(cmds[0].split(' ')) def rm_peers(self, node): return "" # TODO dockercmd.exec_cmd(node, 'rm -f {}/regtest/peers.dat'.format(config.bitcoin_data_dir)) def show_command(self): print(self.daemon + ' '.join(self.args.values())) def clean(self): print("nop")