Bitcoin ABC  0.24.7
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
PeerManager Class Referencefinal

#include <net_processing.h>

Inheritance diagram for PeerManager:
[legend]
Collaboration diagram for PeerManager:
[legend]

Public Member Functions

 PeerManager (const CChainParams &chainparams, CConnman &connman, BanMan *banman, CScheduler &scheduler, ChainstateManager &chainman, CTxMemPool &pool)
 
void BlockConnected (const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected) override
 Overridden from CValidationInterface. More...
 
void BlockDisconnected (const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
 Notifies listeners of a block being disconnected. More...
 
void UpdatedBlockTip (const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
 Overridden from CValidationInterface. More...
 
void BlockChecked (const CBlock &block, const BlockValidationState &state) override
 Overridden from CValidationInterface. More...
 
void NewPoWValidBlock (const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
 Overridden from CValidationInterface. More...
 
void InitializeNode (const Config &config, CNode *pnode) override
 Initialize a peer by adding it to mapNodeState and pushing a message requesting its version. More...
 
void FinalizeNode (const Config &config, NodeId nodeid, bool &fUpdateConnectionTime) override
 Handle removal of a peer by updating various state and removing it from mapNodeState. More...
 
bool ProcessMessages (const Config &config, CNode *pfrom, std::atomic< bool > &interrupt) override
 Process protocol messages received from a given node. More...
 
bool SendMessages (const Config &config, CNode *pto, std::atomic< bool > &interrupt) override EXCLUSIVE_LOCKS_REQUIRED(pto -> cs_sendProcessing)
 Send queued protocol messages to be sent to a give node. More...
 
void ConsiderEviction (CNode &pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Consider evicting an outbound peer based on the amount of time they've been behind our tip. More...
 
void CheckForStaleTipAndEvictPeers ()
 Evict extra outbound peers. More...
 
void EvictExtraOutboundPeers (int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 If we have extra outbound peers, try to disconnect the one with the oldest block announcement. More...
 
void ProcessMessage (const Config &config, CNode &pfrom, const std::string &msg_type, CDataStream &vRecv, const std::chrono::microseconds time_received, const std::atomic< bool > &interruptMsgProc)
 Process a single message from a peer. More...
 
void Misbehaving (const NodeId pnode, const int howmuch, const std::string &message)
 Increment peer's misbehavior score. More...
 
void ReattemptInitialBroadcast (CScheduler &scheduler) const
 Retrieve unbroadcast transactions from the mempool and reattempt sending to peers. More...
 
void UpdateAvalancheStatistics () const
 Update the avalanche statistics for all the nodes. More...
 

Private Member Functions

void Misbehaving (const CNode &node, int howmuch, const std::string &message)
 
bool MaybePunishNodeForBlock (NodeId nodeid, const BlockValidationState &state, bool via_compact_block, const std::string &message="")
 Potentially mark a node discouraged based on the contents of a BlockValidationState object. More...
 
bool MaybePunishNodeForTx (NodeId nodeid, const TxValidationState &state, const std::string &message="")
 Potentially disconnect and discourage a node based on the contents of a TxValidationState object. More...
 
bool MaybeDiscourageAndDisconnect (CNode &pnode)
 Maybe disconnect a peer and discourage future connections from its address. More...
 
void ProcessOrphanTx (const Config &config, std::set< TxId > &orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main
 Reconsider orphan transactions after a parent has been accepted to the mempool. More...
 
void ProcessHeadersMessage (const Config &config, CNode &pfrom, const std::vector< CBlockHeader > &headers, bool via_compact_block)
 Process a single headers message from a peer. More...
 
void SendBlockTransactions (CNode &pfrom, const CBlock &block, const BlockTransactionsRequest &req)
 
InvRequestTracker< TxId > m_txrequest GUARDED_BY (::cs_main)
 
InvRequestTracker< avalanche::ProofId > m_proofrequest GUARDED_BY (cs_proofrequest)
 

Private Attributes

void g_cs_orphans
 
void AddTxAnnouncement(const CNode &node, const TxId &txid, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(void AddProofAnnouncement (const CNode &node, const avalanche::ProofId &proofid, std::chrono::microseconds current_time, bool preferred) EXCLUSIVE_LOCKS_REQUIRED(cs_proofrequest)
 Register with InvRequestTracker that a TX INV has been received from a peer. More...
 
const CChainParamsm_chainparams
 
CConnmanm_connman
 
BanMan *const m_banman
 Pointer to this node's banman. More...
 
ChainstateManagerm_chainman
 
CTxMemPoolm_mempool
 
Mutex cs_proofrequest
 
int64_t m_stale_tip_check_time
 Next time to check for stale tip. More...
 

Additional Inherited Members

- Protected Member Functions inherited from CValidationInterface
 ~CValidationInterface ()=default
 Protected destructor so that instances can only be deleted by derived classes. More...
 
virtual void TransactionAddedToMempool (const CTransactionRef &tx, uint64_t mempool_sequence)
 Notifies listeners of a transaction having been added to mempool. More...
 
virtual void TransactionRemovedFromMempool (const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
 Notifies listeners of a transaction leaving mempool. More...
 
virtual void ChainStateFlushed (const CBlockLocator &locator)
 Notifies listeners of the new active block chain on-disk. More...
 
- Protected Member Functions inherited from NetEventsInterface
 ~NetEventsInterface ()=default
 Protected destructor so that instances can only be deleted by derived classes. More...
 

Detailed Description

Definition at line 45 of file net_processing.h.

Constructor & Destructor Documentation

◆ PeerManager()

PeerManager::PeerManager ( const CChainParams chainparams,
CConnman connman,
BanMan banman,
CScheduler scheduler,
ChainstateManager chainman,
CTxMemPool pool 
)

Definition at line 1544 of file net_processing.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ BlockChecked()

void PeerManager::BlockChecked ( const CBlock block,
const BlockValidationState state 
)
overridevirtual

Overridden from CValidationInterface.

Handle invalid block rejection and consequent peer banning, maintain which peers announce compact blocks.

Reimplemented from CValidationInterface.

Definition at line 1779 of file net_processing.cpp.

Here is the call graph for this function:

◆ BlockConnected()

void PeerManager::BlockConnected ( const std::shared_ptr< const CBlock > &  pblock,
const CBlockIndex pindex 
)
overridevirtual

Overridden from CValidationInterface.

Evict orphan txn pool entries (EraseOrphanTx) based on a newly connected block, remember the recently confirmed transactions, and delete tracked announcements for them.

Also save the time of the last tip update.

Reimplemented from CValidationInterface.

Definition at line 1603 of file net_processing.cpp.

Here is the call graph for this function:

◆ BlockDisconnected()

void PeerManager::BlockDisconnected ( const std::shared_ptr< const CBlock > &  block,
const CBlockIndex pindex 
)
overridevirtual

Notifies listeners of a block being disconnected.

Called on a background thread.

Reimplemented from CValidationInterface.

Definition at line 1656 of file net_processing.cpp.

◆ CheckForStaleTipAndEvictPeers()

void PeerManager::CheckForStaleTipAndEvictPeers ( )

Evict extra outbound peers.

If we think our tip may be stale, connect to an extra outbound.

Definition at line 5168 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConsiderEviction()

void PeerManager::ConsiderEviction ( CNode pto,
int64_t  time_in_seconds 
)

Consider evicting an outbound peer based on the amount of time they've been behind our tip.

Definition at line 4998 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EvictExtraOutboundPeers()

void PeerManager::EvictExtraOutboundPeers ( int64_t  time_in_seconds)

If we have extra outbound peers, try to disconnect the one with the oldest block announcement.

Definition at line 5083 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinalizeNode()

void PeerManager::FinalizeNode ( const Config config,
NodeId  nodeid,
bool &  fUpdateConnectionTime 
)
overridevirtual

Handle removal of a peer by updating various state and removing it from mapNodeState.

Implements NetEventsInterface.

Definition at line 1175 of file net_processing.cpp.

Here is the call graph for this function:

◆ GUARDED_BY() [1/2]

InvRequestTracker<TxId> m_txrequest PeerManager::GUARDED_BY ( ::cs_main  )
private

◆ GUARDED_BY() [2/2]

InvRequestTracker<avalanche::ProofId> m_proofrequest PeerManager::GUARDED_BY ( cs_proofrequest  )
private

◆ InitializeNode()

void PeerManager::InitializeNode ( const Config config,
CNode pnode 
)
overridevirtual

Initialize a peer by adding it to mapNodeState and pushing a message requesting its version.

Implements NetEventsInterface.

Definition at line 1107 of file net_processing.cpp.

Here is the call graph for this function:

◆ MaybeDiscourageAndDisconnect()

bool PeerManager::MaybeDiscourageAndDisconnect ( CNode pnode)
private

Maybe disconnect a peer and discourage future connections from its address.

Parameters
[in]pnodeThe node to check.
Returns
True if the peer was marked for disconnection in this function

Definition at line 4807 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MaybePunishNodeForBlock()

bool PeerManager::MaybePunishNodeForBlock ( NodeId  nodeid,
const BlockValidationState state,
bool  via_compact_block,
const std::string &  message = "" 
)
private

Potentially mark a node discouraged based on the contents of a BlockValidationState object.

Parameters
[in]via_compact_blockthis bool is passed in because net_processing should punish peers differently depending on whether the data was provided in a compact block message or not. If the compact block had a valid header, but contained invalid txs, the peer should not be punished. See BIP 152.
Returns
Returns true if the peer was punished (probably disconnected)

Definition at line 1435 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MaybePunishNodeForTx()

bool PeerManager::MaybePunishNodeForTx ( NodeId  nodeid,
const TxValidationState state,
const std::string &  message = "" 
)
private

Potentially disconnect and discourage a node based on the contents of a TxValidationState object.

Returns
Returns true if the peer was punished (probably disconnected)

Definition at line 1493 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Misbehaving() [1/2]

void PeerManager::Misbehaving ( const CNode node,
int  howmuch,
const std::string &  message 
)
inlineprivate

Definition at line 149 of file net_processing.h.

Here is the call graph for this function:

◆ Misbehaving() [2/2]

void PeerManager::Misbehaving ( const NodeId  pnode,
const int  howmuch,
const std::string &  message 
)

Increment peer's misbehavior score.

If the new value >= DISCOURAGEMENT_THRESHOLD, mark the node to be discouraged, meaning the peer might be disconnected and added to the discouragement filter. Public for unit testing.

Definition at line 1407 of file net_processing.cpp.

Here is the caller graph for this function:

◆ NewPoWValidBlock()

void PeerManager::NewPoWValidBlock ( const CBlockIndex pindex,
const std::shared_ptr< const CBlock > &  pblock 
)
overridevirtual

Overridden from CValidationInterface.

Maintain state about the best-seen block and fast-announce a compact block to compatible peers.

Reimplemented from CValidationInterface.

Definition at line 1683 of file net_processing.cpp.

Here is the call graph for this function:

◆ ProcessHeadersMessage()

void PeerManager::ProcessHeadersMessage ( const Config config,
CNode pfrom,
const std::vector< CBlockHeader > &  headers,
bool  via_compact_block 
)
private

Process a single headers message from a peer.

Definition at line 2326 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProcessMessage()

void PeerManager::ProcessMessage ( const Config config,
CNode pfrom,
const std::string &  msg_type,
CDataStream vRecv,
const std::chrono::microseconds  time_received,
const std::atomic< bool > &  interruptMsgProc 
)

Process a single message from a peer.

Public for fuzz testing

Definition at line 2871 of file net_processing.cpp.

Here is the caller graph for this function:

◆ ProcessMessages()

bool PeerManager::ProcessMessages ( const Config config,
CNode pfrom,
std::atomic< bool > &  interrupt 
)
overridevirtual

Process protocol messages received from a given node.

Implements NetEventsInterface.

Definition at line 4858 of file net_processing.cpp.

Here is the call graph for this function:

◆ ProcessOrphanTx()

void PeerManager::ProcessOrphanTx ( const Config config,
std::set< TxId > &  orphan_work_set 
)
private

Reconsider orphan transactions after a parent has been accepted to the mempool.

Parameters
[in/out]orphan_work_set The set of orphan transactions to reconsider. Generally only one orphan will be reconsidered on each call of this function. This set may be added to if accepting an orphan causes its children to be reconsidered.
Here is the caller graph for this function:

◆ ReattemptInitialBroadcast()

void PeerManager::ReattemptInitialBroadcast ( CScheduler scheduler) const

Retrieve unbroadcast transactions from the mempool and reattempt sending to peers.

Definition at line 1130 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendBlockTransactions()

void PeerManager::SendBlockTransactions ( CNode pfrom,
const CBlock block,
const BlockTransactionsRequest req 
)
private

Definition at line 2308 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendMessages()

bool PeerManager::SendMessages ( const Config config,
CNode pto,
std::atomic< bool > &  interrupt 
) -> cs_sendProcessing)
overridevirtual

Send queued protocol messages to be sent to a give node.

Parameters
[in]ptoThe node which we are sending messages to.
[in]interruptInterrupt condition for processing threads
Returns
True if there is more work to be done

If possible, start at the block preceding the currently best known header. This ensures that we always get a non-empty list of headers back as long as the peer is up-to-date. With a non-empty response, we can initialise the peer's known best block. This wouldn't be possible if we requested starting at pindexBestHeader and got back an empty response.

Implements NetEventsInterface.

Definition at line 5211 of file net_processing.cpp.

Here is the call graph for this function:

◆ UpdateAvalancheStatistics()

void PeerManager::UpdateAvalancheStatistics ( ) const

Update the avalanche statistics for all the nodes.

Definition at line 1167 of file net_processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdatedBlockTip()

void PeerManager::UpdatedBlockTip ( const CBlockIndex pindexNew,
const CBlockIndex pindexFork,
bool  fInitialDownload 
)
overridevirtual

Overridden from CValidationInterface.

Update our best height and announce any block hashes which weren't previously in ChainActive() to our peers.

Reimplemented from CValidationInterface.

Definition at line 1738 of file net_processing.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ AddProofAnnouncement

void PeerManager::AddProofAnnouncement
private

Register with InvRequestTracker that a TX INV has been received from a peer.

The announcement parameters are decided in PeerManager and then passed to InvRequestTracker. Register with InvRequestTracker that a PROOF INV has been received from a peer. The announcement parameters are decided in PeerManager and then passed to InvRequestTracker.

Definition at line 215 of file net_processing.h.

◆ cs_proofrequest

Mutex PeerManager::cs_proofrequest
private

Definition at line 230 of file net_processing.h.

◆ g_cs_orphans

void PeerManager::g_cs_orphans
private

Definition at line 191 of file net_processing.h.

◆ m_banman

BanMan* const PeerManager::m_banman
private

Pointer to this node's banman.

May be nullptr - check existence before dereferencing.

Definition at line 225 of file net_processing.h.

◆ m_chainman

ChainstateManager& PeerManager::m_chainman
private

Definition at line 226 of file net_processing.h.

◆ m_chainparams

const CChainParams& PeerManager::m_chainparams
private

Definition at line 219 of file net_processing.h.

◆ m_connman

CConnman& PeerManager::m_connman
private

Definition at line 220 of file net_processing.h.

◆ m_mempool

CTxMemPool& PeerManager::m_mempool
private

Definition at line 227 of file net_processing.h.

◆ m_stale_tip_check_time

int64_t PeerManager::m_stale_tip_check_time
private

Next time to check for stale tip.

Definition at line 235 of file net_processing.h.


The documentation for this class was generated from the following files: