Skip to content

Commit 12fb00f

Browse files
committed
Merge bitcoin/bitcoin#32927: fuzz: Add missing calls to SetMockTime for determinism
fa88627 fuzz: CheckGlobals in init (MarcoFalke) fa26bfd test: Avoid resetting mocktime in testing setup (MarcoFalke) fa6b45f Add SetMockTime for time_point types (MarcoFalke) Pull request description: (Tracking issue bitcoin/bitcoin#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 bitcoin/bitcoin@fa88627 dergoegge: utACK fa88627 Tree-SHA512: 5a9400f0467c82fa224713af4cc2b525afbefefc7c3f419077110925ad7af6c7fda3dcd2b50f7facf0ee7df2547c6ac20336906d707adcdfd1d652a9d9a735fe
2 parents 3c14186 + fa88627 commit 12fb00f

File tree

10 files changed

+12
-3
lines changed

10 files changed

+12
-3
lines changed

src/init.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,9 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10521052
if (!g_wallet_init_interface.ParameterInteraction()) return false;
10531053

10541054
// Option to startup with mocktime set (used for regression testing):
1055-
SetMockTime(args.GetIntArg("-mocktime", 0)); // SetMockTime(0) is a no-op
1055+
if (const auto mocktime{args.GetIntArg("-mocktime")}) {
1056+
SetMockTime(std::chrono::seconds{*mocktime});
1057+
}
10561058

10571059
if (args.GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
10581060
g_local_services = ServiceFlags(g_local_services | NODE_BLOOM);

src/test/fuzz/fuzz.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const std::function<std::string()> G_TEST_GET_FULL_NAME{[]{
9494

9595
static void initialize()
9696
{
97+
CheckGlobals check{};
9798
// By default, make the RNG deterministic with a fixed seed. This will affect all
9899
// randomness during the fuzz test, except:
99100
// - GetStrongRandBytes(), which is used for the creation of private key material.

src/test/fuzz/package_eval.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void initialize_tx_pool()
4242
{
4343
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
4444
g_setup = testing_setup.get();
45+
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
4546

4647
BlockAssembler::Options options;
4748
options.coinbase_output_script = P2WSH_EMPTY;

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void initialize_process_message()
4444
/*chain_type=*/ChainType::REGTEST,
4545
{.extra_args = {"-txreconciliation"}});
4646
g_setup = testing_setup.get();
47+
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
4748
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
4849
MineBlock(g_setup->m_node, {});
4950
}

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void initialize_process_messages()
3434
/*chain_type=*/ChainType::REGTEST,
3535
{.extra_args = {"-txreconciliation"}});
3636
g_setup = testing_setup.get();
37+
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
3738
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
3839
MineBlock(g_setup->m_node, {});
3940
}

src/test/fuzz/tx_pool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void initialize_tx_pool()
4444
{
4545
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
4646
g_setup = testing_setup.get();
47+
SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
4748

4849
BlockAssembler::Options options;
4950
options.coinbase_output_script = P2WSH_OP_TRUE;

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void initialize_chain()
7272
const auto params{CreateChainParams(ArgsManager{}, ChainType::REGTEST)};
7373
static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
7474
g_chain = &chain;
75+
SetMockTime(chain.back()->Time());
7576

7677
// Make sure we can generate a valid snapshot.
7778
sanity_check_snapshot();

src/test/fuzz/utxo_total_supply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ FUZZ_TARGET(utxo_total_supply)
2424
{
2525
SeedRandomStateForTest(SeedRand::ZEROS);
2626
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
27-
const auto mock_time{ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)}; // regtest genesis block timestamp
27+
SetMockTime(ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)); // regtest genesis block timestamp
2828
/** The testing setup that creates a chainman only (no chainstate) */
2929
ChainTestingSetup test_setup{
3030
ChainType::REGTEST,
3131
{
3232
.extra_args = {
3333
"-testactivationheight=bip34@2",
34-
strprintf("-mocktime=%d", mock_time).c_str()
3534
},
3635
},
3736
};

src/util/time.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ NodeClock::time_point NodeClock::now() noexcept
3838
};
3939

4040
void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
41+
void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); }
4142
void SetMockTime(std::chrono::seconds mock_time_in)
4243
{
4344
Assert(mock_time_in >= 0s);

src/util/time.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void SetMockTime(int64_t nMockTimeIn);
107107

108108
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
109109
void SetMockTime(std::chrono::seconds mock_time_in);
110+
void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock);
110111

111112
/** For testing */
112113
std::chrono::seconds GetMockTime();

0 commit comments

Comments
 (0)