test: Limit clocks to one active instance
What changed, and why it matters
This is a test-only change that adds a safety guard to prevent two test helper objects from being used at the same time. It does not affect the live Bitcoin network or production code, and there is no security vulnerability being fixed.
No action required; treat as routine test-code improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds a CRTP helper class LimitOne in src/test/util/time.h and derives SteadyClockContext and FakeNodeClock from it. LimitOne asserts at construction if another instance of the same type already exists, preventing overlapping scopes from silently clobbering shared global mock clocks. This is purely a test-code hardening measure; no runtime or consensus code is changed.
Changed components
src/test/util/time.hInspect captured patch +16 / −2
diff --git a/src/test/util/time.h b/src/test/util/time.h
index 0d93a61c..2bc2553f 100644
--- a/src/test/util/time.h
+++ b/src/test/util/time.h
@@ -8,10 +8,24 @@
#include <util/check.h>
#include <util/time.h>
+/// CRTP Helper to limit a class to at most one at a time.
+template <class T>
+class LimitOne
+{
+public:
+ LimitOne() { Assert(g_T_available) = false; }
+ ~LimitOne() { g_T_available = true; }
+ LimitOne(const LimitOne&) = delete;
+ LimitOne& operator=(const LimitOne&) = delete;
+
+private:
+ static inline bool g_T_available{true};
+};
+
/// Helper to initialize the global MockableSteadyClock, let a duration elapse,
/// and reset it after use in a test.
-class SteadyClockContext
+class SteadyClockContext : public LimitOne<SteadyClockContext>
{
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
@@ -36,7 +50,7 @@ public:
/// Helper to initialize the global NodeClock, let a duration elapse,
/// and reset it after use in a test.
-class FakeNodeClock
+class FakeNodeClock : public LimitOne<FakeNodeClock>
{
NodeSeconds m_t{std::chrono::seconds::max()};
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.