mmnode-blocks-info 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2017 Philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation, either version 3 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-blocks-info: Display information about a block or range of blocks
  20. """
  21. import time,re
  22. from mmgen.common import *
  23. opts_data = lambda: {
  24. 'desc': 'Display information about or find a transaction within a range of blocks',
  25. 'usage': '[opts] +<last n blocks>|<block num>|<block num range>',
  26. 'options': """
  27. -h, --help Print this help message
  28. --, --longhelp Print help message for long options (common options)
  29. -H, --hashes Display only block numbers and hashes
  30. -m, --miner-info Display miner info in coinbase transaction
  31. -M, --raw-miner-info Display miner info in uninterpreted form
  32. -s, --summary Print the summary only
  33. -t, --transaction=t Search for transaction 't' in specified block range
  34. If no block number is specified, the current block is assumed
  35. """
  36. }
  37. cmd_args = opts.init(opts_data)
  38. if len(cmd_args) not in (0,1): opts.usage()
  39. if opt.raw_miner_info: opt.miner_info = True
  40. c = rpc_connection()
  41. cur_blk = c.getblockcount()
  42. arg = cmd_args[0] if cmd_args else None
  43. if not arg:
  44. first = last = cur_blk
  45. elif arg[0] == '+' and is_int(arg[1:]):
  46. last = cur_blk
  47. first = last - int(arg[1:]) + 1
  48. elif is_int(arg):
  49. first = last = int(arg)
  50. else:
  51. try:
  52. a,b = arg.split('-')
  53. assert is_int(a) and is_int(b)
  54. first,last = int(a),int(b)
  55. except:
  56. opts.usage()
  57. if first > last:
  58. die(2,'{}: invalid block range'.format(arg))
  59. if last > cur_blk:
  60. die(2,'Requested block number ({}) greater than current block height'.format(last))
  61. if opt.summary:
  62. pass
  63. elif opt.transaction:
  64. if len(opt.transaction) != 64 or not is_hex_str(opt.transaction):
  65. die(2,'{}: invalid transaction id'.format(opt.transaction))
  66. elif opt.hashes:
  67. Msg('{:<7} {}'.format('BLOCK','HASH'))
  68. else:
  69. if opt.miner_info:
  70. fs='{:<6} {:<19}{:>9} {:>6} {:>7} {:8} {}'
  71. Msg(fs.format('BLOCK','DATE','INTERVAL','CONFS','SIZE','VERSION','MINER'))
  72. else:
  73. fs='{:<6} {:<19}{:>9} {:>6} {:>7} {}'
  74. Msg(fs.format('BLOCK','DATE','INTERVAL','CONFS','SIZE','VERSION'))
  75. miner_strings = {
  76. 'Bixin':'Bixin',
  77. 'AntPool':'AntPool',
  78. 'Bitfury':'Bitfury',
  79. 'BTCC':'BTCC',
  80. 'BTC.COM':'BTC.COM',
  81. 'BTPOOL':'BTPOOL',
  82. 'ViaBTC':'ViaBTC',
  83. 'slush':'Slush',
  84. 'BitMinter':'BitMinter',
  85. 'BW.COM':'BW.COM',
  86. 'gbminers':'GBMiners',
  87. 'BitClub Network':'BitClub Network',
  88. 'bitcoin.com':'bitcoin.com',
  89. 'KanoPool':'KanoPool',
  90. 'BTC.TOP':'BTC.TOP',
  91. }
  92. total,prev_time = 0,None
  93. for i in range(first,last+1):
  94. b = c.getblock(c.getblockhash(i))
  95. total += b['size']
  96. if opt.transaction:
  97. if opt.transaction in b['tx']:
  98. Msg('Requested transaction is in block {}'.format(i))
  99. sys.exit(0)
  100. msg('Checking block {}'.format(i))
  101. else:
  102. gmt = time.gmtime(b['mediantime'])
  103. date = '{}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*gmt[:6])
  104. if prev_time == None:
  105. b_prev = b if i == 0 else c.getblock(c.getblockhash(i-1))
  106. first_time = prev_time = b_prev['mediantime']
  107. et = b['mediantime'] - prev_time
  108. prev_time = b['mediantime']
  109. if opt.summary:
  110. continue
  111. if opt.hashes:
  112. Msg('{:<7} {}'.format(i,b['hash']))
  113. else:
  114. d = (i,date,'{}:{:02}'.format(et/60,et%60),b['confirmations'],b['size'],b['versionHex'])
  115. if opt.miner_info:
  116. bd = c.getrawtransaction(b['tx'][0],1,on_fail='silent')
  117. if type(bd) == tuple:
  118. Msg(fs.format(*(d+('---',))))
  119. else:
  120. cb = unhexlify(bd['vin'][0]['coinbase'])
  121. scb = re.sub('[^\w /-:,;.]','',cb)[1:]
  122. if opt.raw_miner_info:
  123. Msg(fs.format(*(d+(scb,))))
  124. else:
  125. for ms in miner_strings:
  126. if ms in scb:
  127. s = miner_strings[ms]
  128. break
  129. else:
  130. if re.search('mined by',scb,flags=re.I):
  131. s = scb.split('Mined by')[1].strip()
  132. else:
  133. s = '??'
  134. Msg(fs.format(*(d+((' ','NYA ')['NYA' in scb]+s,))))
  135. else:
  136. Msg(fs.format(*d))
  137. if opt.transaction:
  138. from mmgen.rpc import rpc_error
  139. if rpc_error(c.getmempoolentry(opt.transaction,on_fail='silent')):
  140. Msg('\rTransaction not found in block range {}-{} or in mempool'.format(first,last))
  141. else:
  142. Msg('\rTransaction is in mempool')
  143. else:
  144. blocks = last - first + 1
  145. if blocks > 1:
  146. fs2 = '{:<15} {}\n'
  147. et = b['mediantime'] - first_time
  148. ac = et / blocks
  149. if not opt.summary: Msg('')
  150. Msg_r(fs2.format('Range:','{}-{} ({} blocks)'.format(first,last,blocks)) +
  151. fs2.format('Avg size:',total/blocks) +
  152. fs2.format('MB/hr:','{:0.5f}'.format(float(total)*36/10000/et)) +
  153. fs2.format('Avg conf time:','{}:{:02}'.format(ac/60,ac%60)))