diff options
author | John Newbery <john@johnnewbery.com> | 2020-09-24 10:11:30 +0100 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2020-12-07 11:59:24 +0000 |
commit | 3025ca9e7743d9b96c22e9c6ed7ef051dcea7e54 (patch) | |
tree | 13d7abac95c5cb177fbcbb888ee2e19ac9781648 /src | |
parent | a20ab22786466fe5164b53e62de9d23a4062fbca (diff) |
[net processing] Add RemovePeer()
This allows us to avoid repeated locking in FinalizeNode()
Diffstat (limited to 'src')
-rw-r--r-- | src/net_processing.cpp | 16 | ||||
-rw-r--r-- | 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 7001a6f458..3dd432fac6 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 6076e62732..c179b89ebe 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 * |