0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

Merge bitcoin/bitcoin#22829: refactor: various RecursiveMutex replacements in CConnman

3726a45958 refactor: replace RecursiveMutex m_added_nodes_mutex with Mutex (Sebastian Falbesoner)
7d52ff5c38 refactor: replace RecursiveMutex m_addr_fetches_mutex with Mutex (Sebastian Falbesoner)
d51d2a3bb5 scripted-diff: rename node vector/mutex members in CConnman (Sebastian Falbesoner)
574cc4271a refactor: remove RecursiveMutex cs_totalBytesRecv, use std::atomic instead (Sebastian Falbesoner)

Pull request description:

  This PR is related to #19303 and gets rid of the following RecursiveMutex members in class `CConnman`:
  * for `cs_totalBytesRecv`, protecting `nTotalBytesRecv`, `std::atomic` is used instead (the member is only increment at one and read at another place, so this is sufficient)
  * for `m_addr_fetches_mutex`, protecting `m_addr_fetches`, a regular `Mutex` is used instead (there is no chance that within one critical section, another one is called)
  * for `cs_vAddedNodes`, protecting `vAddedNodes`, a regular `Mutex` is used instead (there is no chance that within one critical section, another one is called)

  Additionally, the PR takes the chance to rename all node vector members (vNodes, vAddedNodes) and its corresponding mutexes (cs_vNodes, cs_vAddedNodes) to match the coding guidelines via a scripted-diff.

ACKs for top commit:
  vasild:
    ACK 3726a45958
  promag:
    Code review ACK 3726a45958.
  hebasto:
    re-ACK 3726a45958

Tree-SHA512: 4f5ad41ba2eca397795080988c1739c6abb44c1204dddaa75cc38a396fa821fbe1010694ba7bead1b606beaa677661e66da2a5dca233b2937214f63a54848348
This commit is contained in:
MarcoFalke 2021-11-25 11:55:20 +01:00
commit 76392b042e
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
3 changed files with 101 additions and 104 deletions

View file

@ -326,8 +326,8 @@ bool IsLocal(const CService& addr)
CNode* CConnman::FindNode(const CNetAddr& ip) CNode* CConnman::FindNode(const CNetAddr& ip)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (static_cast<CNetAddr>(pnode->addr) == ip) { if (static_cast<CNetAddr>(pnode->addr) == ip) {
return pnode; return pnode;
} }
@ -337,8 +337,8 @@ CNode* CConnman::FindNode(const CNetAddr& ip)
CNode* CConnman::FindNode(const CSubNet& subNet) CNode* CConnman::FindNode(const CSubNet& subNet)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) { if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) {
return pnode; return pnode;
} }
@ -348,8 +348,8 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
CNode* CConnman::FindNode(const std::string& addrName) CNode* CConnman::FindNode(const std::string& addrName)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (pnode->m_addr_name == addrName) { if (pnode->m_addr_name == addrName) {
return pnode; return pnode;
} }
@ -359,8 +359,8 @@ CNode* CConnman::FindNode(const std::string& addrName)
CNode* CConnman::FindNode(const CService& addr) CNode* CConnman::FindNode(const CService& addr)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (static_cast<CService>(pnode->addr) == addr) { if (static_cast<CService>(pnode->addr) == addr) {
return pnode; return pnode;
} }
@ -375,8 +375,8 @@ bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
bool CConnman::CheckIncomingNonce(uint64_t nonce) bool CConnman::CheckIncomingNonce(uint64_t nonce)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() == nonce) if (!pnode->fSuccessfullyConnected && !pnode->IsInboundConn() && pnode->GetLocalNonce() == nonce)
return false; return false;
} }
@ -435,7 +435,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
} }
// It is possible that we already have a connection to the IP/port pszDest resolved to. // It is possible that we already have a connection to the IP/port pszDest resolved to.
// In that case, drop the connection that was just created. // In that case, drop the connection that was just created.
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
CNode* pnode = FindNode(static_cast<CService>(addrConnect)); CNode* pnode = FindNode(static_cast<CService>(addrConnect));
if (pnode) { if (pnode) {
LogPrintf("Failed to open new connection, already connected\n"); LogPrintf("Failed to open new connection, already connected\n");
@ -1056,8 +1056,8 @@ bool CConnman::AttemptToEvictConnection()
std::vector<NodeEvictionCandidate> vEvictionCandidates; std::vector<NodeEvictionCandidate> vEvictionCandidates;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* node : vNodes) { for (const CNode* node : m_nodes) {
if (node->HasPermission(NetPermissionFlags::NoBan)) if (node->HasPermission(NetPermissionFlags::NoBan))
continue; continue;
if (!node->IsInboundConn()) if (!node->IsInboundConn())
@ -1084,8 +1084,8 @@ bool CConnman::AttemptToEvictConnection()
if (!node_id_to_evict) { if (!node_id_to_evict) {
return false; return false;
} }
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (pnode->GetId() == *node_id_to_evict) { if (pnode->GetId() == *node_id_to_evict) {
LogPrint(BCLog::NET, "selected %s connection for eviction peer=%d; disconnecting\n", pnode->ConnectionTypeAsString(), pnode->GetId()); LogPrint(BCLog::NET, "selected %s connection for eviction peer=%d; disconnecting\n", pnode->ConnectionTypeAsString(), pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
@ -1141,8 +1141,8 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
} }
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->IsInboundConn()) nInbound++; if (pnode->IsInboundConn()) nInbound++;
} }
} }
@ -1210,8 +1210,8 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString()); LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
vNodes.push_back(pnode); m_nodes.push_back(pnode);
} }
// We received a new connection, harvest entropy from the time (and our peer count) // We received a new connection, harvest entropy from the time (and our peer count)
@ -1238,8 +1238,8 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
// Count existing connections // Count existing connections
int existing_connections = WITH_LOCK(cs_vNodes, int existing_connections = WITH_LOCK(m_nodes_mutex,
return std::count_if(vNodes.begin(), vNodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; });); return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; }););
// Max connections of specified type already exist // Max connections of specified type already exist
if (max_connections != std::nullopt && existing_connections >= max_connections) return false; if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
@ -1255,11 +1255,11 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
void CConnman::DisconnectNodes() void CConnman::DisconnectNodes()
{ {
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
if (!fNetworkActive) { if (!fNetworkActive) {
// Disconnect any connected nodes // Disconnect any connected nodes
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (!pnode->fDisconnect) { if (!pnode->fDisconnect) {
LogPrint(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId()); LogPrint(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
@ -1268,13 +1268,13 @@ void CConnman::DisconnectNodes()
} }
// Disconnect unused nodes // Disconnect unused nodes
std::vector<CNode*> vNodesCopy = vNodes; std::vector<CNode*> nodes_copy = m_nodes;
for (CNode* pnode : vNodesCopy) for (CNode* pnode : nodes_copy)
{ {
if (pnode->fDisconnect) if (pnode->fDisconnect)
{ {
// remove from vNodes // remove from m_nodes
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end()); m_nodes.erase(remove(m_nodes.begin(), m_nodes.end(), pnode), m_nodes.end());
// release outbound grant (if any) // release outbound grant (if any)
pnode->grantOutbound.Release(); pnode->grantOutbound.Release();
@ -1284,18 +1284,18 @@ void CConnman::DisconnectNodes()
// hold in disconnected pool until all refs are released // hold in disconnected pool until all refs are released
pnode->Release(); pnode->Release();
vNodesDisconnected.push_back(pnode); m_nodes_disconnected.push_back(pnode);
} }
} }
} }
{ {
// Delete disconnected nodes // Delete disconnected nodes
std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected; std::list<CNode*> nodes_disconnected_copy = m_nodes_disconnected;
for (CNode* pnode : vNodesDisconnectedCopy) for (CNode* pnode : nodes_disconnected_copy)
{ {
// Destroy the object only after other threads have stopped using it. // Destroy the object only after other threads have stopped using it.
if (pnode->GetRefCount() <= 0) { if (pnode->GetRefCount() <= 0) {
vNodesDisconnected.remove(pnode); m_nodes_disconnected.remove(pnode);
DeleteNode(pnode); DeleteNode(pnode);
} }
} }
@ -1304,15 +1304,15 @@ void CConnman::DisconnectNodes()
void CConnman::NotifyNumConnectionsChanged() void CConnman::NotifyNumConnectionsChanged()
{ {
size_t vNodesSize; size_t nodes_size;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
vNodesSize = vNodes.size(); nodes_size = m_nodes.size();
} }
if(vNodesSize != nPrevNodeCount) { if(nodes_size != nPrevNodeCount) {
nPrevNodeCount = vNodesSize; nPrevNodeCount = nodes_size;
if (m_client_interface) { if (m_client_interface) {
m_client_interface->NotifyNumConnectionsChanged(vNodesSize); m_client_interface->NotifyNumConnectionsChanged(nodes_size);
} }
} }
} }
@ -1716,8 +1716,8 @@ void CConnman::ThreadDNSAddressSeed()
int nRelevant = 0; int nRelevant = 0;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn()) ++nRelevant; if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn()) ++nRelevant;
} }
} }
@ -1825,8 +1825,8 @@ int CConnman::GetExtraFullOutboundCount() const
{ {
int full_outbound_peers = 0; int full_outbound_peers = 0;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsFullOutboundConn()) { if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsFullOutboundConn()) {
++full_outbound_peers; ++full_outbound_peers;
} }
@ -1839,8 +1839,8 @@ int CConnman::GetExtraBlockRelayCount() const
{ {
int block_relay_peers = 0; int block_relay_peers = 0;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsBlockOnlyConn()) { if (pnode->fSuccessfullyConnected && !pnode->fDisconnect && pnode->IsBlockOnlyConn()) {
++block_relay_peers; ++block_relay_peers;
} }
@ -1911,8 +1911,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// Checking !dnsseed is cheaper before locking 2 mutexes. // Checking !dnsseed is cheaper before locking 2 mutexes.
if (!add_fixed_seeds_now && !dnsseed) { if (!add_fixed_seeds_now && !dnsseed) {
LOCK2(m_addr_fetches_mutex, cs_vAddedNodes); LOCK2(m_addr_fetches_mutex, m_added_nodes_mutex);
if (m_addr_fetches.empty() && vAddedNodes.empty()) { if (m_addr_fetches.empty() && m_added_nodes.empty()) {
add_fixed_seeds_now = true; add_fixed_seeds_now = true;
LogPrintf("Adding fixed seeds as -dnsseed=0, -addnode is not provided and all -seednode(s) attempted\n"); LogPrintf("Adding fixed seeds as -dnsseed=0, -addnode is not provided and all -seednode(s) attempted\n");
} }
@ -1937,8 +1937,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
std::set<std::vector<unsigned char> > setConnected; std::set<std::vector<unsigned char> > setConnected;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->IsFullOutboundConn()) nOutboundFullRelay++; if (pnode->IsFullOutboundConn()) nOutboundFullRelay++;
if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++; if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
@ -2126,8 +2126,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
{ {
std::vector<CAddress> ret; std::vector<CAddress> ret;
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->IsBlockOnlyConn()) { if (pnode->IsBlockOnlyConn()) {
ret.push_back(pnode->addr); ret.push_back(pnode->addr);
} }
@ -2142,9 +2142,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
std::list<std::string> lAddresses(0); std::list<std::string> lAddresses(0);
{ {
LOCK(cs_vAddedNodes); LOCK(m_added_nodes_mutex);
ret.reserve(vAddedNodes.size()); ret.reserve(m_added_nodes.size());
std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses)); std::copy(m_added_nodes.cbegin(), m_added_nodes.cend(), std::back_inserter(lAddresses));
} }
@ -2152,8 +2152,8 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
std::map<CService, bool> mapConnected; std::map<CService, bool> mapConnected;
std::map<std::string, std::pair<bool, CService>> mapConnectedByName; std::map<std::string, std::pair<bool, CService>> mapConnectedByName;
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (const CNode* pnode : vNodes) { for (const CNode* pnode : m_nodes) {
if (pnode->addr.IsValid()) { if (pnode->addr.IsValid()) {
mapConnected[pnode->addr] = pnode->IsInboundConn(); mapConnected[pnode->addr] = pnode->IsInboundConn();
} }
@ -2249,8 +2249,8 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
m_msgproc->InitializeNode(pnode); m_msgproc->InitializeNode(pnode);
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
vNodes.push_back(pnode); m_nodes.push_back(pnode);
} }
} }
@ -2264,7 +2264,7 @@ void CConnman::ThreadMessageHandler()
{ {
// Randomize the order in which we process messages from/to our peers. // Randomize the order in which we process messages from/to our peers.
// This prevents attacks in which an attacker exploits having multiple // This prevents attacks in which an attacker exploits having multiple
// consecutive connections in the vNodes list. // consecutive connections in the m_nodes list.
const NodesSnapshot snap{*this, /*shuffle=*/true}; const NodesSnapshot snap{*this, /*shuffle=*/true};
for (CNode* pnode : snap.Nodes()) { for (CNode* pnode : snap.Nodes()) {
@ -2694,7 +2694,7 @@ void CConnman::StopNodes()
// Delete peer connections. // Delete peer connections.
std::vector<CNode*> nodes; std::vector<CNode*> nodes;
WITH_LOCK(cs_vNodes, nodes.swap(vNodes)); WITH_LOCK(m_nodes_mutex, nodes.swap(m_nodes));
for (CNode* pnode : nodes) { for (CNode* pnode : nodes) {
pnode->CloseSocketDisconnect(); pnode->CloseSocketDisconnect();
DeleteNode(pnode); DeleteNode(pnode);
@ -2709,10 +2709,10 @@ void CConnman::StopNodes()
} }
} }
for (CNode* pnode : vNodesDisconnected) { for (CNode* pnode : m_nodes_disconnected) {
DeleteNode(pnode); DeleteNode(pnode);
} }
vNodesDisconnected.clear(); m_nodes_disconnected.clear();
vhListenSocket.clear(); vhListenSocket.clear();
semOutbound.reset(); semOutbound.reset();
semAddnode.reset(); semAddnode.reset();
@ -2785,21 +2785,21 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
bool CConnman::AddNode(const std::string& strNode) bool CConnman::AddNode(const std::string& strNode)
{ {
LOCK(cs_vAddedNodes); LOCK(m_added_nodes_mutex);
for (const std::string& it : vAddedNodes) { for (const std::string& it : m_added_nodes) {
if (strNode == it) return false; if (strNode == it) return false;
} }
vAddedNodes.push_back(strNode); m_added_nodes.push_back(strNode);
return true; return true;
} }
bool CConnman::RemoveAddedNode(const std::string& strNode) bool CConnman::RemoveAddedNode(const std::string& strNode)
{ {
LOCK(cs_vAddedNodes); LOCK(m_added_nodes_mutex);
for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) { for(std::vector<std::string>::iterator it = m_added_nodes.begin(); it != m_added_nodes.end(); ++it) {
if (strNode == *it) { if (strNode == *it) {
vAddedNodes.erase(it); m_added_nodes.erase(it);
return true; return true;
} }
} }
@ -2808,12 +2808,12 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
size_t CConnman::GetNodeCount(ConnectionDirection flags) const size_t CConnman::GetNodeCount(ConnectionDirection flags) const
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
if (flags == ConnectionDirection::Both) // Shortcut if we want total if (flags == ConnectionDirection::Both) // Shortcut if we want total
return vNodes.size(); return m_nodes.size();
int nNum = 0; int nNum = 0;
for (const auto& pnode : vNodes) { for (const auto& pnode : m_nodes) {
if (flags & (pnode->IsInboundConn() ? ConnectionDirection::In : ConnectionDirection::Out)) { if (flags & (pnode->IsInboundConn() ? ConnectionDirection::In : ConnectionDirection::Out)) {
nNum++; nNum++;
} }
@ -2825,9 +2825,9 @@ size_t CConnman::GetNodeCount(ConnectionDirection flags) const
void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
{ {
vstats.clear(); vstats.clear();
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
vstats.reserve(vNodes.size()); vstats.reserve(m_nodes.size());
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
vstats.emplace_back(); vstats.emplace_back();
pnode->CopyStats(vstats.back()); pnode->CopyStats(vstats.back());
vstats.back().m_mapped_as = pnode->addr.GetMappedAS(addrman.GetAsmap()); vstats.back().m_mapped_as = pnode->addr.GetMappedAS(addrman.GetAsmap());
@ -2836,7 +2836,7 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
bool CConnman::DisconnectNode(const std::string& strNode) bool CConnman::DisconnectNode(const std::string& strNode)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
if (CNode* pnode = FindNode(strNode)) { if (CNode* pnode = FindNode(strNode)) {
LogPrint(BCLog::NET, "disconnect by address%s matched peer=%d; disconnecting\n", (fLogIPs ? strprintf("=%s", strNode) : ""), pnode->GetId()); LogPrint(BCLog::NET, "disconnect by address%s matched peer=%d; disconnecting\n", (fLogIPs ? strprintf("=%s", strNode) : ""), pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
@ -2848,8 +2848,8 @@ bool CConnman::DisconnectNode(const std::string& strNode)
bool CConnman::DisconnectNode(const CSubNet& subnet) bool CConnman::DisconnectNode(const CSubNet& subnet)
{ {
bool disconnected = false; bool disconnected = false;
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* pnode : vNodes) { for (CNode* pnode : m_nodes) {
if (subnet.Match(pnode->addr)) { if (subnet.Match(pnode->addr)) {
LogPrint(BCLog::NET, "disconnect by subnet%s matched peer=%d; disconnecting\n", (fLogIPs ? strprintf("=%s", subnet.ToString()) : ""), pnode->GetId()); LogPrint(BCLog::NET, "disconnect by subnet%s matched peer=%d; disconnecting\n", (fLogIPs ? strprintf("=%s", subnet.ToString()) : ""), pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
@ -2866,8 +2866,8 @@ bool CConnman::DisconnectNode(const CNetAddr& addr)
bool CConnman::DisconnectNode(NodeId id) bool CConnman::DisconnectNode(NodeId id)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for(CNode* pnode : vNodes) { for(CNode* pnode : m_nodes) {
if (id == pnode->GetId()) { if (id == pnode->GetId()) {
LogPrint(BCLog::NET, "disconnect by id peer=%d; disconnecting\n", pnode->GetId()); LogPrint(BCLog::NET, "disconnect by id peer=%d; disconnecting\n", pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
@ -2879,7 +2879,6 @@ bool CConnman::DisconnectNode(NodeId id)
void CConnman::RecordBytesRecv(uint64_t bytes) void CConnman::RecordBytesRecv(uint64_t bytes)
{ {
LOCK(cs_totalBytesRecv);
nTotalBytesRecv += bytes; nTotalBytesRecv += bytes;
} }
@ -2956,7 +2955,6 @@ uint64_t CConnman::GetOutboundTargetBytesLeft() const
uint64_t CConnman::GetTotalBytesRecv() const uint64_t CConnman::GetTotalBytesRecv() const
{ {
LOCK(cs_totalBytesRecv);
return nTotalBytesRecv; return nTotalBytesRecv;
} }
@ -3059,8 +3057,8 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func) bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
{ {
CNode* found = nullptr; CNode* found = nullptr;
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (auto&& pnode : vNodes) { for (auto&& pnode : m_nodes) {
if(pnode->GetId() == id) { if(pnode->GetId() == id) {
found = pnode; found = pnode;
break; break;

View file

@ -791,8 +791,8 @@ public:
} }
vWhitelistedRange = connOptions.vWhitelistedRange; vWhitelistedRange = connOptions.vWhitelistedRange;
{ {
LOCK(cs_vAddedNodes); LOCK(m_added_nodes_mutex);
vAddedNodes = connOptions.m_added_nodes; m_added_nodes = connOptions.m_added_nodes;
} }
m_onion_binds = connOptions.onion_binds; m_onion_binds = connOptions.onion_binds;
} }
@ -823,8 +823,8 @@ public:
using NodeFn = std::function<void(CNode*)>; using NodeFn = std::function<void(CNode*)>;
void ForEachNode(const NodeFn& func) void ForEachNode(const NodeFn& func)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (auto&& node : vNodes) { for (auto&& node : m_nodes) {
if (NodeFullyConnected(node)) if (NodeFullyConnected(node))
func(node); func(node);
} }
@ -832,8 +832,8 @@ public:
void ForEachNode(const NodeFn& func) const void ForEachNode(const NodeFn& func) const
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (auto&& node : vNodes) { for (auto&& node : m_nodes) {
if (NodeFullyConnected(node)) if (NodeFullyConnected(node))
func(node); func(node);
} }
@ -968,7 +968,7 @@ private:
/** /**
* Create a `CNode` object from a socket that has just been accepted and add the node to * Create a `CNode` object from a socket that has just been accepted and add the node to
* the `vNodes` member. * the `m_nodes` member.
* @param[in] hSocket Connected socket to communicate with the peer. * @param[in] hSocket Connected socket to communicate with the peer.
* @param[in] permissionFlags The peer's permissions. * @param[in] permissionFlags The peer's permissions.
* @param[in] addr_bind The address and port at our side of the connection. * @param[in] addr_bind The address and port at our side of the connection.
@ -1074,9 +1074,8 @@ private:
static bool NodeFullyConnected(const CNode* pnode); static bool NodeFullyConnected(const CNode* pnode);
// Network usage totals // Network usage totals
mutable RecursiveMutex cs_totalBytesRecv;
mutable RecursiveMutex cs_totalBytesSent; mutable RecursiveMutex cs_totalBytesSent;
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv) {0}; std::atomic<uint64_t> nTotalBytesRecv{0};
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};
// outbound limit & stats // outbound limit & stats
@ -1099,12 +1098,12 @@ private:
bool fAddressesInitialized{false}; bool fAddressesInitialized{false};
AddrMan& addrman; AddrMan& addrman;
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex); std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
RecursiveMutex m_addr_fetches_mutex; Mutex m_addr_fetches_mutex;
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes); std::vector<std::string> m_added_nodes GUARDED_BY(m_added_nodes_mutex);
mutable RecursiveMutex cs_vAddedNodes; mutable Mutex m_added_nodes_mutex;
std::vector<CNode*> vNodes GUARDED_BY(cs_vNodes); std::vector<CNode*> m_nodes GUARDED_BY(m_nodes_mutex);
std::list<CNode*> vNodesDisconnected; std::list<CNode*> m_nodes_disconnected;
mutable RecursiveMutex cs_vNodes; mutable RecursiveMutex m_nodes_mutex;
std::atomic<NodeId> nLastNodeId{0}; std::atomic<NodeId> nLastNodeId{0};
unsigned int nPrevNodeCount{0}; unsigned int nPrevNodeCount{0};
@ -1226,7 +1225,7 @@ private:
std::vector<CService> m_onion_binds; std::vector<CService> m_onion_binds;
/** /**
* RAII helper to atomically create a copy of `vNodes` and add a reference * RAII helper to atomically create a copy of `m_nodes` and add a reference
* to each of the nodes. The nodes are released when this object is destroyed. * to each of the nodes. The nodes are released when this object is destroyed.
*/ */
class NodesSnapshot class NodesSnapshot
@ -1235,8 +1234,8 @@ private:
explicit NodesSnapshot(const CConnman& connman, bool shuffle) explicit NodesSnapshot(const CConnman& connman, bool shuffle)
{ {
{ {
LOCK(connman.cs_vNodes); LOCK(connman.m_nodes_mutex);
m_nodes_copy = connman.vNodes; m_nodes_copy = connman.m_nodes;
for (auto& node : m_nodes_copy) { for (auto& node : m_nodes_copy) {
node->AddRef(); node->AddRef();
} }

View file

@ -25,16 +25,16 @@ struct ConnmanTestMsg : public CConnman {
void AddTestNode(CNode& node) void AddTestNode(CNode& node)
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
vNodes.push_back(&node); m_nodes.push_back(&node);
} }
void ClearTestNodes() void ClearTestNodes()
{ {
LOCK(cs_vNodes); LOCK(m_nodes_mutex);
for (CNode* node : vNodes) { for (CNode* node : m_nodes) {
delete node; delete node;
} }
vNodes.clear(); m_nodes.clear();
} }
void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); } void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }