mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-21 07:39:08 +01:00
p2p: Make timeout mockable and type safe, speed up test
This commit is contained in:
28
src/net.cpp
28
src/net.cpp
@@ -633,7 +633,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
|
||||
complete = false;
|
||||
const auto time = GetTime<std::chrono::microseconds>();
|
||||
LOCK(cs_vRecv);
|
||||
m_last_recv = 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
|
||||
@@ -804,7 +804,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.m_last_send = GetTimeSeconds();
|
||||
node.m_last_send = GetTime<std::chrono::seconds>();
|
||||
node.nSendBytes += nBytes;
|
||||
node.nSendOffset += nBytes;
|
||||
nSentSize += nBytes;
|
||||
@@ -1317,31 +1317,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.m_last_recv == 0 || node.m_last_send == 0) {
|
||||
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d peer=%d\n", m_peer_connect_timeout, node.m_last_recv != 0, node.m_last_send != 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.m_last_send + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket sending timeout: %is peer=%d\n", now - node.m_last_send, 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.m_last_recv + TIMEOUT_INTERVAL) {
|
||||
LogPrint(BCLog::NET, "socket receive timeout: %is peer=%d\n", now - node.m_last_recv, 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