forked from C-Otto/rebalance-lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.py
More file actions
executable file
·204 lines (169 loc) · 5.48 KB
/
audit.py
File metadata and controls
executable file
·204 lines (169 loc) · 5.48 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
import json
from eclair import Eclair
from eclair import Audit
from eclair import Channel
from eclair import Peer
from output import Output, format_alias, format_ppm, format_amount, format_amount_green, format_boring_string, \
print_bar, format_channel_id, format_error
import os
from os.path import expanduser
from eclair import Eclair
from pyhocon import ConfigFactory
import sys
import time
def read_eclair_config(dir):
dir = expanduser(dir)
file = f"{dir}/eclair.conf"
if os.path.exists(file):
return ConfigFactory.parse_file(file)
else:
return None
def received_timestamp(received):
max_timestamp = 0
max_iso = ''
for part in received['parts']:
unix = part['timestamp']['unix']
iso = part['timestamp']['iso']
if unix > max_timestamp:
max_timestamp = unix
max_iso = iso
return [max_timestamp, max_iso]
def received_amount(received):
amount = 0
for part in received['parts']:
amount = amount + part['amount']
return amount
def received_channel_id(received):
ch = None
for part in received['parts']:
if not ch is None and ch != part['fromChannelId']:
raise Exception('multipart payment')
ch = part['fromChannelId']
return ch
def sent_timestamp(sent):
min_timestamp = 9999999999
min_iso = ''
for part in sent['parts']:
unix = part['timestamp']['unix']
iso = part['timestamp']['iso']
if unix < min_timestamp:
min_timestamp = unix
min_iso = iso
return [min_timestamp, min_iso]
def sent_amount(sent):
amount = 0
for part in sent['parts']:
amount = amount + part['amount']
return amount
def sent_fees(sent):
fees = 0
for part in sent['parts']:
fees = fees + part['feesPaid']
return fees
def sent_channel_id(sent):
ch = None
for part in sent['parts']:
if not ch is None and ch != part['toChannelId']:
raise Exception('multipart payment')
ch = part['toChannelId']
return ch
all_aliases = {}
def get_all_aliases():
if not all_aliases:
nodes = eclair.get_nodes()
for node in nodes:
all_aliases[node['nodeId']] = node['alias']
return all_aliases
def get_alias(aliases, channels, node_id, channel_id):
if node_id in aliases and aliases[node_id] != node_id:
return aliases[node_id]
elif node_id in get_all_aliases():
return get_all_aliases()[node_id]
elif channel_id in channels:
return channels[channel_id].chan_id
else:
return node_id
def sort_by_received_timestamp(pair):
rcvd = pair[1]
return received_timestamp(rcvd)[0]
from_ts = int(time.time()) - 60 * 60 * 24 * 31 * 4
if len(sys.argv) == 2:
from_ts = int(sys.argv[1])
eclair_conf = read_eclair_config('~/.eclair')
eclair = Eclair(eclair_conf, None, None)
node_ids = {}
chan_list = eclair.get_channels(active_only=False, public_only=False)
try:
closed_chan_list = eclair.get_closed_channels()
except EclairRPCException:
closed_chan_list = []
chan_list = chan_list + closed_chan_list
channels = {}
for ch in chan_list:
channels[ch.channel_id] = ch
node_ids[ch.channel_id] = ch.remote_pubkey
peer_list = eclair.get_peers()
aliases = {}
for p in peer_list:
aliases[p.pub_key] = p.alias
audit = eclair.get_audit(frm=from_ts)
sent = {}
for s in audit.sent:
sent[s['paymentHash']] = s
received = {}
for r in audit.received:
received[r['paymentHash']] = r
rebalanced = []
for receivedHash in received:
if receivedHash in sent:
rebalanced.append([sent[receivedHash], received[receivedHash]])
rebalanced.sort(key=sort_by_received_timestamp)
print(f"{'=' * 71} Rebalanced {'=' * 75}")
for pair in rebalanced:
sent = pair[0]
rcvd = pair[1]
rcvd_ts = received_timestamp(rcvd)
sent_ts = sent_timestamp(sent)
ts = rcvd_ts[1].ljust(25)
latency = sent_ts[0] - rcvd_ts[0]
sent_amt = sent_amount(sent)
fees = sent_fees(sent)
received_amt = received_amount(rcvd)
to_ch = sent_channel_id(sent)
from_ch = received_channel_id(rcvd)
from_node_id = node_ids.get(from_ch, from_ch)
to_node_id = node_ids.get(to_ch, to_ch)
from_node = get_alias(aliases, channels, from_node_id, from_ch).ljust(32)
to_node = get_alias(aliases, channels, to_node_id, to_ch).ljust(32)
print(
f"{format_boring_string(ts)} "
f"{format_alias(to_node)}\t"
f"{format_alias(from_node)}\t"
f"{format_amount(sent_amt, 16)}"
f"{format_amount(received_amt, 16)}"
f"{format_amount(fees, 10)} "
f"{format_amount(latency, 10)}s"
)
print(f"{'=' * 71} Relayed {'=' * 78}")
for r in audit.relayed:
started_ts = r['receivedAt']['iso'].ljust(25)
settled_ts = r['settledAt']['iso'].ljust(25)
ts = started_ts
latency = r['settledAt']['unix'] - r['receivedAt']['unix']
amount = r['amountOut']
fee = r['amountIn'] - r['amountOut']
from_ch = r['fromChannelId']
to_ch = r['toChannelId']
from_node_id = node_ids.get(from_ch, from_ch)
to_node_id = node_ids.get(to_ch, to_ch)
from_node = get_alias(aliases, channels, from_node_id, from_ch).ljust(32)
to_node = get_alias(aliases, channels, to_node_id, to_ch).ljust(32)
print(
f"{format_boring_string(ts)} "
f"{format_alias(from_node)}\t"
f"{format_alias(to_node)}\t"
f"{format_amount(amount, 16)}"
f"{format_amount(fee, 10)} "
f"{format_amount(latency, 10)}s"
)