main_blocks_info.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.cfg import gc,Config
  22. from mmgen.util import async_run,fmt_list
  23. from .BlocksInfo import BlocksInfo,JSONBlocksInfo
  24. opts_data = {
  25. 'sets': [
  26. ('header_info', True, 'fields', None),
  27. ('header_info', True, 'miner_info', None),
  28. ('header_info', True, 'stats', 'range'),
  29. ('json_raw', True, 'json', True),
  30. ('raw_miner_info', True, 'miner_info', True),
  31. ('stats_only', True, 'no_header', True),
  32. ],
  33. 'text': {
  34. 'desc': 'Display information about a block or range of blocks',
  35. 'usage': '[opts] blocknum ... | blocknum-blocknum[+step] | [blocknum|-nBlocks]+nBlocks[+step]',
  36. 'usage2': [
  37. '[opts] blocknum ...',
  38. '[opts] blocknum-blocknum[+step]',
  39. '[opts] [blocknum|-nBlocks]+nBlocks[+step]',
  40. ],
  41. 'options': """
  42. -h, --help Print this help message
  43. --, --longhelp Print help message for long options (common options)
  44. -f, --full-stats Stats that relate to a specific field are shown only
  45. if that field is configured, whether by default or via
  46. the --fields option. This option adds the fields req-
  47. uired to produce a full display of configured stats.
  48. -H, --header-info Display information from block headers only
  49. -j, --json Produce JSON output
  50. -J, --json-raw Produce JSON output with unformatted values
  51. -m, --miner-info Display miner info in coinbase transaction
  52. -M, --raw-miner-info Display miner info in uninterpreted form
  53. -n, --no-header Don’t print the column header
  54. -o, --fields= Display the specified fields (comma-separated list).
  55. See AVAILABLE FIELDS below. Prefix the list with '+'
  56. to add the fields to the defaults, or '-' to remove
  57. them. The special values 'all' and 'none' select all
  58. available fields or none, respectively. The '+' and
  59. '-'-prefixed lists may be concatenated to specify both
  60. addition and removal of fields. A single '-'-prefixed
  61. list may be additionally prefixed by 'all'.
  62. -s, --stats= Display the specified stats (comma-separated list).
  63. See AVAILABLE STATS below. The prefixes and special
  64. values available to the --fields option are recognized.
  65. -S, --stats-only Display stats only. Suppress display of per-block data.
  66. """,
  67. 'notes': """
  68. If no block number is specified, the current chain tip is assumed.
  69. The special value 'cur' can be used to designate the chain tip wherever a
  70. block number is expected.
  71. If the requested range ends at the current chain tip, an estimate of the next
  72. difficulty adjustment is also displayed. The estimate is based on the average
  73. Block Discovery Interval from the beginning of the current {I}-block period.
  74. All fee fields except for 'totalfee' are in satoshis per virtual byte.
  75. AVAILABLE FIELDS: {F}
  76. AVAILABLE STATS: {S}
  77. EXAMPLES:
  78. Display info for current block:
  79. $ {p}
  80. Display info for the Genesis Block:
  81. $ {p} 0
  82. Display info for the last 20 blocks:
  83. $ {p} +20
  84. Display specified fields for blocks 165-190
  85. $ {p} -o block,date,size,inputs,nTx 165-190
  86. Display info for 10 blocks beginning at block 600000:
  87. $ {p} 600000+10
  88. Display info for every 5th block of 50-block range beginning at 1000
  89. blocks from chain tip:
  90. $ {p} -- -1000+50+5
  91. Display info for block 152817, adding miner field:
  92. $ {p} -o +miner 152817
  93. Display specified fields for listed blocks:
  94. $ {p} -o block,date,hash 245798 170 624044
  95. Display every difficulty adjustment from Genesis Block to chain tip:
  96. $ {p} -o +difficulty 0-cur+{I}
  97. Display roughly a block a day over the last two weeks. Note that
  98. multiplication is allowed in the nBlocks spec:
  99. $ {p} +144*14+144
  100. Display only range stats for the last ten blocks:
  101. $ {p} -o none -s range +10
  102. Display data for the last ten blocks, omitting the 'size' and 'subsidy'
  103. fields from the defaults and skipping stats:
  104. $ {p} -o -size,subsidy -s none +10
  105. Display data for the last ten blocks, omitting the 'size' and 'version'
  106. fields from the defaults and adding the 'inputs' and 'utxo_inc' fields:
  107. $ {p} -o -version,size+utxo_inc,inputs +10
  108. Display all fields and stats for the last ten blocks:
  109. $ {p} -o all -s all +10
  110. Same as above, but omit the 'miner' and 'hash' fields:
  111. $ {p} -o all-miner,hash -s all +10
  112. Same as above, but display only fields relating to stats:
  113. $ {p} -o none -s all -f +10
  114. Same as above, but display stats only:
  115. $ {p} -o none -s all -fS +10
  116. Display headers-only info for the last 1000 blocks. Speed up execution
  117. using the async RPC backend:
  118. $ {p} --rpc-backend=aio -H +1000
  119. This program requires a txindex-enabled daemon for correct operation.
  120. """ },
  121. 'code': {
  122. 'notes': lambda cfg,proto,s: s.format(
  123. I = proto.diff_adjust_interval,
  124. F = fmt_list(BlocksInfo.fields,fmt='bare'),
  125. S = fmt_list(BlocksInfo.all_stats,fmt='bare'),
  126. p = gc.prog_name,
  127. )
  128. }
  129. }
  130. cfg = Config(opts_data=opts_data)
  131. async def main():
  132. from mmgen.rpc import rpc_init
  133. cls = JSONBlocksInfo if cfg.json else BlocksInfo
  134. m = cls( cfg, cfg._args, await rpc_init(cfg,ignore_wallet=True) )
  135. if m.fnames and not cfg.no_header:
  136. m.print_header()
  137. await m.process_blocks()
  138. if m.last:
  139. for i,sname in enumerate(m.stats):
  140. m.process_stats_pre(i)
  141. await m.process_stats(sname)
  142. m.finalize_output()
  143. async_run(main())