Skip to content

Commit 4374a87

Browse files
committed
Merge bitcoin/bitcoin#28895: p2p: do not make automatic outbound connections to addnode peers
5e7cc41 test: add unit test for CConnman::AddedNodesContain() (Jon Atack) cc62716 p2p: do not make automatic outbound connections to addnode peers (Jon Atack) Pull request description: to allocate our limited outbound slots correctly, and to ensure addnode connections benefit from their intended protections. Our addnode logic usually connects the addnode peers before the automatic outbound logic does, but not always, as a connection race can occur. If an addnode peer disconnects us and if it was the only one from its network, there can be a race between reconnecting to it with the addnode thread, and it being picked as automatic network-specific outbound peer. Or our internet connection or router or the addnode peer could be temporarily offline, and then return online during the automatic outbound thread. Or we could add a new manual peer using the addnode RPC at that time. The race can be more apparent when our node doesn't know many peers, or with networks like cjdns that currently have few bitcoin peers. When an addnode peer is connected as an automatic outbound peer and is the only connection we have to a network, it can be protected by our new outbound eviction logic and persist in the "wrong role". Finally, there does not seem to be a reason to make block-relay or short-lived feeler connections to addnode peers, as the addnode logic will ensure we connect to them if they are up, within the addnode connection limit. Fix these issues by checking if the address is an addnode peer in our automatic outbound connection logic. ACKs for top commit: mzumsande: Tested ACK 5e7cc41 brunoerg: utACK 5e7cc41 vasild: ACK 5e7cc41 guggero: utACK 5e7cc41 Tree-SHA512: 2438c3ec92e98aebca2a0da960534e4655a9c6e1192a24a085fc01326d95cdb1b67d8c44e4ee706bc1d8af8564126d446a21b5579dcbec61bdea5fce2f0115ee
2 parents ca041fc + 5e7cc41 commit 4374a87

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
@@ -2715,6 +2715,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
27152715
continue;
27162716
}
27172717

2718+
// Do not make automatic outbound connections to addnode peers, to
2719+
// not use our limited outbound slots for them and to ensure
2720+
// addnode connections benefit from their intended protections.
2721+
if (AddedNodesContain(addr)) {
2722+
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "Not making automatic %s%s connection to %s peer selected for manual (addnode) connection%s\n",
2723+
preferred_net.has_value() ? "network-specific " : "",
2724+
ConnectionTypeAsString(conn_type), GetNetworkName(addr.GetNetwork()),
2725+
fLogIPs ? strprintf(": %s", addr.ToStringAddrPort()) : "");
2726+
continue;
2727+
}
2728+
27182729
addrConnect = addr;
27192730
break;
27202731
}
@@ -3454,6 +3465,17 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
34543465
return false;
34553466
}
34563467

3468+
bool CConnman::AddedNodesContain(const CAddress& addr) const
3469+
{
3470+
AssertLockNotHeld(m_added_nodes_mutex);
3471+
const std::string addr_str{addr.ToStringAddr()};
3472+
const std::string addr_port_str{addr.ToStringAddrPort()};
3473+
LOCK(m_added_nodes_mutex);
3474+
return (m_added_node_params.size() < 24 // bound the query to a reasonable limit
3475+
&& std::any_of(m_added_node_params.cbegin(), m_added_node_params.cend(),
3476+
[&](const auto& p) { return p.m_added_node == addr_str || p.m_added_node == addr_port_str; }));
3477+
}
3478+
34573479
size_t CConnman::GetNodeCount(ConnectionDirection flags) const
34583480
{
34593481
LOCK(m_nodes_mutex);

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ class CConnman
11851185

11861186
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11871187
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1188+
bool AddedNodesContain(const CAddress& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11881189
std::vector<AddedNodeInfo> GetAddedNodeInfo(bool include_connected) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11891190

11901191
/**

src/test/net_peer_connection_tests.cpp

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

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

0 commit comments

Comments
 (0)