test: Use NodeClockContext in more call sites
What changed, and why it matters
This is a test-only code cleanup. It swaps old mock-time helpers for a newer test helper (NodeClockContext) in benchmarks and unit tests. The commit message explicitly says it does not change behavior, and the diff shows only mechanical replacements in test/bench files with no changes to production code.
No security action needed. This is a safe refactor of test code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors test and benchmark code to use NodeClockContext instead of SetMockTime/GetMockTime/GetTime in six files under src/bench and src/test. All changes are in test infrastructure; no consensus, networking, wallet, or RPC runtime logic is modified. The replacements are one-to-one (e.g., SetMockTime(GetTime() + 1) becomes clock_ctx += 1s).
Changed components
src/bench/index_blockfilter.cppsrc/bench/wallet_encrypt.cppsrc/test/addrman_tests.cppsrc/test/chainstate_write_tests.cppsrc/test/private_broadcast_tests.cppsrc/test/rpc_tests.cppInspect captured patch +21 / −17
diff --git a/src/bench/index_blockfilter.cpp b/src/bench/index_blockfilter.cpp
index 805ad050..a1e85b44 100644
--- a/src/bench/index_blockfilter.cpp
+++ b/src/bench/index_blockfilter.cpp
@@ -16,6 +16,7 @@
#include <span.h>
#include <sync.h>
#include <test/util/setup_common.h>
+#include <test/util/time.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <util/time.h>
@@ -37,9 +38,10 @@ static void BlockFilterIndexSync(benchmark::Bench& bench)
CPubKey pubkey{"02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9"_hex_u8};
CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
std::vector<CMutableTransaction> noTxns;
+ NodeClockContext clock_ctx{};
for (int i = 0; i < CHAIN_SIZE - 100; i++) {
test_setup->CreateAndProcessBlock(noTxns, script);
- SetMockTime(GetTime() + 1);
+ clock_ctx += 1s;
}
assert(WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveHeight() == CHAIN_SIZE));
diff --git a/src/bench/wallet_encrypt.cpp b/src/bench/wallet_encrypt.cpp
index 1f4db614..442e4bf5 100644
--- a/src/bench/wallet_encrypt.cpp
+++ b/src/bench/wallet_encrypt.cpp
@@ -8,6 +8,7 @@
#include <random.h>
#include <support/allocators/secure.h>
#include <test/util/setup_common.h>
+#include <test/util/time.h>
#include <util/time.h>
#include <wallet/context.h>
#include <wallet/test/util.h>
@@ -44,7 +45,7 @@ static void WalletEncrypt(benchmark::Bench& bench, unsigned int key_count)
// Setting a mock time is necessary to force default derive iteration count during
// wallet encryption.
- SetMockTime(1);
+ NodeClockContext clock_ctx{1s};
std::unique_ptr<WalletDatabase> database;
std::shared_ptr<CWallet> wallet;
diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
index cd1c651a..8ebafb0d 100644
--- a/src/test/addrman_tests.cpp
+++ b/src/test/addrman_tests.cpp
@@ -97,8 +97,7 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_AUTO_TEST_CASE(addrman_terrible_many_failures)
{
- auto now = Now<NodeSeconds>();
- SetMockTime(now - (ADDRMAN_MIN_FAIL + 24h));
+ NodeClockContext clock_ctx{};
auto addrman{std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node))};
@@ -109,7 +108,7 @@ BOOST_AUTO_TEST_CASE(addrman_terrible_many_failures)
BOOST_CHECK(addrman->Add({addr}, source));
BOOST_CHECK(addrman->Good(addr));
- SetMockTime(now);
+ clock_ctx += ADDRMAN_MIN_FAIL + 24h;
CAddress addr_helper{CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE)};
addr_helper.nTime = Now<NodeSeconds>();
@@ -132,7 +131,7 @@ BOOST_AUTO_TEST_CASE(addrman_terrible_many_failures)
BOOST_AUTO_TEST_CASE(addrman_penalty_self_announcement)
{
- SetMockTime(Now<NodeSeconds>());
+ NodeClockContext clock_ctx{};
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
const auto base_time{Now<NodeSeconds>() - 10000s};
diff --git a/src/test/chainstate_write_tests.cpp b/src/test/chainstate_write_tests.cpp
index d0925b61..b7f7afa7 100644
--- a/src/test/chainstate_write_tests.cpp
+++ b/src/test/chainstate_write_tests.cpp
@@ -70,6 +70,7 @@ BOOST_FIXTURE_TEST_CASE(write_during_multiblock_activation, TestChain100Setup)
auto& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
BlockValidationState state_dummy{};
+ NodeClockContext clock_ctx{};
// Pop two blocks from the tip
const CBlockIndex* tip{chainstate.m_chain.Tip()};
@@ -88,7 +89,7 @@ BOOST_FIXTURE_TEST_CASE(write_during_multiblock_activation, TestChain100Setup)
m_node.validation_signals->SyncWithValidationInterfaceQueue();
// The periodic flush interval is between 50 and 70 minutes (inclusive)
// The next call to a PERIODIC write will flush
- SetMockTime(GetMockTime() + DATABASE_WRITE_INTERVAL_MAX);
+ clock_ctx += DATABASE_WRITE_INTERVAL_MAX;
const auto sub{std::make_shared<TestSubscriber>()};
m_node.validation_signals->RegisterSharedValidationInterface(sub);
diff --git a/src/test/private_broadcast_tests.cpp b/src/test/private_broadcast_tests.cpp
index 048284b2..2e87dc80 100644
--- a/src/test/private_broadcast_tests.cpp
+++ b/src/test/private_broadcast_tests.cpp
@@ -5,6 +5,7 @@
#include <primitives/transaction.h>
#include <private_broadcast.h>
#include <test/util/setup_common.h>
+#include <test/util/time.h>
#include <util/time.h>
#include <algorithm>
@@ -26,7 +27,7 @@ static CTransactionRef MakeDummyTx(uint32_t id, size_t num_witness)
BOOST_AUTO_TEST_CASE(basic)
{
- SetMockTime(Now<NodeSeconds>());
+ NodeClockContext clock_ctx{};
PrivateBroadcast pb;
const NodeId recipient1{1};
@@ -94,7 +95,7 @@ BOOST_AUTO_TEST_CASE(basic)
BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
// 2. Fast-forward the mock clock past the INITIAL_STALE_DURATION.
- SetMockTime(Now<NodeSeconds>() + PrivateBroadcast::INITIAL_STALE_DURATION + 1min);
+ clock_ctx += PrivateBroadcast::INITIAL_STALE_DURATION + 1min;
// 3. Now that the initial duration has passed, both unconfirmed transactions should be stale.
BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
@@ -125,7 +126,7 @@ BOOST_AUTO_TEST_CASE(basic)
BOOST_CHECK_EQUAL(stale_state.size(), 1);
BOOST_CHECK_EQUAL(stale_state[0], tx_for_recipient2);
- SetMockTime(Now<NodeSeconds>() + 10h);
+ clock_ctx += 10h;
BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
@@ -141,7 +142,7 @@ BOOST_AUTO_TEST_CASE(basic)
BOOST_AUTO_TEST_CASE(stale_unpicked_tx)
{
- SetMockTime(Now<NodeSeconds>());
+ NodeClockContext clock_ctx{};
PrivateBroadcast pb;
const auto tx{MakeDummyTx(/*id=*/42, /*num_witness=*/0)};
@@ -149,9 +150,9 @@ BOOST_AUTO_TEST_CASE(stale_unpicked_tx)
// Unpicked transactions use the longer INITIAL_STALE_DURATION.
BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
- SetMockTime(Now<NodeSeconds>() + PrivateBroadcast::INITIAL_STALE_DURATION - 1min);
+ clock_ctx += PrivateBroadcast::INITIAL_STALE_DURATION - 1min;
BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
- SetMockTime(Now<NodeSeconds>() + 2min);
+ clock_ctx += 2min;
const auto stale_state{pb.GetStale()};
BOOST_REQUIRE_EQUAL(stale_state.size(), 1);
BOOST_CHECK_EQUAL(stale_state[0], tx);
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
index 1b6dbda0..e8a3d992 100644
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -11,6 +11,7 @@
#include <rpc/util.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
+#include <test/util/time.h>
#include <univalue.h>
#include <util/time.h>
@@ -341,10 +342,9 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));
- auto now = 10'000s;
- SetMockTime(now);
+ NodeClockContext clock_ctx{10'000s};
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("setban 127.0.0.0/24 add 200")));
- SetMockTime(now += 2s);
+ clock_ctx += 2s;
const int64_t time_remaining_expected{198};
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("listbanned")));
ar = r.get_array();
@@ -355,7 +355,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
const int64_t ban_duration{o1.find_value("ban_duration").getInt<int64_t>()};
const int64_t time_remaining{o1.find_value("time_remaining").getInt<int64_t>()};
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
- BOOST_CHECK_EQUAL(banned_until, time_remaining_expected + now.count());
+ BOOST_CHECK_EQUAL(banned_until, time_remaining_expected + TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()));
BOOST_CHECK_EQUAL(ban_duration, banned_until - ban_created);
BOOST_CHECK_EQUAL(time_remaining, time_remaining_expected);
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.