From aec4fa2de0030f035d41874b2db7efdbf93bba24 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Wed, 15 Oct 2025 15:53:56 +0200 Subject: [PATCH] net: drop the only recursive usage of CConnman::m_nodes_mutex The only recursive usage of `CConnman::m_nodes_mutex` is from `PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs()` which uses nested calls to `CConnman::ForNode()` to trim the size of `lNodesAnnouncingHeaderAndIDs` to `<= 3`. This need not be nested, so take it out. Before: ``` fornode(newnode) if (size >= 3) fornode(front) handle removal of front pop front push back newnode ``` After: ``` fornode(newnode) push back newnode if (size > 3) fornode(front) handle removal of front pop front ``` `lNodesAnnouncingHeaderAndIDs` is protected by `cs_main` which is locked during the entire operation. --- src/net_processing.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b870df66c1e..10168d7f390 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1309,25 +1309,25 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) } } } - m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { + const bool nodeid_was_appended{m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); - if (lNodesAnnouncingHeaderAndIDs.size() >= 3) { - // As per BIP152, we only get 3 of our peers to announce - // blocks using compact encodings. - m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop){ - MakeAndPushMessage(*pnodeStop, NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION); - // save BIP152 bandwidth state: we select peer to be low-bandwidth - pnodeStop->m_bip152_highbandwidth_to = false; - return true; - }); - lNodesAnnouncingHeaderAndIDs.pop_front(); - } MakeAndPushMessage(*pfrom, NetMsgType::SENDCMPCT, /*high_bandwidth=*/true, /*version=*/CMPCTBLOCKS_VERSION); // save BIP152 bandwidth state: we select peer to be high-bandwidth pfrom->m_bip152_highbandwidth_to = true; lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId()); return true; - }); + })}; + if (nodeid_was_appended && lNodesAnnouncingHeaderAndIDs.size() > 3) { + // As per BIP152, we only get 3 of our peers to announce + // blocks using compact encodings. + m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop) { + MakeAndPushMessage(*pnodeStop, NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION); + // save BIP152 bandwidth state: we select peer to be low-bandwidth + pnodeStop->m_bip152_highbandwidth_to = false; + return true; + }); + lNodesAnnouncingHeaderAndIDs.pop_front(); + } } bool PeerManagerImpl::TipMayBeStale()