From faf87c3535c51ab2d5c64ed9660d7b3846975469 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 24 Aug 2026 12:30:34 +0200 Subject: [PATCH 1/2] test: refactor: Use FakeNodeClock over manual/global SetMockTime Using SetMockTime in tests is problematic, because it often requires verbose calls to `SetMockTime(GetTime() + offset)`. Also, it requires manual `SetMockTime(0);` at the end. Fix both issues by using FakeNodeClock. --- src/test/httpserver_tests.cpp | 2 +- src/test/mempool_fee_estimator_tests.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/httpserver_tests.cpp b/src/test/httpserver_tests.cpp index 96d6c387676..9262a940c25 100644 --- a/src/test/httpserver_tests.cpp +++ b/src/test/httpserver_tests.cpp @@ -805,7 +805,7 @@ BOOST_AUTO_TEST_CASE(http_server_socket_tests) { // Hard code the timestamp for the Date header in the HTTP response // Wed Dec 11 00:47:09 2024 UTC - SetMockTime(1733878029); + FakeNodeClock clock{1733878029s}; // Prepare a request handler that just stores received requests so we can examine them. // Mutex is required to prevent a race between this test's main thread and the server's I/O loop. diff --git a/src/test/mempool_fee_estimator_tests.cpp b/src/test/mempool_fee_estimator_tests.cpp index 23b84b0b488..c75c7c7237b 100644 --- a/src/test/mempool_fee_estimator_tests.cpp +++ b/src/test/mempool_fee_estimator_tests.cpp @@ -105,6 +105,7 @@ BOOST_AUTO_TEST_CASE(calculate_max_weight_percentiles) BOOST_AUTO_TEST_CASE(mempool_fee_rate_estimator_cache) { + FakeNodeClock clock{}; MemPoolFeeRateEstimatorCache cache; const uint256 tip_hash{uint256::ONE}; const uint256 next_tip_hash{uint256{2}}; @@ -122,10 +123,9 @@ BOOST_AUTO_TEST_CASE(mempool_fee_rate_estimator_cache) BOOST_CHECK(cached->m_economical == economical); BOOST_CHECK(!cache.GetCachedEstimate(next_tip_hash)); - SetMockTime(GetTime() + CACHE_LIFE + std::chrono::seconds{1}); + clock += CACHE_LIFE + std::chrono::seconds{1}; BOOST_CHECK(cache.IsStale()); BOOST_CHECK(!cache.GetCachedEstimate(tip_hash)); - SetMockTime(0); } BOOST_AUTO_TEST_CASE(MempoolFeeRateEstimator) From fad1e6bf238ef4693464faf4102934c5b2dc7cb2 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 24 Aug 2026 12:36:04 +0200 Subject: [PATCH 2/2] util: refactor: Remove deprecated SetMockTime(i64) alias The deprecated test-only alias is only used in three places and required in none. So remove it. --- src/rpc/node.cpp | 2 +- src/test/fuzz/fuzz.cpp | 2 +- src/util/time.cpp | 1 - src/util/time.h | 8 -------- src/wallet/interfaces.cpp | 2 +- 5 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index 6d0d5511e33..84ffda05fdc 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -69,7 +69,7 @@ static RPCMethod setmocktime() throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime must be in the range [0, %s], not %s.", max_time, time)); } - SetMockTime(time); + SetMockTime(std::chrono::seconds{time}); const NodeContext& node_context{EnsureAnyNodeContext(request.context)}; for (const auto& chain_client : node_context.chain_clients) { chain_client->setMockTime(time); diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index 8c1def4338a..10e08ce0f6d 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -100,7 +100,7 @@ static void initialize() SeedRandomStateForTest(SeedRand::ZEROS); // Set time to the genesis block timestamp for deterministic initialization. - SetMockTime(1231006505); + SetMockTime(1231006505s); // Terminate immediately if a fuzzing harness ever tries to create a socket. // Individual tests can override this by pointing CreateSock to a mocked alternative. diff --git a/src/util/time.cpp b/src/util/time.cpp index 974eb051062..c0c621d14bd 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -49,7 +49,6 @@ NodeClock::time_point NodeClock::now() noexcept return time_point{ret}; }; -void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); } void SetMockTime(std::chrono::time_point mock) { SetMockTime(mock.time_since_epoch()); } void SetMockTime(std::chrono::seconds mock_time_in) { diff --git a/src/util/time.h b/src/util/time.h index 7df7c39a355..df025299f4f 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -112,14 +112,6 @@ using MillisecondsDouble = std::chrono::duration mock); diff --git a/src/wallet/interfaces.cpp b/src/wallet/interfaces.cpp index 0be97c460b2..812e9300af6 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -562,7 +562,7 @@ public: return StartWallets(m_context); } void stop() override { return UnloadWallets(m_context); } - void setMockTime(int64_t time) override { return SetMockTime(time); } + void setMockTime(int64_t time) override { return SetMockTime(std::chrono::seconds{time}); } void schedulerMockForward(std::chrono::seconds delta) override { Assert(m_context.scheduler)->MockForward(delta); } //! WalletLoader methods