Skip to content

Commit 09504bd

Browse files
committed
merge bitcoin#28895: do not make automatic outbound connections to addnode peers
1 parent 6cf206c commit 09504bd

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/net.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
35473547
continue;
35483548
}
35493549

3550+
// Do not make automatic outbound connections to addnode peers, to
3551+
// not use our limited outbound slots for them and to ensure
3552+
// addnode connections benefit from their intended protections.
3553+
if (AddedNodesContain(addr)) {
3554+
LogPrint(BCLog::NET, "Not making automatic %s%s connection to %s peer selected for manual (addnode) connection%s\n",
3555+
preferred_net.has_value() ? "network-specific " : "",
3556+
ConnectionTypeAsString(conn_type), GetNetworkName(addr.GetNetwork()),
3557+
fLogIPs ? strprintf(": %s", addr.ToStringAddrPort()) : "");
3558+
continue;
3559+
}
3560+
35503561
addrConnect = addr;
35513562
break;
35523563
}
@@ -4581,6 +4592,17 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
45814592
return false;
45824593
}
45834594

4595+
bool CConnman::AddedNodesContain(const CAddress& addr) const
4596+
{
4597+
AssertLockNotHeld(m_added_nodes_mutex);
4598+
const std::string addr_str{addr.ToStringAddr()};
4599+
const std::string addr_port_str{addr.ToStringAddrPort()};
4600+
LOCK(m_added_nodes_mutex);
4601+
return (m_added_node_params.size() < 24 // bound the query to a reasonable limit
4602+
&& std::any_of(m_added_node_params.cbegin(), m_added_node_params.cend(),
4603+
[&](const auto& p) { return p.m_added_node == addr_str || p.m_added_node == addr_port_str; }));
4604+
}
4605+
45844606
bool CConnman::AddPendingMasternode(const uint256& proTxHash)
45854607
{
45864608
LOCK(cs_vPendingMasternodes);

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ friend class CNode;
14811481

14821482
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
14831483
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1484+
bool AddedNodesContain(const CAddress& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
14841485
std::vector<AddedNodeInfo> GetAddedNodeInfo(bool include_connected) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
14851486

14861487
/**

src/test/net_peer_connection_tests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ BOOST_AUTO_TEST_CASE(test_addnode_getaddednodeinfo_and_connection_detection)
124124
BOOST_CHECK_EQUAL(connman->GetAddedNodeInfo(/*include_connected=*/true).size(), nodes.size());
125125
BOOST_CHECK(connman->GetAddedNodeInfo(/*include_connected=*/false).empty());
126126

127+
// Test AddedNodesContain()
128+
for (auto node : connman->TestNodes()) {
129+
BOOST_CHECK(connman->AddedNodesContain(node->addr));
130+
}
131+
AddPeer(id, nodes, *peerman, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
132+
BOOST_CHECK(!connman->AddedNodesContain(nodes.back()->addr));
133+
127134
BOOST_TEST_MESSAGE("\nPrint GetAddedNodeInfo contents:");
128135
for (const auto& info : connman->GetAddedNodeInfo(/*include_connected=*/true)) {
129136
BOOST_TEST_MESSAGE(strprintf("\nadded node: %s", info.m_params.m_added_node));

0 commit comments

Comments
 (0)