Merge bitcoin/bitcoin#32927: fuzz: Add missing calls to SetMockTime for determinism

fa8862723c fuzz: CheckGlobals in init (MarcoFalke)
fa26bfde98 test: Avoid resetting mocktime in testing setup (MarcoFalke)
fa6b45fa8e Add SetMockTime for time_point types (MarcoFalke)

Pull request description:

  (Tracking issue https://github.com/bitcoin/bitcoin/issues/29018)

  During fuzzing, `AppInitParameterInteraction` may actually disable a previously set mocktime. This is confusing and can also cause non-determinism.

  Fix this issue, by

  * fixing the erroneous `-mocktime` parsing in `AppInitParameterInteraction`.
  * adding the missing `SetMockTime` calls to the affected fuzz init functions.
  * adding a `CheckGlobals` to the fuzz init, to prevent this issue in the future.

  This can be tested by

  * Cherry-picking the `CheckGlobals`-commit onto current master and observing a fuzz failure in the touched fuzz targets.
  * Reverting the touched fuzz fixups and observing a fuzz failure for each target.

ACKs for top commit:
  w0xlt:
    ACK fa8862723c
  dergoegge:
    utACK fa8862723c

Tree-SHA512: 5a9400f0467c82fa224713af4cc2b525afbefefc7c3f419077110925ad7af6c7fda3dcd2b50f7facf0ee7df2547c6ac20336906d707adcdfd1d652a9d9a735fe
This commit is contained in:
merge-script
2025-07-11 11:18:03 +01:00
10 changed files with 12 additions and 3 deletions

View File

@@ -94,6 +94,7 @@ const std::function<std::string()> G_TEST_GET_FULL_NAME{[]{
static void initialize()
{
CheckGlobals check{};
// By default, make the RNG deterministic with a fixed seed. This will affect all
// randomness during the fuzz test, except:
// - GetStrongRandBytes(), which is used for the creation of private key material.

View File

@@ -42,6 +42,7 @@ void initialize_tx_pool()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
g_setup = testing_setup.get();
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
BlockAssembler::Options options;
options.coinbase_output_script = P2WSH_EMPTY;

View File

@@ -44,6 +44,7 @@ void initialize_process_message()
/*chain_type=*/ChainType::REGTEST,
{.extra_args = {"-txreconciliation"}});
g_setup = testing_setup.get();
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
MineBlock(g_setup->m_node, {});
}

View File

@@ -34,6 +34,7 @@ void initialize_process_messages()
/*chain_type=*/ChainType::REGTEST,
{.extra_args = {"-txreconciliation"}});
g_setup = testing_setup.get();
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
MineBlock(g_setup->m_node, {});
}

View File

@@ -44,6 +44,7 @@ void initialize_tx_pool()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
g_setup = testing_setup.get();
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
BlockAssembler::Options options;
options.coinbase_output_script = P2WSH_OP_TRUE;

View File

@@ -72,6 +72,7 @@ void initialize_chain()
const auto params{CreateChainParams(ArgsManager{}, ChainType::REGTEST)};
static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
g_chain = &chain;
SetMockTime(chain.back()->Time());
// Make sure we can generate a valid snapshot.
sanity_check_snapshot();

View File

@@ -24,14 +24,13 @@ FUZZ_TARGET(utxo_total_supply)
{
SeedRandomStateForTest(SeedRand::ZEROS);
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const auto mock_time{ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)}; // regtest genesis block timestamp
SetMockTime(ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)); // regtest genesis block timestamp
/** The testing setup that creates a chainman only (no chainstate) */
ChainTestingSetup test_setup{
ChainType::REGTEST,
{
.extra_args = {
"-testactivationheight=bip34@2",
strprintf("-mocktime=%d", mock_time).c_str()
},
},
};