UNPKG

@node-lightning/graph

Version:
75 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Graph = void 0; const graph_error_1 = require("./graph-error"); /** * Graph represents a */ class Graph { constructor() { /** * Map containing all nodes in the system */ this.nodes = new Map(); /** * Map containing all channels in the graph */ this.channels = new Map(); /** * The height the graph has been synced through */ this.syncHeight = 0; } /** * Adds a node to the graph */ addNode(node) { this.nodes.set(node.nodeId.toString("hex"), node); } /** * Adds a channgel to the graph */ addChannel(channel) { const node1 = this.getNode(channel.nodeId1); const node2 = this.getNode(channel.nodeId2); if (!node1) throw new graph_error_1.NodeNotFoundError(channel.nodeId1); if (!node2) throw new graph_error_1.NodeNotFoundError(channel.nodeId2); // attach channel const key = channel.shortChannelId.toNumber(); this.channels.set(key, channel); // attach channel to node 1 node1.linkChannel(channel); // attach channel to node 2 node2.linkChannel(channel); } /** * Gets a node in the graph */ getNode(nodeId) { return this.nodes.get(nodeId.toString("hex")); } /** * Gets a node in the channel by shortChannelId */ getChannel(shortChannelId) { return this.channels.get(shortChannelId.toNumber()); } /** * Removes the node from the graph */ removeChannel(channel) { const key = channel.shortChannelId.toNumber(); const n1 = this.getNode(channel.nodeId1); const n2 = this.getNode(channel.nodeId2); // remove from channels list this.channels.delete(key); // detach from node 1 n1.unlinkChannel(channel); // detach from node 2 n2.unlinkChannel(channel); } } exports.Graph = Graph; //# sourceMappingURL=graph.js.map