#!/usr/bin/env python3
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

"""
mmnode-blocks-info: Display information about a block or range of blocks
"""

from mmgen.common import *
from mmgen.node_tools.BlocksInfo import BlocksInfo,JSONBlocksInfo

opts_data = {
	'sets': [
		('hashes',     True,   'fields',     'block,hash'),
		('hashes',     True,   'stats',      'none'),
		('stats',      'none', 'stats_only', False),
		('stats_only', True,   'no_header',  True),
		('json',       True,   'no_header',  True),
	],
	'text': {
		'desc':    'Display information about a block or range of blocks',
		'usage':   '[opts] blocknum [blocknum ...] | blocknum-blocknum[+step] | [blocknum|-nBlocks]+nBlocks[+step]',
		'usage2': [
			'[opts] blocknum [blocknum ...]',
			'[opts] blocknum-blocknum[+step]',
			'[opts] [blocknum|-nBlocks]+nBlocks[+step]',
		],
		'options': """
-h, --help            Print this help message
--, --longhelp        Print help message for long options (common options)
-H, --hashes          Display only block numbers and hashes
-j, --json            Produce JSON output
-m, --miner-info      Display miner info in coinbase transaction
-M, --raw-miner-info  Display miner info in uninterpreted form
-n, --no-header       Don’t print the column header
-o, --fields=         Display the specified fields (comma-separated list).
                      See AVAILABLE FIELDS below.  If the first character
                      is '+', specified fields are added to the defaults.
-s, --stats=          Display the specified stats (comma-separated list).
                      See AVAILABLE STATS below.  If the first character is
                      '+', specified stats are added to the defaults.  Use
                      'none' to disable, or 'all' for all available stats.
-S, --stats-only      Display stats only.  Skip display of per-block data.
""",
	'notes': """
If no block number is specified, the current block is assumed.  The string
'cur' can be used in place of the current block number.

If the requested range ends at the current chain tip, an estimate of the next
difficulty adjustment is also displayed. The estimate is based on the average
Block Discovery Interval from the beginning of the current 2016-block period.

All fee fields except for 'totalfee' are in satoshis per virtual byte.

AVAILABLE FIELDS: {f}

AVAILABLE STATS: {s}

EXAMPLES:

    # Display info for current block:
    {p}

    # Display info for the Genesis Block:
    {p} 0

    # Display info for the last 20 blocks:
    {p} +20

    # Display specified fields for blocks 165-190
    {p} -o block,date,size,inputs,nTx 165-190

    # Display info for 10 blocks beginning at block 600000:
    {p} 600000+10

    # Display info for every 5th block of 50-block range beginning at 1000
    # blocks from chain tip:
    {p} -- -1000+50+5

    # Display info for block 152817, adding miner field:
    {p} --miner-info 152817

    # Display specified fields for listed blocks:
    {p} -o block,date,hash 245798 170 624044

    # Display every difficulty adjustment from Genesis Block to chain tip:
    {p} -o +difficulty 0-cur+2016

    # Display roughly a block a day over the last two weeks.  Note that
    # multiplication is allowed in the nBlocks spec:
    {p} +144*14+144

    # Display only range stats for the last ten blocks:
    {p} -s range -S +10

This program requires a txindex-enabled daemon for correct operation.
""".format(
		f = fmt_list(BlocksInfo.fields,fmt='bare'),
		s = fmt_list(BlocksInfo.all_stats,fmt='bare'),
		p = g.prog_name )
	}
}

cmd_args = opts.init(opts_data)

async def main():

	from mmgen.protocol import init_proto_from_opts
	from mmgen.rpc import rpc_init

	proto = init_proto_from_opts()

	cls = JSONBlocksInfo if opt.json else BlocksInfo

	m = cls( cmd_args, opt, await rpc_init(proto) )

	if not opt.no_header:
		m.print_header()

	await m.process_blocks()

	if m.last and m.stats:
		for i,stat in enumerate(m.stats):
			if stat == 'diff': # Display diff stats by default only if user-requested range ends with chain tip
				if not opt.stats and m.last != m.tip:
					continue
			m.process_stats_pre(i)
			await m.process_stats(stat)

	m.finalize_output()

run_session(main())
