blockstream-electrs/tools/mempool.py

71 lines
2.1 KiB
Python
Raw Normal View History

2018-07-03 12:37:49 +00:00
#!/usr/bin/env python3
2018-05-22 06:25:10 +00:00
import binascii
import json
2018-07-17 07:11:28 +00:00
import os
2018-05-22 06:25:10 +00:00
import socket
2018-07-03 12:19:49 +00:00
import numpy as np
import matplotlib.pyplot as plt
2018-05-22 06:25:10 +00:00
2018-07-17 07:11:28 +00:00
class Daemon:
def __init__(self):
self.sock = socket.create_connection(('localhost', 8332))
self.fd = self.sock.makefile()
path = os.path.expanduser('~/.bitcoin/.cookie')
cookie = binascii.b2a_base64(open(path, 'rb').read())
self.cookie = cookie.decode('ascii').strip()
self.index = 0
2018-05-22 06:25:10 +00:00
2018-07-17 07:11:28 +00:00
def request(self, method, params_list):
obj = [{"method": method, "params": params, "id": self.index}
for params in params_list]
request = json.dumps(obj)
2018-05-22 06:25:10 +00:00
2018-07-17 07:11:28 +00:00
msg = ('POST / HTTP/1.1\n'
'Authorization: Basic {}\n'
'Content-Length: {}\n\n'
'{}'.format(self.cookie, len(request), request))
self.sock.sendall(msg.encode('ascii'))
2018-05-22 06:25:10 +00:00
2018-07-17 07:11:28 +00:00
status = self.fd.readline().strip()
while True:
if self.fd.readline().strip():
continue # skip headers
else:
break # next line will contain the response
2018-05-22 06:25:10 +00:00
2018-07-17 07:11:28 +00:00
data = self.fd.readline().strip()
replies = json.loads(data)
for reply in replies:
assert reply['error'] is None
assert reply['id'] == self.index
self.index += 1
return [d['result'] for d in replies]
2018-05-22 06:25:10 +00:00
def main():
2018-07-17 07:11:28 +00:00
d = Daemon()
txids, = d.request('getrawmempool', [[False]])
2018-05-22 06:25:10 +00:00
txids = list(map(lambda a: [a], txids))
2018-07-03 12:19:49 +00:00
2018-07-17 07:11:28 +00:00
entries = d.request('getmempoolentry', txids)
2018-07-03 12:19:49 +00:00
entries = [{'fee': e['fee']*1e8, 'vsize': e['size']} for e in entries]
for e in entries:
e['rate'] = e['fee'] / e['vsize'] # sat/vbyte
entries.sort(key=lambda e: e['rate'], reverse=True)
vsize = np.array([e['vsize'] for e in entries]).cumsum()
rate = np.array([e['rate'] for e in entries])
plt.semilogy(vsize / 1e6, rate, '-')
plt.xlabel('Mempool size (MB)')
plt.ylabel('Fee rate (sat/vbyte)')
2018-07-03 12:37:49 +00:00
plt.title('{} transactions'.format(len(entries)))
2018-07-03 12:19:49 +00:00
plt.grid()
plt.show()
2018-05-22 06:25:10 +00:00
if __name__ == '__main__':
main()