From fa2605b20471a225bd69f8516508dce78e018344 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 20 Mar 2026 13:23:30 +0100 Subject: [PATCH] refactor: Use NodeClock::time_point for CNetMessage::m_time The field is not a duration, but a time point. This will add two temporary calls to time_since_epoch(), which are fixed in the next commit. --- src/net.cpp | 8 ++++---- src/net.h | 9 +++++---- src/net_processing.cpp | 6 +++--- src/test/fuzz/p2p_transport_serialization.cpp | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 71befd292bd..399b41f3aa5 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -662,9 +662,9 @@ void CNode::CopyStats(CNodeStats& stats) bool CNode::ReceiveMsgBytes(std::span msg_bytes, bool& complete) { complete = false; - const auto time = GetTime(); + const auto time{NodeClock::now()}; LOCK(cs_vRecv); - m_last_recv = std::chrono::duration_cast(time); + m_last_recv = std::chrono::duration_cast(time.time_since_epoch()); nRecvBytes += msg_bytes.size(); while (msg_bytes.size() > 0) { // absorb network data @@ -800,7 +800,7 @@ const uint256& V1Transport::GetMessageHash() const return data_hash; } -CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time, bool& reject_message) +CNetMessage V1Transport::GetReceivedMessage(NodeClock::time_point time, bool& reject_message) { AssertLockNotHeld(m_recv_mutex); // Initialize out parameter @@ -1452,7 +1452,7 @@ std::optional V2Transport::GetMessageType(std::span& return ret; } -CNetMessage V2Transport::GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) noexcept +CNetMessage V2Transport::GetReceivedMessage(NodeClock::time_point time, bool& reject_message) noexcept { AssertLockNotHeld(m_recv_mutex); LOCK(m_recv_mutex); diff --git a/src/net.h b/src/net.h index 135b1fc0a37..acef9035c59 100644 --- a/src/net.h +++ b/src/net.h @@ -237,7 +237,8 @@ class CNetMessage { public: DataStream m_recv; //!< received message data - std::chrono::microseconds m_time{0}; //!< time of message receipt + /// time of message receipt + NodeClock::time_point m_time{NodeClock::epoch}; uint32_t m_message_size{0}; //!< size of the payload uint32_t m_raw_message_size{0}; //!< used wire size of the message (including header/checksum) std::string m_type; @@ -291,7 +292,7 @@ public: * If reject_message=true is returned the message itself is invalid, but (other than false * returned by ReceivedBytes) the transport is not in an inconsistent state. */ - virtual CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) = 0; + virtual CNetMessage GetReceivedMessage(NodeClock::time_point time, bool& reject_message) = 0; // 2. Sending side functions, for converting messages into bytes to be sent over the wire. @@ -443,7 +444,7 @@ public: return ret >= 0; } - CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); + CNetMessage GetReceivedMessage(NodeClock::time_point time, bool& reject_message) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); bool SetMessageToSend(CSerializedNetMsg& msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex); BytesToSend GetBytesToSend(bool have_next_message) const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex); @@ -652,7 +653,7 @@ public: // Receive side functions. bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); bool ReceivedBytes(std::span& msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex, !m_send_mutex); - CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); + CNetMessage GetReceivedMessage(NodeClock::time_point time, bool& reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); // Send side functions. bool SetMessageToSend(CSerializedNetMsg& msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index d9cf3920e0e..507be59cb7c 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -553,7 +553,7 @@ public: ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const override; private: - void ProcessMessage(Peer& peer, CNode& pfrom, const std::string& msg_type, DataStream& vRecv, std::chrono::microseconds time_received, + void ProcessMessage(Peer& peer, CNode& pfrom, const std::string& msg_type, DataStream& vRecv, NodeClock::time_point time_received, const std::atomic& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex, !m_tx_download_mutex); @@ -3570,7 +3570,7 @@ void PeerManagerImpl::PushPrivateBroadcastTx(CNode& node) } void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string& msg_type, DataStream& vRecv, - const std::chrono::microseconds time_received, + const NodeClock::time_point time_received, const std::atomic& interruptMsgProc) { AssertLockHeld(g_msgproc_mutex); @@ -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; + const auto ping_end{time_received.time_since_epoch()}; uint64_t nonce = 0; size_t nAvail = vRecv.in_avail(); bool bPingFinished = false; diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp index 88432ec1009..82b282d3eb0 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -78,13 +78,13 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial break; } if (recv_transport.ReceivedMessageComplete()) { - const std::chrono::microseconds m_time{std::numeric_limits::max()}; + const auto time{NodeClock::time_point::max()}; bool reject_message{false}; - CNetMessage msg = recv_transport.GetReceivedMessage(m_time, reject_message); + CNetMessage msg = recv_transport.GetReceivedMessage(time, reject_message); assert(msg.m_type.size() <= CMessageHeader::MESSAGE_TYPE_SIZE); assert(msg.m_raw_message_size <= mutable_msg_bytes.size()); assert(msg.m_raw_message_size == CMessageHeader::HEADER_SIZE + msg.m_message_size); - assert(msg.m_time == m_time); + assert(msg.m_time == time); std::vector header; auto msg2 = NetMsg::Make(msg.m_type, std::span{msg.m_recv});