Skip to content

Instantly share code, notes, and snippets.

@mutatrum
Created March 5, 2021 13:07
Show Gist options
  • Save mutatrum/f873e8171267c6b9920ffe2ff4176957 to your computer and use it in GitHub Desktop.
Save mutatrum/f873e8171267c6b9920ffe2ff4176957 to your computer and use it in GitHub Desktop.
Find depth of BOS nodes in LND node graph
const fs = require('fs');
// Fill in your own node alias
const NAME = '<nodealias>';
console.log('start');
// lncli describegraph > graph.json
var graph = JSON.parse(fs.readFileSync('graph.json'));
// https://bos.lightning.jorijn.com/data/export.json
var bosnodes = JSON.parse(fs.readFileSync('export.json'));
console.log(`nodes: ${graph.nodes.length}`);
console.log(`edges: ${graph.edges.length}`);
var pub_key;
for (var node of graph.nodes) {
if (node.alias == NAME) {
pub_key = node.pub_key;
}
}
if (!pub_key) {
console.log(`${NAME} not found`);
return;
}
console.log(`${NAME}: ${pub_key}`);
var known_nodes = [];
known_nodes.push(pub_key);
var level_nodes = [];
var level = 0;
do {
level++;
level_nodes[level] = [];
for (var edge of graph.edges) {
var new_node;
var has1 = known_nodes.includes(edge.node1_pub);
var has2 = known_nodes.includes(edge.node2_pub);
if (has1) {
if (has2) {
continue;
} else {
new_node = edge.node2_pub;
}
} else {
if (has2) {
new_node = edge.node1_pub;
} else {
continue;
}
}
if (new_node) {
if (!level_nodes[level].includes(new_node)) {
level_nodes[level].push(new_node);
for (var bos of bosnodes.data) {
if (new_node === bos.publicKey) {
console.log(`distance ${level}: ${bos.publicKey} (${bos.alias})`);
}
}
// print all nodes > 5 hops away
// if (level >= 5) {
// console.log(`distance ${level}: ${new_node}`);
// }
}
}
}
if (level_nodes[level].length > 0) {
console.log(`distance ${level}: ${level_nodes[level].length}`);
}
known_nodes = known_nodes.concat(level_nodes[level]);
} while (level_nodes[level].length > 0)
console.log('done');
@mutatrum
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment