From a529fd3e3f2391e592ac937e291fec51e067ea2e Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 24 Aug 2020 17:39:54 +0100 Subject: [PATCH 1/4] [net processing] Move GetNodeStateStats into PeerManager --- src/net_processing.cpp | 2 +- src/net_processing.h | 20 ++++++++++---------- src/node/interfaces.cpp | 12 +++++++----- src/rpc/net.cpp | 5 +++-- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index ec5400c3d8e..d65b066dbdb 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -887,7 +887,7 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) { LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid); } -bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { +bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { { LOCK(cs_main); CNodeState* state = State(nodeid); diff --git a/src/net_processing.h b/src/net_processing.h index 87eee566deb..8f4ba4c0bbc 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -32,6 +32,13 @@ static const bool DEFAULT_PEERBLOCKFILTERS = false; /** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */ static const int DISCOURAGEMENT_THRESHOLD{100}; +struct CNodeStateStats { + int m_misbehavior_score = 0; + int nSyncHeight = -1; + int nCommonHeight = -1; + std::vector vHeightInFlight; +}; + class PeerManager final : public CValidationInterface, public NetEventsInterface { public: PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman, @@ -94,6 +101,9 @@ public: */ void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message); + /** Get statistics from node state */ + bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats); + private: /** * Potentially mark a node discouraged based on the contents of a BlockValidationState object @@ -145,16 +155,6 @@ private: int64_t m_stale_tip_check_time; //!< Next time to check for stale tip }; -struct CNodeStateStats { - int m_misbehavior_score = 0; - int nSyncHeight = -1; - int nCommonHeight = -1; - std::vector vHeightInFlight; -}; - -/** Get statistics from node state */ -bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats); - /** Relay transaction to every node */ void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main); diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index a8c8be05fb0..3c315c0a536 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -121,11 +121,13 @@ public: } // Try to retrieve the CNodeStateStats for each node. - TRY_LOCK(::cs_main, lockMain); - if (lockMain) { - for (auto& node_stats : stats) { - std::get<1>(node_stats) = - GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats)); + if (m_context->peerman) { + TRY_LOCK(::cs_main, lockMain); + if (lockMain) { + for (auto& node_stats : stats) { + std::get<1>(node_stats) = + m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats)); + } } } return true; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index e72ef24d120..fa71ea11816 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -165,8 +165,9 @@ static RPCHelpMan getpeerinfo() [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { NodeContext& node = EnsureNodeContext(request.context); - if(!node.connman) + if(!node.connman || !node.peerman) { throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); + } std::vector vstats; node.connman->GetNodeStats(vstats); @@ -176,7 +177,7 @@ static RPCHelpMan getpeerinfo() for (const CNodeStats& stats : vstats) { UniValue obj(UniValue::VOBJ); CNodeStateStats statestats; - bool fStateStats = GetNodeStateStats(stats.nodeid, statestats); + bool fStateStats = node.peerman->GetNodeStateStats(stats.nodeid, statestats); obj.pushKV("id", stats.nodeid); obj.pushKV("addr", stats.addrName); if (stats.addrBind.IsValid()) { From ed7e469ceec6f7101a3fb7b15c21a6fb69697866 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Mon, 7 Sep 2020 18:12:19 +0100 Subject: [PATCH 2/4] [net_processing] Move peer_map to PeerManager --- src/net_processing.cpp | 67 +++++++----------------------------------- src/net_processing.h | 48 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index d65b066dbdb..ec8d644813d 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -422,58 +422,6 @@ static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return &it->second; } -/** - * Data structure for an individual peer. This struct is not protected by - * cs_main since it does not contain validation-critical data. - * - * Memory is owned by shared pointers and this object is destructed when - * the refcount drops to zero. - * - * TODO: move most members from CNodeState to this structure. - * TODO: move remaining application-layer data members from CNode to this structure. - */ -struct Peer { - /** Same id as the CNode object for this peer */ - const NodeId m_id{0}; - - /** Protects misbehavior data members */ - Mutex m_misbehavior_mutex; - /** Accumulated misbehavior score for this peer */ - int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0}; - /** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */ - bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false}; - - /** Set of txids to reconsider once their parent transactions have been accepted **/ - std::set m_orphan_work_set GUARDED_BY(g_cs_orphans); - - /** Protects m_getdata_requests **/ - Mutex m_getdata_requests_mutex; - /** Work queue of items requested by this peer **/ - std::deque m_getdata_requests GUARDED_BY(m_getdata_requests_mutex); - - explicit Peer(NodeId id) : m_id(id) {} -}; - -using PeerRef = std::shared_ptr; - -/** - * Map of all Peer objects, keyed by peer id. This map is protected - * by the global g_peer_mutex. Once a shared pointer reference is - * taken, the lock may be released. Individual fields are protected by - * their own locks. - */ -Mutex g_peer_mutex; -static std::map g_peer_map GUARDED_BY(g_peer_mutex); - -/** Get a shared pointer to the Peer object. - * May return nullptr if the Peer object can't be found. */ -static PeerRef GetPeerRef(NodeId id) -{ - LOCK(g_peer_mutex); - auto it = g_peer_map.find(id); - return it != g_peer_map.end() ? it->second : nullptr; -} - static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { nPreferredDownload -= state->fPreferredDownload; @@ -807,8 +755,8 @@ void PeerManager::InitializeNode(CNode *pnode) { } { PeerRef peer = std::make_shared(nodeid); - LOCK(g_peer_mutex); - g_peer_map.emplace_hint(g_peer_map.end(), nodeid, std::move(peer)); + LOCK(m_peer_mutex); + m_peer_map.emplace_hint(m_peer_map.end(), nodeid, std::move(peer)); } if (!pnode->IsInboundConn()) { PushNodeVersion(*pnode, m_connman, GetTime()); @@ -845,8 +793,8 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) { PeerRef peer = GetPeerRef(nodeid); assert(peer != nullptr); misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score); - LOCK(g_peer_mutex); - g_peer_map.erase(nodeid); + LOCK(m_peer_mutex); + m_peer_map.erase(nodeid); } CNodeState *state = State(nodeid); assert(state != nullptr); @@ -887,6 +835,13 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) { LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid); } +PeerRef PeerManager::GetPeerRef(NodeId id) +{ + LOCK(m_peer_mutex); + auto it = m_peer_map.find(id); + return it != m_peer_map.end() ? it->second : nullptr; +} + bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { { LOCK(cs_main); diff --git a/src/net_processing.h b/src/net_processing.h index 8f4ba4c0bbc..0697cf82c3b 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -39,6 +39,40 @@ struct CNodeStateStats { std::vector vHeightInFlight; }; +/** + * Data structure for an individual peer. This struct is not protected by + * cs_main since it does not contain validation-critical data. + * + * Memory is owned by shared pointers and this object is destructed when + * the refcount drops to zero. + * + * TODO: move most members from CNodeState to this structure. + * TODO: move remaining application-layer data members from CNode to this structure. + */ +struct Peer { + /** Same id as the CNode object for this peer */ + const NodeId m_id{0}; + + /** Protects misbehavior data members */ + Mutex m_misbehavior_mutex; + /** Accumulated misbehavior score for this peer */ + int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0}; + /** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */ + bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false}; + + /** Set of txids to reconsider once their parent transactions have been accepted **/ + std::set m_orphan_work_set GUARDED_BY(g_cs_orphans); + + /** Protects m_getdata_requests **/ + Mutex m_getdata_requests_mutex; + /** Work queue of items requested by this peer **/ + std::deque m_getdata_requests GUARDED_BY(m_getdata_requests_mutex); + + explicit Peer(NodeId id) : m_id(id) {} +}; + +using PeerRef = std::shared_ptr; + class PeerManager final : public CValidationInterface, public NetEventsInterface { public: PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman, @@ -105,6 +139,10 @@ public: bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats); private: + /** Get a shared pointer to the Peer object. + * May return an empty shared_ptr if the Peer object can't be found. */ + PeerRef GetPeerRef(NodeId id); + /** * Potentially mark a node discouraged based on the contents of a BlockValidationState object * @@ -153,6 +191,16 @@ private: TxRequestTracker m_txrequest GUARDED_BY(::cs_main); int64_t m_stale_tip_check_time; //!< Next time to check for stale tip + + /** Protects m_peer_map */ + Mutex m_peer_mutex; + /** + * Map of all Peer objects, keyed by peer id. This map is protected + * by the m_peer_mutex. Once a shared pointer reference is + * taken, the lock may be released. Individual fields are protected by + * their own locks. + */ + std::map m_peer_map GUARDED_BY(m_peer_mutex); }; /** Relay transaction to every node */ From a20ab22786466fe5164b53e62de9d23a4062fbca Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 8 Oct 2020 11:00:53 +0100 Subject: [PATCH 3/4] [net processing] Make GetPeerRef const --- src/net_processing.cpp | 2 +- src/net_processing.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index ec8d644813d..7001a6f4582 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -835,7 +835,7 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) { LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid); } -PeerRef PeerManager::GetPeerRef(NodeId id) +PeerRef PeerManager::GetPeerRef(NodeId id) const { LOCK(m_peer_mutex); auto it = m_peer_map.find(id); diff --git a/src/net_processing.h b/src/net_processing.h index 0697cf82c3b..6076e62732a 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -141,7 +141,7 @@ public: private: /** Get a shared pointer to the Peer object. * May return an empty shared_ptr if the Peer object can't be found. */ - PeerRef GetPeerRef(NodeId id); + PeerRef GetPeerRef(NodeId id) const; /** * Potentially mark a node discouraged based on the contents of a BlockValidationState object @@ -193,7 +193,7 @@ private: int64_t m_stale_tip_check_time; //!< Next time to check for stale tip /** Protects m_peer_map */ - Mutex m_peer_mutex; + mutable Mutex m_peer_mutex; /** * Map of all Peer objects, keyed by peer id. This map is protected * by the m_peer_mutex. Once a shared pointer reference is From 3025ca9e7743d9b96c22e9c6ed7ef051dcea7e54 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Thu, 24 Sep 2020 10:11:30 +0100 Subject: [PATCH 4/4] [net processing] Add RemovePeer() This allows us to avoid repeated locking in FinalizeNode() --- src/net_processing.cpp | 16 +++++++++++++--- src/net_processing.h | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 7001a6f4582..3dd432fac66 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -790,11 +790,9 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) { LOCK(cs_main); int misbehavior{0}; { - PeerRef peer = GetPeerRef(nodeid); + PeerRef peer = RemovePeer(nodeid); assert(peer != nullptr); misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score); - LOCK(m_peer_mutex); - m_peer_map.erase(nodeid); } CNodeState *state = State(nodeid); assert(state != nullptr); @@ -842,6 +840,18 @@ PeerRef PeerManager::GetPeerRef(NodeId id) const return it != m_peer_map.end() ? it->second : nullptr; } +PeerRef PeerManager::RemovePeer(NodeId id) +{ + PeerRef ret; + LOCK(m_peer_mutex); + auto it = m_peer_map.find(id); + if (it != m_peer_map.end()) { + ret = std::move(it->second); + m_peer_map.erase(it); + } + return ret; +} + bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { { LOCK(cs_main); diff --git a/src/net_processing.h b/src/net_processing.h index 6076e62732a..c179b89ebe0 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -143,6 +143,10 @@ private: * May return an empty shared_ptr if the Peer object can't be found. */ PeerRef GetPeerRef(NodeId id) const; + /** Get a shared pointer to the Peer object and remove it from m_peer_map. + * May return an empty shared_ptr if the Peer object can't be found. */ + PeerRef RemovePeer(NodeId id); + /** * Potentially mark a node discouraged based on the contents of a BlockValidationState object *