UNPKG

@node-lightning/wire

Version:
138 lines (137 loc) 3.84 kB
import { ILogger } from "@node-lightning/logger"; import { IWireMessage } from "../messages/IWireMessage"; import { IPeer } from "../Peer"; /** * Interface for the sub-system for handling the gossip message relay, * also known as rumor mongering.This system is responsible for * periodically flushing messages to connected peers and makes a best * effort to not send message that have already been sent to a peer. * * The idea of rumor mongering is that a piece of information is hot. A * node attempts to infect connected peers with this information by * sending it to them. Once it has been sent, we no longer need to * infect them with information. */ export interface IGossipRelay { /** * The current state of gossip relay */ state: GossipRelayState; /** * Starts gossip relay */ start(): void; /** * Stops gossip relay */ stop(): void; /** * Adds a new peer to relay messages to * @param peer */ addPeer(peer: IPeer): void; /** * Removes the peer from relay * @param peer */ removePeer(peer: IPeer): void; /** * Enqueues a message to be broadcast to peers * @param msg */ enqueue(msg: IWireMessage): void; } /** * The state of a IGossipRelay rumor mongerer */ export declare enum GossipRelayState { /** * Rumor mongering is not active */ Inactive = 0, /** * Rumor mongering is active */ Active = 1 } /** * This is a basic implementation of IGossipRelay that enques all * messages and maintaining an index of each peer in the queue. When * messages are flushed, only messages that haven't been sent to a peer * are sent and the index position for that peer is updated. When the * queue of messages has reached a maximum length, older messages are * pruned and the index positions are updated. */ export declare class GossipRelay implements IGossipRelay { readonly logger: ILogger; readonly relayPeriodMs: number; readonly maxQueueLen: number; private _queue; private _peers; private _timer; private _state; constructor(logger: ILogger, relayPeriodMs?: number, maxQueueLen?: number); /** * Gets the current state of gossip relay */ get state(): GossipRelayState; /** * Starts relay to peers. This enables messages to be enqueued and * periodically sent to the peers. */ start(): void; /** * Stops relay to peers. */ stop(): void; /** * Adds a new peer to relay messages to * @param peer */ addPeer(peer: IPeer): void; /** * Removes the peer from relay * @param peer */ removePeer(peer: IPeer): void; /** * Enqueues a message to be broadcast to peers. * @param msg */ enqueue(msg: IWireMessage): void; /** * Finds a channel_announcement message based on the short_channel_id * @param scid */ private _findChanAnn; /** * Finds a channel_update message based on the short_channel_id and * direction. The found message can then be compared to an inbound * message to determine if the new message is newer. * @param scid * @param direction */ private _findChanUpd; /** * Finds a node_announcement message based on the node_id. The * returned message can be compared to newer messages using the * timestamp. * @param nodeId */ private _findNodeAnn; /** * Fires when the timer ticks and will flush messages to peers and * prune the queue */ private _onTimer; /** * Flushes message to a peer based on the index of messages that * the peer has received. * @param peer */ private _flushToPeer; /** * Prunes excess message */ private _pruneQueue; }