Skip to content

Commit 3025ca9

Browse files
committed
[net processing] Add RemovePeer()
This allows us to avoid repeated locking in FinalizeNode()
1 parent a20ab22 commit 3025ca9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/net_processing.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,9 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
790790
LOCK(cs_main);
791791
int misbehavior{0};
792792
{
793-
PeerRef peer = GetPeerRef(nodeid);
793+
PeerRef peer = RemovePeer(nodeid);
794794
assert(peer != nullptr);
795795
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
796-
LOCK(m_peer_mutex);
797-
m_peer_map.erase(nodeid);
798796
}
799797
CNodeState *state = State(nodeid);
800798
assert(state != nullptr);
@@ -842,6 +840,18 @@ PeerRef PeerManager::GetPeerRef(NodeId id) const
842840
return it != m_peer_map.end() ? it->second : nullptr;
843841
}
844842

843+
PeerRef PeerManager::RemovePeer(NodeId id)
844+
{
845+
PeerRef ret;
846+
LOCK(m_peer_mutex);
847+
auto it = m_peer_map.find(id);
848+
if (it != m_peer_map.end()) {
849+
ret = std::move(it->second);
850+
m_peer_map.erase(it);
851+
}
852+
return ret;
853+
}
854+
845855
bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
846856
{
847857
LOCK(cs_main);

src/net_processing.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
143143
* May return an empty shared_ptr if the Peer object can't be found. */
144144
PeerRef GetPeerRef(NodeId id) const;
145145

146+
/** Get a shared pointer to the Peer object and remove it from m_peer_map.
147+
* May return an empty shared_ptr if the Peer object can't be found. */
148+
PeerRef RemovePeer(NodeId id);
149+
146150
/**
147151
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
148152
*

0 commit comments

Comments
 (0)