Skip to content

Commit

Permalink
net: attempt v2 transport for manual connections if we support it
Browse files Browse the repository at this point in the history
This affects manual connections made either with -connect, or with
-addnode provided as a bitcoind config arg (the addnode RPC has an
extra option for v2).

We don't necessarily know if our peer supports v2, but will reconnect
with v1 if they don't. In order to do that, improve the reconnection
behavior such that we will reconnect after a sleep of 500ms
(which usually should be enough for our peer to send us their
version message).
  • Loading branch information
mzumsande authored and lateminer committed May 15, 2024
1 parent df60968 commit 079e2b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2426,12 +2426,15 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// Connect to specific addresses
if (!connect.empty())
{
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
// peer doesn't support it or immediately disconnects us for another reason.
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
for (int64_t nLoop = 0;; nLoop++)
{
for (const std::string& strAddr : connect)
{
CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, {}, strAddr.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
OpenNetworkConnection(addr, false, {}, strAddr.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/use_v2transport);
for (int i = 0; i < 10 && i < nLoop; i++)
{
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
Expand All @@ -2440,6 +2443,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
}
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
return;
PerformReconnections();
}
}

Expand Down Expand Up @@ -2845,11 +2849,11 @@ void CConnman::ThreadOpenAddedConnections()
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
}
}
// See if any reconnections are desired.
PerformReconnections();
// Retry every 60 seconds if a connection was attempted, otherwise two seconds
if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
return;
// See if any reconnections are desired.
PerformReconnections();
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,11 @@ class CConnman
vWhitelistedRange = connOptions.vWhitelistedRange;
{
LOCK(m_added_nodes_mutex);

// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
// peer doesn't support it or immediately disconnects us for another reason.
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
for (const std::string& added_node : connOptions.m_added_nodes) {
// -addnode cli arg does not currently have a way to signal BIP324 support
m_added_node_params.push_back({added_node, false});
m_added_node_params.push_back({added_node, use_v2transport});
}
}
m_onion_binds = connOptions.onion_binds;
Expand Down

0 comments on commit 079e2b0

Please sign in to comment.