Class: Pool

net~Pool(options)

Pool A pool of peers for handling all network activity.

Constructor

new Pool(options)

Create a pool.

Extends:
  • EventEmitter
Parameters:
Name Type Description
options Object
Source:

Extends

  • EventEmitter

Members

(static, constant) DISCOVERY_INTERVAL :Number

Discovery interval for UPNP and DNS seeds.

Type:
  • Number
Default Value:
  • 120000
Source:

Methods

(async, private) _addBlock(peer, block) → {Promise}

Attempt to add block to chain (without a lock).

Parameters:
Name Type Description
peer Peer
block Block
Source:
Returns:
Type
Promise

(async) _connect() → {Promise}

Connect to the network (no lock).

Source:
Returns:
Type
Promise

(async) _disconnect() → {Promise}

Disconnect from the network.

Source:
Returns:
Type
Promise

(async, private) _handleHeaders(peer, packet) → {Promise}

Handle headers packet from a given peer without a lock.

Parameters:
Name Type Description
peer Peer
packet HeadersPacket
Source:
Returns:
Type
Promise

(async, private) _handleInv(peer, packet)

Handle inv packet (without a lock).

Parameters:
Name Type Description
peer Peer
packet InvPacket
Source:

(async, private) _handleMerkleBlock(peer, block)

Handle merkleblock packet (without a lock).

Parameters:
Name Type Description
peer Peer
block MerkleBlockPacket
Source:

(async, private) _handleTX(peer, packet) → {Promise}

Handle a transaction. Attempt to add to mempool (without a lock).

Parameters:
Name Type Description
peer Peer
packet TXPacket
Source:
Returns:
Type
Promise

(async, private) addBlock(peer, block) → {Promise}

Attempt to add block to chain.

Parameters:
Name Type Description
peer Peer
block Block
Source:
Returns:
Type
Promise

(private) addInbound(socket)

Create an inbound peer from an existing socket.

Parameters:
Name Type Description
socket net.Socket
Source:

(private) addLoader()

Add a loader peer. Necessary for a sync to even begin.

Source:

(private) addOutbound()

Create an outbound non-loader peer. These primarily exist for transaction relaying.

Source:

announceBlock(blocks)

Announce a block to all peers.

Parameters:
Name Type Description
blocks Block | Array.<Blocks>
Source:

announceList(peer)

Announce broadcast list to peer.

Parameters:
Name Type Description
peer Peer
Source:

announceTX(txs)

Announce a transaction to all peers.

Parameters:
Name Type Description
txs TX | Array.<TX>
Source:

ban(addr)

Ban peer.

Parameters:
Name Type Description
addr NetAddress
Source:

(private) bindPeer(peer)

Bind to peer events.

Parameters:
Name Type Description
peer Peer
Source:

broadcast(msg) → {Promise}

Broadcast a transaction or block.

Parameters:
Name Type Description
msg TX | Block
Source:
Returns:
Type
Promise

(async) connect() → {Promise}

Connect to the network.

Source:
Returns:
Type
Promise

(private) createInbound(socket) → {Peer}

Accept an inbound socket.

Parameters:
Name Type Description
socket net.Socket
Source:
Returns:
Type
Peer

(private) createOutbound(addr) → {Peer}

Create an outbound peer with no special purpose.

Parameters:
Name Type Description
addr NetAddress
Source:
Returns:
Type
Peer

(async) disconnect() → {Promise}

Disconnect from the network.

Source:
Returns:
Type
Promise

(async) discover() → {Promise}

Rediscover seeds and internet gateway. Attempt to add port mapping once again.

Source:
Returns:
Type
Promise

(async) discoverExternal() → {Promise}

Attempt to discover external IP via DNS.

Source:
Returns:
Type
Promise

(async) discoverGateway() → {Promise}

Attempt to add port mapping (i.e. remote:8333->local:8333) via UPNP.

Source:
Returns:
Type
Promise

(async) discoverSeeds(checkPeers) → {Promise}

Attempt to resolve DNS seeds if necessary.

Parameters:
Name Type Description
checkPeers Boolean
Source:
Returns:
Type
Promise

ensureTX(peer, hashes)

Queue a getdata request to be sent. Check tx existence before requesting.

Parameters:
Name Type Description
peer Peer
hashes Array.<Hash>
Source:

(private) fillOutbound()

Attempt to refill the pool with peers (no lock).

Source:

forceSync()

Force sending of a sync to each peer.

Source:

getBlock(peer, hashes)

Queue a getdata request to be sent.

Parameters:
Name Type Description
peer Peer
hashes Array.<Hash>
Source:

(async) getBlocks(peer, tip, stopnullable) → {Promise}

Send getblocks to peer after building locator.

Parameters:
Name Type Attributes Description
peer Peer
tip Hash

Tip hash to build chain locator from.

stop Hash <nullable>
Source:
Returns:
Type
Promise

(private) getBroadcasted(peer, item) → {Promise}

Get a block/tx from the broadcast map.

Parameters:
Name Type Description
peer Peer
item InvItem
Source:
Returns:
Type
Promise

(async) getHeaders(peer, tip, stopnullable) → {Promise}

Send getheaders to peer after building locator.

Parameters:
Name Type Attributes Description
peer Peer
tip Hash

Tip to build chain locator from.

stop Hash <nullable>
Source:
Returns:
Type
Promise

getHost() → {NetAddress}

Allocate a host from the host list.

Source:
Returns:
Type
NetAddress

(async, private) getItem(peer, item) → {Promise}

Get a block/tx either from the broadcast map, mempool, or blockchain.

Parameters:
Name Type Description
peer Peer
item InvItem
Source:
Returns:
Type
Promise

(private) getNextTip(height) → {Object}

Find the next checkpoint.

Parameters:
Name Type Description
height Number
Source:
Returns:
Type
Object

getServiceNames() → {Array.<String>}

Returns human readable list of services that are available.

Source:
Returns:
Type
Array.<String>

getTX(peer, hashes)

Queue a getdata request to be sent.

Parameters:
Name Type Description
peer Peer
hashes Array.<Hash>
Source:

(async, private) handleAddr(peer, packet)

Handle peer addr event.

Parameters:
Name Type Description
peer Peer
packet AddrPacket
Source:

(private) handleBadOrphan(msg, err, id)

Handle bad orphan.

Parameters:
Name Type Description
msg String
err VerifyError
id Number
Source:

(async, private) handleBan(peer)

Handle ban event.

Parameters:
Name Type Description
peer Peer
Source:

(async, private) handleBlock(peer, packet) → {Promise}

Handle block packet. Attempt to add to chain.

Parameters:
Name Type Description
peer Peer
packet BlockPacket
Source:
Returns:
Type
Promise

(async, private) handleBlockInv(peer, hashes) → {Promise}

Handle inv packet from peer (containing only BLOCK types).

Parameters:
Name Type Description
peer Peer
hashes Array.<Hash>
Source:
Returns:
Type
Promise

(async, private) handleBlockTxn(peer, packet)

Handle blocktxn packet.

Parameters:
Name Type Description
peer Peer
packet BlockTxnPacket
Source:

(async, private) handleClose(peer, connected)

Handle peer close event.

Parameters:
Name Type Description
peer Peer
connected Boolean
Source:

(async, private) handleCmpctBlock(peer, packet)

Handle cmpctblock packet.

Parameters:
Name Type Description
peer Peer
packet CompactBlockPacket
Source:

(async, private) handleConnect(peer)

Handle peer connect event.

Parameters:
Name Type Description
peer Peer
Source:

(async, private) handleFeeFilter(peer, packet)

Handle sendcmpct packet.

Parameters:
Name Type Description
peer Peer
packet FeeFilterPacket
Source:

(async, private) handleFilterAdd(peer, packet)

Handle filteradd packet.

Parameters:
Name Type Description
peer Peer
packet FilterAddPacket
Source:

(async, private) handleFilterClear(peer, packet)

Handle filterclear packet.

Parameters:
Name Type Description
peer Peer
packet FilterClearPacket
Source:

(async, private) handleFilterLoad(peer, packet)

Handle filterload packet.

Parameters:
Name Type Description
peer Peer
packet FilterLoadPacket
Source:

(async, private) handleGetAddr(peer, packet)

Handle getaddr packet.

Parameters:
Name Type Description
peer Peer
packet GetAddrPacket
Source:

(async, private) handleGetBlocks(peer, packet)

Handle getblocks packet.

Parameters:
Name Type Description
peer Peer
packet GetBlocksPacket
Source:

(async, private) handleGetBlockTxn(peer, packet)

Handle getblocktxn packet.

Parameters:
Name Type Description
peer Peer
packet GetBlockTxnPacket
Source:

(async, private) handleGetData(peer, packet)

Handle getdata packet.

Parameters:
Name Type Description
peer Peer
packet GetDataPacket
Source:

(async, private) handleGetHeaders(peer, packet)

Handle getheaders packet.

Parameters:
Name Type Description
peer Peer
packet GetHeadersPacket
Source:

(async, private) handleHeaders(peer, packet) → {Promise}

Handle headers packet from a given peer.

Parameters:
Name Type Description
peer Peer
packet HeadersPacket
Source:
Returns:
Type
Promise

(async, private) handleInv(peer, packet)

Handle inv packet.

Parameters:
Name Type Description
peer Peer
packet InvPacket
Source:

(async, private) handleMempool(peer, packet)

Handle mempool packet.

Parameters:
Name Type Description
peer Peer
packet MempoolPacket
Source:

(async, private) handleMerkleBlock(peer, block)

Handle merkleblock packet.

Parameters:
Name Type Description
peer Peer
block MerkleBlockPacket
Source:

(async, private) handleNotFound(peer, packet)

Handle peer notfound packet.

Parameters:
Name Type Description
peer Peer
packet NotFoundPacket
Source:

(async, private) handleOpen(peer)

Handle peer open event.

Parameters:
Name Type Description
peer Peer
Source:

(async, private) handlePacket(peer, packet) → {Promise}

Handle peer packet event.

Parameters:
Name Type Description
peer Peer
packet Packet
Source:
Returns:
Type
Promise

(async, private) handlePing(peer, packet)

Handle ping packet.

Parameters:
Name Type Description
peer Peer
packet PingPacket
Source:

(async, private) handlePong(peer, packet)

Handle pong packet.

Parameters:
Name Type Description
peer Peer
packet PongPacket
Source:

(async, private) handleReject(peer, packet)

Handle peer reject event.

Parameters:
Name Type Description
peer Peer
packet RejectPacket
Source:

(async, private) handleSendCmpct(peer, packet)

Handle sendcmpct packet.

Parameters:
Name Type Description
peer Peer
packet SendCmpctPacket
Source:

(async, private) handleSendHeaders(peer, packet) → {Promise}

Handle sendheaders packet.

Parameters:
Name Type Description
peer Peer
packet SendHeadersPacket
Source:
Returns:
Type
Promise

(private) handleSocket(socket)

Handle incoming connection.

Parameters:
Name Type Description
socket net.Socket
Source:

(async, private) handleTX(peer, packet) → {Promise}

Handle a transaction. Attempt to add to mempool.

Parameters:
Name Type Description
peer Peer
packet TXPacket
Source:
Returns:
Type
Promise

(async, private) handleTXInv(peer, hashes)

Handle peer inv packet (txs).

Parameters:
Name Type Description
peer Peer
hashes Array.<Hash>
Source:

(async, private) handleUnknown(peer, packet)

Handle unknown packet.

Parameters:
Name Type Description
peer Peer
packet UnknownPacket
Source:

(async, private) handleVerack(peer, packet)

Handle verack packet.

Parameters:
Name Type Description
peer Peer
packet VerackPacket
Source:

(async, private) handleVersion(peer, packet)

Handle peer version event.

Parameters:
Name Type Description
peer Peer
packet VersionPacket
Source:

(async) hasBlock(hash) → {Promise}

Test whether the chain has or has seen an item.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Boolean.
Type
Promise

hasTX(hash) → {Boolean}

Test whether the mempool has or has seen an item.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

(private) init()

Initialize the pool.

Source:

isSyncable(peer) → {Boolean}

Test whether a peer is sync-worthy.

Parameters:
Name Type Description
peer Peer
Source:
Returns:
Type
Boolean

(async, private) listen() → {Promise}

Start listening on a server socket.

Source:
Returns:
Type
Promise

(private) logStatus(block)

Log sync status.

Parameters:
Name Type Description
block Block
Source:

(async) open() → {Promise}

Open the pool, wait for the chain to load.

Source:
Returns:
Type
Promise

queueFilterLoad()

Queue a resend of the bloom filter.

Source:

(private) refill()

Attempt to refill the pool with peers (no lock).

Source:

(private) removePeer(peer)

Remove a peer from any list. Drop all load requests.

Parameters:
Name Type Description
peer Peer
Source:

resetChain()

Reset header chain.

Source:

resolveBlock(peer, hash) → {Boolean}

Fulfill a requested block.

Parameters:
Name Type Description
peer Peer
hash Hash
Source:
Returns:
Type
Boolean

(async, private) resolveChain(peer, hash) → {Promise}

Resolve header chain.

Parameters:
Name Type Description
peer Peer
hash Hash
Source:
Returns:
Type
Promise

(private) resolveHeaders(peer)

Request current header chain blocks.

Parameters:
Name Type Description
peer Peer
Source:

resolveHeight(hash, height)

Update all peer heights by their best hash.

Parameters:
Name Type Description
hash Hash
height Number
Source:

resolveItem(peer, item) → {Boolean}

Fulfill a requested item.

Parameters:
Name Type Description
peer Peer
item InvItem
Source:
Returns:
Type
Boolean

(async) resolveOrphan(peer, orphan) → {Promise}

Send getblocks to peer after building locator and resolving orphan root.

Parameters:
Name Type Description
peer Peer
orphan Hash

Orphan hash to resolve.

Source:
Returns:
Type
Promise

resolveTX(peer, hash) → {Boolean}

Fulfill a requested tx.

Parameters:
Name Type Description
peer Peer
hash Hash
Source:
Returns:
Type
Boolean

(async, private) resync(forcenullable) → {Promise}

Send a sync to each peer.

Parameters:
Name Type Attributes Description
force Boolean <nullable>
Source:
Returns:
Type
Promise

(async, private) sendBlock(peer, item) → {Boolean}

Send a block from the broadcast list or chain.

Parameters:
Name Type Description
peer Peer
item InvItem
Source:
Returns:
Type
Boolean

sendFilterLoad()

Resend the bloom filter to peers.

Source:

sendGetAddr()

Send getaddr to all peers.

Source:

sendLocator(locator, peer) → {Boolean}

Send a chain locator and start syncing from peer.

Parameters:
Name Type Description
locator Array.<Hash>
peer Peer
Source:
Returns:
Type
Boolean

sendMempool()

Send mempool to all peers.

Source:

(async) sendSync(peer) → {Promise}

Start syncing from peer.

Parameters:
Name Type Description
peer Peer
Source:
Returns:
Type
Promise

setFilter(filter, encnullable)

Set the spv filter.

Parameters:
Name Type Attributes Description
filter BloomFilter
enc String <nullable>
Source:

(private) setLoader()

Add a loader peer. Necessary for a sync to even begin.

Source:

startSync()

Start the blockchain sync.

Source:

(private) startTimer()

Start discovery timer.

Source:

(private) stopSync()

Stop the sync.

Source:

(private) stopTimer()

Stop discovery timer.

Source:

(async, private) switchSync(peer, hash) → {Promise}

Switch to getblocks.

Parameters:
Name Type Description
peer Peer
hash Hash
Source:
Returns:
Type
Promise

sync()

Send a sync to each peer.

Source:

uid() → {Number}

Allocate new peer id.

Source:
Returns:
Type
Number

unban(addr)

Unban peer.

Parameters:
Name Type Description
addr NetAddress
Source:

(async, private) unlisten() → {Promise}

Stop listening on server socket.

Source:
Returns:
Type
Promise

unwatch()

Reset the spv filter (filterload, SPV-only).

Source:

watch(data, encnullable)

Watch a an address hash (filterload, SPV-only).

Parameters:
Name Type Attributes Description
data Buffer | Hash
enc String <nullable>
Source:

watchAddress(address)

Add an address to the bloom filter (SPV-only).

Parameters:
Name Type Description
address Address | AddressString
Source:

watchOutpoint(outpoint)

Add an outpoint to the bloom filter (SPV-only).

Parameters:
Name Type Description
outpoint Outpoint
Source: