Skip to content

Commit dd2646d

Browse files
committed
[net processing] Move ping timeout logic to net processing
Ping messages are an application-level mechanism. Move timeout logic from net to net processing.
1 parent 0b43b81 commit dd2646d

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/net.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,14 +1247,6 @@ bool CConnman::InactivityCheck(const CNode& node) const
12471247
return true;
12481248
}
12491249

1250-
if (node.nPingNonceSent && node.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL} < GetTime<std::chrono::microseconds>()) {
1251-
// We use mockable time for ping timeouts. This means that setmocktime
1252-
// may cause pings to time out for peers that have been connected for
1253-
// longer than m_peer_connect_timeout.
1254-
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(GetTime<std::chrono::microseconds>() - node.m_ping_start.load()), node.GetId());
1255-
return true;
1256-
}
1257-
12581250
if (!node.fSuccessfullyConnected) {
12591251
LogPrint(BCLog::NET, "version handshake timeout peer=%d\n", node.GetId());
12601252
return true;

src/net_processing.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ class PeerManagerImpl final : public PeerManager
324324
/** Send a version message to a peer */
325325
void PushNodeVersion(CNode& pnode, int64_t nTime);
326326

327-
/** Send a ping message every PING_INTERVAL or if requested via RPC. */
327+
/** Send a ping message every PING_INTERVAL or if requested via RPC. May
328+
* mark the peer to be disconnected if a ping has timed out. */
328329
void MaybeSendPing(CNode& node_to);
329330

330331
const CChainParams& m_chainparams;
@@ -4297,6 +4298,17 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
42974298

42984299
void PeerManagerImpl::MaybeSendPing(CNode& node_to)
42994300
{
4301+
// Use mockable time for ping timeouts.
4302+
// This means that setmocktime may cause pings to time out.
4303+
auto now = GetTime<std::chrono::microseconds>();
4304+
4305+
if (m_connman.RunInactivityChecks(node_to) && node_to.nPingNonceSent &&
4306+
now > node_to.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
4307+
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - node_to.m_ping_start.load()), node_to.GetId());
4308+
node_to.fDisconnect = true;
4309+
return;
4310+
}
4311+
43004312
const CNetMsgMaker msgMaker(node_to.GetCommonVersion());
43014313
bool pingSend = false;
43024314

@@ -4305,7 +4317,7 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to)
43054317
pingSend = true;
43064318
}
43074319

4308-
if (node_to.nPingNonceSent == 0 && node_to.m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) {
4320+
if (node_to.nPingNonceSent == 0 && now > node_to.m_ping_start.load() + PING_INTERVAL) {
43094321
// Ping automatically sent as a latency probe & keepalive.
43104322
pingSend = true;
43114323
}
@@ -4316,7 +4328,7 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to)
43164328
GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
43174329
}
43184330
node_to.fPingQueued = false;
4319-
node_to.m_ping_start = GetTime<std::chrono::microseconds>();
4331+
node_to.m_ping_start = now;
43204332
if (node_to.GetCommonVersion() > BIP0031_VERSION) {
43214333
node_to.nPingNonceSent = nonce;
43224334
m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING, nonce));
@@ -4368,6 +4380,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
43684380

43694381
MaybeSendPing(*pto);
43704382

4383+
// MaybeSendPing may have marked peer for disconnection
4384+
if (pto->fDisconnect) return true;
4385+
43714386
{
43724387
LOCK(cs_main);
43734388

0 commit comments

Comments
 (0)