test: Turn ElapseSteady into SteadyClockContext
What changed, and why it matters
This is a small internal cleanup of Bitcoin Core's test-only code. It renames a helper struct used in fuzz tests and adds a destructor that resets the mock clock after each test. There is no change to the live Bitcoin network code, no bug fix for user funds or consensus, and no security relevance.
No action required. This is a test-only refactor and does not affect production security.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors ElapseSteady into SteadyClockContext inside src/test/util/time.h, a header used only by tests. The new class is non-copyable, initializes the mock steady clock in its constructor, clears the mock time in its destructor, and changes the call operator to operator+= with an assertion that the delta is non-negative. The only production-adjacent code touched is a fuzz target (p2p_headers_presync.cpp), which simply renames the local variable. The assertion Assert(d >= 0s) is a test-only sanity check for monotonic mock time.
Changed components
src/test/util/time.hsrc/test/fuzz/p2p_headers_presync.cppInspect captured patch +21 / −7
diff --git a/src/test/fuzz/p2p_headers_presync.cpp b/src/test/fuzz/p2p_headers_presync.cpp
index d5871374..e9de1850 100644
--- a/src/test/fuzz/p2p_headers_presync.cpp
+++ b/src/test/fuzz/p2p_headers_presync.cpp
@@ -172,7 +172,7 @@ FUZZ_TARGET(p2p_headers_presync, .init = initialize)
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
// The steady clock is currently only used for logging, so a constant
// time-point seems acceptable for now.
- ElapseSteady elapse_steady{};
+ SteadyClockContext steady_ctx{};
ChainstateManager& chainman = *g_testing_setup->m_node.chainman;
CBlockHeader base{chainman.GetParams().GenesisBlock()};
diff --git a/src/test/util/time.h b/src/test/util/time.h
index 4e5d760b..bcb97eb4 100644
--- a/src/test/util/time.h
+++ b/src/test/util/time.h
@@ -5,16 +5,30 @@
#ifndef BITCOIN_TEST_UTIL_TIME_H
#define BITCOIN_TEST_UTIL_TIME_H
+#include <util/check.h>
#include <util/time.h>
-struct ElapseSteady {
+
+/// Helper to initialize the global MockableSteadyClock, let a duration elapse,
+/// and reset it after use in a test.
+class SteadyClockContext
+{
MockableSteadyClock::mock_time_point::duration t{MockableSteadyClock::INITIAL_MOCK_TIME};
- ElapseSteady()
- {
- (*this)(0s); // init
- }
- void operator()(std::chrono::milliseconds d)
+
+public:
+ /** Initialize with INITIAL_MOCK_TIME. */
+ explicit SteadyClockContext() { (*this) += 0s; }
+
+ /** Unset mocktime */
+ ~SteadyClockContext() { MockableSteadyClock::ClearMockTime(); }
+
+ SteadyClockContext(const SteadyClockContext&) = delete;
+ SteadyClockContext& operator=(const SteadyClockContext&) = delete;
+
+ /** Change mocktime by the given duration delta */
+ void operator+=(std::chrono::milliseconds d)
{
+ Assert(d >= 0s); // Steady time can only increase monotonically.
t += d;
MockableSteadyClock::SetMockTime(t);
}
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.