Skip to content

Commit

Permalink
[net processing] Add RemovePeer()
Browse files Browse the repository at this point in the history
This allows us to avoid repeated locking in FinalizeNode()
  • Loading branch information
jnewbery committed Dec 1, 2020
1 parent 6bfcef3 commit aedd418
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -844,6 +842,18 @@ PeerRef PeerManager::GetPeerRef(NodeId id) const
return ret;
}

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);
Expand Down
4 changes: 4 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
* May return nullptr 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 nullptr 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
*
Expand Down

0 comments on commit aedd418

Please sign in to comment.