refactor: Use NodeClock::time_point for CNetMessage::m_time
What changed, and why it matters
This is a straightforward code cleanup (refactor) that changes how message timestamps are represented internally. It does not fix a bug, add a feature, or change network behavior. There is no security relevance.
No security action needed. Treat as normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit changes CNetMessage::m_time from std::chrono::microseconds (a duration) to NodeClock::time_point (a time point), updating call sites in net.cpp, net.h, net_processing.cpp, and a fuzz test. It introduces temporary time_since_epoch() calls to preserve existing behavior, noting they will be removed in the next commit. No logic changes affecting protocol parsing, validation, or resource handling.
Changed components
src/net.cppsrc/net.hsrc/net_processing.cppsrc/test/fuzz/p2p_transport_serialization.cppInspect captured patch +15 / −14
diff --git a/src/net.cpp b/src/net.cpp
index 71befd29..399b41f3 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -662,9 +662,9 @@ void CNode::CopyStats(CNodeStats& stats)
bool CNode::ReceiveMsgBytes(std::span<const uint8_t> msg_bytes, bool& complete)
{
complete = false;
- const auto time = GetTime<std::chrono::microseconds>();
+ const auto time{NodeClock::now()};
LOCK(cs_vRecv);
- m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
+ m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(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<std::string> V2Transport::GetMessageType(std::span<const uint8_t>&
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 135b1fc0..acef9035 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<const uint8_t>& 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 d9cf3920..507be59c 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<bool>& 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<bool>& 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 88432ec1..82b282d3 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<int64_t>::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<unsigned char> header;
auto msg2 = NetMsg::Make(msg.m_type, std::span{msg.m_recv});
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.