Class: Peer

net.Peer

new Peer(options)

Represents a remote peer.

Parameters:
Name Type Description
options PeerOptions
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
ts 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:
Fires:
  • Peer#event:ack

Members

(static, constant) BLOCK_TIMEOUT :Number

Required time for loader to respond with block/merkleblock during initial sync.

Type:
  • Number
Default Value:
  • 60000
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:

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

(private) _init()

Begin peer initialization.

Source:

_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 Array:.<Block:>
Source:

announceTX(txs)

Broadcast transactions to peer.

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

ban()

Ban peer.

Source:

bind(socket)

Bind to socket.

Parameters:
Name Type Description
socket net.Socket
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:

(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:

getItems(type, hashes)

Send batched getdata to peer.

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

getTX(hashes)

Send batched getdata to peer (txs).

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

(private) handleAuthChallenge(packet)

Handle authchallenge packet.

Parameters:
Name Type Description
packet AuthChallengePacket
Source:

(private) handleAuthPropose(packet)

Handle authpropose packet.

Parameters:
Name Type Description
packet AuthProposePacket
Source:

(private) handleAuthReply(packet)

Handle authreply packet.

Parameters:
Name Type Description
packet AuthReplyPacket
Source:

(private) handleDrain()

Handle drain event.

Source:

(private) handleEncack(packet)

Handle encack packet.

Parameters:
Name Type Description
packet EncackPacket
Source:

(private) handleEncinit(packet)

Handle encinit packet.

Parameters:
Name Type Description
packet EncinitPacket
Source:

(private) handleFeeFilter(packet)

Handle feefilter packet.

Parameters:
Name Type Description
packet FeeFilterPacket
Source:

(private) handleFilterAdd(packet)

Handle filteradd packet.

Parameters:
Name Type Description
packet FilterAddPacket
Source:

(private) handleFilterClear(packet)

Handle filterclear packet.

Parameters:
Name Type Description
packet FilterClearPacket
Source:

(private) handleFilterLoad(packet)

Handle filterload packet.

Parameters:
Name Type Description
packet FilterLoadPacket
Source:

(private) handlePacket(packet)

Handle a packet payload without a lock.

Parameters:
Name Type Description
packet Packet
Source:

(private) handlePing(packet)

Handle ping packet.

Parameters:
Name Type Description
packet PingPacket
Source:

(private) handlePong(packet)

Handle pong packet.

Parameters:
Name Type Description
packet PongPacket
Source:

(private) handleSendCmpct()

Handle sendcmpct packet.

Parameters:
Type Description
SendCmpctPacket
Source:

(private) handleSendHeaders(packet)

Handle sendheaders packet.

Parameters:
Name Type Description
packet SendHeadersPacket
Source:

(private) handleVerack(packet)

Handle verack packet.

Parameters:
Name Type Description
packet VerackPacket
Source:

(private) handleVersion(packet)

Handle version packet.

Parameters:
Name Type Description
packet VersionPacket
Source:

hasCompact() → {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) initBIP150() → {Promise}

Handle post bip151-handshake.

Source:
Returns:
Type
Promise

(private) initBIP151() → {Promise}

Handle connect event (called immediately if a socket was passed into peer).

Source:
Returns:
Type
Promise

(private) initConnect() → {Promise}

Wait for connection.

Source:
Returns:
Type
Promise

(private) initStall() → {Promise}

Setup stall timer.

Source:
Returns:
Type
Promise

(private) initVersion() → {Promise}

Handle post handshake.

Source:
Returns:
Type
Promise

inspect() → {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:

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:

(private) readPacket(packet)

Handle a packet payload.

Parameters:
Name Type Description
packet Packet
Source:

reject(msgnullable, code, reason, score) → {Boolean}

Send a reject packet to peer.

Parameters:
Name Type Attributes Description
msg TX | Block <nullable>
code String
reason String
score Number
Source:
See:
  • Framer.reject
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)

Send reject to peer.

Parameters:
Name Type Description
code Number
reason String
msg TX | Block
Source:

sendVersion()

Send a version packet.

Source:

setAuth(db, key)

Set BIP150 auth.

Parameters:
Name Type Description
db AuthDB
key Buffer
Source:

setCipher(cipher)

Set BIP151 cipher type.

Parameters:
Name Type Description
cipher Number
Source:

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: