merge bitcoin#21198: Address outstanding review comments from PR20721

This commit is contained in:
Kittywhiskers Van Gogh 2024-03-24 20:57:05 +00:00
parent d34d2c4efb
commit 39384ba461
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 9 additions and 6 deletions

View File

@ -1488,9 +1488,10 @@ void CConnman::CalculateNumConnectionsChangedStats()
statsClient.gauge("peers.torConnections", torNodes, 1.0f);
}
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
@ -1499,6 +1500,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;
@ -2038,7 +2041,7 @@ void CConnman::ThreadSocketHandler()
SocketHandler();
if (GetTimeMillis() - nLastCleanupNodes > 1000) {
ForEachNode(AllNodes, [&](CNode* pnode) {
if (RunInactivityChecks(*pnode) && InactivityCheck(*pnode)) pnode->fDisconnect = true;
if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
});
nLastCleanupNodes = GetTimeMillis();
}

View File

@ -1295,8 +1295,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 {

View File

@ -4868,7 +4868,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;