diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -1053,7 +1053,7 @@ * (nullopt = all). */ std::vector GetAddresses(size_t max_addresses, size_t max_pct, - std::optional network); + std::optional network) const; /** * Cache is used to minimize topology leaks, so it should * be used for all non-trusted calls, for example, p2p. @@ -1066,7 +1066,7 @@ // This allows temporarily exceeding m_max_outbound_full_relay, with the // goal of finding a peer that is better than all our current peers. void SetTryNewOutboundPeer(bool flag); - bool GetTryNewOutboundPeer(); + bool GetTryNewOutboundPeer() const; void StartExtraBlockRelayPeers() { LogPrint(BCLog::NET, "net: enabling extra block-relay-only peers\n"); @@ -1079,13 +1079,13 @@ // return a value less than (num_outbound_connections - num_outbound_slots) // in cases where some outbound connections are not yet fully connected, or // not yet fully disconnected. - int GetExtraFullOutboundCount(); + int GetExtraFullOutboundCount() const; // Count the number of block-relay-only peers we have over our limit. - int GetExtraBlockRelayCount(); + int GetExtraBlockRelayCount() const; bool AddNode(const std::string &node); bool RemoveAddedNode(const std::string &node); - std::vector GetAddedNodeInfo(); + std::vector GetAddedNodeInfo() const; /** * Attempts to open a connection. Currently only used from tests. @@ -1103,8 +1103,8 @@ */ bool AddConnection(const std::string &address, ConnectionType conn_type); - size_t GetNodeCount(NumConnections num); - void GetNodeStats(std::vector &vstats); + size_t GetNodeCount(NumConnections num) const; + void GetNodeStats(std::vector &vstats) const; bool DisconnectNode(const std::string &node); bool DisconnectNode(const CSubNet &subnet); bool DisconnectNode(const CNetAddr &addr); @@ -1118,24 +1118,24 @@ //! that peer during `net_processing.cpp:PushNodeVersion()`. ServiceFlags GetLocalServices() const; - uint64_t GetMaxOutboundTarget(); - std::chrono::seconds GetMaxOutboundTimeframe(); + uint64_t GetMaxOutboundTarget() const; + std::chrono::seconds GetMaxOutboundTimeframe() const; //! check if the outbound target is reached. If param //! historicalBlockServingLimit is set true, the function will response true //! if the limit for serving historical blocks has been reached. - bool OutboundTargetReached(bool historicalBlockServingLimit); + bool OutboundTargetReached(bool historicalBlockServingLimit) const; //! response the bytes left in the current max outbound cycle in case of no //! limit, it will always response 0 - uint64_t GetOutboundTargetBytesLeft(); + uint64_t GetOutboundTargetBytesLeft() const; //! returns the time in second left in the current max outbound cycle in //! case of no limit, it will always return 0 - std::chrono::seconds GetMaxOutboundTimeLeftInCycle(); + std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const; - uint64_t GetTotalBytesRecv(); - uint64_t GetTotalBytesSent(); + uint64_t GetTotalBytesRecv() const; + uint64_t GetTotalBytesSent() const; /** Get a unique deterministic randomizer. */ CSipHasher GetDeterministicRandomizer(uint64_t id) const; @@ -1259,8 +1259,8 @@ const Config *config; // Network usage totals - RecursiveMutex cs_totalBytesRecv; - RecursiveMutex cs_totalBytesSent; + mutable RecursiveMutex cs_totalBytesRecv; + mutable RecursiveMutex cs_totalBytesSent; uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv){0}; uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent){0}; @@ -1287,7 +1287,7 @@ std::deque m_addr_fetches GUARDED_BY(m_addr_fetches_mutex); RecursiveMutex m_addr_fetches_mutex; std::vector vAddedNodes GUARDED_BY(cs_vAddedNodes); - RecursiveMutex cs_vAddedNodes; + mutable RecursiveMutex cs_vAddedNodes; std::vector vNodes GUARDED_BY(cs_vNodes); std::list vNodesDisconnected; mutable RecursiveMutex cs_vNodes; diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2067,7 +2067,7 @@ } } -bool CConnman::GetTryNewOutboundPeer() { +bool CConnman::GetTryNewOutboundPeer() const { return m_try_another_outbound_peer; } @@ -2083,7 +2083,7 @@ // Also exclude peers that haven't finished initial connection handshake yet (so // that we don't decide we're over our desired connection limit, and then evict // some peer that has finished the handshake). -int CConnman::GetExtraFullOutboundCount() { +int CConnman::GetExtraFullOutboundCount() const { int full_outbound_peers = 0; { LOCK(cs_vNodes); @@ -2099,7 +2099,7 @@ 0); } -int CConnman::GetExtraBlockRelayCount() { +int CConnman::GetExtraBlockRelayCount() const { int block_relay_peers = 0; { LOCK(cs_vNodes); @@ -2470,7 +2470,7 @@ return ret; } -std::vector CConnman::GetAddedNodeInfo() { +std::vector CConnman::GetAddedNodeInfo() const { std::vector ret; std::list lAddresses(0); @@ -3143,9 +3143,9 @@ Stop(); } -std::vector CConnman::GetAddresses(size_t max_addresses, - size_t max_pct, - std::optional network) { +std::vector +CConnman::GetAddresses(size_t max_addresses, size_t max_pct, + std::optional network) const { std::vector addresses = addrman.GetAddr(max_addresses, max_pct, network); if (m_banman) { @@ -3234,7 +3234,7 @@ return false; } -size_t CConnman::GetNodeCount(NumConnections flags) { +size_t CConnman::GetNodeCount(NumConnections flags) const { LOCK(cs_vNodes); // Shortcut if we want total if (flags == CConnman::CONNECTIONS_ALL) { @@ -3252,7 +3252,7 @@ return nNum; } -void CConnman::GetNodeStats(std::vector &vstats) { +void CConnman::GetNodeStats(std::vector &vstats) const { vstats.clear(); LOCK(cs_vNodes); vstats.reserve(vNodes.size()); @@ -3328,16 +3328,16 @@ nMaxOutboundTotalBytesSentInCycle += bytes; } -uint64_t CConnman::GetMaxOutboundTarget() { +uint64_t CConnman::GetMaxOutboundTarget() const { LOCK(cs_totalBytesSent); return nMaxOutboundLimit; } -std::chrono::seconds CConnman::GetMaxOutboundTimeframe() { +std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const { return MAX_UPLOAD_TIMEFRAME; } -std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() { +std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) { return 0s; @@ -3353,7 +3353,7 @@ return (cycleEndTime < now) ? 0s : cycleEndTime - now; } -bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) { +bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) { return false; @@ -3376,7 +3376,7 @@ return false; } -uint64_t CConnman::GetOutboundTargetBytesLeft() { +uint64_t CConnman::GetOutboundTargetBytesLeft() const { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) { return 0; @@ -3387,12 +3387,12 @@ : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle; } -uint64_t CConnman::GetTotalBytesRecv() { +uint64_t CConnman::GetTotalBytesRecv() const { LOCK(cs_totalBytesRecv); return nTotalBytesRecv; } -uint64_t CConnman::GetTotalBytesSent() { +uint64_t CConnman::GetTotalBytesSent() const { LOCK(cs_totalBytesSent); return nTotalBytesSent; } diff --git a/src/net_processing.h b/src/net_processing.h --- a/src/net_processing.h +++ b/src/net_processing.h @@ -60,7 +60,8 @@ virtual void StartScheduledTasks(CScheduler &scheduler) = 0; /** Get statistics from node state */ - virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) = 0; + virtual bool GetNodeStateStats(NodeId nodeid, + CNodeStateStats &stats) const = 0; /** Whether this node ignores txs received over p2p. */ virtual bool IgnoresIncomingTxs() = 0; diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -494,7 +494,8 @@ /** Implement PeerManager */ void StartScheduledTasks(CScheduler &scheduler) override; void CheckForStaleTipAndEvictPeers() override; - bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) override; + bool GetNodeStateStats(NodeId nodeid, + CNodeStateStats &stats) const override; bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } void SendPings() override; void RelayTransaction(const TxId &txid) override; @@ -1867,7 +1868,8 @@ return ret; } -bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { +bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, + CNodeStateStats &stats) const { { LOCK(cs_main); CNodeState *state = State(nodeid);