Class: Peer

net.Peer(options)

Represents a network peer.

Constructor

new Peer(options)

Create a peer.

Extends:
  • EventEmitter
Parameters:
Name Type Description
options PeerOptions | PoolOptions
Properties:
Name Type Attributes Description
socket net.Socket
address NetAddress
parser Parser
framer Framer
version Number
destroyed Boolean
ack Boolean

Whether verack has been received.

connected Boolean
time Number
preferHeaders Boolean

Whether the peer has requested getheaders.

hashContinue Hash <nullable>

The block hash at which to continue the sync for the peer.

spvFilter Bloom <nullable>

The peer's bloom spvFilter.

noRelay Boolean

Whether to relay transactions immediately to the peer.

challenge BN

Local nonce.

lastPong Number

Timestamp for last pong received (unix time).

lastPing Number

Timestamp for last ping sent (unix time).

minPing Number

Lowest ping time seen.

banScore Number
Source:

Extends

  • EventEmitter

Members

(static, constant) BLOCK_TIMEOUT :Number

Required time for loader to respond with block/merkleblock.

Type:
  • Number
Default Value:
  • 120000
Source:

(static, constant) DRAIN_MAX :Number

Max output bytes buffered before invoking stall behavior for peer.

Type:
  • Number
Source:

(static, constant) INV_INTERVAL :Number

Interval to flush invs. Higher means more invs (usually txs) will be accumulated before flushing.

Type:
  • Number
Default Value:
  • 5000
Source:

(static, constant) PING_INTERVAL :Number

Interval for pinging peers.

Type:
  • Number
Default Value:
  • 30000
Source:

(static, constant) RESPONSE_TIMEOUT :Number

Required time for peers to respond to messages (i.e. getblocks/getdata).

Type:
  • Number
Default Value:
  • 30000
Source:

(static, constant) STALL_INTERVAL :Number

Interval to check for drainage and required responses from peer.

Type:
  • Number
Default Value:
  • 5000
Source:

(static, constant) TIMEOUT_INTERVAL :Number

Generic timeout interval.

Type:
  • Number
Source:

(static, constant) TX_TIMEOUT :Number

Required time for loader to respond with a tx.

Type:
  • Number
Default Value:
  • 120000
Source:

Methods

(static) fromInbound(options, socket) → {Peer}

Create inbound peer from socket.

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

(static) fromOptions(options) → {Peer}

Create a peer from options.

Parameters:
Name Type Description
options Object
Source:
Returns:
Type
Peer

(static) fromOutbound(options, addr) → {Peer}

Create outbound peer from net address.

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

_bind(socket)

Bind to socket.

Parameters:
Name Type Description
socket net.Socket
Source:

(async) _open() → {Promise}

Open and perform initial handshake.

Source:
Returns:
Type
Promise

accept(socket) → {net.Socket}

Accept an inbound socket.

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

(private) addTimeout(packet)

Potentially add response timeout.

Parameters:
Name Type Description
packet Packet
Source:

announceBlock(blocks)

Broadcast blocks to peer.

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

announceTX(txs)

Broadcast transactions to peer.

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

ban()

Ban peer.

Source:

blockType() → {Number}

Calculate peer block inv type (filtered, compact, witness, or non-witness).

Source:
Returns:
Type
Number

connect(addr) → {net.Socket}

Create the socket and begin connecting. This method will use options.createSocket if provided.

Parameters:
Name Type Description
addr NetAddress
Source:
Returns:
Type
net.Socket

destroy()

Disconnect from and destroy the peer.

Source:

drain() → {Promise}

Wait for a drain event.

Source:
Returns:
Type
Promise

(private) error(err)

Emit an error and destroy the peer.

Parameters:
Name Type Description
err String | Error
Source:

feedParser(data)

Feed data to the parser.

Parameters:
Name Type Description
data Buffer
Source:

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

Finalize peer after handshake.

Source:
Returns:
Type
Promise

(private) flushInv()

Flush inv queue.

Source:

framePacket(cmd, payload) → {Buffer}

Frame a payload with a header.

Parameters:
Name Type Description
cmd String

Packet type.

payload Buffer
Source:
Returns:

Payload with header prepended.

Type
Buffer

(private) fulfill(packet)

Potentially finish response timeout.

Parameters:
Name Type Description
packet Packet
Source:

getBlock(hashes)

Send batched getdata to peer (blocks).

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

getData(items)

Send getdata to peer.

Parameters:
Name Type Description
items Array.<InvItem>
Source:

getFullBlock(hash)

Send getdata to peer for a single block.

Parameters:
Name Type Description
hash Hash
Source:

getItems(type, hashes)

Send batched getdata to peer.

Parameters:
Name Type Description
type InvType
hashes Array.<Hash>
Source:

(async) getName() → {Promise}

Do a reverse dns lookup on peer's addr.

Source:
Returns:
Type
Promise

getServiceNames() → {Array.<String>}

Returns human readable list of services that are available.

Source:
Returns:
Type
Array.<String>

getTX(hashes)

Send batched getdata to peer (txs).

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

(private) handleDrain()

Handle drain event.

Source:

(async, private) handleFeeFilter(packet)

Handle feefilter packet.

Parameters:
Name Type Description
packet FeeFilterPacket
Source:

(async, private) handleFilterAdd(packet)

Handle filteradd packet.

Parameters:
Name Type Description
packet FilterAddPacket
Source:

(async, private) handleFilterClear(packet)

Handle filterclear packet.

Parameters:
Name Type Description
packet FilterClearPacket
Source:

(async, private) handleFilterLoad(packet)

Handle filterload packet.

Parameters:
Name Type Description
packet FilterLoadPacket
Source:

(async, private) handlePacket(packet)

Handle a packet payload without a lock.

Parameters:
Name Type Description
packet Packet
Source:

(async, private) handlePing(packet)

Handle ping packet.

Parameters:
Name Type Description
packet PingPacket
Source:

(async, private) handlePong(packet)

Handle pong packet.

Parameters:
Name Type Description
packet PongPacket
Source:

(async, private) handleSendCmpct(packet)

Handle sendcmpct packet.

Parameters:
Name Type Description
packet SendCmpctPacket
Source:

(async, private) handleSendHeaders(packet)

Handle sendheaders packet.

Parameters:
Name Type Description
packet SendHeadersPacket
Source:

(async, private) handleVerack(packet)

Handle verack packet.

Parameters:
Name Type Description
packet VerackPacket
Source:

(async, private) handleVersion(packet)

Handle version packet.

Parameters:
Name Type Description
packet VersionPacket
Source:

hasCompact() → {Boolean}

Test whether the peer sent us a compatible compact block handshake.

Source:
Returns:
Type
Boolean

hasCompactSupport() → {Boolean}

Test whether the peer supports compact blocks.

Source:
Returns:
Type
Boolean

hasServices(services) → {Boolean}

Test whether required services are available.

Parameters:
Name Type Description
services Number
Source:
Returns:
Type
Boolean

hasWitness() → {Boolean}

Test whether the WITNESS service bit is set.

Source:
Returns:
Type
Boolean

hostname() → {String}

Getter to retrieve hostname.

Source:
Returns:
Type
String

increaseBan(score) → {Boolean}

Increase banscore on peer.

Parameters:
Name Type Description
score Number
Source:
Returns:
Type
Boolean

(private) init()

Begin peer initialization.

Source:

(private) initConnect() → {Promise}

Wait for connection.

Source:
Returns:
Type
Promise

(private) initStall() → {Promise}

Setup stall timer.

Source:
Returns:
Type
Promise

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

Handle post handshake.

Source:
Returns:
Type
Promise

inspectSymbol() → {String}

Inspect the peer.

Source:
Returns:
Type
String

(private) maybeTimeout()

Potentially timeout peer if it hasn't responded.

Source:

(private) needsDrain(size)

Add to drain counter.

Parameters:
Name Type Description
size Number
Source:

(async) open() → {Promise}

Open and perform initial handshake.

Source:
Returns:
Type
Promise

queueInv(items)

Send inv to a peer.

Parameters:
Name Type Description
items Array.<InvItem>
Source:

(async, private) readPacket(packet)

Handle a packet payload.

Parameters:
Name Type Description
packet Packet
Source:

reject(msg, err) → {Boolean}

Send a reject packet to peer.

Parameters:
Name Type Description
msg String
err VerifyError
Source:
Returns:
Type
Boolean

(private) request(type, timeout) → {RequestEntry}

Wait for a packet to be received from peer.

Parameters:
Name Type Description
type Number

Packet type.

timeout Number
Source:
Returns:
Type
RequestEntry

(private) response(type, payload)

Fulfill awaiting requests created with Peer#request.

Parameters:
Name Type Description
type Number

Packet type.

payload Object
Source:

send(packet)

Send a packet.

Parameters:
Name Type Description
packet Packet
Source:

sendCompact(mode)

Send a sendcmpct packet.

Parameters:
Name Type Description
mode Number
Source:

(private) sendCompactBlock(block) → {Boolean}

Send a compact block.

Parameters:
Name Type Description
block Block
Source:
Returns:
Type
Boolean

sendFeeRate(rate)

Set a fee rate filter for the peer.

Parameters:
Name Type Description
rate Rate
Source:

sendFilterLoad()

Send filterload to update the local bloom filter.

Source:

sendGetAddr()

Send a getaddr packet.

Source:

sendGetBlocks(locator, stopnullable)

Send getblocks to peer.

Parameters:
Name Type Attributes Description
locator Array.<Hash>

Chain locator.

stop Hash <nullable>

Hash to stop at.

Source:

sendGetHeaders(locatornullable, stopnullable)

Send getheaders to peer. Note that unlike getblocks, getheaders can have a null locator.

Parameters:
Name Type Attributes Description
locator Array.<Hash> <nullable>

Chain locator.

stop Hash <nullable>

Hash to stop at.

Source:

sendHeaders(items)

Send headers to a peer.

Parameters:
Name Type Description
items Array.<Headers>
Source:

sendInv(items)

Force send an inv (no filter check).

Parameters:
Name Type Description
items Array.<InvItem>
Source:

sendMempool()

Send mempool to peer.

Source:

sendPing()

Send a ping packet.

Source:

sendRaw(packet)

Send a packet.

Parameters:
Name Type Description
packet Packet
Source:

sendReject(code, reason, msg, hash)

Send reject to peer.

Parameters:
Name Type Description
code Number
reason String
msg String
hash Hash
Source:

sendVersion()

Send a version packet.

Source:

(async) tryOpen() → {Promise}

Open and perform initial handshake (without rejection).

Source:
Returns:
Type
Promise

txType() → {Number}

Calculate peer tx inv type (witness or non-witness).

Source:
Returns:
Type
Number

(private) wait(type) → {Promise}

Wait for a packet to be received from peer.

Parameters:
Name Type Description
type Number

Packet type.

Source:
Returns:
  • Returns Object(payload). Executed on timeout or once packet is received.
Type
Promise

write(data)

Write data to the peer's socket.

Parameters:
Name Type Description
data Buffer
Source: