From 751c9e66c4929dc0cd291646b13eae44a4cd4a48 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 21 Apr 2021 07:20:35 +0200 Subject: [PATCH] Merge bitcoin/bitcoin#21719: refactor: Add and use EnsureConnman in rpc code fafb68add5e16e8bd5b9428bcffcaee2639747cf refactor: Add and use EnsureConnman in rpc code (MarcoFalke) faabeb854a6e46b46e4f26b22dc2c81e68e2d863 refactor: Mark member functions const (MarcoFalke) Pull request description: This removes the 10 occurrences of `throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");` and replaces them with `EnsureConnman`. ACKs for top commit: jarolrod: re-ACK fafb68add5e16e8bd5b9428bcffcaee2639747cf theStack: ACK fafb68add5e16e8bd5b9428bcffcaee2639747cf ryanofsky: Code review ACK fafb68add5e16e8bd5b9428bcffcaee2639747cf Tree-SHA512: 84c63cfe31e548645d906f7191a3526c7bea99ed0d54c2a75c2041452a44fe149ede343d8e1943b0e7770816c828bb047dfec8bc541a1f2b89920a126ee54d68 --- src/Makefile.am | 1 + src/net.cpp | 28 ++++++------ src/net.h | 34 +++++++-------- src/net_processing.cpp | 4 +- src/net_processing.h | 2 +- src/rpc/mining.cpp | 7 ++- src/rpc/misc.cpp | 7 ++- src/rpc/net.cpp | 98 ++++++++++++++++++++++-------------------- src/rpc/net.h | 15 +++++++ 9 files changed, 107 insertions(+), 89 deletions(-) create mode 100644 src/rpc/net.h diff --git a/src/Makefile.am b/src/Makefile.am index 9ca10e66d0..472d88aa2c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -286,6 +286,7 @@ BITCOIN_CORE_H = \ rpc/blockchain.h \ rpc/client.h \ rpc/mining.h \ + rpc/net.h \ rpc/protocol.h \ rpc/rawtransaction_util.h \ rpc/register.h \ diff --git a/src/net.cpp b/src/net.cpp index 9beb8ff700..3e1cd850da 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2256,7 +2256,7 @@ void CConnman::ProcessAddrFetch() } } -bool CConnman::GetTryNewOutboundPeer() +bool CConnman::GetTryNewOutboundPeer() const { return m_try_another_outbound_peer; } @@ -2273,7 +2273,7 @@ void CConnman::SetTryNewOutboundPeer(bool flag) // 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; { @@ -2291,7 +2291,7 @@ int CConnman::GetExtraFullOutboundCount() return std::max(full_outbound_peers - m_max_outbound_full_relay, 0); } -int CConnman::GetExtraBlockRelayCount() +int CConnman::GetExtraBlockRelayCount() const { int block_relay_peers = 0; { @@ -2619,7 +2619,7 @@ std::vector CConnman::GetCurrentBlockRelayOnlyConns() const return ret; } -std::vector CConnman::GetAddedNodeInfo() +std::vector CConnman::GetAddedNodeInfo() const { std::vector ret; @@ -3621,7 +3621,7 @@ CConnman::~CConnman() 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) { @@ -3842,7 +3842,7 @@ void CConnman::AddPendingProbeConnections(const std::set &proTxHashes) masternodePendingProbes.insert(proTxHashes.begin(), proTxHashes.end()); } -size_t CConnman::GetNodeCount(ConnectionDirection flags) +size_t CConnman::GetNodeCount(ConnectionDirection flags) const { LOCK(cs_vNodes); @@ -3869,7 +3869,7 @@ size_t CConnman::GetMaxOutboundNodeCount() return m_max_outbound; } -void CConnman::GetNodeStats(std::vector& vstats) +void CConnman::GetNodeStats(std::vector& vstats) const { vstats.clear(); LOCK(cs_vNodes); @@ -4008,18 +4008,18 @@ void CConnman::RecordBytesSent(uint64_t bytes) 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) @@ -4033,7 +4033,7 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() return (cycleEndTime < now) ? 0s : cycleEndTime - now; } -bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) +bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) @@ -4053,7 +4053,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) return false; } -uint64_t CConnman::GetOutboundTargetBytesLeft() +uint64_t CConnman::GetOutboundTargetBytesLeft() const { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) @@ -4062,13 +4062,13 @@ uint64_t CConnman::GetOutboundTargetBytesLeft() return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : 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.h b/src/net.h index 79c90606e3..75bfe72bac 100644 --- a/src/net.h +++ b/src/net.h @@ -1128,7 +1128,7 @@ public: * @param[in] max_pct Maximum percentage of addresses to return (0 = all). * @param[in] network Select only addresses of this network (nullopt = all). */ - std::vector GetAddresses(size_t max_addresses, size_t max_pct, std::optional network); + std::vector GetAddresses(size_t max_addresses, size_t max_pct, std::optional network) const; /** * Cache is used to minimize topology leaks, so it should @@ -1141,7 +1141,7 @@ public: // 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"); @@ -1154,13 +1154,13 @@ public: // 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. @@ -1187,9 +1187,9 @@ public: bool IsMasternodeQuorumRelayMember(const uint256& protxHash); void AddPendingProbeConnections(const std::set& proTxHashes); - size_t GetNodeCount(ConnectionDirection); + size_t GetNodeCount(ConnectionDirection) const; size_t GetMaxOutboundNodeCount(); - void GetNodeStats(std::vector& vstats); + void GetNodeStats(std::vector& vstats) const; bool DisconnectNode(const std::string& node); bool DisconnectNode(const CSubNet& subnet); bool DisconnectNode(const CNetAddr& addr); @@ -1203,24 +1203,24 @@ public: //! 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 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; @@ -1344,8 +1344,8 @@ private: void UnregisterEvents(CNode* pnode); // 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}; @@ -1371,7 +1371,7 @@ private: 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 vPendingMasternodes; mutable RecursiveMutex cs_vPendingMasternodes; std::map, std::set> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 63e7df604d..7a6b5c2392 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -355,7 +355,7 @@ public: /** Implement PeerManager */ 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 uint256& txid) override; @@ -1440,7 +1440,7 @@ PeerRef PeerManagerImpl::RemovePeer(NodeId id) return ret; } -bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) +bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const { { LOCK(cs_main); diff --git a/src/net_processing.h b/src/net_processing.h index 2ef1b8473c..6b29d2e8ff 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -63,7 +63,7 @@ public: virtual ~PeerManager() { } /** 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/rpc/mining.cpp b/src/rpc/mining.cpp index ead13eddf3..82242f14dd 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include