mmnode-blocks-info: move BlocksInfo class to separate module, add test

This commit is contained in:
The MMGen Project 2021-03-18 12:07:29 +00:00
commit 6b6f426be3
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 611 additions and 423 deletions

3
MANIFEST.in Normal file
View file

@ -0,0 +1,3 @@
include README.md LICENSE
include test/test-release.sh
include test/unit_tests_d/*.py

View file

@ -0,0 +1,446 @@
#!/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/>.
"""
mmgen.node_tools.BlocksInfo: Display information about a block or range of blocks
"""
import re
from collections import namedtuple
from time import strftime,gmtime
from decimal import Decimal
from mmgen.common import *
class BlocksInfo:
total_bytes = 0
total_weight = 0
total_solve_time = 0
bf = namedtuple('block_info_fields',['hdr1','hdr2','fs','bs_key','varname','deps','key'])
# bs=getblockstats(), bh=getblockheader()
# If 'bs_key' is set, it's included in self.bs_keys instead of 'key'
fields = {
'block': bf('', 'Block', '{:<6}', None, 'height',[], None),
'hash': bf('', 'Hash', '{:<64}', None, 'H', [], None),
'date': bf('', 'Date', '{:<19}', None, 'df', [], None),
'interval': bf('Solve','Time ', '{:>8}', None, 'td', [], None),
'size': bf('', 'Size', '{:>7}', None, 'bs', [], 'total_size'),
'weight': bf('', 'Weight', '{:>7}', None, 'bs', [], 'total_weight'),
'utxo_inc': bf(' UTXO',' Incr', '{:>5}', None, 'bs', [], 'utxo_increase'),
'fee10': bf('10%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 0),
'fee25': bf('25%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 1),
'fee50': bf('50%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 2),
'fee75': bf('75%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 3),
'fee90': bf('90%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 4),
'fee_avg': bf('Avg', 'Fee', '{:>3}', None, 'bs', [], 'avgfeerate'),
'fee_min': bf('Min', 'Fee', '{:>3}', None, 'bs', [], 'minfeerate'),
'fee_max': bf('Max', 'Fee', '{:>5}', None, 'bs', [], 'maxfeerate'),
'totalfee': bf('', 'Total Fee','{:>10}', 'totalfee', 'tf', ['bs'], None),
'outputs': bf('Out-', 'puts', '{:>5}', None, 'bs', [], 'outs'),
'inputs': bf('In- ', 'puts', '{:>5}', None, 'bs', [], 'ins'),
'version': bf('', 'Version', '{:8}', None, 'bh', [], 'versionHex'),
'nTx': bf('', ' nTx ', '{:>5}', None, 'bh', [], 'nTx'),
'subsidy': bf('Sub-', 'sidy', '{:5}', 'subsidy', 'su', ['bs'], None),
'difficulty':bf('Diffi-','culty', '{:8}', None, 'di', [], None),
}
dfl_fields = [
'block',
'date',
'interval',
'subsidy',
'totalfee',
'size',
'weight',
'fee50',
'fee25',
'fee10',
'fee_avg',
'fee_min',
'version',
]
fixed_fields = [
'block', # until ≈ 09/01/2028 (block 1000000)
'hash',
'date',
'size', # until ≈ 6x block size increase
'weight', # until ≈ 2.5x block size increase
'version',
'subsidy', # until ≈ 01/04/2028 (increases by 1 digit per halving until 9th halving [max 10 digits])
'difficulty', # until 1.00e+100 (i.e. never)
]
# column width adjustment data:
fs_lsqueeze = ['totalfee','inputs','outputs','nTx']
fs_rsqueeze = []
fs_groups = [
('fee10','fee25','fee50','fee75','fee90','fee_avg','fee_min','fee_max'),
]
fs_lsqueeze2 = ['interval']
funcs = {
'df': lambda self,loc: strftime('%Y-%m-%d %X',gmtime(self.t_cur)),
'td': lambda self,loc: (
'-{:02}:{:02}'.format(abs(self.t_diff)//60,abs(self.t_diff)%60) if self.t_diff < 0 else
' {:02}:{:02}'.format(self.t_diff//60,self.t_diff%60) ),
'tf': lambda self,loc: '{:.8f}'.format(loc.bs["totalfee"] * Decimal('0.00000001')),
'fp': lambda self,loc: loc.bs['feerate_percentiles'],
'su': lambda self,loc: str(loc.bs['subsidy'] * Decimal('0.00000001')).rstrip('0').rstrip('.'),
'di': lambda self,loc: '{:.2e}'.format(loc.bh['difficulty']),
}
range_data = namedtuple('parsed_range_data',['first','last','from_tip','nblocks','step'])
t_fmt = lambda self,t: f'{t/86400:.2f} days' if t > 172800 else secs_to_hms(int(t))
def __init__(self,cmd_args,opt,rpc):
def get_fields():
if opt.fields:
ufields = opt.fields.lstrip('+').split(',')
for field in ufields:
if field not in self.fields:
die(1,f'{field!r}: unrecognized field')
return self.dfl_fields + ufields if opt.fields[0] == '+' else ufields
else:
return self.dfl_fields
def gen_fs(fnames):
for i in range(len(fnames)):
name = fnames[i]
ls = (' ','')[name in self.fs_lsqueeze + self.fs_lsqueeze2]
rs = (' ','')[name in self.fs_rsqueeze]
if i < len(fnames) - 1 and fnames[i+1] in self.fs_lsqueeze2:
rs = ''
if i:
for group in self.fs_groups:
if name in group and fnames[i-1] in group:
ls = ''
break
yield ls + self.fields[name].fs + rs
def parse_cmd_args():
if not cmd_args:
return (None,self.tip,self.tip)
elif len(cmd_args) == 1:
r = self.parse_rangespec(cmd_args[0])
return (
list(range(r.first,r.last+1,r.step)) if r.step else None,
r.first,
r.last
)
else:
return ([self.conv_blkspec(a) for a in cmd_args],None,None)
self.rpc = rpc
self.opt = opt
self.tip = rpc.blockcount
self.block_list,self.first,self.last = parse_cmd_args()
fnames = get_fields()
self.fvals = list(self.fields[name] for name in fnames)
self.fs = ''.join(gen_fs(fnames)).strip()
self.deps = set(' '.join(v.varname + ' ' + ' '.join(v.deps) for v in self.fvals).split())
self.bs_keys = [(v.bs_key or v.key) for v in self.fvals if v.bs_key or v.varname == 'bs']
self.bs_keys.extend(['total_size','total_weight'])
self.ufuncs = {v.varname:self.funcs[v.varname] for v in self.fvals if v.varname in self.funcs}
if opt.miner_info:
self.fs += ' {}'
self.miner_pats = [re.compile(pat) for pat in (
rb'`/([_a-zA-Z0-9&. #/-]+)/',
rb'[\xe3\xe4\xe5][\^/](.*?)\xfa',
rb'([a-zA-Z0-9&. -]+/Mined by [a-zA-Z0-9. ]+)',
rb'\x08/(.*Mined by [a-zA-Z0-9. ]+)',
rb'Mined by ([a-zA-Z0-9. ]+)',
rb'[`]([_a-zA-Z0-9&. #/-]+)[/\xfa]',
rb'[/^]([a-zA-Z0-9&. #/-]{5,})',
rb'[/^]([_a-zA-Z0-9&. #/-]+)/',
)]
else:
self.miner_pats = None
def conv_blkspec(self,arg):
if arg == 'cur':
return self.tip
elif is_int(arg):
if int(arg) < 0:
die(1,f'{arg}: block number must be non-negative')
elif int(arg) > self.tip:
die(1,f'{arg}: requested block height greater than current chain tip!')
else:
return int(arg)
else:
die(1,f'{arg}: invalid block specifier')
def check_nblocks(self,arg):
if arg <= 0:
die(1,'nBlocks must be a positive integer')
elif arg > self.tip:
die(1, f"'{arg}': nBlocks must be less than current chain height")
return arg
def parse_rangespec(self,arg):
class RangeParser:
debug = False
def __init__(rp,arg):
rp.arg = rp.orig_arg = arg
def parse(rp,target):
ret = getattr(rp,'parse_'+target)()
if rp.debug: print(f'arg after parse({target}): {rp.arg}')
return ret
def finalize(rp):
if rp.arg:
die(1,f'{rp.orig_arg!r}: invalid range specifier')
def parse_from_tip(rp):
m = re.match(r'-([0-9]+)(.*)',rp.arg)
if m:
res,rp.arg = (m[1],m[2])
return self.check_nblocks(int(res))
def parse_abs_range(rp):
m = re.match(r'([^+-]+)(-([^+-]+)){0,1}(.*)',rp.arg)
if m:
if rp.debug: print(f'abs_range parse: first={m[1]}, last={m[3]}')
rp.arg = m[4]
return (
self.conv_blkspec(m[1]),
self.conv_blkspec(m[3]) if m[3] else None
)
return (None,None)
def parse_add(rp):
m = re.match(r'\+([0-9*]+)(.*)',rp.arg)
if m:
res,rp.arg = (m[1],m[2])
if res.strip('*') != res:
die(1,f"'+{res}': malformed nBlocks specifier")
if len(res) > 30:
die(1,f"'+{res}': overly long nBlocks specifier")
return self.check_nblocks(eval(res)) # res is only digits plus '*', so eval safe
p = RangeParser(arg)
from_tip = p.parse('from_tip')
first,last = (self.tip-from_tip,None) if from_tip else p.parse('abs_range')
add1 = p.parse('add')
add2 = p.parse('add')
p.finalize()
if add2 and last is not None:
die(1,f'{arg!r}: invalid range specifier')
nblocks,step = (add1,add2) if last is None else (None,add1)
if p.debug: print(self.range_data(first,last,from_tip,nblocks,step))
if nblocks:
if first == None:
first = self.tip - nblocks + 1
last = first + nblocks - 1
first = self.conv_blkspec(first)
last = self.conv_blkspec(last or first)
if p.debug: print(self.range_data(first,last,from_tip,nblocks,step))
if first > last:
die(1,f'{first}-{last}: invalid block range')
return self.range_data(first,last,from_tip,nblocks,step)
async def run(self):
c = self.rpc
heights = self.block_list or range(self.first,self.last+1)
hashes = await c.gathered_call('getblockhash',[(height,) for height in heights])
self.hdrs = await c.gathered_call('getblockheader',[(H,) for H in hashes])
async def init(count):
h0 = (
self.hdrs[count] if heights[count] == 0 else
await c.call('getblockheader',await c.call('getblockhash',heights[count]-1))
)
self.t_cur = h0['time']
if count == 0:
self.first_prev_hdr = h0
if not self.block_list:
await init(0)
for n in range(len(heights)):
if self.block_list:
await init(n)
await self.process_block(heights[n],hashes[n],self.hdrs[n])
async def process_block(self,height,H,hdr):
class local_vars: pass
loc = local_vars()
loc.height = height
loc.H = H
loc.bh = hdr
self.t_diff = hdr['time'] - self.t_cur
self.t_cur = hdr['time']
self.total_solve_time += self.t_diff
if 'bs' in self.deps:
loc.bs = self.genesis_stats if height == 0 else await self.rpc.call('getblockstats',H,self.bs_keys)
self.total_bytes += loc.bs['total_size']
self.total_weight += loc.bs['total_weight']
if self.opt.summary:
return
for varname,func in self.ufuncs.items():
setattr(loc,varname,func(self,loc))
if self.opt.miner_info:
miner_info = '-' if height == 0 else await self.get_miner_string(H)
def gen():
for v in self.fvals:
if v.key is None:
yield getattr(loc,v.varname)
else:
yield getattr(loc,v.varname)[v.key]
if self.opt.miner_info:
yield miner_info
Msg(self.fs.format(*gen()))
async def get_miner_string(self,H):
tx0 = (await self.rpc.call('getblock',H))['tx'][0]
bd = await self.rpc.call('getrawtransaction',tx0,1)
if type(bd) == tuple:
return '---'
else:
cb = bytes.fromhex(bd['vin'][0]['coinbase'])
if self.opt.raw_miner_info:
return repr(cb)
else:
for pat in self.miner_pats:
m = pat.search(cb)
if m:
return ''.join(chr(b) for b in m[1] if 31 < b < 127).strip('^').strip('/').replace('/',' ')
def print_header(self):
hdr1 = [v.hdr1 for v in self.fvals]
hdr2 = [v.hdr2 for v in self.fvals]
if self.opt.miner_info:
hdr1.append(' ')
hdr2.append('Miner')
if ''.join(hdr1).replace(' ',''):
Msg(self.fs.format(*hdr1))
Msg(self.fs.format(*hdr2))
async def print_range_stats(self):
# These figures don’t include the Genesis Block:
elapsed = self.hdrs[-1]['time'] - self.first_prev_hdr['time']
nblocks = self.hdrs[-1]['height'] - self.first_prev_hdr['height']
Msg('Range: {}-{} ({} blocks [{}])'.format(
self.hdrs[0]['height'],
self.hdrs[-1]['height'],
self.hdrs[-1]['height'] - self.hdrs[0]['height'] + 1, # includes Genesis Block
self.t_fmt(elapsed) ))
if elapsed:
avg_bdi = int(elapsed / nblocks)
if 'bs' in self.deps:
total_blocks = len(self.hdrs)
rate = (self.total_bytes / 10000) / (self.total_solve_time / 36)
Msg_r(fmt(f"""
Avg size: {self.total_bytes//total_blocks} bytes
Avg weight: {self.total_weight//total_blocks} bytes
MB/hr: {rate:0.4f}
"""))
Msg(f'Avg BDI: {avg_bdi/60:.2f} min')
async def print_diff_stats(self):
c = self.rpc
# Only display stats if user-requested range ends with chain tip
if self.last != self.tip:
return
cur_diff_disp = 'Cur difficulty: {:.2e}'.format(self.hdrs[-1]['difficulty'])
rel = self.tip % 2016
if rel:
rel_hdr = await c.call('getblockheader',await c.call('getblockhash',self.tip-rel))
tip_time = (
self.hdrs[-1]['time'] if self.hdrs[-1]['height'] == self.tip else
(await c.call('getblockheader',await c.call('getblockhash',self.tip)))['time']
)
tdiff = tip_time - rel_hdr['time']
if tdiff: # if the 2 timestamps are equal (very unlikely), skip display to avoid div-by-zero error
bdi = tdiff / rel
adj_pct = ((600 / bdi) - 1) * 100
Msg_r(fmt(f"""
Current height: {self.tip}
Next diff adjust: {self.tip-rel+2016} (in {2016-rel} blocks [{self.t_fmt((2016-rel)*bdi)}])
BDI (cur period): {bdi/60:.2f} min
{cur_diff_disp}
Est. diff adjust: {adj_pct:+.2f}%
"""))
else:
Msg_r(fmt(f"""
Current height: {self.tip}
{cur_diff_disp}
Next diff adjust: {self.tip-rel+2016} (in {2016-rel} blocks)
"""))
# 'getblockstats' RPC raises exception on Genesis Block, so provide our own stats:
genesis_stats = {
'avgfee': 0,
'avgfeerate': 0,
'avgtxsize': 0,
'feerate_percentiles': [ 0, 0, 0, 0, 0 ],
'height': 0,
'ins': 0,
'maxfee': 0,
'maxfeerate': 0,
'maxtxsize': 0,
'medianfee': 0,
'mediantxsize': 0,
'minfee': 0,
'minfeerate': 0,
'mintxsize': 0,
'outs': 1,
'subsidy': 5000000000,
'swtotal_size': 0,
'swtotal_weight': 0,
'swtxs': 0,
'total_out': 0,
'total_size': 0,
'total_weight': 0,
'totalfee': 0,
'txs': 1,
'utxo_increase': 1,
'utxo_size_inc': 117
}

View file

@ -20,396 +20,8 @@
mmnode-blocks-info: Display information about a block or range of blocks
"""
import re
from collections import namedtuple
from time import strftime,gmtime
from mmgen.common import *
from mmgen.util import secs_to_hms
from decimal import Decimal
class local_vars: pass
class BlocksInfo:
total_bytes = 0
total_weight = 0
total_solve_time = 0
step = None
bf = namedtuple('block_info_fields',['hdr1','hdr2','fs','bs_key','varname','deps','key'])
# bs=getblockstats(), bh=getblockheader()
# If 'bs_key' is set, it's included in self.bs_keys instead of 'key'
fields = {
'block': bf('', 'Block', '{:<6}', None, 'height',[], None),
'hash': bf('', 'Hash', '{:<64}', None, 'H', [], None),
'date': bf('', 'Date', '{:<19}', None, 'df', [], None),
'interval': bf('Solve','Time ', '{:>8}', None, 'td', [], None),
'size': bf('', 'Size', '{:>7}', None, 'bs', [], 'total_size'),
'weight': bf('', 'Weight', '{:>7}', None, 'bs', [], 'total_weight'),
'utxo_inc': bf(' UTXO',' Incr', '{:>5}', None, 'bs', [], 'utxo_increase'),
'fee10': bf('10%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 0),
'fee25': bf('25%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 1),
'fee50': bf('50%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 2),
'fee75': bf('75%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 3),
'fee90': bf('90%', 'Fee', '{:>3}', 'feerate_percentiles','fp', ['bs'], 4),
'fee_avg': bf('Avg', 'Fee', '{:>3}', None, 'bs', [], 'avgfeerate'),
'fee_min': bf('Min', 'Fee', '{:>3}', None, 'bs', [], 'minfeerate'),
'fee_max': bf('Max', 'Fee', '{:>5}', None, 'bs', [], 'maxfeerate'),
'totalfee': bf('', 'Total Fee','{:>10}', 'totalfee', 'tf', ['bs'], None),
'outputs': bf('Out-', 'puts', '{:>5}', None, 'bs', [], 'outs'),
'inputs': bf('In- ', 'puts', '{:>5}', None, 'bs', [], 'ins'),
'version': bf('', 'Version', '{:8}', None, 'bh', [], 'versionHex'),
'nTx': bf('', ' nTx ', '{:>5}', None, 'bh', [], 'nTx'),
'subsidy': bf('Sub-', 'sidy', '{:5}', 'subsidy', 'su', ['bs'], None),
'difficulty':bf('Diffi-','culty', '{:8}', None, 'di', [], None),
}
dfl_fields = [
'block',
'date',
'interval',
'subsidy',
'totalfee',
'size',
'weight',
'fee50',
'fee25',
'fee10',
'fee_avg',
'fee_min',
'version',
]
fixed_fields = [
'block', # until ≈ 09/01/2028 (block 1000000)
'hash',
'date',
'size', # until ≈ 6x block size increase
'weight', # until ≈ 2.5x block size increase
'version',
'subsidy', # until ≈ 01/04/2028 (increases by 1 digit per halving until 9th halving [max 10 digits])
'difficulty', # until 1.00e+100 (i.e. never)
]
# column width adjustment data:
fs_lsqueeze = ['totalfee','inputs','outputs','nTx']
fs_rsqueeze = []
fs_groups = [
('fee10','fee25','fee50','fee75','fee90','fee_avg','fee_min','fee_max'),
]
fs_lsqueeze2 = ['interval']
funcs = {
'df': lambda self,loc: strftime('%Y-%m-%d %X',gmtime(self.t_cur)),
'td': lambda self,loc: (
'-{:02}:{:02}'.format(abs(self.t_diff)//60,abs(self.t_diff)%60) if self.t_diff < 0 else
' {:02}:{:02}'.format(self.t_diff//60,self.t_diff%60) ),
'tf': lambda self,loc: '{:.8f}'.format(loc.bs["totalfee"] * Decimal('0.00000001')),
'fp': lambda self,loc: loc.bs['feerate_percentiles'],
'su': lambda self,loc: str(loc.bs['subsidy'] * Decimal('0.00000001')).rstrip('0').rstrip('.'),
'di': lambda self,loc: '{:.2e}'.format(loc.bh['difficulty']),
}
def __init__(self):
def get_fields():
if opt.fields:
ufields = opt.fields.lstrip('+').split(',')
for field in ufields:
if field not in self.fields:
die(1,f'{field!r}: unrecognized field')
return self.dfl_fields + ufields if opt.fields[0] == '+' else ufields
else:
return self.dfl_fields
def gen_fs(fnames):
for i in range(len(fnames)):
name = fnames[i]
ls = (' ','')[name in self.fs_lsqueeze + self.fs_lsqueeze2]
rs = (' ','')[name in self.fs_rsqueeze]
if i < len(fnames) - 1 and fnames[i+1] in self.fs_lsqueeze2:
rs = ''
if i:
for group in self.fs_groups:
if name in group and fnames[i-1] in group:
ls = ''
break
yield ls + self.fields[name].fs + rs
self.block_list,self.first,self.last = self.parse_block_range(cmd_args)
fnames = get_fields()
self.fvals = list(self.fields[name] for name in fnames)
self.fs = ''.join(gen_fs(fnames)).strip()
self.deps = set(' '.join(v.varname + ' ' + ' '.join(v.deps) for v in self.fvals).split())
self.bs_keys = [(v.bs_key or v.key) for v in self.fvals if v.bs_key or v.varname == 'bs']
self.bs_keys.extend(['total_size','total_weight'])
self.ufuncs = {v.varname:self.funcs[v.varname] for v in self.fvals if v.varname in self.funcs}
if opt.miner_info:
self.fs += ' {}'
self.miner_pats = [re.compile(pat) for pat in (
rb'[\xe3\xe4\xe5][\^/](.*?)\xfa',
rb'([a-zA-Z0-9&. -]+/Mined by [a-zA-Z0-9. ]+)',
rb'\x08/(.*Mined by [a-zA-Z0-9. ]+)',
rb'Mined by ([a-zA-Z0-9. ]+)',
rb'[`]([_a-zA-Z0-9&. #/-]+)[/\xfa]',
rb'[/^]([a-zA-Z0-9&. #/-]{5,})',
rb'[/^]([_a-zA-Z0-9&. #/-]+)/',
)]
else:
self.miner_pats = None
def parse_block_range(self,args):
def conv_blkspec(arg):
if arg == 'cur':
return c.blockcount
elif is_int(arg):
if int(arg) < 0:
die(1,f'{arg}: block number must be non-negative')
elif int(arg) > c.blockcount:
die(1,f'{arg}: requested block height greater than current chain tip!')
else:
return int(arg)
else:
die(1,f'{arg}: invalid block specifier')
def check_nblocks(arg):
if arg <= 0:
die(1,'nBlocks must be a positive integer')
if arg > c.blockcount:
die(1, f"'{arg}': nBlocks must be less than current chain height")
return arg
def parse_rangespec(arg):
class RangeParser:
debug = True
def __init__(self,arg):
self.arg = self.orig_arg = arg
def parse(self,target):
ret = getattr(self,'parse_'+target)()
if self.debug: print(f'arg after parse({target}): {self.arg}')
return ret
def finalize(self):
if self.arg:
die(1,f'{self.orig_arg!r}: invalid range specifier')
def parse_from_tip(self):
m = re.match(r'-([0-9]+)(.*)',self.arg)
if m:
res,self.arg = (m[1],m[2])
return check_nblocks(int(res))
def parse_abs_range(self):
m = re.match(r'([^+-]+)(-([^+-]+)){0,1}(.*)',self.arg)
if m:
if self.debug: print(f'abs_range parse: first={m[1]}, last={m[3]}')
self.arg = m[4]
return (
conv_blkspec(m[1]),
conv_blkspec(m[3]) if m[3] else None
)
return (None,None)
def parse_add(self):
m = re.match(r'\+([0-9*]+)(.*)',self.arg)
if m:
res,self.arg = (m[1],m[2])
if res.strip('*') != res:
die(1,f"'+{res}': malformed nBlocks specifier")
if len(res) > 30:
die(1,f"'+{res}': overly long nBlocks specifier")
return check_nblocks(eval(res)) # res is only digits plus '*', so eval safe
range_data = namedtuple('parsed_range_data',['first','last','from_tip','nblocks','step'])
p = RangeParser(arg)
from_tip = p.parse('from_tip')
first,last = (c.blockcount-from_tip,None) if from_tip else p.parse('abs_range')
add1 = p.parse('add')
add2 = p.parse('add')
p.finalize()
if add2 and last is not None:
die(1,f'{arg!r}: invalid range specifier')
nblocks,step = (add1,add2) if last is None else (None,add1)
if p.debug: print(range_data(first,last,from_tip,nblocks,step))
if nblocks:
if first == None:
first = c.blockcount - nblocks + 1
last = first + nblocks - 1
first = conv_blkspec(first)
last = conv_blkspec(last or first)
if p.debug: print(range_data(first,last,from_tip,nblocks,step))
if first > last:
die(1,f'{first}-{last}: invalid block range')
return range_data(first,last,from_tip,nblocks,step)
# return (block_list,first,last)
if not args:
return (None,c.blockcount,c.blockcount)
elif len(args) == 1:
r = parse_rangespec(args[0])
return (
list(range(r.first,r.last+1,r.step)) if r.step else None,
r.first,
r.last
)
else:
return ([conv_blkspec(a) for a in args],None,None)
async def run(self):
heights = self.block_list or range(self.first,self.last+1)
hashes = await c.gathered_call('getblockhash',[(height,) for height in heights])
self.hdrs = await c.gathered_call('getblockheader',[(H,) for H in hashes])
async def init(count):
h0 = (
self.hdrs[count] if heights[count] == 0 else
await c.call('getblockheader',await c.call('getblockhash',heights[count]-1))
)
self.t_cur = h0['time']
if count == 0:
self.first_prev_hdr = h0
if not self.block_list:
await init(0)
for n in range(len(heights)):
if self.block_list:
await init(n)
await self.process_block(heights[n],hashes[n],self.hdrs[n])
async def process_block(self,height,H,hdr):
loc = local_vars()
loc.height = height
loc.H = H
loc.bh = hdr
self.t_diff = hdr['time'] - self.t_cur
self.t_cur = hdr['time']
self.total_solve_time += self.t_diff
if 'bs' in self.deps:
loc.bs = genesis_stats if height == 0 else await c.call('getblockstats',H,self.bs_keys)
self.total_bytes += loc.bs['total_size']
self.total_weight += loc.bs['total_weight']
if opt.summary:
return
for varname,func in self.ufuncs.items():
setattr(loc,varname,func(self,loc))
if opt.miner_info:
miner_info = '-' if height == 0 else await self.get_miner_string(H)
def gen():
for v in self.fvals:
if v.key is None:
yield getattr(loc,v.varname)
else:
yield getattr(loc,v.varname)[v.key]
if opt.miner_info:
yield miner_info
Msg(self.fs.format(*gen()))
async def get_miner_string(self,H):
tx0 = (await c.call('getblock',H))['tx'][0]
bd = await c.call('getrawtransaction',tx0,1)
if type(bd) == tuple:
return '---'
else:
cb = bytes.fromhex(bd['vin'][0]['coinbase'])
if opt.raw_miner_info:
return repr(cb)
else:
for pat in self.miner_pats:
m = pat.search(cb)
if m:
return ''.join(chr(b) for b in m[1] if 31 < b < 127).strip('^').strip('/').replace('/',' ')
def print_header(self):
hdr1 = [v.hdr1 for v in self.fvals]
hdr2 = [v.hdr2 for v in self.fvals]
if opt.miner_info:
hdr1.append(' ')
hdr2.append('Miner')
if ''.join(hdr1).replace(' ',''):
Msg(self.fs.format(*hdr1))
Msg(self.fs.format(*hdr2))
async def print_range_stats(self):
# These figures don’t include the Genesis Block:
elapsed = self.hdrs[-1]['time'] - self.first_prev_hdr['time']
nblocks = self.hdrs[-1]['height'] - self.first_prev_hdr['height']
Msg('Range: {}-{} ({} blocks [{}])'.format(
self.hdrs[0]['height'],
self.hdrs[-1]['height'],
self.hdrs[-1]['height'] - self.hdrs[0]['height'] + 1, # includes Genesis Block
secs_to_hms(elapsed) ))
if elapsed:
avg_bdi = int(elapsed / nblocks)
if 'bs' in self.deps:
total_blocks = len(self.hdrs)
rate = (self.total_bytes / 10000) / (self.total_solve_time / 36)
Msg_r(fmt(f"""
Avg size: {self.total_bytes//total_blocks} bytes
Avg weight: {self.total_weight//total_blocks} bytes
MB/hr: {rate:0.4f}
"""))
Msg(f'Avg BDI: {avg_bdi/60:.2f} min')
async def print_diff_stats(self):
tip = c.blockcount
# Only display stats if user-requested range ends with chain tip
if self.last != tip:
return
cur_diff_disp = 'Cur difficulty: {:.2e}'.format(self.hdrs[-1]['difficulty'])
rel = tip % 2016
if rel:
rel_hdr = await c.call('getblockheader',await c.call('getblockhash',tip-rel))
tip_time = (
self.hdrs[-1]['time'] if self.hdrs[-1]['height'] == tip else
(await c.call('getblockheader',await c.call('getblockhash',tip)))['time']
)
tdiff = tip_time - rel_hdr['time']
if tdiff: # if the 2 timestamps are equal (very unlikely), skip display to avoid div-by-zero error
bdi = tdiff / rel
adj_pct = ((600 / bdi) - 1) * 100
Msg_r(fmt(f"""
Current height: {tip}
Next diff adjust: {tip-rel+2016} (in {2016-rel} blocks [{((2016-rel)*bdi)/86400:.2f} days])
BDI (cur period): {bdi/60:.2f} min
{cur_diff_disp}
Est. diff adjust: {adj_pct:+.2f}%
"""))
else:
Msg_r(fmt(f"""
Current height: {tip}
{cur_diff_disp}
Next diff adjust: {tip-rel+2016} (in {2016-rel} blocks)
"""))
from mmgen.node_tools.BlocksInfo import BlocksInfo
opts_data = {
'sets': [
@ -496,46 +108,13 @@ This program requires a txindex-enabled daemon for correct operation.
cmd_args = opts.init(opts_data)
# 'getblockstats' RPC raises exception on Genesis Block, so provide our own stats:
genesis_stats = {
'avgfee': 0,
'avgfeerate': 0,
'avgtxsize': 0,
'feerate_percentiles': [ 0, 0, 0, 0, 0 ],
'height': 0,
'ins': 0,
'maxfee': 0,
'maxfeerate': 0,
'maxtxsize': 0,
'medianfee': 0,
'mediantxsize': 0,
'minfee': 0,
'minfeerate': 0,
'mintxsize': 0,
'outs': 1,
'subsidy': 5000000000,
'swtotal_size': 0,
'swtotal_weight': 0,
'swtxs': 0,
'total_out': 0,
'total_size': 0,
'total_weight': 0,
'totalfee': 0,
'txs': 1,
'utxo_increase': 1,
'utxo_size_inc': 117
}
async def main():
from mmgen.protocol import init_proto_from_opts
proto = init_proto_from_opts()
from mmgen.rpc import rpc_init
global c
c = await rpc_init(proto)
m = BlocksInfo()
m = BlocksInfo( cmd_args, opt, await rpc_init(proto) )
if not (opt.summary or opt.no_header):
m.print_header()

77
test/test-release.sh Executable file
View file

@ -0,0 +1,77 @@
#!/bin/bash
export MMGEN_TEST_SUITE=1
export PYTHONPATH=.
RED="\e[31;1m" GREEN="\e[32;1m" YELLOW="\e[33;1m" BLUE="\e[34;1m" MAGENTA="\e[35;1m" CYAN="\e[36;1m"
RESET="\e[0m"
set -o errtrace
set -o functrace
trap 'echo -e "${GREEN}Exiting at user request$RESET"; exit' INT
trap 'echo -e "${RED}Node tools test suite exited with error$RESET"' ERR
umask 0022
unit_tests_py='test/unit_tests.py --names --quiet'
PROGNAME=$(basename $0)
while getopts hv OPT
do
case "$OPT" in
h) printf " %-16s The MMGen node tools test suite\n" "${PROGNAME}:"
echo " USAGE: $PROGNAME [options] [tests or test group]"
echo " OPTIONS: '-h' Print this help message"
echo " '-v' Run commands with '--verbose' switch"
exit ;;
v) VERBOSE=1
unit_tests_py="${unit_tests_py/--quiet/--verbose}" ;;
*) exit ;;
esac
done
shift $((OPTIND-1))
nt_repo='../mmgen-node-tools'
mm_repo='../mmgen'
die() { echo -e $YELLOW$1$RESET; false; }
gecho() { echo -e $GREEN$1$RESET; }
pecho() { echo -e $MAGENTA$1$RESET; }
becho() { echo -e $BLUE$1$RESET; }
check_mmgen_repo() {
( cd $mm_repo; python3 ./setup.py --url | grep -iq 'mmgen' )
}
create_links() {
( cd 'mmgen'; [ -L 'node_tools' ] || ln -s "../$nt_repo/mmgen/node_tools" )
(
cd 'test/unit_tests_d'
for fn in ../../$nt_repo/test/unit_tests_d/nt_*.py; do
[ -L "$(basename $fn)" ] || ln -s "$fn"
done
)
}
run_unit_tests() {
pecho 'Running unit tests:'
$unit_tests_py --node-tools
pecho 'Completed unit tests'
}
# start execution
set -e
becho 'Starting node tools test suite (WIP)'
check_mmgen_repo || die "No MMGen repository found at $mm_repo!"
cd $mm_repo
create_links
run_unit_tests
becho 'Node tools test suite completed successfully'

View file

@ -0,0 +1,83 @@
#!/usr/bin/env python3
"""
test.unit_tests_d.nt_BlocksInfo:
BlocksInfo unit test for the MMGen Node Tools suite
"""
from mmgen.common import *
from mmgen.exception import *
from mmgen.node_tools.BlocksInfo import BlocksInfo
tip = 50000
vecs = (
# First Last FromTip nBlocks Step First Last BlockList
( (), (), (tip, tip, None) ),
( (199,2,37), (), (None, None, [199,2,37]) ),
( '0', (0, 0, None, None, None), (0, 0, None) ),
( '0-0', (0, 0, None, None, None), (0, 0, None) ),
(f'-{tip}', (0, 0, tip, None, None), (0, 0, None) ),
( '0-10', (0, 10, None, None, None), (0, 10, None) ),
( '0+10', (0, 9, None, 10, None), (0, 9, None) ),
( '0+10+2', (0, 9, None, 10, 2 ), (0, 9, [0,2,4,6,8]) ),
( '1', (1, 1, None, None, None), (1, 1, None) ),
( '1-1', (1, 1, None, None, None), (1, 1, None) ),
( '1-10', (1, 10, None, None, None), (1, 10, None) ),
( '1+10', (1, 10, None, 10, None), (1, 10, None) ),
( '1+10+2', (1, 10, None, 10, 2 ), (1, 10, [1,3,5,7,9]) ),
( '+1', (tip, tip, None, 1, None), (tip, tip, None) ),
( '+10', (tip-9, tip, None, 10, None), (tip-9, tip, None) ),
( '-1', (tip-1, tip-1, 1, None, None), (tip-1, tip-1, None) ),
( '-1+1', (tip-1, tip-1, 1, 1, None), (tip-1, tip-1, None) ),
( '-1+2', (tip-1, tip, 1, 2, None), (tip-1, tip, None) ),
( '-10', (tip-10, tip-10, 10, None, None), (tip-10, tip-10, None) ),
( '-10+11', (tip-10, tip, 10, 11, None), (tip-10, tip, None) ),
( '-10+11+2', (tip-10, tip, 10, 11, 2 ), (tip-10, tip, list(range(tip-10,tip+1,2))) ),
( 'cur', (tip, tip, None, None, None), (tip, tip, None) ),
( 'cur-cur', (tip, tip, None, None, None), (tip, tip, None) ),
( '0-cur', (0, tip, None, None, None), (0, tip, None) ),
(f'{tip-1}-cur', (tip-1, tip, None, None, None), (tip-1, tip, None) ),
( '0-cur+3000', (0, tip, None, None, 3000 ), (0, tip, list(range(0,tip+1,3000))) ),
( '+1440+144', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
( '+144*10+12*12', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
)
class dummyRPC:
blockcount = tip
class dummyOpt:
fields = None
miner_info = None
class unit_tests:
def rangespec(self,name,ut):
b = BlocksInfo(0,dummyOpt(),dummyRPC())
def test(spec,chk,foo):
ret = b.parse_rangespec(spec)
vmsg(f'{spec:13} => {BlocksInfo.range_data(*chk)}')
assert ret == chk, f'{ret} != {chk}'
for vec in vecs:
if vec[1]:
test(*vec)
return True
def parse_cmd_args(self,name,ut):
def test(spec,foo,chk):
b = BlocksInfo(spec if type(spec) == tuple else [spec],dummyOpt(),dummyRPC())
ret = (b.first,b.last,b.block_list)
vmsg('{:13} => {}'.format(repr(spec) if type(spec) == tuple else spec,chk))
assert ret == chk, f'{ret} != {chk}'
for vec in vecs:
test(*vec)
return True