Add method for retrieving block headers from daemon

This commit is contained in:
Michał Sałaban 2019-10-16 13:36:50 +02:00
parent eb88f7cd6f
commit fb13992c77
3 changed files with 29 additions and 4 deletions

View file

@ -60,6 +60,15 @@ class JSONRPCDaemon(object):
confirmations=0))
return txs
def headers(self, start_height, end_height=None):
end_height = end_height or start_height
res = self.raw_jsonrpc_request('get_block_headers_range', {
'start_height': start_height,
'end_height': end_height})
if res['status'] == 'OK':
return res['headers']
raise Exception()
def raw_request(self, path, data):
hdr = {'Content-Type': 'application/json'}
_log.debug(u"Request: {path}\nData: {data}".format(
@ -76,7 +85,6 @@ class JSONRPCDaemon(object):
_log.debug(u"Result:\n{result}".format(result=_ppresult))
return result
def raw_jsonrpc_request(self, method, params=None):
hdr = {'Content-Type': 'application/json'}
data = {'jsonrpc': '2.0', 'id': 0, 'method': method, 'params': params or {}}

View file

@ -41,3 +41,13 @@ class Daemon(object):
:rtype: list of :class:`Transaction <monero.transaction.Transaction>`
"""
return self._backend.mempool()
def headers(self, start_height, end_height=None):
"""
Returns block headers for given height range.
If no :param end_height: is given, it's assumed to be equal to :param start_height:
:rtype: list of dict
"""
return self._backend.headers(start_height, end_height)

View file

@ -31,8 +31,15 @@ def get_daemon():
d = get_daemon()
info = d.info()
print("Net: {net:>18s}\n"
print("Net: {net:>15s}net\n"
"Height: {height:10d}\n"
"Difficulty: {difficulty:10d}".format(
net='test' if info['testnet'] else 'live',
"Difficulty: {difficulty:10d}\n"
"Alt blocks: {alt_blocks_count:10d}\n".format(
net='test' if info['testnet'] \
else 'stage' if info['stagenet'] \
else 'main' if info['mainnet'] else 'unknown',
**info))
for hdr in reversed(d.headers(info['height']-7, info['height']-1)):
print("{height:10d} {hash} {block_size_kb:6.2f} kB {num_txes:3d} txn(s) "
"v{major_version:d}".format(
block_size_kb=hdr['block_size']/1024.0, **hdr))