net: Pass time to InactivityChecks fuctions
What changed, and why it matters
This commit is a small performance and consistency cleanup in Bitcoin Core's network code. It changes the inactivity timeout checks so that the current time is fetched once per loop and passed into helper functions, rather than each helper fetching its own slightly different timestamp. There is no security-relevant change here.
No security action required. Treat as a normal code-quality/performance refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors CConnman::InactivityCheck() and CConnman::ShouldRunInactivityChecks() to accept a std::chrono::microseconds timestamp instead of std::chrono::seconds, and moves the GetTime<>() call from inside InactivityCheck() to the caller SocketHandlerConnected(). This ensures all nodes in a poll iteration are evaluated against the same time snapshot and avoids redundant clock reads. The functional behavior (timeout thresholds, comparisons, logging) is unchanged.
Changed components
src/net.cppsrc/net.hCConnman::InactivityCheck()CConnman::ShouldRunInactivityChecks()CConnman::SocketHandlerConnected()Inspect captured patch +9 / −8
diff --git a/src/net.cpp b/src/net.cpp
index ef1c6304..a6e115b2 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2000,16 +2000,15 @@ void CConnman::NotifyNumConnectionsChanged()
}
}
-bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
+bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const
{
return node.m_connected + m_peer_connect_timeout < now;
}
-bool CConnman::InactivityCheck(const CNode& node) const
+bool CConnman::InactivityCheck(const CNode& node, std::chrono::microseconds now) const
{
// Tests that see disconnects after using mocktime can start nodes with a
// large timeout. For example, -peertimeout=999999999.
- const auto now{GetTime<std::chrono::seconds>()};
const auto last_send{node.m_last_send.load()};
const auto last_recv{node.m_last_recv.load()};
@@ -2033,7 +2032,7 @@ bool CConnman::InactivityCheck(const CNode& node) const
if (now > last_send + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET,
- "socket sending timeout: %is, %s\n", count_seconds(now - last_send),
+ "socket sending timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_send),
node.DisconnectMsg(fLogIPs)
);
return true;
@@ -2041,7 +2040,7 @@ bool CConnman::InactivityCheck(const CNode& node) const
if (now > last_recv + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET,
- "socket receive timeout: %is, %s\n", count_seconds(now - last_recv),
+ "socket receive timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_recv),
node.DisconnectMsg(fLogIPs)
);
return true;
@@ -2123,6 +2122,8 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
{
AssertLockNotHeld(m_total_bytes_sent_mutex);
+ auto now = GetTime<std::chrono::microseconds>();
+
for (CNode* pnode : nodes) {
if (m_interrupt_net->interrupted()) {
return;
@@ -2214,7 +2215,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
}
}
- if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
+ if (InactivityCheck(*pnode, now)) pnode->fDisconnect = true;
}
}
diff --git a/src/net.h b/src/net.h
index c822afe0..387a8c06 100644
--- a/src/net.h
+++ b/src/net.h
@@ -1314,7 +1314,7 @@ public:
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
/** Return true if we should disconnect the peer for failing an inactivity check. */
- bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const;
+ bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
@@ -1364,7 +1364,7 @@ private:
void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
void NotifyNumConnectionsChanged();
/** Return true if the peer is inactive and should be disconnected. */
- bool InactivityCheck(const CNode& node) const;
+ bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
/**
* Generate a collection of sockets to check for IO readiness.
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.