UNPKG

@node-lightning/wire

Version:
120 lines (119 loc) 4.31 kB
/// <reference types="node" /> import { BitField, ShortChannelId } from "@node-lightning/core"; import { OutPoint } from "@node-lightning/core"; import { ILogger } from "@node-lightning/logger"; import { EventEmitter } from "events"; import { IWireMessage } from "../messages/IWireMessage"; import { Peer } from "../Peer"; import { DnsPeerQuery } from "./DnsPeerQuery"; import { GossipPeer } from "./GossipPeer"; import { IGossipRelay } from "./GossipRelay"; import { IGossipStore } from "./GossipStore"; import { IGossipFilterChainClient } from "./IGossipFilterChainClient"; export declare enum SyncState { Unsynced = 0, Syncing = 1, Synced = 2 } export declare interface GossipManager { on(event: "message", fn: (msg: IWireMessage) => void): this; on(event: "error", fn: (err: Error) => void): this; on(event: "flushed", fn: () => void): this; off(event: "restored", fn: (block: number) => void): this; off(event: "message", fn: (msg: IWireMessage) => void): this; off(event: "error", fn: (err: Error) => void): this; off(event: "flushed", fn: () => void): this; } /** * GossipManager provides is a facade for many parts of gossip. It * orchestrates for validating, storing, and emitting * routing gossip traffic obtained by peers. */ export declare class GossipManager extends EventEmitter { readonly gossipStore: IGossipStore; readonly pendingStore: IGossipStore; readonly chainClient?: IGossipFilterChainClient; blockHeight: number; started: boolean; syncState: SyncState; isSynchronizing: boolean; gossipRelay: IGossipRelay; dnsPeerQuery: DnsPeerQuery; readonly peers: Set<GossipPeer>; readonly logger: ILogger; constructor(logger: ILogger, gossipStore: IGossipStore, pendingStore: IGossipStore, chainClient?: IGossipFilterChainClient); /** * The number of peers managed by the PeerManager */ get peerCount(): number; /** * Starts the gossip manager. This method will load information * from the gossip store, determine when the last information * was obtained, validate the existing messages (to see if any * channels have closed), and finally emit all messages that * exist in the system. */ start(): Promise<void>; /** * Adds a new peer to the GossipManager and subscribes to events that will * allow it to iteract with other sub-systems managed by the GossipManager. */ addPeer(peer: Peer): void; /** * Uses a dns seed to discover and add peers to be managed by the GossipManager. */ bootstrapPeers(ls: Buffer, localFeatures: BitField<any>, localChains: Buffer[], logger: ILogger, dnsSeed: string): Promise<void>; /** * Removes the channel from storage by the gossip manager. This * will likely be called by a chain-monitoring service. */ removeChannel(scid: ShortChannelId): Promise<void>; /** * Removes the channel from storage by the gossip manager. This will * likely be called by a chain-monitoring service. * @param outpoint */ removeChannelByOutpoint(outpoint: OutPoint): Promise<void>; /** * Retrieves the valid chan_ann, chan_update, node_ann messages * while making sure to not send duplicate node_ann messages. * * @remarks * For now we are going to buffer messages into memory. We could * return a stream and yield messages as they are streamed from * the gossip_store. */ allMessages(): AsyncGenerator<IWireMessage, void, unknown>; /** * Handles when a peer has been added to the manager and it is finally * ready and has negotiated the gossip technique. * @param peer */ private _onPeerReady; /** * Handles when a peer closes * @param gossipPeer */ private _onPeerClose; /** * Handles when a peer becomes readable * @param peer */ private _onPeerReadable; /** * Handles receieved gossip messages * @param msg */ private _onGossipMessage; /** * Handles Gossip Errors */ private _onGossipError; /** * Synchronize the peer using the peer's synchronization mechanism. * @param peer */ private _syncPeer; private _restoreState; private _validateUtxos; }