-
Notifications
You must be signed in to change notification settings - Fork 1
/
startup.js
350 lines (314 loc) · 15.5 KB
/
startup.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const {getLND} = require('./lnd');
const {getNetworkGraph} = require('ln-service');
const neo4j = require('neo4j-driver');
const {getNode} = require('ln-service');
const {subscribeToLNDGraph} = require('./lndService');
const {getDB} = require('./db');
const config = require('./configs/config.json');
const {DB_QUERIES} = require('./constants');
const logger = require('log4js').getLogger("startup");
async function loadGraphToDB() {
try {
const lndGraphToDBHandler = subscribeToLNDGraph();
const lnd = getLND();
let {channels, nodes} = await getNetworkGraph({lnd});
const node_count = nodes.length;
const channel_count = channels.length;
logger.info('LND returned ' + node_count + ' LN nodes and ' + (channel_count) + ' LN channels for describegraph');
logger.info('Neo4j DB will load ' + (node_count + channel_count) + ' graph nodes and ' + (channel_count * 2) + ' graph edges.');
let validNodes = new Set();
await populateNodes(nodes, validNodes);
nodes = null; // freeing heavy objects for GC.
let validChannels = new Set();
await populateChannels(channels, validNodes, validChannels);
channels = null;
await deleteStaleChannels(validChannels);
validChannels = null;
await deleteStaleNodes(validNodes);
validNodes = null;
await updateNodeInfo();
logger.info('processing of LND graph notifications to be started.');
lndGraphToDBHandler.on('move', processLndGraphNotifications);
lndGraphToDBHandler.moveToDB();
logger.info('Neo4j DB has loaded ' + (node_count + channel_count) + ' graph nodes and ' + (channel_count * 2) + ' graph edges.');
}
catch (e) {
logger.fatal('Error while loading describegraph data from LND to Neo4j DB. Restart the server. Error: ' + e);
throw e;
}
}
async function populateNodes(nodes, validNodes) {
const driver = getDB();
const nodeCount = nodes.length;
let startIndex = 0;
while (startIndex < nodeCount) { // breaking nodes to 1k queries per transaction to reduce reduce Neo4j memory overload.
const start = startIndex;
let end = startIndex + config.queriesPerTransaction-1;
if (end >= nodeCount) {
end = nodeCount-1;
}
const session = driver.session();
try {
await session.writeTransaction(async tx => {
const txPromises = [];
for (let i = start; i <= end; i++) {
const node = nodes[i];
validNodes.add(node.public_key);
txPromises.push(tx.run(
DB_QUERIES.POPULATE_NODES,
{
public_key: node.public_key,
alias: node.alias,
color: node.color,
sockets: node.sockets,
updated_at: node.updated_at
}
));
}
return await Promise.all(txPromises);
});
} finally {
await session.close();
}
startIndex = end+1;
}
}
async function populateChannels(channels, validNodes, validChannels) {
const driver = getDB();
const channelCount = channels.length;
let startIndex = 0;
while (startIndex < channelCount) {
const start = startIndex;
let end = startIndex + config.queriesPerTransaction-1;
if (end >= channelCount) {
end = channelCount-1;
}
const session = driver.session();
try {
await session.writeTransaction(async tx => {
const txPromises = [];
for (let i = start; i <= end; i++) {
const channel = channels[i];
validChannels.add(channel.id);
validNodes.add(channel.policies[0].public_key);
validNodes.add(channel.policies[1].public_key);
txPromises.push(tx.run(
DB_QUERIES.POPULATE_CHANNELS,
{
n0_public_key: channel.policies[0].public_key,
n1_public_key: channel.policies[1].public_key,
c_channel_id: channel.id,
c_channel_point: channel.transaction_id + ':' + channel.transaction_vout,
c_capacity: neo4j.int(channel.capacity),
c_updated_at: channel.updated_at != null ? channel.updated_at : null,
r0_base_fee_mtokens: channel.policies[0].base_fee_mtokens != null ? neo4j.int(channel.policies[0].base_fee_mtokens) : null,
r0_cltv_delta: channel.policies[0].cltv_delta != null ? neo4j.int(channel.policies[0].cltv_delta) : null,
r0_fee_rate: channel.policies[0].fee_rate != null ? neo4j.int(channel.policies[0].fee_rate) : null,
r0_is_disabled: channel.policies[0].is_disabled != null ? channel.policies[0].is_disabled : null,
r0_max_htlc_mtokens: channel.policies[0].max_htlc_mtokens != null ? neo4j.int(channel.policies[0].max_htlc_mtokens) : null,
r0_min_htlc_mtokens: channel.policies[0].min_htlc_mtokens != null ? neo4j.int(channel.policies[0].min_htlc_mtokens) : null,
r0_updated_at: channel.policies[0].updated_at != null ? channel.policies[0].updated_at : null,
r1_base_fee_mtokens: channel.policies[1].base_fee_mtokens != null ? neo4j.int(channel.policies[1].base_fee_mtokens) : null,
r1_cltv_delta: channel.policies[1].cltv_delta != null ? neo4j.int(channel.policies[1].cltv_delta) : null,
r1_fee_rate: channel.policies[1].fee_rate != null ? neo4j.int(channel.policies[1].fee_rate) : null,
r1_is_disabled: channel.policies[1].is_disabled != null ? channel.policies[1].is_disabled : null,
r1_max_htlc_mtokens: channel.policies[1].max_htlc_mtokens != null ? neo4j.int(channel.policies[1].max_htlc_mtokens) : null,
r1_min_htlc_mtokens: channel.policies[1].min_htlc_mtokens != null ? neo4j.int(channel.policies[1].min_htlc_mtokens) : null,
r1_updated_at: channel.policies[1].updated_at != null ? channel.policies[1].updated_at : null
}
));
}
return Promise.all(txPromises);
});
} finally {
await session.close();
}
startIndex = end+1;
}
}
async function deleteStaleChannels(validChannels) { // deletion of stale channels if any left by previous server runs
const driver = getDB();
const session = driver.session();
try {
await session.writeTransaction(async tx => {
const existing_channel_ids = await tx.run(
DB_QUERIES.ALL_CHANNEL_IDS
);
let channelGraphNodeCleanupPromises = [];
for (let i = 0; i < existing_channel_ids.records.length; i++) {
const existing_channel_id = existing_channel_ids.records[0].get('channel_id');
if (!validChannels.has(existing_channel_id)) {
channelGraphNodeCleanupPromises.push(tx.run(
DB_QUERIES.DELETE_CHANNELS_BY_CHANNEL_IDS,
{
channel_id : existing_channel_id
}
));
}
}
return Promise.all(channelGraphNodeCleanupPromises);
});
} finally {
await session.close();
}
}
async function deleteStaleNodes(validNodes) {
const driver = getDB();
const session = driver.session();
try {
await session.writeTransaction(async tx => {
const existing_public_keys = await tx.run(
DB_QUERIES.ALL_NODE_PUBLIC_KEYS
);
let nodeGraphNodeCleanupPromises = [];
for (let i = 0; i < existing_public_keys.records.length; i++) {
const existing_public_key = existing_public_keys.records[0].get('public_key');
if (!validNodes.has(existing_public_key)) {
nodeGraphNodeCleanupPromises.push(tx.run(
DB_QUERIES.DELETE_NODES_BY_NODE_PUBLIC_KEYS,
{
public_key : existing_public_key
}
));
}
}
return Promise.all(nodeGraphNodeCleanupPromises);
});
} finally {
await session.close();
}
}
async function updateNodeInfo() { // update of node total capacity and channel counts.
const driver = getDB();
const session = driver.session();
try {
await session.writeTransaction(async tx => {
return tx.run(
DB_QUERIES.UPDATE_NODES_INFO
)
});
} finally {
await session.close();
}
}
async function processNodeInfo(key) {
let nodeDetail;
try {
const lnd = getLND();
nodeDetail = await getNode({lnd, public_key: key});
}
catch (e) {
logger.error('ERROR while getting total channel capacity for node key: ' + key + ', error: ' + e)
return;
}
logger.trace('Total channel capacity for node key: '+ key +', capacity: ' + nodeDetail.capacity +', + channel count: ' + nodeDetail.channel_count)
const driver = getDB();
const session = driver.session();
try {
await session.writeTransaction(async tx => {
return tx.run(
DB_QUERIES.UPDATE_NODE_INFO,
{
public_key : key,
capacity : neo4j.int(nodeDetail.capacity),
channel_count : neo4j.int(nodeDetail.channel_count)
}
)
});
} finally {
await session.close();
}
}
async function processLndGraphNotifications() {
const notifications = [...this.notifications];
this.notifications = [];
for (let i = 0; i < notifications.length; i++) {
await processLndGraphNotification(notifications[i], this); // awaiting to reduce load on DB. Improvement in queries might help.
}
}
async function processLndGraphNotification(notification, lndGraphToDBHandler) {
const driver = getDB();
const session = driver.session();
try {
await session.writeTransaction(async tx => {
const lnd = getLND();
if (notification.public_key) {
const nodeDetail = await getNode({lnd, public_key: notification.public_key});
await tx.run(
DB_QUERIES.UPDATE_NODE_NOTIFICATION, // updated_at is checked before updating the node.
{
public_key: notification.public_key,
alias: notification.alias,
color: notification.color,
sockets: notification.sockets != null ? notification.sockets : null,
capacity : neo4j.int(nodeDetail.capacity),
channel_count : neo4j.int(nodeDetail.channel_count),
updated_at: notification.updated_at
}
);
}
else if (notification.close_height) {
const public_keys = await tx.run(
DB_QUERIES.UPDATE_CHANNEL_CLOSE_NOTIFICATION, // removing relation between nodes and the channel and marking the channel as closed.
{
c_channel_id: notification.id,
c_close_height: neo4j.int(notification.close_height),
c_channel_point: (notification.transaction_id != null && notification.transaction_vout != null) ? (notification.transaction_id + ':' + notification.transaction_vout) : null,
c_capacity: notification.capacity != null ? neo4j.int(notification.capacity) : null,
c_updated_at: notification.updated_at
}
);
if (public_keys && public_keys.records
&& typeof public_keys.records.length !== 'undefined' && public_keys.records.length === 2) {
await Promise.all([processNodeInfo(public_keys.records[0].get('public_keys')),
processNodeInfo(public_keys.records[1].get('public_keys'))]);
}
} else {
const nodeDetails = await Promise.all([getNode({lnd, public_key: notification.public_keys[0]}),
getNode({lnd, public_key: notification.public_keys[1]})]);
await tx.run(
DB_QUERIES.UPDATE_CHANNEL_NOTIFICATION, // closed and updated_at are checked before updating the channel
{
n0_public_key: notification.public_keys[0],
n1_public_key: notification.public_keys[1],
n0_capacity : neo4j.int(nodeDetails[0].capacity),
n0_channel_count : neo4j.int(nodeDetails[0].channel_count),
n1_capacity : neo4j.int(nodeDetails[1].capacity),
n1_channel_count : neo4j.int(nodeDetails[1].channel_count),
c_channel_id: notification.id,
c_channel_point: (notification.transaction_id != null && notification.transaction_vout != null) ? (notification.transaction_id + ':' + notification.transaction_vout) : null,
c_capacity: (notification.capacity != null) ? neo4j.int(notification.capacity) : null,
c_updated_at: notification.updated_at,
r0_base_fee_mtokens: neo4j.int(notification.base_fee_mtokens),
r0_cltv_delta: neo4j.int(notification.cltv_delta),
r0_fee_rate: neo4j.int(notification.fee_rate),
r0_is_disabled: notification.is_disabled,
r0_max_htlc_mtokens: notification.max_htlc_mtokens != null ? neo4j.int(notification.max_htlc_mtokens) : null,
r0_min_htlc_mtokens: neo4j.int(notification.min_htlc_mtokens),
r0_updated_at: notification.updated_at
}
);
}
});
await session.close();
}
catch(e) {
try {
await session.close();
}
catch(se) {
logger.warn('Error while DB session close in error case of lightning graph notification processing: ' + se);
}
if (notification.lh_max_retry && notification.lh_max_retry === config.lh_max_retry) {
logger.fatal('CRITICAL ERROR Last retry failed to process the lightning graph notification.' + JSON.stringify(notification) + ', error: ' + e);
}
else {
logger.error('Failed to process lightning graph notification: ' + JSON.stringify(notification) + ', retry attempt: ' + notification.lh_max_retry + ', error: ' + e);
notification.lh_max_retry = notification.lh_max_retry ? notification.lh_max_retry + 1 : 1;
lndGraphToDBHandler.notifications.push(notification); // pushing failed notifications to be retried along with next notification.
}
}
}
module.exports = {
loadGraphToDB
};