Guest User

Untitled

a guest
Oct 30th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  2. import json
  3. import requests
  4. import pprint
  5. import time
  6. import os
  7.  
  8.  
  9. objectivestart_time=time.time()
  10. pp= pprint.PrettyPrinter(indent=4)
  11.  
  12.  
  13.  
  14. rpc_user='n'
  15. rpc_password='n'
  16.  
  17. url=("http://%s:%s@1"%(rpc_user, rpc_password))
  18.  
  19.  
  20. def req(payload):
  21. return requests.post(url,data=json.dumps(payload)).json()
  22.  
  23.  
  24. def BlockHash_JsonRPCpayload(number):
  25. dic={
  26.  
  27. "jsonrpc":"2.0",
  28. "method": "getblockhash",
  29. "params": [number] ,
  30. "id": 1
  31.  
  32. }
  33. return (dic)
  34.  
  35.  
  36. def Block_JsonRPCpayload(h):
  37. dic={
  38.  
  39. "jsonrpc":"2.0",
  40. "method": "getblock",
  41. "params": [str(h)] ,
  42. "id": 1
  43.  
  44. }
  45. return (dic)
  46.  
  47.  
  48. def rawtx_JsonRPCpayload(tx):
  49. dic={
  50.  
  51. "jsonrpc":"2.0",
  52. "method": "getrawtransaction",
  53. "params": [tx, True] ,
  54. "id": 1
  55.  
  56. }
  57. return (dic)
  58.  
  59. def rawvin_JsonRPCpayload(tx,vin):
  60. block_payload={
  61.  
  62. "jsonrpc":"2.0",
  63. "method": "getrawtransaction",
  64. "params": [tx,1] ,
  65. "id": 1
  66.  
  67. }
  68.  
  69. request=req(block_payload)
  70. value=request['result']['vout'][int(vin)]['value']
  71.  
  72.  
  73. return float(value)
  74.  
  75. BTC_dic={}
  76.  
  77.  
  78. hash_payload=[]
  79. start_time=time.time()
  80.  
  81. block_start=481101
  82. block_end=490000
  83. start=block_start
  84. end= block_start
  85.  
  86. while end < block_end:
  87. block_tries=0
  88. while True:
  89. try:
  90.  
  91. end=start+100
  92. hash_payload=[]
  93. print ( 'start', 'end', start, end )
  94. for i in range(start,end):
  95. hash_payload.append(BlockHash_JsonRPCpayload(i))
  96.  
  97.  
  98. start_time=time.time()
  99. print ( 'mark 1 ')
  100. hash_response=requests.post(url, data=json.dumps(hash_payload)).json()
  101. print('time to get hash batch',start, "_", end, time.time()-start_time)
  102. print ( 'success in getting hashes for ranges', start, "_ ", end )
  103.  
  104. time.sleep(0.4)
  105.  
  106. block_payload=[]
  107. for i in hash_response:
  108. h=i['result']
  109. block_payload.append(Block_JsonRPCpayload(h))
  110. start_time=time.time()
  111. block_response=requests.post(url,data=json.dumps(block_payload)).json()
  112. print ( 'time to get block batch ',start, "_", end, time.time()-start_time)
  113. print ( 'success in getting blocks for ranges', start, "_ ", end )
  114.  
  115. time.sleep(0.4)
  116.  
  117.  
  118.  
  119. for i in range(len(block_response)):
  120.  
  121.  
  122. block=block_response[i]['result']
  123.  
  124. height=block['height']
  125. print('getting', len(block['tx']),'transactions for block ', height)
  126. BTC_dic[height]=block
  127. tx_payload=[]
  128. for tx in block['tx']:
  129. tx_payload.append(rawtx_JsonRPCpayload(tx))
  130.  
  131. try_errors=0
  132. while True:
  133. try:
  134. #print ( len(tx_payload), 'number of transactions in block ', height)
  135. start_time=time.time()
  136. transaction=requests.post(url,data=json.dumps(tx_payload)).json()
  137. time.sleep(0.5)
  138. #print ('time to get tx', time.time()-start_time)
  139. print ( 'success in getting transactions for block ', height )
  140. except:
  141. try_errors+=1
  142. print ( '!! Try error !!')
  143. print ( try_errors, ' transaction tries for block height', height)
  144. time.sleep(3)
  145. continue
  146.  
  147. break
  148.  
  149.  
  150. for i in range(len(transaction)):
  151.  
  152.  
  153. tx_result=transaction[i]['result']
  154. vin=tx_result['vin']
  155. vout=tx_result['vout']
  156.  
  157.  
  158.  
  159. try:
  160. fees=0
  161. for j in range(len(vout)):
  162. fees -= tx_result['vout'][j]['value']
  163.  
  164. for j in range(len(vin)):
  165.  
  166. vout=vin[j]['vout']
  167. txid=vin[j]['txid']
  168.  
  169. vin_error=0
  170. while True:
  171. try:
  172. value=rawvin_JsonRPCpayload(txid,vout)
  173. except:
  174. print ( '!! vin error !!')
  175. print ( 'attempt', vin_error )
  176. vin_error+=1
  177.  
  178. time.sleep(0.4)
  179. continue
  180. break
  181.  
  182. fees+=value
  183. tx_result['vin'][j]['value']=value
  184.  
  185.  
  186.  
  187. tx_result['fees']=fees
  188. except:
  189. tx_result['vin'][0]['value']='No Inputs (Newly Generated Coins)'
  190. tx_result['fees']=0
  191.  
  192.  
  193. BTC_dic[height]['tx'][i]=tx_result
  194.  
  195.  
  196.  
  197. if len(BTC_dic) >= 100:
  198. print ( 'len BTC_dic =', len(BTC_dic))
  199.  
  200.  
  201. for block in BTC_dic:
  202. block_fee=0
  203. transactions= BTC_dic[block]['tx']
  204.  
  205.  
  206. #pp.pprint(transactions)
  207. for j in transactions:
  208. fee=j['fees']
  209. block_fee+=fee
  210. BTC_dic[block]['block fees']=block_fee
  211.  
  212.  
  213.  
  214. block_array=[]
  215. for block in BTC_dic:
  216. block_array.append(block)
  217.  
  218.  
  219. file_name='jcampbell_btc_txdata'+str(min(block_array))+'_'+str(max(block_array))+'.json'
  220. print ('saving', file_name, 'time', time.time()-objectivestart_time)
  221.  
  222. with open(file_name, 'w') as outfile:
  223. json.dump(BTC_dic,outfile)
  224. BTC_dic={}
  225.  
  226.  
  227.  
  228.  
  229. start=end
  230.  
  231.  
  232.  
  233. except:
  234. block_tries+=1
  235. print(block_tries, 'tries to get data for block range ', start,"_",end)
  236. time.sleep(5)
  237. continue
  238. break
  239.  
  240.  
  241.  
  242.  
  243. for block in BTC_dic:
  244. block_fee=0
  245. transactions= BTC_dic[block]['tx']
  246.  
  247.  
  248. #pp.pprint(transactions)
  249. for j in transactions:
  250. fee=j['fees']
  251. block_fee+=fee
  252. BTC_dic[block]['block fees']=block_fee
  253.  
  254.  
  255.  
  256.  
  257.  
  258. block_array=[]
  259. for block in BTC_dic:
  260. block_array.append(block)
  261.  
  262.  
  263. file_name='jcampbell_btc_txdata'+str(min(block_array))+'_'+str(max(block_array))+'.json'
  264. print ('saving', file_name, 'time', time.time()-objectivestart_time)
  265. with open(file_name, 'w') as outfile:
  266. json.dump(BTC_dic,outfile)
Add Comment
Please, Sign In to add comment