Bitcoin Core  21.99.0
P2P Digital Currency
Classes | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
CNode Class Reference

Information about a peer. More...

#include <net.h>

Collaboration diagram for CNode:
[legend]

Classes

struct  TxRelay
 

Public Member Functions

SOCKET hSocket GUARDED_BY (cs_hSocket)
 
size_t nSendSize GUARDED_BY (cs_vSend)
 Total size of all vSendMsg entries. More...
 
size_t nSendOffset GUARDED_BY (cs_vSend)
 Offset inside the first vSendMsg already sent. More...
 
uint64_t nSendBytes GUARDED_BY (cs_vSend)
 
std::deque< std::vector< unsigned char > > vSendMsg GUARDED_BY (cs_vSend)
 
std::list< CNetMessage > vProcessMsg GUARDED_BY (cs_vProcessMsg)
 
uint64_t nRecvBytes GUARDED_BY (cs_vRecv)
 
std::string cleanSubVer GUARDED_BY (cs_SubVer)
 cleanSubVer is a sanitized string of the user agent byte array we read from the wire. More...
 
bool HasPermission (NetPermissionFlags permission) const
 
bool IsOutboundOrBlockRelayConn () const
 
bool IsFullOutboundConn () const
 
bool IsManualConn () const
 
bool IsBlockOnlyConn () const
 
bool IsFeelerConn () const
 
bool IsAddrFetchConn () const
 
bool IsInboundConn () const
 
bool RelayAddrsWithConn () const
 
bool ExpectServicesFromConn () const
 
Network ConnectedThroughNetwork () const
 Get network the peer connected through. More...
 
std::chrono::microseconds m_next_addr_send GUARDED_BY (cs_sendProcessing)
 
std::chrono::microseconds m_next_local_addr_send GUARDED_BY (cs_sendProcessing)
 
 CNode (NodeId id, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn, ConnectionType conn_type_in, bool inbound_onion=false)
 
 ~CNode ()
 
 CNode (const CNode &)=delete
 
CNodeoperator= (const CNode &)=delete
 
NodeId GetId () const
 
uint64_t GetLocalNonce () const
 
int GetRefCount () const
 
bool ReceiveMsgBytes (Span< const uint8_t > msg_bytes, bool &complete)
 Receive bytes from the buffer and deserialize them into messages. More...
 
void SetCommonVersion (int greatest_common_version)
 
int GetCommonVersion () const
 
CService GetAddrLocal () const
 
void SetAddrLocal (const CService &addrLocalIn)
 May not be called more than once. More...
 
CNodeAddRef ()
 
void Release ()
 
void AddAddressKnown (const CAddress &_addr)
 
bool IsAddrCompatible (const CAddress &addr) const
 Whether the peer supports the address. More...
 
void PushAddress (const CAddress &_addr, FastRandomContext &insecure_rand)
 
void AddKnownTx (const uint256 &hash)
 
void PushTxInventory (const uint256 &hash)
 
void CloseSocketDisconnect ()
 
void copyStats (CNodeStats &stats, const std::vector< bool > &m_asmap)
 
ServiceFlags GetLocalServices () const
 
std::string GetAddrName () const
 
void MaybeSetAddrName (const std::string &addrNameIn)
 Sets the addrName only if it was not previously set. More...
 
std::string ConnectionTypeAsString () const
 
bool IsInboundOnion () const
 Whether this peer is an inbound onion, e.g. More...
 

Public Attributes

std::unique_ptr< TransportDeserializerm_deserializer
 
std::unique_ptr< TransportSerializerm_serializer
 
NetPermissionFlags m_permissionFlags {PF_NONE}
 
std::atomic< ServiceFlagsnServices {NODE_NONE}
 
Mutex cs_vSend
 
Mutex cs_hSocket
 
Mutex cs_vRecv
 
RecursiveMutex cs_vProcessMsg
 
size_t nProcessQueueSize {0}
 
RecursiveMutex cs_sendProcessing
 
std::atomic< int64_t > nLastSend {0}
 
std::atomic< int64_t > nLastRecv {0}
 
const int64_t nTimeConnected
 
std::atomic< int64_t > nTimeOffset {0}
 
const CAddress addr
 
const CAddress addrBind
 
std::atomic< int > nVersion {0}
 
RecursiveMutex cs_SubVer
 
bool m_prefer_evict {false}
 
bool fClient {false}
 
bool m_limited_node {false}
 
std::atomic_bool m_wants_addrv2 {false}
 Whether the peer has signaled support for receiving ADDRv2 (BIP155) messages, implying a preference to receive ADDRv2 instead of ADDR ones. More...
 
std::atomic_bool fSuccessfullyConnected {false}
 
std::atomic_bool fDisconnect {false}
 
bool fSentAddr {false}
 
CSemaphoreGrant grantOutbound
 
std::atomic< int > nRefCount {0}
 
const uint64_t nKeyedNetGroup
 
std::atomic_bool fPauseRecv {false}
 
std::atomic_bool fPauseSend {false}
 
std::atomic< bool > m_bip152_highbandwidth_to {false}
 
std::atomic< bool > m_bip152_highbandwidth_from {false}
 
std::vector< CAddressvAddrToSend
 
std::unique_ptr< CRollingBloomFilterm_addr_known {nullptr}
 
bool fGetAddr {false}
 
std::unique_ptr< TxRelaym_tx_relay
 
std::atomic< int64_t > nLastBlockTime {0}
 UNIX epoch time of the last block received from this peer that we had not yet seen (e.g. More...
 
std::atomic< int64_t > nLastTXTime {0}
 UNIX epoch time of the last transaction received from this peer that we had not yet seen (e.g. More...
 
std::atomic< uint64_t > nPingNonceSent {0}
 
std::atomic< std::chrono::microseconds > m_ping_start {0us}
 When the last ping was sent, or 0 if no ping was ever sent. More...
 
std::atomic< int64_t > nPingUsecTime {0}
 
std::atomic< int64_t > nMinPingUsecTime {std::numeric_limits<int64_t>::max()}
 
std::atomic< bool > fPingQueued {false}
 

Private Member Functions

std::string addrName GUARDED_BY (cs_addrName)
 
CService addrLocal GUARDED_BY (cs_addrLocal)
 
mapMsgCmdSize mapSendBytesPerMsgCmd GUARDED_BY (cs_vSend)
 
mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY (cs_vRecv)
 

Private Attributes

const NodeId id
 
const uint64_t nLocalHostNonce
 
const ConnectionType m_conn_type
 
std::atomic< int > m_greatest_common_version {INIT_PROTO_VERSION}
 
const ServiceFlags nLocalServices
 Services offered to this peer. More...
 
std::list< CNetMessagevRecvMsg
 
RecursiveMutex cs_addrName
 
RecursiveMutex cs_addrLocal
 
const bool m_inbound_onion {false}
 Whether this peer is an inbound onion, e.g. connected via our Tor onion service. More...
 

Friends

class CConnman
 
struct ConnmanTestMsg
 

Detailed Description

Information about a peer.

Definition at line 388 of file net.h.

Constructor & Destructor Documentation

◆ CNode() [1/2]

CNode::CNode ( NodeId  id,
ServiceFlags  nLocalServicesIn,
SOCKET  hSocketIn,
const CAddress addrIn,
uint64_t  nKeyedNetGroupIn,
uint64_t  nLocalHostNonceIn,
const CAddress addrBindIn,
const std::string &  addrNameIn,
ConnectionType  conn_type_in,
bool  inbound_onion = false 
)

Definition at line 2817 of file net.cpp.

Here is the call graph for this function:

◆ ~CNode()

CNode::~CNode ( )

Definition at line 2853 of file net.cpp.

Here is the call graph for this function:

◆ CNode() [2/2]

CNode::CNode ( const CNode )
delete

Member Function Documentation

◆ AddAddressKnown()

void CNode::AddAddressKnown ( const CAddress _addr)
inline

Definition at line 652 of file net.h.

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

◆ AddKnownTx()

void CNode::AddKnownTx ( const uint256 hash)
inline

Definition at line 683 of file net.h.

Here is the caller graph for this function:

◆ AddRef()

CNode* CNode::AddRef ( )
inline

Definition at line 641 of file net.h.

Here is the caller graph for this function:

◆ CloseSocketDisconnect()

void CNode::CloseSocketDisconnect ( )

Definition at line 484 of file net.cpp.

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

◆ ConnectedThroughNetwork()

Network CNode::ConnectedThroughNetwork ( ) const

Get network the peer connected through.

Returns Network::NET_ONION for inbound onion connections, and CNetAddr::GetNetClass() otherwise. The latter cannot be used directly because it doesn't detect the former, and it's not the responsibility of the CNetAddr class to know the actual network a peer is connected through.

Returns
network the peer connected through.

Definition at line 547 of file net.cpp.

◆ ConnectionTypeAsString()

std::string CNode::ConnectionTypeAsString ( ) const
inline

Definition at line 713 of file net.h.

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

◆ copyStats()

void CNode::copyStats ( CNodeStats stats,
const std::vector< bool > &  m_asmap 
)

Definition at line 554 of file net.cpp.

Here is the call graph for this function:

◆ ExpectServicesFromConn()

bool CNode::ExpectServicesFromConn ( ) const
inline

Definition at line 504 of file net.h.

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

◆ GetAddrLocal()

CService CNode::GetAddrLocal ( ) const

Definition at line 533 of file net.cpp.

Here is the caller graph for this function:

◆ GetAddrName()

std::string CNode::GetAddrName ( ) const

Definition at line 521 of file net.cpp.

Here is the caller graph for this function:

◆ GetCommonVersion()

int CNode::GetCommonVersion ( ) const
inline

Definition at line 632 of file net.h.

Here is the caller graph for this function:

◆ GetId()

NodeId CNode::GetId ( ) const
inline

Definition at line 602 of file net.h.

Here is the caller graph for this function:

◆ GetLocalNonce()

uint64_t CNode::GetLocalNonce ( ) const
inline

Definition at line 606 of file net.h.

◆ GetLocalServices()

ServiceFlags CNode::GetLocalServices ( ) const
inline

Definition at line 704 of file net.h.

Here is the caller graph for this function:

◆ GetRefCount()

int CNode::GetRefCount ( ) const
inline

Definition at line 610 of file net.h.

Here is the call graph for this function:

◆ GUARDED_BY() [1/14]

SOCKET hSocket CNode::GUARDED_BY ( cs_hSocket  )

◆ GUARDED_BY() [2/14]

size_t nSendSize CNode::GUARDED_BY ( cs_vSend  )
inline

Total size of all vSendMsg entries.

Definition at line 401 of file net.h.

◆ GUARDED_BY() [3/14]

size_t nSendOffset CNode::GUARDED_BY ( cs_vSend  )
inline

Offset inside the first vSendMsg already sent.

Definition at line 403 of file net.h.

◆ GUARDED_BY() [4/14]

uint64_t nSendBytes CNode::GUARDED_BY ( cs_vSend  )
inline

Definition at line 404 of file net.h.

Here is the call graph for this function:

◆ GUARDED_BY() [5/14]

std::deque<std::vector<unsigned char> > vSendMsg CNode::GUARDED_BY ( cs_vSend  )

◆ GUARDED_BY() [6/14]

std::list<CNetMessage> vProcessMsg CNode::GUARDED_BY ( cs_vProcessMsg  )

◆ GUARDED_BY() [7/14]

uint64_t nRecvBytes CNode::GUARDED_BY ( cs_vRecv  )
inline

Definition at line 416 of file net.h.

◆ GUARDED_BY() [8/14]

std::string cleanSubVer CNode::GUARDED_BY ( cs_SubVer  )
inline

cleanSubVer is a sanitized string of the user agent byte array we read from the wire.

This cleaned string can safely be logged or displayed.

Definition at line 432 of file net.h.

◆ GUARDED_BY() [9/14]

std::chrono::microseconds m_next_addr_send CNode::GUARDED_BY ( cs_sendProcessing  )
inline

Definition at line 540 of file net.h.

◆ GUARDED_BY() [10/14]

std::chrono::microseconds m_next_local_addr_send CNode::GUARDED_BY ( cs_sendProcessing  )
inline

Definition at line 541 of file net.h.

◆ GUARDED_BY() [11/14]

std::string addrName CNode::GUARDED_BY ( cs_addrName  )
private

◆ GUARDED_BY() [12/14]

CService addrLocal CNode::GUARDED_BY ( cs_addrLocal  )
private

◆ GUARDED_BY() [13/14]

mapMsgCmdSize mapSendBytesPerMsgCmd CNode::GUARDED_BY ( cs_vSend  )
private

◆ GUARDED_BY() [14/14]

mapMsgCmdSize mapRecvBytesPerMsgCmd CNode::GUARDED_BY ( cs_vRecv  )
private

◆ HasPermission()

bool CNode::HasPermission ( NetPermissionFlags  permission) const
inline

Definition at line 434 of file net.h.

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

◆ IsAddrCompatible()

bool CNode::IsAddrCompatible ( const CAddress addr) const
inline

Whether the peer supports the address.

For example, a peer that does not implement BIP155 cannot receive Tor v3 addresses because it requires ADDRv2 (BIP155) encoding.

Definition at line 663 of file net.h.

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

◆ IsAddrFetchConn()

bool CNode::IsAddrFetchConn ( ) const
inline

Definition at line 487 of file net.h.

Here is the caller graph for this function:

◆ IsBlockOnlyConn()

bool CNode::IsBlockOnlyConn ( ) const
inline

Definition at line 479 of file net.h.

Here is the caller graph for this function:

◆ IsFeelerConn()

bool CNode::IsFeelerConn ( ) const
inline

Definition at line 483 of file net.h.

Here is the caller graph for this function:

◆ IsFullOutboundConn()

bool CNode::IsFullOutboundConn ( ) const
inline

Definition at line 471 of file net.h.

Here is the caller graph for this function:

◆ IsInboundConn()

bool CNode::IsInboundConn ( ) const
inline

Definition at line 491 of file net.h.

Here is the caller graph for this function:

◆ IsInboundOnion()

bool CNode::IsInboundOnion ( ) const
inline

Whether this peer is an inbound onion, e.g.

connected via our Tor onion service.

Definition at line 716 of file net.h.

◆ IsManualConn()

bool CNode::IsManualConn ( ) const
inline

Definition at line 475 of file net.h.

Here is the caller graph for this function:

◆ IsOutboundOrBlockRelayConn()

bool CNode::IsOutboundOrBlockRelayConn ( ) const
inline

Definition at line 456 of file net.h.

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

◆ MaybeSetAddrName()

void CNode::MaybeSetAddrName ( const std::string &  addrNameIn)

Sets the addrName only if it was not previously set.

Definition at line 526 of file net.cpp.

Here is the caller graph for this function:

◆ operator=()

CNode& CNode::operator= ( const CNode )
delete

◆ PushAddress()

void CNode::PushAddress ( const CAddress _addr,
FastRandomContext insecure_rand 
)
inline

Definition at line 668 of file net.h.

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

◆ PushTxInventory()

void CNode::PushTxInventory ( const uint256 hash)
inline

Definition at line 691 of file net.h.

Here is the caller graph for this function:

◆ ReceiveMsgBytes()

bool CNode::ReceiveMsgBytes ( Span< const uint8_t >  msg_bytes,
bool &  complete 
)

Receive bytes from the buffer and deserialize them into messages.

Parameters
[in]msg_bytesThe raw data
[out]completeSet True if at least one message has been deserialized and is ready to be processed
Returns
True if the peer should stay connected, False if the peer should be disconnected from.

Definition at line 624 of file net.cpp.

Here is the call graph for this function:

◆ RelayAddrsWithConn()

bool CNode::RelayAddrsWithConn ( ) const
inline

Definition at line 496 of file net.h.

Here is the caller graph for this function:

◆ Release()

void CNode::Release ( )
inline

Definition at line 647 of file net.h.

◆ SetAddrLocal()

void CNode::SetAddrLocal ( const CService addrLocalIn)

May not be called more than once.

Definition at line 538 of file net.cpp.

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

◆ SetCommonVersion()

void CNode::SetCommonVersion ( int  greatest_common_version)
inline

Definition at line 627 of file net.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ CConnman

friend class CConnman
friend

Definition at line 390 of file net.h.

◆ ConnmanTestMsg

friend struct ConnmanTestMsg
friend

Definition at line 391 of file net.h.

Member Data Documentation

◆ addr

const CAddress CNode::addr

Definition at line 423 of file net.h.

◆ addrBind

const CAddress CNode::addrBind

Definition at line 425 of file net.h.

◆ cs_addrLocal

RecursiveMutex CNode::cs_addrLocal
mutableprivate

Definition at line 748 of file net.h.

◆ cs_addrName

RecursiveMutex CNode::cs_addrName
mutableprivate

Definition at line 743 of file net.h.

◆ cs_hSocket

Mutex CNode::cs_hSocket

Definition at line 407 of file net.h.

◆ cs_sendProcessing

RecursiveMutex CNode::cs_sendProcessing

Definition at line 414 of file net.h.

◆ cs_SubVer

RecursiveMutex CNode::cs_SubVer

Definition at line 427 of file net.h.

◆ cs_vProcessMsg

RecursiveMutex CNode::cs_vProcessMsg

Definition at line 410 of file net.h.

◆ cs_vRecv

Mutex CNode::cs_vRecv

Definition at line 408 of file net.h.

◆ cs_vSend

Mutex CNode::cs_vSend

Definition at line 406 of file net.h.

◆ fClient

bool CNode::fClient {false}

Definition at line 437 of file net.h.

◆ fDisconnect

std::atomic_bool CNode::fDisconnect {false}

Definition at line 447 of file net.h.

◆ fGetAddr

bool CNode::fGetAddr {false}

Definition at line 539 of file net.h.

◆ fPauseRecv

std::atomic_bool CNode::fPauseRecv {false}

Definition at line 453 of file net.h.

◆ fPauseSend

std::atomic_bool CNode::fPauseSend {false}

Definition at line 454 of file net.h.

◆ fPingQueued

std::atomic<bool> CNode::fPingQueued {false}

Definition at line 595 of file net.h.

◆ fSentAddr

bool CNode::fSentAddr {false}

Definition at line 448 of file net.h.

◆ fSuccessfullyConnected

std::atomic_bool CNode::fSuccessfullyConnected {false}

Definition at line 444 of file net.h.

◆ grantOutbound

CSemaphoreGrant CNode::grantOutbound

Definition at line 449 of file net.h.

◆ id

const NodeId CNode::id
private

Definition at line 719 of file net.h.

◆ m_addr_known

std::unique_ptr<CRollingBloomFilter> CNode::m_addr_known {nullptr}

Definition at line 538 of file net.h.

◆ m_bip152_highbandwidth_from

std::atomic<bool> CNode::m_bip152_highbandwidth_from {false}

Definition at line 534 of file net.h.

◆ m_bip152_highbandwidth_to

std::atomic<bool> CNode::m_bip152_highbandwidth_to {false}

Definition at line 532 of file net.h.

◆ m_conn_type

const ConnectionType CNode::m_conn_type
private

Definition at line 721 of file net.h.

◆ m_deserializer

std::unique_ptr<TransportDeserializer> CNode::m_deserializer

Definition at line 394 of file net.h.

◆ m_greatest_common_version

std::atomic<int> CNode::m_greatest_common_version {INIT_PROTO_VERSION}
private

Definition at line 722 of file net.h.

◆ m_inbound_onion

const bool CNode::m_inbound_onion {false}
private

Whether this peer is an inbound onion, e.g. connected via our Tor onion service.

Definition at line 751 of file net.h.

◆ m_limited_node

bool CNode::m_limited_node {false}

Definition at line 438 of file net.h.

◆ m_permissionFlags

NetPermissionFlags CNode::m_permissionFlags {PF_NONE}

Definition at line 397 of file net.h.

◆ m_ping_start

std::atomic<std::chrono::microseconds> CNode::m_ping_start {0us}

When the last ping was sent, or 0 if no ping was ever sent.

Definition at line 589 of file net.h.

◆ m_prefer_evict

bool CNode::m_prefer_evict {false}

Definition at line 433 of file net.h.

◆ m_serializer

std::unique_ptr<TransportSerializer> CNode::m_serializer

Definition at line 395 of file net.h.

◆ m_tx_relay

std::unique_ptr<TxRelay> CNode::m_tx_relay

Definition at line 570 of file net.h.

◆ m_wants_addrv2

std::atomic_bool CNode::m_wants_addrv2 {false}

Whether the peer has signaled support for receiving ADDRv2 (BIP155) messages, implying a preference to receive ADDRv2 instead of ADDR ones.

Definition at line 443 of file net.h.

◆ nKeyedNetGroup

const uint64_t CNode::nKeyedNetGroup

Definition at line 452 of file net.h.

◆ nLastBlockTime

std::atomic<int64_t> CNode::nLastBlockTime {0}

UNIX epoch time of the last block received from this peer that we had not yet seen (e.g.

not already received from another peer), that passed preliminary validity checks and was saved to disk, even if we don't connect the block or it eventually fails connection. Used as an inbound peer eviction criterium in CConnman::AttemptToEvictConnection.

Definition at line 577 of file net.h.

◆ nLastRecv

std::atomic<int64_t> CNode::nLastRecv {0}

Definition at line 419 of file net.h.

◆ nLastSend

std::atomic<int64_t> CNode::nLastSend {0}

Definition at line 418 of file net.h.

◆ nLastTXTime

std::atomic<int64_t> CNode::nLastTXTime {0}

UNIX epoch time of the last transaction received from this peer that we had not yet seen (e.g.

not already received from another peer) and that was accepted into our mempool. Used as an inbound peer eviction criterium in CConnman::AttemptToEvictConnection.

Definition at line 583 of file net.h.

◆ nLocalHostNonce

const uint64_t CNode::nLocalHostNonce
private

Definition at line 720 of file net.h.

◆ nLocalServices

const ServiceFlags CNode::nLocalServices
private

Services offered to this peer.

This is supplied by the parent CConnman during peer connection (CConnman::ConnectNode()) from its attribute of the same name.

This is const because there is no protocol defined for renegotiating services initially offered to a peer. The set of local services we offer should not change after initialization.

An interesting example of this is NODE_NETWORK and initial block download: a node which starts up from scratch doesn't have any blocks to serve, but still advertises NODE_NETWORK because it will eventually fulfill this role after IBD completes. P2P code is written in such a way that it can gracefully handle peers who don't make good on their service advertisements.

Definition at line 739 of file net.h.

◆ nMinPingUsecTime

std::atomic<int64_t> CNode::nMinPingUsecTime {std::numeric_limits<int64_t>::max()}

Definition at line 593 of file net.h.

◆ nPingNonceSent

std::atomic<uint64_t> CNode::nPingNonceSent {0}

Definition at line 587 of file net.h.

◆ nPingUsecTime

std::atomic<int64_t> CNode::nPingUsecTime {0}

Definition at line 591 of file net.h.

◆ nProcessQueueSize

size_t CNode::nProcessQueueSize {0}

Definition at line 412 of file net.h.

◆ nRefCount

std::atomic<int> CNode::nRefCount {0}

Definition at line 450 of file net.h.

◆ nServices

std::atomic<ServiceFlags> CNode::nServices {NODE_NONE}

Definition at line 398 of file net.h.

◆ nTimeConnected

const int64_t CNode::nTimeConnected

Definition at line 420 of file net.h.

◆ nTimeOffset

std::atomic<int64_t> CNode::nTimeOffset {0}

Definition at line 421 of file net.h.

◆ nVersion

std::atomic<int> CNode::nVersion {0}

Definition at line 426 of file net.h.

◆ vAddrToSend

std::vector<CAddress> CNode::vAddrToSend

Definition at line 537 of file net.h.

◆ vRecvMsg

std::list<CNetMessage> CNode::vRecvMsg
private

Definition at line 741 of file net.h.


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