Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
751const uint64_t nKeyedNetGroup;752std::atomic_bool fPauseRecv{false};753std::atomic_bool fPauseSend{false};754/** When was fPauseSend last set from false to true */755std::atomic<NodeClock::time_point> m_pause_send_since{NodeClock::epoch};757/** Network key used to prevent fingerprinting our node across networks.758* Influenced by the network and the bind address (+ bind port for inbounds) */759const uint64_t m_network_key;761const ConnectionType m_conn_type;763/** Move all messages from the received queue to the processing queue. */764void MarkReceivedMsgsForProcessing()765EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);767/** Poll the next message from the processing queue of this connection.768*769* Returns std::nullopt if the processing queue is empty, or a pair770* consisting of the message and a bool that indicates if the processing771* queue has more entries. */772std::optional<std::pair<CNetMessage, bool>> PollMessage()773EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);775/** Account for the total size of a sent message in the per msg type connection stats. */776void AccountForSentBytes(const std::string& msg_type, size_t sent_bytes)777EXCLUSIVE_LOCKS_REQUIRED(cs_vSend)778{779mapSendBytesPerMsgType[msg_type] += sent_bytes;780}782bool IsOutboundOrBlockRelayConn() const {783switch (m_conn_type) {784case ConnectionType::OUTBOUND_FULL_RELAY:785case ConnectionType::BLOCK_RELAY:786return true;787case ConnectionType::INBOUND:788case ConnectionType::MANUAL:789case ConnectionType::ADDR_FETCH:790case ConnectionType::FEELER:791case ConnectionType::PRIVATE_BROADCAST:792return false;793} // no default case, so the compiler can warn about missing cases795assert(false);796}798bool IsFullOutboundConn() const {799return m_conn_type == ConnectionType::OUTBOUND_FULL_RELAY;800}802bool IsManualConn() const {803return m_conn_type == ConnectionType::MANUAL;804}806bool IsManualOrFullOutboundConn() const807{808switch (m_conn_type) {809case ConnectionType::INBOUND:810case ConnectionType::FEELER:811case ConnectionType::BLOCK_RELAY:812case ConnectionType::ADDR_FETCH:813case ConnectionType::PRIVATE_BROADCAST:814return false;815case ConnectionType::OUTBOUND_FULL_RELAY:816case ConnectionType::MANUAL:817return true;818} // no default case, so the compiler can warn about missing cases820assert(false);821}823bool IsBlockOnlyConn() const {824return m_conn_type == ConnectionType::BLOCK_RELAY;825}827bool IsFeelerConn() const {828return m_conn_type == ConnectionType::FEELER;829}831bool IsAddrFetchConn() const {832return m_conn_type == ConnectionType::ADDR_FETCH;833}835bool IsPrivateBroadcastConn() const836{837return m_conn_type == ConnectionType::PRIVATE_BROADCAST;838}840/** Protocol version advertised in our VERSION message.841* Private broadcast connections use a fixed version to maximise anonymity. */842int AdvertisedVersion() const843{844return IsPrivateBroadcastConn() ? WTXID_RELAY_VERSION : PROTOCOL_VERSION;845}847bool IsInboundConn() const {848return m_conn_type == ConnectionType::INBOUND;849}851bool ExpectServicesFromConn() const {852switch (m_conn_type) {853case ConnectionType::INBOUND:854case ConnectionType::MANUAL:855case ConnectionType::FEELER:856return false;857case ConnectionType::OUTBOUND_FULL_RELAY:858case ConnectionType::BLOCK_RELAY:859case ConnectionType::ADDR_FETCH:860case ConnectionType::PRIVATE_BROADCAST:861return true;862} // no default case, so the compiler can warn about missing cases864assert(false);865}867/**868* Get network the peer connected through.869*870* Returns Network::NET_ONION for *inbound* onion connections,871* and CNetAddr::GetNetClass() otherwise. The latter cannot be used directly872* because it doesn't detect the former, and it's not the responsibility of873* the CNetAddr class to know the actual network a peer is connected through.874*875* @return network the peer connected through.876*/877Network ConnectedThroughNetwork() const;879/** Whether this peer connected through a privacy network. */880[[nodiscard]] bool IsConnectedThroughPrivacyNet() const;882// We selected peer as (compact blocks) high-bandwidth peer (BIP152)883std::atomic<bool> m_bip152_highbandwidth_to{false};884// Peer selected us as (compact blocks) high-bandwidth peer (BIP152)885std::atomic<bool> m_bip152_highbandwidth_from{false};887/** Whether this peer provides all services that we want. Used for eviction decisions */888std::atomic_bool m_has_all_wanted_services{false};890/** Whether we should relay transactions to this peer. This only changes891* from false to true. It will never change back to false. */892std::atomic_bool m_relays_txs{false};894/** Whether this peer has loaded a bloom filter. Used only in inbound895* eviction logic. */896std::atomic_bool m_bloom_filter_loaded{false};898/// UNIX epoch time of the last block received from this peer that we had899/// not yet seen (e.g. not already received from another peer), that passed900/// preliminary validity checks and was saved to disk, even if we don't901/// connect the block or it eventually fails to connect. Used as an inbound902/// peer eviction criterion in CConnman::AttemptToEvictConnection.903std::atomic<std::chrono::seconds> m_last_block_time{0s};905/// UNIX epoch time of the last transaction received from this peer that we906/// had not yet seen (e.g. not already received from another peer) and that907/// was accepted into our mempool. Used as an inbound peer eviction criterion908/// in CConnman::AttemptToEvictConnection.909std::atomic<std::chrono::seconds> m_last_tx_time{0s};911/// Last measured round-trip duration. Used only for stats.912std::atomic<NodeClock::duration> m_last_ping_time{0us};914/// Lowest measured round-trip duration. Used as an inbound peer eviction915/// criterion in CConnman::AttemptToEvictConnection.916std::atomic<NodeClock::duration> m_min_ping_time{NodeClock::duration::max()};918CNode(NodeId id,919std::shared_ptr<Sock> sock,920const CAddress& addrIn,921uint64_t nKeyedNetGroupIn,922uint64_t nLocalHostNonceIn,923const CService& addrBindIn,924const std::string& addrNameIn,925ConnectionType conn_type_in,926bool inbound_onion,927uint64_t network_key,928CNodeOptions&& node_opts = {});929CNode(const CNode&) = delete;930CNode& operator=(const CNode&) = delete;932NodeId GetId() const {933return id;934}936uint64_t GetLocalNonce() const {937return nLocalHostNonce;938}940int GetRefCount() const941{942assert(nRefCount >= 0);943return nRefCount;944}946/**947* Receive bytes from the buffer and deserialize them into messages.948*949* @param[in] msg_bytes The raw data950* @param[out] complete Set True if at least one message has been951* deserialized and is ready to be processed952* @return True if the peer should stay connected,953* False if the peer should be disconnected from.954*/955bool ReceiveMsgBytes(std::span<const uint8_t> msg_bytes, bool& complete) EXCLUSIVE_LOCKS_REQUIRED(!cs_vRecv);957void SetCommonVersion(int greatest_common_version)958{959Assume(m_greatest_common_version == INIT_PROTO_VERSION);960m_greatest_common_version = greatest_common_version;961}962int GetCommonVersion() const963{964return m_greatest_common_version;965}967CService GetAddrLocal() const EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex);968//! May not be called more than once969void SetAddrLocal(const CService& addrLocalIn) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex);971CNode* AddRef()972{973nRefCount++;974return this;975}977void Release()978{979nRefCount--;980}982void CloseSocketDisconnect() EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex);984void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);986std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }988/**989* Helper function to log the peer id, optionally including IP address.990*991* @return "peer=..." and optionally ", peeraddr=..."992*/993std::string LogPeer() const;995/**996* Helper function to log disconnects.997*998* @return "disconnecting peer=..." and optionally ", peeraddr=..."999*/1000std::string DisconnectMsg() const;
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.