Skip to content

Commit 21f8426

Browse files
jonatackvasild
andcommitted
p2p, bugfix: do not make automatic outbound connections to addnode peers
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. For instance, 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. Examples on mainnet using logging added in the same pull request: 2023-08-12T14:51:05.681743Z [opencon] [net.cpp:1949] [ThreadOpenConnections] [net:debug] Not making automatic network-specific outbound-full-relay connection to i2p peer selected for manual (addnode) connection: [geh...odq.b32.i2p]:0 2023-08-13T03:59:28.050853Z [opencon] [net.cpp:1949] [ThreadOpenConnections] [net:debug] Not making automatic block-relay-only connection to onion peer selected for manual (addnode) connection: kpg...aid.onion:8333 2023-08-13T16:21:26.979052Z [opencon] [net.cpp:1949] [ThreadOpenConnections] [net:debug] Not making automatic network-specific outbound-full-relay connection to cjdns peer selected for manual (addnode) connection: [fcc...8ce]:8333 2023-08-14T20:43:53.401271Z [opencon] [net.cpp:1949] [ThreadOpenConnections] [net:debug] Not making automatic network-specific outbound-full-relay connection to cjdns peer selected for manual (addnode) connection: [fc7...59e]:8333 2023-08-15T00:10:01.894147Z [opencon] [net.cpp:1949] [ThreadOpenConnections] [net:debug] Not making automatic feeler connection to i2p peer selected for manual (addnode) connection: geh...odq.b32.i2p:8333 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. Co-authored-by: Vasil Dimov <[email protected]>
1 parent e5e75f6 commit 21f8426

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/net.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
27452745
continue;
27462746
}
27472747

2748+
// Do not make automatic outbound connections to addnode peers,
2749+
// to allocate our limited outbound slots correctly and to ensure
2750+
// addnode connections benefit from their intended protections.
2751+
if (AddedNodesContain(addr)) {
2752+
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "Not making automatic %s%s connection to %s peer selected for manual (addnode) connection%s\n",
2753+
preferred_net.has_value() ? "network-specific " : "",
2754+
ConnectionTypeAsString(conn_type), GetNetworkName(addr.GetNetwork()),
2755+
fLogIPs ? strprintf(": %s", addr.ToStringAddrPort()) : "");
2756+
continue;
2757+
}
2758+
27482759
addrConnect = addr;
27492760
break;
27502761
}
@@ -3475,6 +3486,18 @@ bool CConnman::RemoveAddedNode(const AddedNodeParams& params)
34753486
return m_added_node_params.erase(params);
34763487
}
34773488

3489+
bool CConnman::AddedNodesContain(const CAddress& addr) const
3490+
{
3491+
AssertLockNotHeld(m_added_nodes_mutex);
3492+
const std::string addr_str{addr.ToStringAddr()};
3493+
const std::string addr_port_str{addr.ToStringAddrPort()};
3494+
LOCK(m_added_nodes_mutex);
3495+
return m_added_node_params.count({addr_str, /*m_use_v2transport=*/true})
3496+
|| m_added_node_params.count({addr_str, /*m_use_v2transport=*/false})
3497+
|| m_added_node_params.count({addr_port_str, /*m_use_v2transport=*/true})
3498+
|| m_added_node_params.count({addr_port_str, /*m_use_v2transport=*/false});
3499+
}
3500+
34783501
size_t CConnman::GetNodeCount(ConnectionDirection flags) const
34793502
{
34803503
LOCK(m_nodes_mutex);

src/net.h

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

12011201
bool AddNode(const AddedNodeParams& params) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
12021202
bool RemoveAddedNode(const AddedNodeParams& params) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1203+
bool AddedNodesContain(const CAddress& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
12031204
std::vector<AddedNodeInfo> GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
12041205

12051206
/**

0 commit comments

Comments
 (0)