[net processing] Add RemovePeer()

This allows us to avoid repeated locking in FinalizeNode()
This commit is contained in:
John Newbery 2020-09-24 10:11:30 +01:00
parent a20ab22786
commit 3025ca9e77
2 changed files with 17 additions and 3 deletions

View File

@ -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);

View File

@ -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
*