From fae9623c8db703f8277755b606420400a6cb0d3c Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 9 Jun 2026 19:07:56 +0200 Subject: [PATCH] test: Add FakeNodeClock m_clock to TestChain100Setup Currently, all test cases using TestChain100Setup or a derived class like BuildChainTestingSetup are using mocktime by default due to the SetMockTime call in the TestChain100Setup ctor. This is confusing, because test cases using mocktime explicitly seem to imply that before they set the mocktime, real time was used. E.g. index_reorg_crash claimed in a comment to "Enable mock time". Fix this issue by adding a FakeNodeClock m_clock field to TestChain100Setup. Then, use the m_clock instead of explicit calls to SetMockTime or to a (now) shadowing local FakeNodeClock variable. --- src/bench/index_blockfilter.cpp | 3 +-- src/test/blockfilter_index_tests.cpp | 14 ++++++-------- src/test/chainstate_write_tests.cpp | 3 +-- src/test/util/setup_common.cpp | 3 +-- src/test/util/setup_common.h | 2 ++ 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/bench/index_blockfilter.cpp b/src/bench/index_blockfilter.cpp index 781aa097801..d813f1d30d5 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 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 e9d9a409477..a48a355d453 100644 --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -21,11 +21,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -332,14 +332,15 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup) class IndexReorgCrash : public BaseIndex { private: + FakeNodeClock& m_clock; std::unique_ptr m_db; std::shared_future m_blocker; int m_blocking_height; public: explicit IndexReorgCrash(std::unique_ptr chain, std::shared_future 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() + 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::promise promise; std::shared_future 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 64a3fc1b8f7..01e000115d7 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()}; m_node.validation_signals->RegisterSharedValidationInterface(sub); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index d7b7b29d584..8097494dfa3 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 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 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 62bfaba9a97..72a3a620d5a 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -14,6 +14,7 @@ #include #include #include +#include #include // IWYU pragma: export #include #include @@ -226,6 +227,7 @@ struct TestChain100Setup : public TestingSetup { */ std::vector PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit); + FakeNodeClock m_clock{std::chrono::seconds{1598887952}}; std::vector m_coinbase_txns; // For convenience, coinbase transactions CKey coinbaseKey; // private/public key needed to spend coinbase transactions };