From 74918891a5624755d6d4c3e0333c4da84df668f8 Mon Sep 17 00:00:00 2001 From: dhruv <856960+dhruv@users.noreply.github.com> Date: Tue, 15 Feb 2022 21:41:22 -0800 Subject: [PATCH] p2p: BIP324 v2.0 clients retry with v1 protocol --- src/net.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 1d7ff4f4f2..31a1f0c222 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -415,7 +415,10 @@ CNode* CConnman::FindNode(const CService& addr) bool CConnman::AlreadyConnectedToAddress(const CAddress& addr) { - return FindNode(static_cast(addr)) || FindNode(addr.ToStringIPPort()); + CNode* found_by_addr = FindNode(static_cast(addr)); + CNode* found_by_ip_port = FindNode(addr.ToStringIPPort()); + return (found_by_addr && !found_by_addr->fDisconnect) || + (found_by_ip_port && !found_by_ip_port->fDisconnect); } bool CConnman::CheckIncomingNonce(uint64_t nonce) @@ -486,8 +489,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo // Look for an existing connection CNode* pnode = FindNode(static_cast(addrConnect)); - if (pnode) - { + if (pnode && !pnode->fDisconnect) { LogPrintf("Failed to open new connection, already connected\n"); return nullptr; } @@ -514,7 +516,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo // In that case, drop the connection that was just created. LOCK(m_nodes_mutex); CNode* pnode = FindNode(static_cast(addrConnect)); - if (pnode) { + if (pnode && !pnode->fDisconnect) { LogPrintf("Failed to open new connection, already connected\n"); return nullptr; } @@ -1976,6 +1978,13 @@ void CConnman::SocketHandlerConnected(const std::vector& nodes, LogPrint(BCLog::NET, "socket closed for peer=%d\n", pnode->GetId()); } pnode->CloseSocketDisconnect(); + + if (pnode->PreferV2Conn() && !pnode->tried_v2_handshake) { + CAddress addr = pnode->addr; + addr.nServices = ServiceFlags(addr.nServices & ~NODE_P2P_V2); + CSemaphoreGrant grant(*semOutbound); + OpenNetworkConnection(addr, false, &grant, addr.ToStringIPPort().c_str(), pnode->m_conn_type); + } } else if (nBytes < 0) { // error int nErr = WSAGetLastError(); @@ -2609,8 +2618,10 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai if (IsLocal(addrConnect) || banned_or_discouraged || AlreadyConnectedToAddress(addrConnect)) { return; } - } else if (FindNode(std::string(pszDest))) - return; + } else { + auto existing_node = FindNode(std::string(pszDest)); + if (existing_node && !existing_node->fDisconnect) return; + } CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type);