Skip to content

Instantly share code, notes, and snippets.

@emesik
Last active October 18, 2017 15:46
Show Gist options
  • Save emesik/a3ea95adf8b22d287c94259b0f473f4a to your computer and use it in GitHub Desktop.
Save emesik/a3ea95adf8b22d287c94259b0f473f4a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
Fetch and print current Monero mempool.
"""
from datetime import datetime
import json
import logging
import requests
class MemPool(object):
def __init__(self, host, port=18081):
self.url = 'http://{host}:{port}/get_transaction_pool'.format(host=host, port=port)
self._log = logging.getLogger(__name__)
def refresh(self):
req = requests.post(self.url, headers={'Content-Type': 'application/json'})
self._raw_data = req.json()
try:
return self._process()
except Exception:
self._log.exception("Error while processing data")
self._log.debug(str(self._raw_data))
raise
def _process(self):
txs = []
for tx in self._raw_data.get('transactions', []):
tx = tx.copy()
tx['tx_json'] = json.loads(tx['tx_json'])
txs.append({
'timestamp': tx['receive_time'],
'size': tx['blob_size'],
'fee': tx['fee'],
'inputs': len(tx['tx_json']['vin']),
'outputs': len(tx['tx_json']['vout']),
'ring': len(tx['tx_json']['vin'][0]['key']['key_offsets']),
'version': tx['tx_json']['version'],
'payment_id': tx['tx_json']['extra'],
'id': tx['id_hash']})
return txs
def print_data(data):
now = datetime.now().timestamp()
print(" age[hms] ver size fee ins outs ring hash")
for tx in data:
age = int(now - tx['timestamp'])
print("{h:4d}:{m:02d}:{s:02d} {version:2d} {kbs:3.2f}k {fee:1.12f} {ins:3d} {outs:3d} {ring:3d} {id:s}".format(
h=int(age / 60 / 60), m=int(age / 60 % 60), s=int(age % 60),
version=tx['version'], kbs=tx['size']/1024.0, fee=tx['fee']/1000000000000,
ins=tx['inputs'], outs=tx['outputs'], ring=tx['ring'], id=tx['id']))
if __name__ == '__main__':
import sys
try:
arg1 = sys.argv[1]
if ':' in arg1:
host, port = arg1.split(':')
else:
host = arg1
port = 18081
except IndexError:
host, port = '127.0.0.1', 18081
p = MemPool(host, port)
data = p.refresh()
print_data(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment