mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-08 17:30:36 +02:00
Merge bitcoin/bitcoin#19499: p2p: Make timeout mockable and type safe, speed up test
fadc0c80ae
p2p: Make timeout mockable and type safe, speed up test (MarcoFalke)fa6d5a238d
scripted-diff: Rename m_last_send and m_last_recv (MarcoFalke) Pull request description: Use type-safe time for better code readability/maintainability and mockable time for better testability. This speeds up the p2p_timeout test. This is also a bugfix for intermittent test issues like: https://cirrus-ci.com/task/4769904156999680?command=ci#L2836 Fixes #20654 ACKs for top commit: laanwj: Code review ACKfadc0c80ae
naumenkogs: ACKfadc0c80ae
Tree-SHA512: 28c6544c97f188c8a0fbc80411c74ab74ffd055885322c325aa3d1c404b29c3fd70a737e86083eecae58ef394db1cb56bc122d06cff63742aa89a8e868730c64
This commit is contained in:
32
src/net.cpp
32
src/net.cpp
@ -586,8 +586,8 @@ void CNode::CopyStats(CNodeStats& stats)
|
||||
} else {
|
||||
stats.fRelayTxes = false;
|
||||
}
|
||||
X(nLastSend);
|
||||
X(nLastRecv);
|
||||
X(m_last_send);
|
||||
X(m_last_recv);
|
||||
X(nLastTXTime);
|
||||
X(nLastBlockTime);
|
||||
X(nTimeConnected);
|
||||
@ -634,7 +634,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
|
||||
complete = false;
|
||||
const auto time = GetTime<std::chrono::microseconds>();
|
||||
LOCK(cs_vRecv);
|
||||
nLastRecv = std::chrono::duration_cast<std::chrono::seconds>(time).count();
|
||||
m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
|
||||
nRecvBytes += msg_bytes.size();
|
||||
while (msg_bytes.size() > 0) {
|
||||
// absorb network data
|
||||
@ -805,7 +805,7 @@ size_t CConnman::SocketSendData(CNode& node) const
|
||||
nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
}
|
||||
if (nBytes > 0) {
|
||||
node.nLastSend = GetTimeSeconds();
|
||||
node.m_last_send = GetTime<std::chrono::seconds>();
|
||||
node.nSendBytes += nBytes;
|
||||
node.nSendOffset += nBytes;
|
||||
nSentSize += nBytes;
|
||||
@ -1318,31 +1318,33 @@ void CConnman::NotifyNumConnectionsChanged()
|
||||
}
|
||||
}
|
||||
|
||||
bool CConnman::ShouldRunInactivityChecks(const CNode& node, int64_t now) const
|
||||
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
|
||||
{
|
||||
return node.nTimeConnected + m_peer_connect_timeout < now;
|
||||
return std::chrono::seconds{node.nTimeConnected} + m_peer_connect_timeout < now;
|
||||
}
|
||||
|
||||
bool CConnman::InactivityCheck(const CNode& node) const
|
||||
{
|
||||
// Use non-mockable system time (otherwise these timers will pop when we
|
||||
// use setmocktime in the tests).
|
||||
int64_t now = GetTimeSeconds();
|
||||
// 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;
|
||||
|
||||
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());
|
||||
if (last_recv.count() == 0 || last_send.count() == 0) {
|
||||
LogPrint(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 > node.nLastSend + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket sending timeout: %is peer=%d\n", now - node.nLastSend, node.GetId());
|
||||
if (now > last_send + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket sending timeout: %is peer=%d\n", count_seconds(now - last_send), node.GetId());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (now > node.nLastRecv + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket receive timeout: %is peer=%d\n", now - node.nLastRecv, node.GetId());
|
||||
if (now > last_recv + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket receive timeout: %is peer=%d\n", count_seconds(now - last_recv), node.GetId());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user