Skip to content

Commit 770c031

Browse files
committed
net: attempt v2 transport for manual connections if we support it
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).
1 parent d5e5810 commit 770c031

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/net.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,12 +2423,15 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24232423
// Connect to specific addresses
24242424
if (!connect.empty())
24252425
{
2426+
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
2427+
// peer doesn't support it or immediately disconnects us for another reason.
2428+
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
24262429
for (int64_t nLoop = 0;; nLoop++)
24272430
{
24282431
for (const std::string& strAddr : connect)
24292432
{
24302433
CAddress addr(CService(), NODE_NONE);
2431-
OpenNetworkConnection(addr, false, {}, strAddr.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
2434+
OpenNetworkConnection(addr, false, {}, strAddr.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/use_v2transport);
24322435
for (int i = 0; i < 10 && i < nLoop; i++)
24332436
{
24342437
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
@@ -2437,6 +2440,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24372440
}
24382441
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
24392442
return;
2443+
PerformReconnections();
24402444
}
24412445
}
24422446

@@ -2846,11 +2850,11 @@ void CConnman::ThreadOpenAddedConnections()
28462850
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
28472851
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
28482852
}
2853+
// See if any reconnections are desired.
2854+
PerformReconnections();
28492855
// Retry every 60 seconds if a connection was attempted, otherwise two seconds
28502856
if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))
28512857
return;
2852-
// See if any reconnections are desired.
2853-
PerformReconnections();
28542858
}
28552859
}
28562860

src/net.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,10 +1084,11 @@ class CConnman
10841084
vWhitelistedRange = connOptions.vWhitelistedRange;
10851085
{
10861086
LOCK(m_added_nodes_mutex);
1087-
1087+
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
1088+
// peer doesn't support it or immediately disconnects us for another reason.
1089+
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
10881090
for (const std::string& added_node : connOptions.m_added_nodes) {
1089-
// -addnode cli arg does not currently have a way to signal BIP324 support
1090-
m_added_node_params.push_back({added_node, false});
1091+
m_added_node_params.push_back({added_node, use_v2transport});
10911092
}
10921093
}
10931094
m_onion_binds = connOptions.onion_binds;

0 commit comments

Comments
 (0)