Pool

A pool maintains a connection of Peers. A pool will discover peers via DNS seeds, as well as when peer addresses are announced through the network.

The quickest way to get connected is to run the following:


var Pool = require('bitcore-p2p').Pool;
var Networks = require('bitcore-lib').Networks;

var pool = new Pool({network: Networks.livenet});

// connect to the network
pool.connect();

// attach peer events
pool.on('peerinv', function(peer, message) {
  // a new peer message has arrived
});

// will disconnect all peers
pool.disconnect()

For more information about Peer events please read the Peer documentation. Peer events are relayed to the pool, a peer event inv in the pool would be peerinv. When a peer is disconnected the pool will try to connect to the list of known addresses to maintain connection.

Trusted Peers

By default, peers will be added via DNS discovery and as peers are announced in the network. Configuration options can be included to connect only to specific trusted peers:


var pool = new Pool({
  network: Networks.livenet, // the network object
  dnsSeed: false, // prevent seeding with DNS discovered known peers upon connecting
  listenAddr: false, // prevent new peers being added from addr messages
  addrs: [ // initial peers to connect to
    {
      ip: {
        v4: '127.0.0.1'
      }
    }
  ]
});

pool.connect();

Listening for Peers

It's also possible to listen to incoming socket connections to add peers to the pool. To enable this capability, you can do the following:

var pool = new Pool({network: Networks.livenet});
pool.listen();

When there are incoming connections the peer will be added to the pool.

Pool

Kind: global class

new Pool([options])

A pool is a collection of Peers. A pool will discover peers from DNS seeds, and collect information about new peers in the network. When a peer disconnects the pool will connect to others that are available to maintain a max number of ongoing peer connections. Peer events are relayed to the pool.

Param Type Description
[options] Object
[options.network] Network The network configuration
[options.listenAddr] Boolean Prevent new peers being added from addr messages
[options.dnsSeed] Boolean Prevent seeding with DNS discovered known peers
[options.relay] Boolean Prevent inventory announcements until a filter is loaded
[options.maxSize] Number The max number of peers

Example


var pool = new Pool({network: Networks.livenet});
pool.on('peerinv', function(peer, message) {
  // do something with the inventory announcement
});
pool.connect();

pool.connect()

Will initiate connection to peers, if available peers have been added to the pool, it will connect to those, otherwise will use DNS seeds to find peers to connect. When a peer disconnects it will add another.

Kind: instance method of Pool

pool.disconnect()

Will disconnect all peers that are connected.

Kind: instance method of Pool

pool.numberConnected() ⇒ Number

Kind: instance method of Pool
Returns: Number - The number of peers currently connected.

pool._fillConnections()

Will fill the connected peers to the maximum amount.

Kind: instance method of Pool

pool._removeConnectedPeer(addr)

Will remove a peer from the list of connected peers.

Kind: instance method of Pool

Param Type Description
addr Object An addr from the list of addrs

pool._connectPeer(addr)

Will connect a peer and add to the list of connected peers.

Kind: instance method of Pool

Param Type Description
addr Object An addr from the list of addrs

pool._addConnectedPeer(socket, addr)

Adds a peer with a connected socket to the _connectedPeers object, and initializes the associated event handlers.

Kind: instance method of Pool

Param Type Description
socket Socket socket - A new connected socket
addr Object addr - The associated addr object for the peer

pool._addPeerEventHandlers()

Will add disconnect and ready events for a peer and intialize handlers for relay peer message events.

Kind: instance method of Pool

pool._deprioritizeAddr(addr)

Will deprioritize an addr in the list of addrs by moving it to the end of the array, and setting a retryTime

Kind: instance method of Pool

Param Type Description
addr Object An addr from the list of addrs

pool._addAddr(addr)

Will add an addr to the beginning of the addrs array

Kind: instance method of Pool

Param Type
addr Object

pool._addAddrsFromSeed(seed, done)

Will add addrs to the list of addrs from a DNS seed

Kind: instance method of Pool

Param Type Description
seed String A domain name to resolve known peers
done function

pool._addAddrsFromSeeds(done)

Will add addrs to the list of addrs from network DNS seeds

Kind: instance method of Pool

Param Type
done function

pool.inspect() ⇒ String

Kind: instance method of Pool
Returns: String - A string formatted for the console

pool.sendMessage(message)

Will send a message to all of the peers in the pool.

Kind: instance method of Pool

Param Type Description
message Message An instance of the message to send

pool.listen()

Will enable a listener for peer connections, when a peer connects it will be added to the pool.

Kind: instance method of Pool