fuzz: Add and use NodeClockContext #34479

pull maflcko wants to merge 3 commits into bitcoin:master from maflcko:2602-fuzz-clock-ctx changing 28 files +101 −48
  1. maflcko commented at 12:36 pm on February 2, 2026: member

    Iterating over fuzz inputs will usually be done in the same process. As the mocktime is global, it can theoretically leak from one fuzz input run into the next run, making it less deterministic.

    Fix this issue, by adding and using a context manager to handle the mocktime and reset it before the end.

    This refactor should not change any behavior.

  2. DrahtBot renamed this:
    fuzz: Add and use NodeClockContext
    fuzz: Add and use NodeClockContext
    on Feb 2, 2026
  3. DrahtBot added the label Fuzzing on Feb 2, 2026
  4. DrahtBot commented at 12:36 pm on February 2, 2026: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK seduless, dergoegge, brunoerg

    If your review is incorrectly listed, please copy-paste <!–meta-tag:bot-skip–> into the comment that the bot should ignore.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #33421 (node: add BlockTemplateCache by ismaelsadeeq)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  5. brunoerg commented at 11:52 pm on February 3, 2026: contributor
    Wouldn’t it need to change the warning (replacing SetMockTime to NodeClockContext) in CheckGlobalsImpl for when the target accesses system time?
  6. maflcko force-pushed on Feb 9, 2026
  7. maflcko force-pushed on Feb 9, 2026
  8. maflcko force-pushed on Feb 9, 2026
  9. maflcko force-pushed on Feb 9, 2026
  10. maflcko force-pushed on Feb 9, 2026
  11. seduless commented at 0:32 am on March 8, 2026: contributor

    Approach ACK 666643be8fcdbf587f8fd18edea064ee3c547936

    grep -rn 'SetMockTime' src/test/fuzz/ src/wallet/test/fuzz/ shows several remaining calls. Of those, these two follow the same ConsumeTime pattern and could be included:

    • src/test/fuzz/block_index_tree.cpp:44
    • src/wallet/test/fuzz/fees.cpp:65

    Built with libfuzzer, verified both targets pass with this diff applied:

     0bitcoin pr-34479 !❄ ❯ git diff -U1
     1diff --git a/src/test/fuzz/block_index_tree.cpp b/src/test/fuzz/block_index_tree.cpp
     2index a4bbe95840..bff8eafa92 100644
     3--- a/src/test/fuzz/block_index_tree.cpp
     4+++ b/src/test/fuzz/block_index_tree.cpp
     5@@ -14,2 +14,3 @@
     6 #include <test/util/setup_common.h>
     7+#include <test/util/time.h>
     8 #include <test/util/validation.h>
     9@@ -43,3 +44,3 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
    10     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
    11-    SetMockTime(ConsumeTime(fuzzed_data_provider));
    12+    NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
    13     auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
    14diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp
    15index 6d9de0cc0f..03a5e2abc9 100644
    16--- a/src/wallet/test/fuzz/fees.cpp
    17+++ b/src/wallet/test/fuzz/fees.cpp
    18@@ -8,2 +8,3 @@
    19 #include <test/util/setup_common.h>
    20+#include <test/util/time.h>
    21 #include <test/util/txmempool.h>
    22@@ -64,3 +65,3 @@ FUZZ_TARGET(wallet_fees, .init = initialize_setup)
    23     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
    24-    SetMockTime(ConsumeTime(fuzzed_data_provider));
    25+    NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider)};
    26     auto& node{g_setup->m_node};
    
  12. test: Add NodeClockContext
    This makes it easier to use mock-time in tests. Also, it resets the
    global mocktime, so that no state is leaked between test cases.
    fa4fae6227
  13. fuzz: Use NodeClockContext
    This refactor does not change any behavior.
    
    However, it is nice to know that no global mocktime leaks from the fuzz
    init step to the first fuzz input, or from one fuzz input execution to
    the next.
    With the clock context, the global is re-set at the end of the context.
    eeeeb2a0b9
  14. maflcko force-pushed on Mar 10, 2026
  15. maflcko commented at 10:06 am on March 10, 2026: member
    Thx, I pushed your diff and rebased. Should be easy to re-review via git range-diff bitcoin-core/master 666643be8f fa1f69d02d
  16. in src/util/time.h:36 in fa1f69d02d
    31@@ -31,10 +32,9 @@ using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, st
    32 
    33 using SystemClock = std::chrono::system_clock;
    34 
    35-/**
    36- * Version of SteadyClock that is mockable in the context of tests (set the
    37- * current value with SetMockTime), otherwise the system steady clock.
    38- */
    39+/// Version of SteadyClock that is mockable in the context of tests (via
    40+/// SteadyClockContext, or self.SetMockTime), otherwise the system steady
    


    seduless commented at 6:44 pm on March 11, 2026:
    nit: The LLM linter caught self.SetMockTime in the doc comment, however the suggested fix seems off since it shouldn’t use the global ::SetMockTime. Not blocking, but if you need another push you can update to MockableSteadyClock::SetMockTime.

    maflcko commented at 5:14 pm on March 12, 2026:
    I didn’t want to repeat the class name, but I see it is static, so self. makes less sense than Self::, so I switched to that for now.
  17. seduless commented at 6:50 pm on March 11, 2026: contributor

    ACK fa1f69d02d80aedabc94a4de33091deb511440a0

    • Verified via git range-diff bitcoin-core/master 666643be8f fa1f69d02d that the only changes from the prior version are the two additional targets (block_index_tree.cpp, fees.cpp) and rebase context
    • Re: #34479#pullrequestreview-3748248600, CheckGlobalsImpl is handled and the warning message now references NodeClockContext and SteadyClockContext
    • Built with libfuzzer, verified all impacted fuzz targets pass
  18. test: Fixup docs for NodeClockContext and SteadyClockContext faea12ecd9
  19. maflcko force-pushed on Mar 12, 2026
  20. seduless commented at 11:25 pm on March 13, 2026: contributor
    re-ACK faea12ecd9fe026876a3278782a9320934214905
  21. dergoegge approved
  22. dergoegge commented at 2:11 pm on March 18, 2026: member
    utACK faea12ecd9fe026876a3278782a9320934214905
  23. brunoerg approved
  24. brunoerg commented at 2:16 pm on March 18, 2026: contributor
    code review ACK faea12ecd9fe026876a3278782a9320934214905
  25. sedited merged this on Mar 18, 2026
  26. sedited closed this on Mar 18, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-03-23 09:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me