@node-lightning/wire
Version:
Lightning Network Wire Protocol
111 lines • 4.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GossipMemoryStore = void 0;
const core_1 = require("@node-lightning/core");
const ExtendedChannelAnnouncementMessage_1 = require("../messages/ExtendedChannelAnnouncementMessage");
/**
* In-memory implementation of the IGossipStore.
*/
class GossipMemoryStore {
constructor() {
this._channelAnn = new Map();
this._channelByOutPoint = new Map();
this._channelUpd = new Map();
this._nodeAnn = new Map();
this._nodeChannels = new Map();
}
get channelAnnouncementCount() {
return this._channelAnn.size;
}
get channelUpdateCount() {
return this._channelUpd.size;
}
get nodeAnnouncementCount() {
return this._nodeAnn.size;
}
async saveChannelAnnouncement(msg) {
const chanKey = getChanKey(msg.shortChannelId);
this._channelAnn.set(chanKey, msg);
if (msg instanceof ExtendedChannelAnnouncementMessage_1.ExtendedChannelAnnouncementMessage) {
this._channelByOutPoint.set(msg.outpoint.toString(), msg.shortChannelId.toNumber());
}
await this._saveNodeChannel(msg.nodeId1, chanKey);
await this._saveNodeChannel(msg.nodeId2, chanKey);
}
async saveChannelUpdate(msg) {
this._channelUpd.set(getChanUpdKey(getChanKey(msg.shortChannelId), msg.direction), msg);
}
async saveNodeAnnouncement(msg) {
this._nodeAnn.set(getNodeKey(msg.nodeId), msg);
}
async findChannelsForNode(nodeId) {
const scidInts = this._nodeChannels.get(getNodeKey(nodeId));
const results = [];
if (!scidInts)
return results;
for (const scidInt of scidInts) {
results.push(core_1.shortChannelIdFromNumber(scidInt));
}
return results;
}
async findNodeAnnouncement(nodeId) {
return this._nodeAnn.get(getNodeKey(nodeId));
}
async findNodeAnnouncements() {
return Array.from(this._nodeAnn.values());
}
async findChannelAnnouncemnts() {
return Array.from(this._channelAnn.values());
}
async findChannelAnnouncement(scid) {
return this._channelAnn.get(getChanKey(scid));
}
async findChannelAnnouncementByOutpoint(outpoint) {
const scidNum = this._channelByOutPoint.get(outpoint.toString());
return this._channelAnn.get(scidNum);
}
async findChannelUpdate(scid, dir) {
return this._channelUpd.get(getChanUpdKey(getChanKey(scid), dir));
}
async deleteChannelAnnouncement(scid) {
const msg = await this.findChannelAnnouncement(scid);
if (!msg)
return;
const chanKey = getChanKey(scid);
// delete channel
this._channelAnn.delete(getChanKey(scid));
// delete outpoint link
if (msg instanceof ExtendedChannelAnnouncementMessage_1.ExtendedChannelAnnouncementMessage) {
this._channelByOutPoint.delete(msg.outpoint.toString());
}
// delete channel_updates
this._channelUpd.delete(getChanUpdKey(scid.toNumber(), 0));
this._channelUpd.delete(getChanUpdKey(scid.toNumber(), 0));
// delete node links
this._nodeChannels.get(getNodeKey(msg.nodeId1)).delete(chanKey);
this._nodeChannels.get(getNodeKey(msg.nodeId2)).delete(chanKey);
}
async deleteChannelUpdate(scid, dir) {
this._channelUpd.delete(getChanUpdKey(getChanKey(scid), dir));
}
async deleteNodeAnnouncement(nodeId) {
this._nodeAnn.delete(getNodeKey(nodeId));
this._nodeChannels.get(getNodeKey(nodeId));
}
async _saveNodeChannel(nodeId, chanKey) {
const nodeKey = getNodeKey(nodeId);
this._nodeChannels.set(nodeKey, this._nodeChannels.get(nodeKey) || new Set());
this._nodeChannels.get(nodeKey).add(chanKey);
}
}
exports.GossipMemoryStore = GossipMemoryStore;
function getNodeKey(nodeId) {
return nodeId.toString("hex");
}
function getChanKey(scid) {
return scid.toNumber();
}
function getChanUpdKey(chanKey, direction) {
return (chanKey << BigInt(8)) | BigInt(direction);
}
//# sourceMappingURL=GossipMemoryStore.js.map