@node-lightning/wire
Version:
Lightning Network Wire Protocol
120 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GossipPeer2 = exports.GossipManager2 = exports.PeerGossipState = void 0;
const InitFeatureFlags_1 = require("../flags/InitFeatureFlags");
const MessageType_1 = require("../MessageType");
const GossipManager_1 = require("./GossipManager");
const GossipQueriesSync_1 = require("./GossipQueriesSync");
class PeerGossipState {
}
exports.PeerGossipState = PeerGossipState;
class GossipManager2 {
constructor(logger, chainHash, filter) {
this.chainHash = chainHash;
this.filter = filter;
this.logger = logger.sub(GossipManager2.name);
this.peers = [];
this.syncState = GossipManager_1.SyncState.Unsynced;
}
start() {
// wait for chain monitor to sync
// rebroadcast from database, this should ultimately be refactored as well
}
onPeerReady(peer) {
const gossipPeer = new GossipPeer2(peer);
this.peers.push(gossipPeer);
// Add the peer to the relay manager
// this.gossipRelay.addPeer(gossipPeer);
// If we have not yet performed a full synchronization then we can
// perform the full gossip state restore from this node
if (this.syncState === GossipManager_1.SyncState.Unsynced) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._syncPeer(gossipPeer);
}
// If we've already synced, simply enable gossip receiving for the peer
// else {
// gossipPeer.enableGossip();
// }
}
async _syncPeer(peer) {
// Disable gossip relay
// this.gossipRelay.stop();
this.logger.trace("sync status now 'syncing'");
this.syncState = GossipManager_1.SyncState.Syncing;
try {
// perform synchronization
if (peer.gossipQueries) {
const chainHash = this.chainHash;
peer.syncTask = new GossipQueriesSync_1.GossipQueriesSync(chainHash, peer.peer, this.logger);
await peer.syncTask.queryRange();
}
// finally transition to sync complete status
this.logger.trace("sync status now 'synced'");
this.syncState = GossipManager_1.SyncState.Synced;
// enable gossip for all the peers
// this.logger.trace("enabling gossip for all peers");
// for (const gossipPeer of this.peers) {
// gossipPeer.enableGossip();
// }
}
catch (ex) {
// TODO select next peer
this.syncState = GossipManager_1.SyncState.Unsynced;
}
// Enable gossip relay now that sync is complete
// this.gossipRelay.start();
}
findPeer(peer) {
return this.peers.find(p => p.peer === peer);
}
async handlePeerMessage(peer, msg) {
// process inbound messages
if (msg.type === MessageType_1.MessageType.ChannelAnnouncement ||
msg.type === MessageType_1.MessageType.ChannelUpdate ||
msg.type === MessageType_1.MessageType.NodeAnnouncement) {
try {
const result = await this.filter.validateMessage(msg);
// TODO handle the result
if (result.isOk) {
this.logger.info(result.value);
}
else {
// Handled error should be emitted to the caller
// but we prevent the transform stream from
// stopping by calling the callback without an
// error.
}
// Unhandled error is something unexpected and our peer
// is now in a broken state and we need to disconnect.
}
catch (err) {
//
}
}
// process any sync task as well
const gossipPeer = this.findPeer(peer);
if (gossipPeer.syncTask) {
gossipPeer.syncTask.handleWireMessage(msg);
}
}
}
exports.GossipManager2 = GossipManager2;
class GossipPeer2 {
constructor(peer) {
this.peer = peer;
}
/**
* Returns true if the gossip_queries feature has been negotiated with the
* remote peer.
*/
get gossipQueries() {
return (this.peer.remoteFeatures.isSet(InitFeatureFlags_1.InitFeatureFlags.gossipQueriesOptional) ||
this.peer.remoteFeatures.isSet(InitFeatureFlags_1.InitFeatureFlags.gossipQueriesRequired));
}
}
exports.GossipPeer2 = GossipPeer2;
// export class PeerGossipRelayState {}
// export class PeerGossipRelayController {
// public start(): {};
// }
//# sourceMappingURL=GossipManager2.js.map