diff options
author | W. J. van der Laan <laanwj@protonmail.com> | 2021-04-01 15:51:05 +0200 |
---|---|---|
committer | W. J. van der Laan <laanwj@protonmail.com> | 2021-04-01 16:36:22 +0200 |
commit | 086226d98ae8c194a0a38b2fbfffd0dc3773e879 (patch) | |
tree | e902132d836647b814dd495c7274aa62c5775d7d /src | |
parent | 6e22b522f9505d6a3c71ef9972aea6ae3fb10d2e (diff) | |
parent | 5ed535a02f8f0a6f65bbe19f48a8c81f43298393 (diff) |
Merge #21198: net: Address outstanding review comments from PR20721
5ed535a02f8f0a6f65bbe19f48a8c81f43298393 [net] Changes to RunInactivityChecks (John Newbery)
Pull request description:
Updates the RunInactivityChecks() function:
- rename to ShouldRunInactivityChecks (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r576394790)
- take optional time now (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r575895661)
- call from within InactivityChecks (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r575894665)
- update comment (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r575894343)
- change ordering of inequality (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r574925129)
- ~make inline (https://github.com/bitcoin/bitcoin/pull/20721#discussion_r574903578)~
ACKs for top commit:
laanwj:
Code review ACK 5ed535a02f8f0a6f65bbe19f48a8c81f43298393
Tree-SHA512: e6ac8e8cce5cddc84a52a40c908634c25f58be74512d642840d7bd7fa65c3d90a0f46cc19e4865b3fae7c933138247f58356167a60a5c519305cfd6d05e51f51
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 9 | ||||
-rw-r--r-- | src/net.h | 4 | ||||
-rw-r--r-- | src/net_processing.cpp | 2 |
3 files changed, 9 insertions, 6 deletions
diff --git a/src/net.cpp b/src/net.cpp index 1dcb141421..c43c53795e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1255,9 +1255,10 @@ void CConnman::NotifyNumConnectionsChanged() } } -bool CConnman::RunInactivityChecks(const CNode& node) const +bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now_in) const { - return GetSystemTimeInSeconds() > node.nTimeConnected + m_peer_connect_timeout; + const int64_t now = now_in ? now_in.value() : GetSystemTimeInSeconds(); + return node.nTimeConnected + m_peer_connect_timeout < now; } bool CConnman::InactivityCheck(const CNode& node) const @@ -1266,6 +1267,8 @@ bool CConnman::InactivityCheck(const CNode& node) const // use setmocktime in the tests). int64_t now = GetSystemTimeInSeconds(); + if (!ShouldRunInactivityChecks(node, now)) return false; + if (node.nLastRecv == 0 || node.nLastSend == 0) { 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()); return true; @@ -1562,7 +1565,7 @@ void CConnman::SocketHandler() if (bytes_sent) RecordBytesSent(bytes_sent); } - if (RunInactivityChecks(*pnode) && InactivityCheck(*pnode)) pnode->fDisconnect = true; + if (InactivityCheck(*pnode)) pnode->fDisconnect = true; } { LOCK(cs_vNodes); @@ -1018,8 +1018,8 @@ public: void SetAsmap(std::vector<bool> asmap) { addrman.m_asmap = std::move(asmap); } - /** Return true if the peer has been connected for long enough to do inactivity checks. */ - bool RunInactivityChecks(const CNode& node) const; + /** Return true if we should disconnect the peer for failing an inactivity check. */ + bool ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now=std::nullopt) const; private: struct ListenSocket { diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 17d7619ff5..4108de2c8a 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4109,7 +4109,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers() void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now) { - if (m_connman.RunInactivityChecks(node_to) && peer.m_ping_nonce_sent && + if (m_connman.ShouldRunInactivityChecks(node_to) && peer.m_ping_nonce_sent && now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) { LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id); node_to.fDisconnect = true; |