mmnode-blocks-info 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  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. mmnode-blocks-info: Display information about a block or range of blocks
  20. """
  21. from mmgen.common import *
  22. from mmgen.node_tools.BlocksInfo import BlocksInfo,JSONBlocksInfo
  23. opts_data = {
  24. 'sets': [
  25. ('hashes', True, 'fields', 'block,hash'),
  26. ('hashes', True, 'stats', 'none'),
  27. ('stats', 'none', 'stats_only', False),
  28. ('stats_only', True, 'no_header', True),
  29. ('json_raw', True, 'json', True),
  30. ('raw_miner_info',True,'miner_info', True),
  31. ],
  32. 'text': {
  33. 'desc': 'Display information about a block or range of blocks',
  34. 'usage': '[opts] blocknum [blocknum ...] | blocknum-blocknum[+step] | [blocknum|-nBlocks]+nBlocks[+step]',
  35. 'usage2': [
  36. '[opts] blocknum [blocknum ...]',
  37. '[opts] blocknum-blocknum[+step]',
  38. '[opts] [blocknum|-nBlocks]+nBlocks[+step]',
  39. ],
  40. 'options': """
  41. -h, --help Print this help message
  42. --, --longhelp Print help message for long options (common options)
  43. -H, --hashes Display only block numbers and hashes
  44. -j, --json Produce JSON output
  45. -J, --json-raw Produce JSON output with unformatted values
  46. -m, --miner-info Display miner info in coinbase transaction
  47. -M, --raw-miner-info Display miner info in uninterpreted form
  48. -n, --no-header Don’t print the column header
  49. -o, --fields= Display the specified fields (comma-separated list).
  50. See AVAILABLE FIELDS below. If the first character
  51. is '+', specified fields are added to the defaults.
  52. -s, --stats= Display the specified stats (comma-separated list).
  53. See AVAILABLE STATS below. If the first character is
  54. '+', specified stats are added to the defaults. Use
  55. 'none' to disable, or 'all' for all available stats.
  56. -S, --stats-only Display stats only. Skip display of per-block data.
  57. """,
  58. 'notes': """
  59. If no block number is specified, the current block is assumed. The string
  60. 'cur' can be used in place of the current block number.
  61. If the requested range ends at the current chain tip, an estimate of the next
  62. difficulty adjustment is also displayed. The estimate is based on the average
  63. Block Discovery Interval from the beginning of the current 2016-block period.
  64. All fee fields except for 'totalfee' are in satoshis per virtual byte.
  65. AVAILABLE FIELDS: {f}
  66. AVAILABLE STATS: {s}
  67. EXAMPLES:
  68. # Display info for current block:
  69. {p}
  70. # Display info for the Genesis Block:
  71. {p} 0
  72. # Display info for the last 20 blocks:
  73. {p} +20
  74. # Display specified fields for blocks 165-190
  75. {p} -o block,date,size,inputs,nTx 165-190
  76. # Display info for 10 blocks beginning at block 600000:
  77. {p} 600000+10
  78. # Display info for every 5th block of 50-block range beginning at 1000
  79. # blocks from chain tip:
  80. {p} -- -1000+50+5
  81. # Display info for block 152817, adding miner field:
  82. {p} -o +miner 152817
  83. # Display specified fields for listed blocks:
  84. {p} -o block,date,hash 245798 170 624044
  85. # Display every difficulty adjustment from Genesis Block to chain tip:
  86. {p} -o +difficulty 0-cur+2016
  87. # Display roughly a block a day over the last two weeks. Note that
  88. # multiplication is allowed in the nBlocks spec:
  89. {p} +144*14+144
  90. # Display only range stats for the last ten blocks:
  91. {p} -s range -S +10
  92. This program requires a txindex-enabled daemon for correct operation.
  93. """.format(
  94. f = fmt_list(BlocksInfo.fields,fmt='bare'),
  95. s = fmt_list(BlocksInfo.all_stats,fmt='bare'),
  96. p = g.prog_name )
  97. }
  98. }
  99. cmd_args = opts.init(opts_data)
  100. async def main():
  101. from mmgen.protocol import init_proto_from_opts
  102. from mmgen.rpc import rpc_init
  103. proto = init_proto_from_opts()
  104. cls = JSONBlocksInfo if opt.json else BlocksInfo
  105. m = cls( cmd_args, opt, await rpc_init(proto) )
  106. if not opt.no_header:
  107. m.print_header()
  108. await m.process_blocks()
  109. if m.last and m.stats:
  110. for i,stat in enumerate(m.stats):
  111. if stat == 'diff': # Display diff stats by default only if user-requested range ends with chain tip
  112. if not opt.stats and m.last != m.tip:
  113. continue
  114. m.process_stats_pre(i)
  115. await m.process_stats(stat)
  116. m.finalize_output()
  117. run_session(main())