aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-12-10 10:02:03 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-12-10 10:02:12 +0100
commit9f7661c0c4cbed06c558c2c8688c0a7286c5a6e2 (patch)
tree7d34ed083d7b9d323d515f516cec7087df4d881e /src/net.cpp
parentd5724b8c6a1f542edc0b02b6bd069121e2cd2542 (diff)
parentfadc0c80ae4e526fb2b503f7cc02f6122aaf1de5 (diff)
downloadbitcoin-9f7661c0c4cbed06c558c2c8688c0a7286c5a6e2.tar.xz
Merge bitcoin/bitcoin#19499: p2p: Make timeout mockable and type safe, speed up test
fadc0c80ae4e526fb2b503f7cc02f6122aaf1de5 p2p: Make timeout mockable and type safe, speed up test (MarcoFalke) fa6d5a238d2c94440105ddd4f1554f85659d6c5b 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 ACK fadc0c80ae4e526fb2b503f7cc02f6122aaf1de5 naumenkogs: ACK fadc0c80ae4e526fb2b503f7cc02f6122aaf1de5 Tree-SHA512: 28c6544c97f188c8a0fbc80411c74ab74ffd055885322c325aa3d1c404b29c3fd70a737e86083eecae58ef394db1cb56bc122d06cff63742aa89a8e868730c64
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 2d9e69b6fb..2adccce11e 100644
--- a/src/net.cpp
+++ b/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;
}