What changed, and why it matters
This commit only adds a new test-only helper class in a header file under the test directory. It makes it easier for Bitcoin Core's own unit tests to set and reset mock time, and prevents mock-time state from leaking between test cases. There is no change to production code, no network-facing change, and no security fix or vulnerability introduced.
No security action needed. This is a routine test infrastructure improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds NodeClockContext in src/test/util/time.h. It is an RAII wrapper around SetMockTime() that initializes global mock time on construction and resets it to 0 on destruction. It also provides convenience methods to set, advance, or rewind mock time. The class is explicitly test-only (lives in src/test), is non-copyable, and does not alter any consensus, networking, or wallet code.
Changed components
src/test/util/time.hInspect captured patch +28 / −0
diff --git a/src/test/util/time.h b/src/test/util/time.h
index bcb97eb4..8cd9a1da 100644
--- a/src/test/util/time.h
+++ b/src/test/util/time.h
@@ -34,4 +34,32 @@ public:
}
};
+/// Helper to initialize the global NodeClock, let a duration elapse,
+/// and reset it after use in a test.
+class NodeClockContext
+{
+ NodeSeconds m_t{std::chrono::seconds::max()};
+
+public:
+ /// Initialize with the given time.
+ explicit NodeClockContext(NodeSeconds init_time) { set(init_time); }
+ explicit NodeClockContext(std::chrono::seconds init_time) { set(init_time); }
+ /// Initialize with current time, using the next tick to avoid going back by rounding to seconds.
+ explicit NodeClockContext() { set(++Now<NodeSeconds>().time_since_epoch()); }
+
+ /// Unset mocktime.
+ ~NodeClockContext() { set(0s); }
+
+ NodeClockContext(const NodeClockContext&) = delete;
+ NodeClockContext& operator=(const NodeClockContext&) = delete;
+
+ /// Set mocktime.
+ void set(NodeSeconds t) { SetMockTime(m_t = t); }
+ void set(std::chrono::seconds t) { set(NodeSeconds{t}); }
+
+ /// Change mocktime by the given duration delta.
+ void operator+=(std::chrono::seconds d) { set(m_t += d); }
+ void operator-=(std::chrono::seconds d) { set(m_t -= d); }
+};
+
#endif // BITCOIN_TEST_UTIL_TIME_H
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.