test: Add FakeNodeClock m_clock to TestChain100Setup
What changed, and why it matters
This commit is a test-only cleanup. It replaces scattered mock-time calls with a single shared fake clock in the test harness, making test behavior clearer and removing a misleading comment. It does not change production Bitcoin Core code or fix any security bug.
No security action needed. Treat as normal code-quality/test-maintenance change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds a FakeNodeClock m_clock member to TestChain100Setup and updates test/benchmark files to use it instead of local FakeNodeClock variables or direct SetMockTime calls. The previous SetMockTime(1598887952) in the constructor is replaced by initializing m_clock with the same timestamp. This is purely a refactor of test infrastructure; no consensus, networking, wallet, or validation logic is modified.
Changed components
src/test/util/setup_common.hsrc/test/util/setup_common.cppsrc/test/blockfilter_index_tests.cppsrc/test/chainstate_write_tests.cppsrc/bench/index_blockfilter.cppInspect captured patch +11 / −14
diff --git a/src/bench/index_blockfilter.cpp b/src/bench/index_blockfilter.cpp
index 781aa097..d813f1d3 100644
--- a/src/bench/index_blockfilter.cpp
+++ b/src/bench/index_blockfilter.cpp
@@ -38,10 +38,9 @@ static void BlockFilterIndexSync(benchmark::Bench& bench)
CPubKey pubkey{"02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9"_hex_u8};
CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
std::vector<CMutableTransaction> noTxns;
- FakeNodeClock clock{};
for (int i = 0; i < CHAIN_SIZE - 100; i++) {
test_setup->CreateAndProcessBlock(noTxns, script);
- clock += 1s;
+ test_setup->m_clock += 1s;
}
assert(WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveHeight() == CHAIN_SIZE));
diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp
index e9d9a409..a48a355d 100644
--- a/src/test/blockfilter_index_tests.cpp
+++ b/src/test/blockfilter_index_tests.cpp
@@ -21,11 +21,11 @@
#include <test/util/blockfilter.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
+#include <test/util/time.h>
#include <tinyformat.h>
#include <uint256.h>
#include <util/check.h>
#include <util/fs.h>
-#include <util/time.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
@@ -332,14 +332,15 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
class IndexReorgCrash : public BaseIndex
{
private:
+ FakeNodeClock& m_clock;
std::unique_ptr<BaseIndex::DB> m_db;
std::shared_future<void> m_blocker;
int m_blocking_height;
public:
explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker,
- int blocking_height) : BaseIndex(std::move(chain), "test index"), m_blocker(blocker),
- m_blocking_height(blocking_height)
+ int blocking_height, FakeNodeClock& clock)
+ : BaseIndex(std::move(chain), "test index"), m_clock(clock), m_blocker(blocker), m_blocking_height(blocking_height)
{
const fs::path path = gArgs.GetDataDirNet() / "index";
fs::create_directories(path);
@@ -356,7 +357,7 @@ public:
// Move mock time forward so the best index gets updated only when we are not at the blocking height
if (block.height == m_blocking_height - 1 || block.height > m_blocking_height) {
- SetMockTime(GetTime<std::chrono::seconds>() + 31s);
+ m_clock += 31s;
}
return true;
@@ -365,14 +366,11 @@ public:
BOOST_FIXTURE_TEST_CASE(index_reorg_crash, BuildChainTestingSetup)
{
- // Enable mock time
- SetMockTime(GetTime<std::chrono::minutes>());
-
std::promise<void> promise;
std::shared_future<void> blocker(promise.get_future());
int blocking_height = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight);
- IndexReorgCrash index(interfaces::MakeChain(m_node), blocker, blocking_height);
+ IndexReorgCrash index{interfaces::MakeChain(m_node), blocker, blocking_height, m_clock};
BOOST_REQUIRE(index.Init());
BOOST_REQUIRE(index.StartBackgroundSync());
diff --git a/src/test/chainstate_write_tests.cpp b/src/test/chainstate_write_tests.cpp
index 64a3fc1b..01e00011 100644
--- a/src/test/chainstate_write_tests.cpp
+++ b/src/test/chainstate_write_tests.cpp
@@ -70,7 +70,6 @@ BOOST_FIXTURE_TEST_CASE(write_during_multiblock_activation, TestChain100Setup)
auto& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
BlockValidationState state_dummy{};
- FakeNodeClock clock{};
// Pop two blocks from the tip
const CBlockIndex* tip{chainstate.m_chain.Tip()};
@@ -89,7 +88,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
- clock += DATABASE_WRITE_INTERVAL_MAX;
+ m_clock += DATABASE_WRITE_INTERVAL_MAX;
const auto sub{std::make_shared<TestSubscriber>()};
m_node.validation_signals->RegisterSharedValidationInterface(sub);
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index d7b7b29d..8097494d 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -416,7 +416,6 @@ TestChain100Setup::TestChain100Setup(
TestOpts opts)
: TestingSetup{ChainType::REGTEST, opts}
{
- SetMockTime(1598887952);
constexpr std::array<unsigned char, 32> vchKey = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}};
coinbaseKey.Set(vchKey.begin(), vchKey.end(), true);
@@ -438,7 +437,7 @@ void TestChain100Setup::mineBlocks(int num_blocks)
for (int i = 0; i < num_blocks; i++) {
std::vector<CMutableTransaction> noTxns;
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
- SetMockTime(GetTime() + 1);
+ m_clock += 1s;
m_coinbase_txns.push_back(b.vtx[0]);
}
}
diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h
index 62bfaba9..72a3a620 100644
--- a/src/test/util/setup_common.h
+++ b/src/test/util/setup_common.h
@@ -14,6 +14,7 @@
#include <primitives/transaction.h>
#include <random.h>
#include <test/util/random.h>
+#include <test/util/time.h>
#include <util/chaintype.h> // IWYU pragma: export
#include <util/fs.h>
#include <util/signalinterrupt.h>
@@ -226,6 +227,7 @@ struct TestChain100Setup : public TestingSetup {
*/
std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
+ FakeNodeClock m_clock{std::chrono::seconds{1598887952}};
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
};
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.