-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnodestats.py
executable file
·76 lines (62 loc) · 2.4 KB
/
nodestats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import json
from pyln.client import Plugin
from lib.amount import valid_units, is_valid_unit, convert_msat, convert_sat
from lib.node_stats import nodeid
from statistics import median
plugin = Plugin()
# A copy of lnd's nodestats rpc command
@plugin.method("nodestats")
def nodestats(plugin, node_id='me', unit='sat', verbose=False):
"""Get statistical information about a node's channel
"""
if node_id == 'me':
node_id = nodeid(plugin.rpc)
if len(node_id) != 66:
return "Must enter a valid node id"
if not is_valid_unit(unit):
return "Value units are %s" % ', '.join(valid_units)
try:
node_info = plugin.rpc.listnodes(node_id)['nodes'][0]
except IndexError:
return "Node id not found"
node_channels = [c for c in plugin.rpc.listchannels()['channels'] if c['source'] == node_id]
active_channels = [c for c in node_channels if c['active'] is True]
capacity = [c['satoshis'] for c in active_channels]
fee_rate = [c['fee_per_millionth'] for c in active_channels]
base_fee = [c['base_fee_millisatoshi'] for c in active_channels]
num_active_channels = len(active_channels)
num_closed_channels = len(node_channels) - num_active_channels
if num_active_channels > 0:
capacity_sats = sum(capacity)
capacity_med = median(capacity)
capacity_avg = capacity_sats / num_active_channels
fee_rate_med = median(fee_rate)
base_fee_med = median(base_fee)
else:
capacity_sats = 0
capacity_med = 0
capacity_avg = 0
fee_rate_med = 0
fee_rate_avg = 0
base_fee_med = 0
base_fee_avg = 0
data = {
'node': node_id,
'alias': node_info['alias'],
'color': node_info['color'],
'ip_addrs': [addr['address'] for addr in node_info['addresses']],
'unit': unit,
'total_capacity': convert_sat(capacity_sats, unit),
'active_channels': num_active_channels,
'closed_channels': num_closed_channels,
'median_channel_capacity': convert_sat(capacity_med, unit),
'average_channel_capacity': convert_sat(capacity_avg, unit),
'median_fee_rate': convert_msat(fee_rate_med, unit),
'median_base_fee': convert_msat(base_fee_med, unit),
}
if verbose:
data['channels'] = node_channels
plugin.log(json.dumps(data, indent=4))
return data
plugin.run()