Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Pool

Represents a pool of peers that handles all p2p network activity. This tracks all active and pending peers, optionally runs a server to listen for incoming connections, and is the primary interface for other modules to interact with the p2p layer.

Hierarchy

  • EventEmitter
    • Pool

Index

Constructors

constructor

  • Parameters

    • __namedParameters: { config: { addresses: string[]; detectexternalip: boolean; discover: boolean; discoverminutes: number; listen: boolean; port: number; tor: boolean; torport: number }; logger: Logger; models: { Currency: ModelCtor<CurrencyInstance>; Node: ModelCtor<NodeInstance>; Order: ModelCtor<OrderInstance>; Pair: ModelCtor<PairInstance>; ReputationEvent: ModelCtor<ReputationEventInstance>; SwapDeal: ModelCtor<SwapDealInstance>; Trade: ModelCtor<TradeInstance> }; nodeKey: NodeKey; strict: boolean; version: string; xuNetwork: XuNetwork }
      • config: { addresses: string[]; detectexternalip: boolean; discover: boolean; discoverminutes: number; listen: boolean; port: number; tor: boolean; torport: number }
        • addresses: string[]

          An array of IP addresses or host names which can be used to connect to this server. It will be advertised with peers for them to try to connect to the server in the future.

        • detectexternalip: boolean

          Whether or not to automatically detect and share current external ip address on startup.

        • discover: boolean

          Whether to send a GetNodes packet to discover new nodes upon connecting to peers, defaults to true.

        • discoverminutes: number

          Time interval between sending GetNodes packets to already connected peers. Measured in minutes, only applies if discover option is true.

        • listen: boolean

          Whether or not to listen for incoming connections from peers.

        • port: number

          Which port to listen on. If 0, a random unused port will be used.

        • tor: boolean

          Whether to allow connections to tor nodes.

        • torport: number

          The port that Tor's exposed SOCKS5 proxy is listening on.

      • logger: Logger
      • models: { Currency: ModelCtor<CurrencyInstance>; Node: ModelCtor<NodeInstance>; Order: ModelCtor<OrderInstance>; Pair: ModelCtor<PairInstance>; ReputationEvent: ModelCtor<ReputationEventInstance>; SwapDeal: ModelCtor<SwapDealInstance>; Trade: ModelCtor<TradeInstance> }
      • nodeKey: NodeKey
      • strict: boolean
      • version: string
      • xuNetwork: XuNetwork

    Returns Pool

Properties

alias

alias: string

Our alias.

Private config

config: PoolConfig

Points to config comes during construction.

Private connected

connected: boolean = false

Private disconnecting

disconnecting: boolean = false

Private Optional listenPort

listenPort: undefined | number

The port on which to listen for peer connections, undefined if this node is not listening.

Private Optional loadingNodesPromise

loadingNodesPromise: Promise<void>

Private logger

logger: Logger

Private network

network: Network

Private nodeKey

nodeKey: NodeKey

nodePubKey

nodePubKey: string

Our node pub key.

Private nodeState

nodeState: NodeState

The local handshake data to be sent to newly connected peers.

Private nodes

nodes: NodeList

A collection of known nodes on the XU network.

Private peers

peers: Map<string, Peer> = new Map<string, Peer>()

A collection of opened, active peers.

Private pendingInboundPeers

pendingInboundPeers: Set<Peer> = new Set<Peer>()

A set of peers for which we have pending incoming connections.

Private pendingOutboundPeers

pendingOutboundPeers: Map<string, Peer> = new Map<string, Peer>()

A map of pub keys to nodes for which we have pending outgoing connections.

Private repository

repository: P2PRepository

Private Optional server

server: Server

Private strict

strict: boolean

version

version: string

The version of xud we are using.

Static defaultMaxListeners

defaultMaxListeners: number

Static Readonly errorMonitor

errorMonitor: keyof symbol

This symbol shall be used to install a listener for only monitoring 'error' events. Listeners installed using this symbol are called before the regular 'error' listeners are called.

Installing a listener using this symbol does not change the behavior once an 'error' event is emitted, therefore the process will still crash if no regular 'error' listener is installed.

Accessors

addresses

  • get addresses(): { host: string; lastConnected?: undefined | number; port: number }[]
  • Returns { host: string; lastConnected?: undefined | number; port: number }[]

peerCount

  • get peerCount(): number

Methods

Private addInbound

  • addInbound(socket: Socket): Promise<void>

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

addOutbound

  • addOutbound(address: Address, nodePubKey: string, retryConnecting: boolean, revokeConnectionRetries: boolean): Promise<Peer>
  • Attempt to add an outbound peer by connecting to a given socket address and nodePubKey. Throws an error if a socket connection to or the handshake with the node fails for any reason.

    Parameters

    • address: Address

      the socket address of the node to connect to

    • nodePubKey: string

      the nodePubKey of the node to connect to

    • retryConnecting: boolean
    • revokeConnectionRetries: boolean

    Returns Promise<Peer>

    a promise that resolves to the connected and opened peer

addReputationEvent

  • addReputationEvent(nodePubKey: string, event: ReputationEvent): Promise<boolean>

Private addressIsSelf

  • addressIsSelf(address: Address): boolean

banNode

  • banNode(nodePubKey: string): Promise<void>

Private bindNodeList

  • bindNodeList(): void

Private bindPeer

  • bindPeer(peer: Peer): void

Private bindServer

  • bindServer(): void

broadcastOrder

broadcastOrderInvalidation

  • broadcastOrderInvalidation(__namedParameters: { id: string; pairId: string; quantity: number }, nodeToExclude?: undefined | string): void
  • Broadcasts an OrderInvalidationPacket to all currently connected peers.

    Parameters

    • __namedParameters: { id: string; pairId: string; quantity: number }
      • id: string
      • pairId: string
      • quantity: number
    • Optional nodeToExclude: undefined | string

      the node pub key of a node to exclude from the packet broadcast

    Returns void

closePeer

  • closePeer(nodePubKey: string, reason?: DisconnectionReason, reasonPayload?: undefined | string): Promise<void>

Private closePeers

  • closePeers(): Promise<void[]>

Private closePendingConnections

  • closePendingConnections(): Promise<void[]>

Private connectNodes

  • Iterate over a collection of nodes and attempt to connect to them. If the node is banned, already connected, or has no listening addresses, then do nothing. Additionally, if we're already trying to connect to a given node also do nothing.

    Parameters

    • nodes: NodeConnectionIterator

      a collection of nodes with a forEach iterator to attempt to connect to

    • Default value allowKnown: boolean = true

      whether to allow connecting to nodes we are already aware of, defaults to true

    • Default value retryConnecting: boolean = false

      whether to attempt retry connecting, defaults to false

    Returns Promise<void>

    a promise that will resolve when all outbound connections resolve

Private detectExternalIpAddress

  • detectExternalIpAddress(): Promise<void>

disconnect

  • disconnect(): Promise<void>

discoverNodes

  • discoverNodes(peerPubKey: string): Promise<number>

emit

  • emit(event: "packet.order", order: IncomingOrder): boolean
  • emit(event: "packet.getOrders", peer: Peer, reqId: string, pairIds: string[]): boolean
  • emit(event: "packet.orderInvalidation", orderInvalidation: OrderPortion, peer: string): boolean
  • emit(event: "peer.active", peerPubKey: string): boolean
  • emit(event: "peer.close", peerPubKey?: undefined | string): boolean
  • emit(event: "peer.verifyPairs", peer: Peer): boolean
  • emit(event: "peer.pairDropped", peerPubKey: string, pairId: string): boolean
  • emit(event: "peer.nodeStateUpdate", peer: Peer): boolean
  • emit(event: "packet.sanitySwapInit", packet: SanitySwapInitPacket, peer: Peer): boolean
  • emit(event: "packet.swapRequest", packet: SwapRequestPacket, peer: Peer): boolean
  • emit(event: "packet.swapAccepted", packet: SwapAcceptedPacket, peer: Peer): boolean
  • emit(event: "packet.swapFailed", packet: SwapFailedPacket): boolean
  • Parameters

    Returns boolean

  • Parameters

    • event: "packet.getOrders"
    • peer: Peer
    • reqId: string
    • pairIds: string[]

    Returns boolean

  • Parameters

    • event: "packet.orderInvalidation"
    • orderInvalidation: OrderPortion
    • peer: string

    Returns boolean

  • Parameters

    • event: "peer.active"
    • peerPubKey: string

    Returns boolean

  • Parameters

    • event: "peer.close"
    • Optional peerPubKey: undefined | string

    Returns boolean

  • Notifies listeners that a peer's advertised but inactive pairs should be verified.

    Parameters

    • event: "peer.verifyPairs"
    • peer: Peer

    Returns boolean

  • Notifies listeners that a previously active pair was dropped by the peer or deactivated.

    Parameters

    • event: "peer.pairDropped"
    • peerPubKey: string
    • pairId: string

    Returns boolean

  • Parameters

    • event: "peer.nodeStateUpdate"
    • peer: Peer

    Returns boolean

  • Parameters

    Returns boolean

  • Parameters

    Returns boolean

  • Parameters

    Returns boolean

  • Parameters

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

getMaxListeners

  • getMaxListeners(): number

getNetwork

getNodeAlias

  • getNodeAlias(nodePubKey: string): undefined | string

getNodeId

  • getNodeId(nodePubKey: string): undefined | number

getNodePubKeyById

  • getNodePubKeyById(nodeId: number): undefined | string

getNodeReputation

  • Gets a node's reputation score and whether it is banned

    Parameters

    • nodePubKey: string

      The node pub key of the node for which to get reputation information

    Returns Promise<NodeReputationInfo>

    true if the specified node exists and the event was added, false otherwise

getPeer

  • getPeer(peerPubKey: string): Peer
  • Gets a peer by its node pub key or alias. Throws a NOT_CONNECTED error if the supplied identifier does not match any currently connected peer.

    Parameters

    • peerPubKey: string

    Returns Peer

getTokenIdentifier

  • getTokenIdentifier(currency: string): undefined | string

Private handleGetNodes

  • handleGetNodes(peer: Peer, reqId: string): Promise<void>

Private handleOpenedPeer

  • handleOpenedPeer(peer: Peer): Promise<void>

Private handlePacket

  • handlePacket(peer: Peer, packet: Packet): Promise<void>

Private handlePeerClose

  • handlePeerClose(peer: Peer): Promise<void>

Private handleSocket

  • handleSocket(socket: Socket): Promise<void>

init

  • init(): Promise<void>
  • Initialize the Pool by connecting to known nodes and listening to incoming peer connections, if configured to do so.

    Returns Promise<void>

listPeers

Private listen

  • listen(): Promise<void>
  • Starts listening for incoming p2p connections on the configured host and port. If this.listenPort is 0 or undefined, a random available port is used and will be assigned to this.listenPort.

    Returns Promise<void>

    a promise that resolves once the server is listening, or rejects if it fails to listen

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: "packet.order", listener: (order: IncomingOrder) => void): this
  • on(event: "packet.getOrders", listener: (peer: Peer, reqId: string, pairIds: string[]) => void): this
  • on(event: "packet.orderInvalidation", listener: (orderInvalidation: OrderPortion, peer: string) => void): this
  • on(event: "peer.active", listener: (peerPubKey: string) => void): this
  • on(event: "peer.close", listener: (peerPubKey?: undefined | string) => void): this
  • on(event: "peer.verifyPairs", listener: (peer: Peer) => void): this
  • on(event: "peer.pairDropped", listener: (peerPubKey: string, pairId: string) => void): this
  • on(event: "peer.nodeStateUpdate", listener: (peer: Peer) => void): this
  • on(event: "packet.sanitySwapInit", listener: (packet: SanitySwapInitPacket, peer: Peer) => void): this
  • on(event: "packet.swapRequest", listener: (packet: SwapRequestPacket, peer: Peer) => void): this
  • on(event: "packet.swapAccepted", listener: (packet: SwapAcceptedPacket, peer: Peer) => void): this
  • on(event: "packet.swapFailed", listener: (packet: SwapFailedPacket) => void): this

once

  • once(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

Private openPeer

  • openPeer(peer: Peer, expectedNodePubKey?: undefined | string, retryConnecting?: boolean): Promise<void>
  • Opens a connection to a peer and performs a routine for newly opened peers that includes requesting open orders and updating the database with the peer's information.

    Parameters

    • peer: Peer
    • Optional expectedNodePubKey: undefined | string
    • Default value retryConnecting: boolean = false

    Returns Promise<void>

    a promise that resolves once the connection has opened and the newly opened peer routine is complete

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

resolveAlias

  • resolveAlias(alias: string): string
  • Resolves an alias to a known node's public key. Throws an error if a unique pub key cannot be found for the provided alias.

    Parameters

    • alias: string

    Returns string

Private sendNodeStateUpdate

  • sendNodeStateUpdate(): void

sendToPeer

  • sendToPeer(nodePubKey: string, packet: Packet): Promise<void>

setMaxListeners

  • setMaxListeners(n: number): this
  • Parameters

    • n: number

    Returns this

Private tryConnectNode

  • Attempt to create an outbound connection to a node using its known listening addresses.

    Parameters

    Returns Promise<void>

Private tryConnectWithAdvertisedAddresses

Private tryConnectWithLastAddress

  • tryConnectWithLastAddress(node: NodeConnectionInfo, retryConnecting?: boolean): Promise<boolean>

tryGetPeer

  • tryGetPeer(peerPubKey: string): undefined | Peer

Private tryOpenPeer

  • tryOpenPeer(peer: Peer, peerPubKey?: undefined | string, retryConnecting?: boolean): Promise<void>
  • Parameters

    • peer: Peer
    • Optional peerPubKey: undefined | string
    • Default value retryConnecting: boolean = false

    Returns Promise<void>

unbanNode

  • unbanNode(nodePubKey: string, reconnect: boolean): Promise<void>
  • Parameters

    • nodePubKey: string
    • reconnect: boolean

    Returns Promise<void>

Private unlisten

  • unlisten(): Promise<void>
  • Stops listening for incoming p2p connections.

    Returns Promise<void>

    a promise that resolves once the server is no longer listening

updateConnextState

  • updateConnextState(tokenAddresses: Map<string, string>, pubKey?: undefined | string): void
  • Updates our connext public key and supported token addresses, then sends a node state update packet to currently connected peers to notify them of the change.

    Parameters

    • tokenAddresses: Map<string, string>
    • Optional pubKey: undefined | string

    Returns void

updateLndState

  • updateLndState(__namedParameters: { chain: undefined | string; currency: string; pubKey: string; uris: undefined | string[] }): void
  • Updates our lnd pub key and chain identifier for a given currency and sends a node state update packet to currently connected peers to notify them of the change.

    Parameters

    • __namedParameters: { chain: undefined | string; currency: string; pubKey: string; uris: undefined | string[] }
      • chain: undefined | string
      • currency: string
      • pubKey: string
      • uris: undefined | string[]

    Returns void

updatePairs

  • updatePairs(pairIds: string[]): void
  • Updates our active trading pairs and sends a node state update packet to currently connected peers to notify them of the change.

    Parameters

    • pairIds: string[]

    Returns void

updateRaidenState

  • updateRaidenState(tokenAddresses: Map<string, string>, raidenAddress?: undefined | string): void
  • Updates our raiden address and supported token addresses, then sends a node state update packet to currently connected peers to notify them of the change.

    Parameters

    • tokenAddresses: Map<string, string>
    • Optional raidenAddress: undefined | string

    Returns void

Private validatePeer

  • validatePeer(peer: Peer): Promise<void>
  • Validates a peer. If a check fails, closes the peer and throws a p2p error.

    Parameters

    Returns Promise<void>

Private verifyReachability

  • verifyReachability(): void

Static listenerCount

  • listenerCount(emitter: EventEmitter, event: string | symbol): number
  • deprecated

    since v4.0.0

    Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

Generated using TypeDoc