diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -1446,19 +1446,6 @@ return true; } - if (node.nPingNonceSent && - node.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL} < - GetTime()) { - // We use mockable time for ping timeouts. This means that setmocktime - // may cause pings to time out for peers that have been connected for - // longer than m_peer_connect_timeout. - LogPrintf("ping timeout: %fs\n", - 0.000001 * - count_microseconds(GetTime() - - node.m_ping_start.load())); - return true; - } - if (!node.fSuccessfullyConnected) { LogPrint(BCLog::NET, "version handshake timeout from %d\n", node.GetId()); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -522,7 +522,10 @@ /** Send a version message to a peer */ void PushNodeVersion(const Config &config, CNode &pnode, int64_t nTime); - /** Send a ping message every PING_INTERVAL or if requested via RPC. */ + /** + * Send a ping message every PING_INTERVAL or if requested via RPC. May mark + * the peer to be disconnected if a ping has timed out. + */ void MaybeSendPing(CNode &node_to); const CChainParams &m_chainparams; @@ -5467,6 +5470,21 @@ } void PeerManagerImpl::MaybeSendPing(CNode &node_to) { + // Use mockable time for ping timeouts. + // This means that setmocktime may cause pings to time out. + auto now = GetTime(); + + if (m_connman.RunInactivityChecks(node_to) && node_to.nPingNonceSent && + now > node_to.m_ping_start.load() + + std::chrono::seconds{TIMEOUT_INTERVAL}) { + LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", + 0.000001 * + count_microseconds(now - node_to.m_ping_start.load()), + node_to.GetId()); + node_to.fDisconnect = true; + return; + } + const CNetMsgMaker msgMaker(node_to.GetCommonVersion()); bool pingSend = false; @@ -5476,8 +5494,7 @@ } if (node_to.nPingNonceSent == 0 && - node_to.m_ping_start.load() + PING_INTERVAL < - GetTime()) { + now > node_to.m_ping_start.load() + PING_INTERVAL) { // Ping automatically sent as a latency probe & keepalive. pingSend = true; } @@ -5488,7 +5505,7 @@ GetRandBytes((uint8_t *)&nonce, sizeof(nonce)); } node_to.fPingQueued = false; - node_to.m_ping_start = GetTime(); + node_to.m_ping_start = now; if (node_to.GetCommonVersion() > BIP0031_VERSION) { node_to.nPingNonceSent = nonce; m_connman.PushMessage(&node_to, @@ -5544,6 +5561,11 @@ MaybeSendPing(*pto); + // MaybeSendPing may have marked peer for disconnection + if (pto->fDisconnect) { + return true; + } + auto current_time = GetTime(); bool fFetch;