[#PeerManager]
= PeerManager
:mrdocs:

Drives the peer&hyphen;to&hyphen;peer message processing and relay logic for a node&period;

== Synopsis

Declared in `&lt;net&lowbar;processing&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class PeerManager
    : public xref:CValidationInterface.adoc[CValidationInterface]
    , public xref:NetEventsInterface.adoc[NetEventsInterface]
----

== Base Classes

[cols="1,4"]
|===
| Name| Description
| `xref:CValidationInterface.adoc[CValidationInterface]`
| Implement this to subscribe to events generated in validation and mempool
| `xref:NetEventsInterface.adoc[NetEventsInterface]`
| Interface for message handling
|===

== Types

[cols="1,4"]
|===
| Name| Description
| xref:PeerManager/Options.adoc[`Options`] 
| Tunable options controlling peer manager behavior&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:PeerManager/2destructor.adoc[`&#126;PeerManager`] [.small]#[destructor]# [.small]#[virtual]#
| Destroy the peer manager&period;
| xref:PeerManager/AbortPrivateBroadcast.adoc[`AbortPrivateBroadcast`]  [.small]#[virtual]#
| Abort private broadcast attempts for transactions currently being privately broadcast&period;
| xref:PeerManager/CheckForStaleTipAndEvictPeers.adoc[`CheckForStaleTipAndEvictPeers`]  [.small]#[virtual]#
| Evict extra outbound peers&period; If we think our tip may be stale, connect to an extra outbound&period; Public for unit testing&period;
| xref:PeerManager/FetchBlock.adoc[`FetchBlock`]  [.small]#[virtual]#
| Attempt to manually fetch block from a given peer&period; We must already have the header&period;
| xref:NetEventsInterface/FinalizeNode.adoc[`FinalizeNode`]  [.small]#[virtual]#
| Handle removal of a peer (clear state)&period;
| xref:PeerManager/GetDesirableServiceFlags.adoc[`GetDesirableServiceFlags`]  [.small]#[virtual]#
| Gets the set of service flags which are &quot;desirable&quot; for a given peer&period;
| xref:PeerManager/GetInfo.adoc[`GetInfo`]  [.small]#[virtual]#
| Get peer manager info&period;
| xref:PeerManager/GetNodeStateStats.adoc[`GetNodeStateStats`]  [.small]#[virtual]#
| Get statistics from node state
| xref:PeerManager/GetOrphanTransactions.adoc[`GetOrphanTransactions`]  [.small]#[virtual]#
| Get information about transactions currently held in the orphan pool&period;
| xref:PeerManager/GetPrivateBroadcastInfo.adoc[`GetPrivateBroadcastInfo`]  [.small]#[virtual]#
| Get info about transactions currently being privately broadcast&period;
| xref:NetEventsInterface/HasAllDesirableServiceFlags.adoc[`HasAllDesirableServiceFlags`]  [.small]#[virtual]#
| Callback to determine whether the given set of service flags are sufficient for a peer to be &quot;relevant&quot;&period;
| xref:NetEventsInterface/InitializeNode.adoc[`InitializeNode`]  [.small]#[virtual]#
| Initialize a peer (setup state)&period;
| xref:PeerManager/InitiateTxBroadcastPrivate.adoc[`InitiateTxBroadcastPrivate`]  [.small]#[virtual]#
| Initiate a private transaction broadcast&period; This is done asynchronously via short&hyphen;lived connections to peers on privacy networks&period;
| xref:PeerManager/InitiateTxBroadcastToAll.adoc[`InitiateTxBroadcastToAll`]  [.small]#[virtual]#
| Initiate a transaction broadcast to eligible peers&period; Queue the witness transaction id to the inbound and outbound inv backlogs&period; Later, depending on `&hyphen;txsendrate`, the peer&apos;s next inventory send time and if the transaction is in the mempool, an `INV` about it may be sent to the peer&period;
| xref:NetEventsInterface/ProcessMessages.adoc[`ProcessMessages`]  [.small]#[virtual]#
| Process protocol messages received from a given node
| xref:NetEventsInterface/SendMessages.adoc[`SendMessages`]  [.small]#[virtual]#
| Send queued protocol messages to a given node&period;
| xref:PeerManager/SendPings.adoc[`SendPings`]  [.small]#[virtual]#
| Send ping message to all peers
| xref:PeerManager/SetBestBlock.adoc[`SetBestBlock`]  [.small]#[virtual]#
| Set the height of the best block and its time (seconds since epoch)&period;
| xref:PeerManager/StartScheduledTasks.adoc[`StartScheduledTasks`]  [.small]#[virtual]#
| Begin running background tasks, should only be called once
| xref:PeerManager/UnitTestMisbehaving.adoc[`UnitTestMisbehaving`]  [.small]#[virtual]#
| Apply misbehavior scoring to a peer directly&period; Public for unit testing&period;
| xref:PeerManager/UpdateLastBlockAnnounceTime.adoc[`UpdateLastBlockAnnounceTime`]  [.small]#[virtual]#
| This function is used for testing the stale tip eviction logic, see denialofservice&lowbar;tests&period;cpp
|===


== Static Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:PeerManager/make.adoc[`make`] 
| Create a PeerManager instance wired to the node&apos;s networking and validation components&period;
|===


== Static Data Members

[cols="1,4"]
|===
| Name| Description
| xref:NetEventsInterface/g_msgproc_mutex.adoc[`g&lowbar;msgproc&lowbar;mutex`] 
| Mutex for anything that is only accessed via the msg processing thread
|===


== Protected Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:CValidationInterface/ActiveTipChange.adoc[`ActiveTipChange`]  [.small]#[virtual]#
| Notifies listeners any time the block chain tip changes, synchronously&period;
| xref:CValidationInterface/BlockChecked.adoc[`BlockChecked`]  [.small]#[virtual]#
| Notifies listeners of a block validation result&period; If the provided BlockValidationState IsValid, the provided block is guaranteed to be the current best block at the time the callback was generated (not necessarily now)&period;
| xref:CValidationInterface/BlockConnected.adoc[`BlockConnected`]  [.small]#[virtual]#
| Notifies listeners of a block being connected&period;
| xref:CValidationInterface/BlockDisconnected.adoc[`BlockDisconnected`]  [.small]#[virtual]#
| Notifies listeners of a block being disconnected Provides the block that was disconnected&period;
| xref:CValidationInterface/ChainStateFlushed.adoc[`ChainStateFlushed`]  [.small]#[virtual]#
| Notifies listeners of the new active block chain on&hyphen;disk&period;
| xref:CValidationInterface/MempoolTransactionsRemovedForBlock.adoc[`MempoolTransactionsRemovedForBlock`]  [.small]#[virtual]#
| Notifies listeners of transactions removed from the mempool as as a result of new block being connected&period; MempoolTransactionsRemovedForBlock will be fired before BlockConnected&period;
| xref:CValidationInterface/NewPoWValidBlock.adoc[`NewPoWValidBlock`]  [.small]#[virtual]#
| Notifies listeners that a block which builds directly on our current tip has been received and connected to the headers tree, though not validated yet&period;
| xref:CValidationInterface/TransactionAddedToMempool.adoc[`TransactionAddedToMempool`]  [.small]#[virtual]#
| Notifies listeners of a transaction having been added to mempool&period;
| xref:CValidationInterface/TransactionRemovedFromMempool.adoc[`TransactionRemovedFromMempool`]  [.small]#[virtual]#
| Notifies listeners of a transaction leaving mempool&period;
| xref:CValidationInterface/UpdatedBlockTip.adoc[`UpdatedBlockTip`]  [.small]#[virtual]#
| Notifies listeners when the block chain tip advances&period;
|===


== Non-Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:EnsurePeerman.adoc[`EnsurePeerman`]
| Get the peer manager from a node context, asserting it is present&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
