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.
This commit is contained in:
MarcoFalke
2026-06-09 19:07:56 +02:00
parent bcbf5bae16
commit fae9623c8d
5 changed files with 11 additions and 14 deletions

View File

@@ -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));

View File

@@ -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());

View File

@@ -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);

View File

@@ -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]);
}
}

View File

@@ -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
};