Skip to content

Commit 5ed535a

Browse files
committed
[net] Changes to RunInactivityChecks
- rename to ShouldRunInactivityChecks (bitcoin/bitcoin#20721 (comment)) - take optional time now (bitcoin/bitcoin#20721 (comment)) - call from within InactivityChecks (bitcoin/bitcoin#20721 (comment)) - update comment (bitcoin/bitcoin#20721 (comment)) - change ordering of inequality (bitcoin/bitcoin#20721 (comment))
1 parent 2b2ab9a commit 5ed535a

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/net.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,10 @@ void CConnman::NotifyNumConnectionsChanged()
12551255
}
12561256
}
12571257

1258-
bool CConnman::RunInactivityChecks(const CNode& node) const
1258+
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now_in) const
12591259
{
1260-
return GetSystemTimeInSeconds() > node.nTimeConnected + m_peer_connect_timeout;
1260+
const int64_t now = now_in ? now_in.value() : GetSystemTimeInSeconds();
1261+
return node.nTimeConnected + m_peer_connect_timeout < now;
12611262
}
12621263

12631264
bool CConnman::InactivityCheck(const CNode& node) const
@@ -1266,6 +1267,8 @@ bool CConnman::InactivityCheck(const CNode& node) const
12661267
// use setmocktime in the tests).
12671268
int64_t now = GetSystemTimeInSeconds();
12681269

1270+
if (!ShouldRunInactivityChecks(node, now)) return false;
1271+
12691272
if (node.nLastRecv == 0 || node.nLastSend == 0) {
12701273
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d peer=%d\n", m_peer_connect_timeout, node.nLastRecv != 0, node.nLastSend != 0, node.GetId());
12711274
return true;
@@ -1562,7 +1565,7 @@ void CConnman::SocketHandler()
15621565
if (bytes_sent) RecordBytesSent(bytes_sent);
15631566
}
15641567

1565-
if (RunInactivityChecks(*pnode) && InactivityCheck(*pnode)) pnode->fDisconnect = true;
1568+
if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
15661569
}
15671570
{
15681571
LOCK(cs_vNodes);

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,8 @@ class CConnman
10181018

10191019
void SetAsmap(std::vector<bool> asmap) { addrman.m_asmap = std::move(asmap); }
10201020

1021-
/** Return true if the peer has been connected for long enough to do inactivity checks. */
1022-
bool RunInactivityChecks(const CNode& node) const;
1021+
/** Return true if we should disconnect the peer for failing an inactivity check. */
1022+
bool ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now=std::nullopt) const;
10231023

10241024
private:
10251025
struct ListenSocket {

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4108,7 +4108,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
41084108

41094109
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
41104110
{
4111-
if (m_connman.RunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
4111+
if (m_connman.ShouldRunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
41124112
now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
41134113
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
41144114
node_to.fDisconnect = true;

0 commit comments

Comments
 (0)