refactor: Use NodeClock::time_point for m_last_send/recv and m_ping_start

The two fields represent a time point, not a duration. Also, it is
unclear why they use second precision.

Fix both issues by using NodeClock::time_point.

This refactor should not change any behavior.

This resolves the two temporary calls to time_since_epoch() added in the
previous commit. However, it adds one new call to time_since_epoch(),
which is resolved in the next commit.
This commit is contained in:
MarcoFalke
2026-03-20 14:59:34 +01:00
parent fa2605b204
commit fa244b984c
6 changed files with 35 additions and 28 deletions

View File

@@ -275,7 +275,7 @@ struct Peer {
/** The pong reply we're expecting, or 0 if no pong expected. */
std::atomic<uint64_t> m_ping_nonce_sent{0};
/** When the last ping was sent, or 0 if no ping was ever sent */
std::atomic<std::chrono::microseconds> m_ping_start{0us};
std::atomic<NodeClock::time_point> m_ping_start{NodeClock::epoch};
/** Whether a ping has been requested by the user */
std::atomic<bool> m_ping_queued{false};
@@ -732,7 +732,7 @@ private:
* May mark the peer to be disconnected if a ping has timed out.
* We use mockable time for ping timeouts, so setmocktime may cause pings
* to time out. */
void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
void MaybeSendPing(CNode& node_to, Peer& peer, NodeClock::time_point now);
/** Send `addr` messages on a regular schedule. */
void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
@@ -1809,9 +1809,9 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c
// since pingtime does not update until the ping is complete, which might take a while.
// So, if a ping is taking an unusually long time in flight,
// the caller can immediately detect that this is happening.
auto ping_wait{0us};
if ((0 != peer->m_ping_nonce_sent) && (0 != peer->m_ping_start.load().count())) {
ping_wait = GetTime<std::chrono::microseconds>() - peer->m_ping_start.load();
NodeClock::duration ping_wait{0us};
if ((0 != peer->m_ping_nonce_sent) && (peer->m_ping_start.load() > NodeClock::epoch)) {
ping_wait = NodeClock::now() - peer->m_ping_start.load();
}
if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
@@ -4158,7 +4158,7 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
MakeAndPushMessage(pfrom, NetMsgType::TX, TX_WITH_WITNESS(*pushed_tx));
peer.m_ping_queued = true; // Ensure a ping will be sent: mimic a request via RPC.
MaybeSendPing(pfrom, peer, GetTime<std::chrono::microseconds>());
MaybeSendPing(pfrom, peer, NodeClock::now());
} else {
LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: got an unexpected GETDATA message, %s",
pfrom.LogPeer());
@@ -4900,7 +4900,7 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
}
if (msg_type == NetMsgType::PONG) {
const auto ping_end{time_received.time_since_epoch()};
const auto ping_end{time_received};
uint64_t nonce = 0;
size_t nAvail = vRecv.in_avail();
bool bPingFinished = false;
@@ -5396,7 +5396,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
}
}
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, NodeClock::time_point now)
{
if (m_connman.ShouldRunInactivityChecks(node_to, now) &&
peer.m_ping_nonce_sent &&
@@ -5733,6 +5733,7 @@ bool PeerManagerImpl::SendMessages(CNode& node)
if (!node.fSuccessfullyConnected || node.fDisconnect)
return true;
const auto now{NodeClock::now()};
const auto current_time{GetTime<std::chrono::microseconds>()};
// The logic below does not apply to private broadcast peers, so skip it.
@@ -5753,7 +5754,7 @@ bool PeerManagerImpl::SendMessages(CNode& node)
return true;
}
MaybeSendPing(node, peer, current_time);
MaybeSendPing(node, peer, now);
// MaybeSendPing may have marked peer for disconnection
if (node.fDisconnect) return true;