Repository: bitcoin/bitcoin
File: src/net.cpp (C++)

Code before:

    if (!ShouldRunInactivityChecks(node, now)) return false;

    if (last_recv.count() == 0 || last_send.count() == 0) {
        LogDebug(BCLog::NET, "socket no message in first %i seconds, %d %d peer=%d\n", count_seconds(m_peer_connect_timeout), last_recv.count() != 0, last_send.count() != 0, node.GetId());
        return true;
    }

    if (now > last_send + TIMEOUT_INTERVAL) {
        LogDebug(BCLog::NET, "socket sending timeout: %is peer=%d\n", count_seconds(now - last_send), node.GetId());
        return true;
    }

    if (now > last_recv + TIMEOUT_INTERVAL) {
        LogDebug(BCLog::NET, "socket receive timeout: %is peer=%d\n", count_seconds(now - last_recv), node.GetId());
        return true;
    }

    if (!node.fSuccessfullyConnected) {
        if (node.m_transport->GetInfo().transport_type == TransportProtocolType::DETECTING) {
            LogDebug(BCLog::NET, "V2 handshake timeout peer=%d\n", node.GetId());
        } else {
            LogDebug(BCLog::NET, "version handshake timeout peer=%d\n", node.GetId());
        }
        return true;
    }

    return false;
}

Sock::EventsPerSock CConnman::GenerateWaitSockets(Span<CNode* const> nodes)
{
    Sock::EventsPerSock events_per_sock;

    for (const ListenSocket& hListenSocket : vhListenSocket) {
        events_per_sock.emplace(hListenSocket.sock, Sock::Events{Sock::RECV});
    }

    for (CNode* pnode : nodes) {
        bool select_recv = !pnode->fPauseRecv;
        bool select_send;
        {
            LOCK(pnode->cs_vSend);
            // Sending is possible if either there are bytes to send right now, or if there will be
            // once a potential message from vSendMsg is handed to the transport. GetBytesToSend
            // determines both of these in a single call.
            const auto& [to_send, more, _msg_type] = pnode->m_transport->GetBytesToSend(!pnode->vSendMsg.empty());
            select_send = !to_send.empty() || more;
        }
        if (!select_recv && !select_send) continue;


Review comment (suggestion):
This `%d %d`, `last_recv.count() != 0, last_send.count() != 0` is super obscure. It was like that before, so it is fine wrt this PR.

But would be nice to fix it, since this is changing the same log message already. I am not sure how though. Maybe change that `%d` to something like "has never received from the peer" + "has never sent to the peer".

Code after:
{
    return node.m_connected + m_peer_connect_timeout < now;
}

bool CConnman::InactivityCheck(const CNode& node) 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()};

    if (!ShouldRunInactivityChecks(node, now)) return false;

    bool has_received{last_recv.count() != 0};
    bool has_sent{last_send.count() != 0};

    if (!has_received || !has_sent) {
        std::string has_never;
        if (!has_received) has_never += ", never received from peer";
        if (!has_sent) has_never += ", never sent to peer";
        LogDebug(BCLog::NET,
            "socket no message in first %i seconds%s, %s\n",
            count_seconds(m_peer_connect_timeout),
            has_never,
            node.DisconnectMsg(fLogIPs)
        );
        return true;
    }

    if (now > last_send + TIMEOUT_INTERVAL) {
        LogDebug(BCLog::NET,
            "socket sending timeout: %is, %s\n", count_seconds(now - last_send),
            node.DisconnectMsg(fLogIPs)
        );
        return true;
    }

    if (now > last_recv + TIMEOUT_INTERVAL) {
        LogDebug(BCLog::NET,
            "socket receive timeout: %is, %s\n", count_seconds(now - last_recv),
            node.DisconnectMsg(fLogIPs)
        );
        return true;
    }

    if (!node.fSuccessfullyConnected) {
        if (node.m_transport->GetInfo().transport_type == TransportProtocolType::DETECTING) {
            LogDebug(BCLog::NET, "V2 handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));
        } else {
            LogDebug(BCLog::NET, "version handshake timeout, %s\n", node.DisconnectMsg(fLogIPs));