@@ -439,7 +439,7 @@ static CAddress GetBindAddress(const Sock& sock)
439
439
return addr_bind;
440
440
}
441
441
442
- CNode* CConnman::ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure , ConnectionType conn_type)
442
+ CNode* CConnman::ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure , ConnectionType conn_type, bool use_v2transport )
443
443
{
444
444
AssertLockNotHeld (m_unused_i2p_sessions_mutex);
445
445
assert (conn_type != ConnectionType::INBOUND);
@@ -457,7 +457,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
457
457
}
458
458
}
459
459
460
- LogPrintLevel (BCLog::NET, BCLog::Level::Debug, " trying connection %s lastseen=%.1fhrs\n " ,
460
+ LogPrintLevel (BCLog::NET, BCLog::Level::Debug, " trying %s connection %s lastseen=%.1fhrs\n " ,
461
+ use_v2transport ? " v2" : " v1" ,
461
462
pszDest ? pszDest : addrConnect.ToStringAddrPort (),
462
463
Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime ));
463
464
@@ -580,6 +581,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
580
581
CNodeOptions{
581
582
.i2p_sam_session = std::move (i2p_transient_session),
582
583
.recv_flood_size = nReceiveFloodSize,
584
+ .use_v2transport = use_v2transport,
583
585
});
584
586
pnode->AddRef ();
585
587
@@ -1794,6 +1796,10 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1794
1796
}
1795
1797
1796
1798
const bool inbound_onion = std::find (m_onion_binds.begin (), m_onion_binds.end (), addr_bind) != m_onion_binds.end ();
1799
+ // The V2Transport transparently falls back to V1 behavior when an incoming V1 connection is
1800
+ // detected, so use it whenever we signal NODE_P2P_V2.
1801
+ const bool use_v2transport (nodeServices & NODE_P2P_V2);
1802
+
1797
1803
CNode* pnode = new CNode (id,
1798
1804
std::move (sock),
1799
1805
addr,
@@ -1807,6 +1813,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1807
1813
.permission_flags = permission_flags,
1808
1814
.prefer_evict = discouraged,
1809
1815
.recv_flood_size = nReceiveFloodSize,
1816
+ .use_v2transport = use_v2transport,
1810
1817
});
1811
1818
pnode->AddRef ();
1812
1819
m_msgproc->InitializeNode (*pnode, nodeServices);
@@ -1855,7 +1862,7 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
1855
1862
CSemaphoreGrant grant (*semOutbound, true );
1856
1863
if (!grant) return false ;
1857
1864
1858
- OpenNetworkConnection (CAddress (), false , &grant, address.c_str (), conn_type);
1865
+ OpenNetworkConnection (CAddress (), false , &grant, address.c_str (), conn_type, /* use_v2transport= */ false );
1859
1866
return true ;
1860
1867
}
1861
1868
@@ -2289,7 +2296,7 @@ void CConnman::ProcessAddrFetch()
2289
2296
CAddress addr;
2290
2297
CSemaphoreGrant grant (*semOutbound, true );
2291
2298
if (grant) {
2292
- OpenNetworkConnection (addr, false , &grant, strDest.c_str (), ConnectionType::ADDR_FETCH);
2299
+ OpenNetworkConnection (addr, false , &grant, strDest.c_str (), ConnectionType::ADDR_FETCH, /* use_v2transport= */ false );
2293
2300
}
2294
2301
}
2295
2302
@@ -2391,7 +2398,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
2391
2398
for (const std::string& strAddr : connect)
2392
2399
{
2393
2400
CAddress addr (CService (), NODE_NONE);
2394
- OpenNetworkConnection (addr, false , nullptr , strAddr.c_str (), ConnectionType::MANUAL);
2401
+ OpenNetworkConnection (addr, false , nullptr , strAddr.c_str (), ConnectionType::MANUAL, /* use_v2transport= */ false );
2395
2402
for (int i = 0 ; i < 10 && i < nLoop; i++)
2396
2403
{
2397
2404
if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
@@ -2694,7 +2701,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
2694
2701
// Don't record addrman failure attempts when node is offline. This can be identified since all local
2695
2702
// network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
2696
2703
const bool count_failures{((int )outbound_ipv46_peer_netgroups.size () + outbound_privacy_network_peers) >= std::min (nMaxConnections - 1 , 2 )};
2697
- OpenNetworkConnection (addrConnect, count_failures, &grant, /* strDest=*/ nullptr , conn_type);
2704
+ // Use BIP324 transport when both us and them have NODE_V2_P2P set.
2705
+ const bool use_v2transport (addrConnect.nServices & GetLocalServices () & NODE_P2P_V2);
2706
+ OpenNetworkConnection (addrConnect, count_failures, &grant, /* strDest=*/ nullptr , conn_type, use_v2transport);
2698
2707
}
2699
2708
}
2700
2709
}
@@ -2783,7 +2792,7 @@ void CConnman::ThreadOpenAddedConnections()
2783
2792
}
2784
2793
tried = true ;
2785
2794
CAddress addr (CService (), NODE_NONE);
2786
- OpenNetworkConnection (addr, false , &grant, info.strAddedNode .c_str (), ConnectionType::MANUAL);
2795
+ OpenNetworkConnection (addr, false , &grant, info.strAddedNode .c_str (), ConnectionType::MANUAL, /* use_v2transport= */ false );
2787
2796
if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
2788
2797
return ;
2789
2798
}
@@ -2795,7 +2804,7 @@ void CConnman::ThreadOpenAddedConnections()
2795
2804
}
2796
2805
2797
2806
// if successful, this moves the passed grant to the constructed node
2798
- void CConnman::OpenNetworkConnection (const CAddress& addrConnect, bool fCountFailure , CSemaphoreGrant *grantOutbound, const char *pszDest, ConnectionType conn_type)
2807
+ void CConnman::OpenNetworkConnection (const CAddress& addrConnect, bool fCountFailure , CSemaphoreGrant *grantOutbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport )
2799
2808
{
2800
2809
AssertLockNotHeld (m_unused_i2p_sessions_mutex);
2801
2810
assert (conn_type != ConnectionType::INBOUND);
@@ -2817,7 +2826,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
2817
2826
} else if (FindNode (std::string (pszDest)))
2818
2827
return ;
2819
2828
2820
- CNode* pnode = ConnectNode (addrConnect, pszDest, fCountFailure , conn_type);
2829
+ CNode* pnode = ConnectNode (addrConnect, pszDest, fCountFailure , conn_type, use_v2transport );
2821
2830
2822
2831
if (!pnode)
2823
2832
return ;
@@ -3579,6 +3588,15 @@ ServiceFlags CConnman::GetLocalServices() const
3579
3588
return nLocalServices;
3580
3589
}
3581
3590
3591
+ static std::unique_ptr<Transport> MakeTransport (NodeId id, bool use_v2transport, bool inbound) noexcept
3592
+ {
3593
+ if (use_v2transport) {
3594
+ return std::make_unique<V2Transport>(id, /* initiating=*/ !inbound, SER_NETWORK, INIT_PROTO_VERSION);
3595
+ } else {
3596
+ return std::make_unique<V1Transport>(id, SER_NETWORK, INIT_PROTO_VERSION);
3597
+ }
3598
+ }
3599
+
3582
3600
CNode::CNode (NodeId idIn,
3583
3601
std::shared_ptr<Sock> sock,
3584
3602
const CAddress& addrIn,
@@ -3589,7 +3607,7 @@ CNode::CNode(NodeId idIn,
3589
3607
ConnectionType conn_type_in,
3590
3608
bool inbound_onion,
3591
3609
CNodeOptions&& node_opts)
3592
- : m_transport{std::make_unique<V1Transport> (idIn, SER_NETWORK, INIT_PROTO_VERSION )},
3610
+ : m_transport{MakeTransport (idIn, node_opts. use_v2transport , conn_type_in == ConnectionType::INBOUND )},
3593
3611
m_permission_flags{node_opts.permission_flags },
3594
3612
m_sock{sock},
3595
3613
m_connected{GetTime<std::chrono::seconds>()},
0 commit comments