You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[net] Cleanup InactivityChecks() and add commenting about time
Also clean up and better comment the function. InactivityChecks() uses a
mixture of (non-mockable) system time and mockable time. Make sure
that's well documented.
Despite being marked as const in CConnman before this commit, the
function did mutate the state of the passed in CNode, which is contained
in vNodes, which is a member of CConnman. To make the function truly
const in CConnman and all its data, instead make InactivityChecks() a
pure function, return whether the peer should be disconnected, and let
the calling function (SocketHandler()) update the CNode object. Also
make the CNode& argument const.
if (nTime - node.nTimeConnected > m_peer_connect_timeout)
1223
-
{
1224
-
if (node.nLastRecv == 0 || node.nLastSend == 0)
1225
-
{
1226
-
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d from %d\n", m_peer_connect_timeout, node.nLastRecv != 0, node.nLastSend != 0, node.GetId());
LogPrint(BCLog::NET, "version handshake timeout from %d\n", node.GetId());
1247
-
node.fDisconnect = true;
1248
-
}
1221
+
// Use non-mockable system time (otherwise these timers will pop when we
1222
+
// use setmocktime in the tests).
1223
+
int64_t now = GetSystemTimeInSeconds();
1224
+
1225
+
if (now <= node.nTimeConnected + m_peer_connect_timeout) {
1226
+
// Only run inactivity checks if the peer has been connected longer
1227
+
// than m_peer_connect_timeout.
1228
+
returnfalse;
1229
+
}
1230
+
1231
+
if (node.nLastRecv == 0 || node.nLastSend == 0) {
1232
+
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d from %d\n", m_peer_connect_timeout, node.nLastRecv != 0, node.nLastSend != 0, node.GetId());
1233
+
returntrue;
1234
+
}
1235
+
1236
+
if (now > node.nLastSend + TIMEOUT_INTERVAL) {
1237
+
LogPrintf("socket sending timeout: %is\n", now - node.nLastSend);
1238
+
returntrue;
1249
1239
}
1240
+
1241
+
if (now > node.nLastRecv + TIMEOUT_INTERVAL) {
1242
+
LogPrintf("socket receive timeout: %is\n", now - node.nLastRecv);
1243
+
returntrue;
1244
+
}
1245
+
1246
+
if (node.nPingNonceSent && node.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL} < GetTime<std::chrono::microseconds>()) {
1247
+
// We use mockable time for ping timeouts. This means that setmocktime
1248
+
// may cause pings to time out for peers that have been connected for
0 commit comments