kernel, validation: Add btck_chainstate_manager_set_clock_time #35557

Open
ryanofsky wants to merge 4 commits from pr/kclock into master AGit
ryanofsky commented 2026-06-18 02:52:12 +00:00 (Migrated from github.com)

Problem: Kernel applications can only override the time validation uses for the whole process, with btck_set_mock_time (#35496). Every chainstate manager in the process sees the same time, so a time set for one test, run, or thread leaks into the others unless the application remembers to reset it.

Solution: Add btck_chainstate_manager_options_set_clock_time and btck_chainstate_manager_set_clock_time, which set the time for a single chainstate manager, before it is created (so it also applies to checks made while it loads the chainstate) and afterwards. Validation code gets the time from a new ChainstateManager::Now() method instead of calling NodeClock::now() directly. Details are in the commit messages.

Keeping the time in the chainstate manager instead of in a global makes kernel applications more reliable, because state from one run or thread can't affect another.

Other commits:

Two related commits that were previously part of this PR are in a follow-up branch based on it, https://github.com/ryanofsky/bitcoin/commits/pr/kclock2: a mempool refactor to represent clock times consistently, and a change that makes NodeClock::now() unavailable in libbitcoinkernel so kernel code can't use the system clock by accident.

**Problem:** Kernel applications can only override the time validation uses for the whole process, with `btck_set_mock_time` (#35496). Every chainstate manager in the process sees the same time, so a time set for one test, run, or thread leaks into the others unless the application remembers to reset it. **Solution:** Add `btck_chainstate_manager_options_set_clock_time` and `btck_chainstate_manager_set_clock_time`, which set the time for a single chainstate manager, before it is created (so it also applies to checks made while it loads the chainstate) and afterwards. Validation code gets the time from a new `ChainstateManager::Now()` method instead of calling `NodeClock::now()` directly. Details are in the commit messages. Keeping the time in the chainstate manager instead of in a global makes kernel applications more reliable, because state from one run or thread can't affect another. Other commits: - The first commit fixes a rare timing race in the `chainstatemanager_ibd_exit_after_loading_blocks` test, where test code and validation code read the clock separately and assumed they got the same time. - The last two commits switch that test and the `utxo_total_supply` fuzz target to the new per-chainman clock, as suggested in https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4763429282. Two related commits that were previously part of this PR are in a follow-up branch based on it, https://github.com/ryanofsky/bitcoin/commits/pr/kclock2: a mempool refactor to represent clock times consistently, and a change that makes `NodeClock::now()` unavailable in libbitcoinkernel so kernel code can't use the system clock by accident.
DrahtBot commented 2026-06-18 02:52:20 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36856 (kernel: add manual pruning by pzafonte)
  • #36854 (kernel: let callers create the block manager by pzafonte)
  • #35906 (First steps towards a stateless, side-effect free validation library by purpleKarrot)
  • #35187 (kernel: Block validation without a complete UTXO set by sedited)
  • #33847 (kernel: Attach logging connections to contexts, per-connection log levels by ryanofsky)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35557 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35557.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35557#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36856](https://github.com/bitcoin/bitcoin/pull/36856) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte) * [#36854](https://github.com/bitcoin/bitcoin/pull/36854) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte) * [#35906](https://github.com/bitcoin/bitcoin/pull/35906) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35906.svg"></sub> (First steps towards a stateless, side-effect free validation library by purpleKarrot) * [#35187](https://github.com/bitcoin/bitcoin/pull/35187) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35187.svg"></sub> (kernel: Block validation without a complete UTXO set by sedited) * [#33847](https://github.com/bitcoin/bitcoin/pull/33847) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33847.svg"></sub> (kernel: Attach logging connections to contexts, per-connection log levels by ryanofsky) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
DrahtBot commented 2026-06-18 04:07:49 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/27733525041/job/82045427048
LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check reported changes needed (generated “Failure generated from IWYU”).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/27733525041/job/82045427048</sub> <sub>LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check reported changes needed (generated “Failure generated from IWYU”).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
maflcko (Migrated from github.com) reviewed 2026-06-18 05:19:23 +00:00
maflcko (Migrated from github.com) commented 2026-06-18 05:02:52 +00:00

9f67c9b269cbc0da459eafa1a5ba357e82f07212: You can use tip->Time(), to avoid the cast here.

Generally, it is better to use the chrono types and let the compiler do the type checks. After compilation, there won't be a difference anyway.

9f67c9b269cbc0da459eafa1a5ba357e82f07212: You can use `tip->Time()`, to avoid the cast here. Generally, it is better to use the chrono types and let the compiler do the type checks. After compilation, there won't be a difference anyway.
maflcko (Migrated from github.com) commented 2026-06-18 05:12:15 +00:00

Seems fine, but this may be promoting ugly code, such as count_seconds(DurationSinceEpoch<std::chrono::seconds>(...

I think generally it is better to use the correct std chrono type and let the compiler do the type checks. Casting a time point to a duration (since epoch) seems like a violation that should be avoided, for the same reasons that GetTime is deprecated.

At call sites you can just replace block->GetBlockTime() with block->Time() to avoid any casts, and this helper.

Seems fine, but this may be promoting ugly code, such as `count_seconds(DurationSinceEpoch<std::chrono::seconds>(...` I think generally it is better to use the correct std chrono type and let the compiler do the type checks. Casting a time point to a duration (since epoch) seems like a violation that should be avoided, for the same reasons that `GetTime` is deprecated. At call sites you can just replace block->GetBlockTime() with block->Time() to avoid any casts, and this helper.
maflcko (Migrated from github.com) commented 2026-06-18 05:17:38 +00:00

Do we really want the cast here? The answer is probably, "doesn't matter", because consensus time checks are seconds-based, and the double-precision rolling update mempool code doesn't care about sub-second precision. Though, I wanted to point it out, that this is intentional and not just something that was most likely to the LLM.

An alternative could be to remove the cast here and only add the cast in call sites that need it (implicilty "upgrading" the call-sites to sub-second precision)

Do we really want the cast here? The answer is probably, "doesn't matter", because consensus time checks are seconds-based, and the double-precision rolling update mempool code doesn't care about sub-second precision. Though, I wanted to point it out, that this is intentional and not just something that was most likely to the LLM. An alternative could be to remove the cast here and only add the cast in call sites that need it (implicilty "upgrading" the call-sites to sub-second precision)
ryanofsky commented 2026-06-18 09:52:49 +00:00 (Migrated from github.com)

Thanks @maflcko! I was very dissatisfied with all the casts in the second commit and your tips should be helpful for getting rid of them. I will also implement your suggestion to not cast away precision in ChainstateManager::Now(). (The LLM actually didn't write the cast, but called ::Now<NodeSeconds>() instead and I manually added the cast to try to be more explicit.)

Thanks @maflcko! I was very dissatisfied with all the casts in the second commit and your tips should be helpful for getting rid of them. I will also implement your suggestion to not cast away precision in ChainstateManager::Now(). (The LLM actually didn't write the cast, but called `::Now<NodeSeconds>()` instead and I manually added the cast to try to be more explicit.)
ryanofsky (Migrated from github.com) reviewed 2026-06-18 14:10:18 +00:00
ryanofsky (Migrated from github.com) left a comment

Updated 7d656ac341140dc80f73abcc45adc19f0430eaae -> 94622b041571f73e8e5e661b9bce4213b957a561 (pr/kclock.1 -> pr/kclock.2, compare) adding a new commit to use time points to represent times in mempool code

Updated 94622b041571f73e8e5e661b9bce4213b957a561 -> 27ea72916b71e40859d7d86348eb3d99c4211b34 (pr/kclock.2 -> pr/kclock.3, compare) to fix CI: compile error (duration_cast on time_point in TRACEPOINT calls) and nondeterministic test (clock race in chainstatemanager_ibd_exit_after_loading_blocks); also prepends a standalone test fix commit using only pre-existing SetMockTime API https://github.com/bitcoin/bitcoin/actions/runs/27764264509/job/82146558123

Updated 27ea72916b71e40859d7d86348eb3d99c4211b34 -> 8be500e6bec2e4e664f36dea22c6419237ba9ec2 (pr/kclock.3 -> pr/kclock.4, compare) to fix fuzz UBSan signed integer overflow converting seconds to nanoseconds in ConsumeTxMemPoolEntry https://github.com/bitcoin/bitcoin/actions/runs/15491561394/job/82175291056

Updated 8be500e6bec2e4e664f36dea22c6419237ba9ec2 -> 37fb1b596e2006ffb1af82945f81a27c2bc1777a (pr/kclock.4 -> pr/kclock.5, compare) to fix UBSan signed-integer-overflow: throw in LoadMempool when nTime is out of MempoolTime range; also use ConsumeTime() in ConsumeTxMemPoolEntry https://github.com/bitcoin/bitcoin/actions/runs/27776901912/job/82191293065

Updated 7d656ac341140dc80f73abcc45adc19f0430eaae -> 94622b041571f73e8e5e661b9bce4213b957a561 ([`pr/kclock.1`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.1) -> [`pr/kclock.2`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.2), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.1..pr/kclock.2))<!-- end --> adding a new commit to use time points to represent times in mempool code <!-- begin push-3 --> Updated 94622b041571f73e8e5e661b9bce4213b957a561 -> 27ea72916b71e40859d7d86348eb3d99c4211b34 ([`pr/kclock.2`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.2) -> [`pr/kclock.3`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.3), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.2..pr/kclock.3)) to fix CI: compile error (duration_cast on time_point in TRACEPOINT calls) and nondeterministic test (clock race in chainstatemanager_ibd_exit_after_loading_blocks); also prepends a standalone test fix commit using only pre-existing SetMockTime API https://github.com/bitcoin/bitcoin/actions/runs/27764264509/job/82146558123<!-- end --> <!-- begin push-4 --> Updated 27ea72916b71e40859d7d86348eb3d99c4211b34 -> 8be500e6bec2e4e664f36dea22c6419237ba9ec2 ([`pr/kclock.3`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.3) -> [`pr/kclock.4`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.4), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.3..pr/kclock.4)) to fix fuzz UBSan signed integer overflow converting seconds to nanoseconds in ConsumeTxMemPoolEntry https://github.com/bitcoin/bitcoin/actions/runs/15491561394/job/82175291056<!-- end --> <!-- begin push-5 --> Updated 8be500e6bec2e4e664f36dea22c6419237ba9ec2 -> 37fb1b596e2006ffb1af82945f81a27c2bc1777a ([`pr/kclock.4`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.4) -> [`pr/kclock.5`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.5), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.4..pr/kclock.5)) to fix UBSan signed-integer-overflow: throw in LoadMempool when nTime is out of MempoolTime range; also use ConsumeTime() in ConsumeTxMemPoolEntry https://github.com/bitcoin/bitcoin/actions/runs/27776901912/job/82191293065<!-- end -->
ryanofsky (Migrated from github.com) commented 2026-06-18 13:55:34 +00:00

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3433180374

9f67c9b: You can use tip->Time(), to avoid the cast here.

Thanks! That is so much better and I applied this many places

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3433180374 > [9f67c9b](https://github.com/bitcoin/bitcoin/commit/9f67c9b269cbc0da459eafa1a5ba357e82f07212): You can use `tip->Time()`, to avoid the cast here. Thanks! That is so much better and I applied this many places
ryanofsky (Migrated from github.com) commented 2026-06-18 13:56:27 +00:00
re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3433217273 > Seems fine, but this may be promoting ugly code You're right, dropped this now
ryanofsky (Migrated from github.com) commented 2026-06-18 14:03:55 +00:00

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3433238010

Do we really want the cast here?

I don't think so. It's definitely simpler not to have this and it is dropped now.

Though, I wanted to point it out, that this is intentional and not just something that was most likely to the LLM.

I guess it's fair to blame the LLM for this. Technically I added this cast, but the LLM originally wrote ::Now<NodeSeconds>() which was casting internally. Smarter to avoid it entirely.

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3433238010 > Do we really want the cast here? I don't think so. It's definitely simpler not to have this and it is dropped now. > Though, I wanted to point it out, that this is intentional and not just something that was most likely to the LLM. I guess it's fair to blame the LLM for this. Technically I added this cast, but the LLM originally wrote `::Now<NodeSeconds>()` which was casting internally. Smarter to avoid it entirely.
maflcko (Migrated from github.com) reviewed 2026-06-18 14:24:35 +00:00
maflcko (Migrated from github.com) commented 2026-06-18 14:24:36 +00:00

probably not a problem in reality, but the call to GetMockTime here looks wrong for several reasons:

  • It directly access the test-only global of the mock time, which may be 0, and thus may lead to silent bugs if mocktime was not used here?
  • It side-steps the g_used_system_time runtime sanitizer.
  • It returns the wrong type (it should be a time point, not a duration)

I guess it would be better to just call Now() or tx_pool.Now() to fix all issues and avoid the NodeSeconds{} cast.

probably not a problem in reality, but the call to `GetMockTime` here looks wrong for several reasons: * It directly access the test-only global of the mock time, which may be `0`, and thus may lead to silent bugs if mocktime was not used here? * It side-steps the `g_used_system_time` runtime sanitizer. * It returns the wrong type (it should be a time point, not a duration) I guess it would be better to just call `Now()` or `tx_pool.Now()` to fix all issues and avoid the NodeSeconds{} cast.
ryanofsky (Migrated from github.com) reviewed 2026-06-18 21:01:53 +00:00
ryanofsky (Migrated from github.com) left a comment

Marked ready for review now that CI is passing. Possible questions for reviewers:

  • Should this PR be split up? I am thinking of dropping everything except the main commit 8a93ea0f2904ec780a690d987a25e5fa113552c9 and moving other commits to a followup PR. But happy to do anything.
  • Is btck_chainstate_manager_set_clock_time(chainman, now) a good kernel API? I think it is, giving applications control over time in a pretty granular way that doesn't get in the way of adding more control later.
  • Are the changes to validtion code ok? There's a new ChainstateManager::m_clock_now_seconds field which adds new state, but not much changes otherwise and the changes make code more consistent.
Marked ready for review now that CI is passing. Possible questions for reviewers: - Should this PR be split up? I am thinking of dropping everything except the main commit 8a93ea0f2904ec780a690d987a25e5fa113552c9 and moving other commits to a followup PR. But happy to do anything. - Is `btck_chainstate_manager_set_clock_time(chainman, now)` a good kernel API? I think it is, giving applications control over time in a pretty granular way that doesn't get in the way of adding more control later. - Are the changes to validtion code ok? There's a new `ChainstateManager::m_clock_now_seconds` field which adds new state, but not much changes otherwise and the changes make code more consistent.
ryanofsky (Migrated from github.com) commented 2026-06-18 20:45:15 +00:00
re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3436545216 Makes sense, switched GetMockTime() -> Now()
sedited commented 2026-06-21 22:05:26 +00:00 (Migrated from github.com)

Should this PR be split up?

My preference is larger pull requests usually, so I would keep this in one piece, but I would go a bit further. Can we use this new clock in one of the tests (maybe one of the utxo* fuzz tests, haven't looked too closely yet).

Is btck_chainstate_manager_set_clock_time(chainman, now) a good kernel API?

If we set it in the options instead, we can fake the time once per instance, if we set it like here, it can be moved any number of times. I think I prefer that, as it seems to overlap more closely with the current global mechanics. The only reservation I have with this is that there might be a case where the clock needs to be set during chainman construction. I don't think that is the case right now, but that might change in the future.

Are the changes to validation code ok?

I would feel more confident if something (a test, a utility, etc.) was actually exercising the various instances where the chainman-scoped fake time is used now.

> Should this PR be split up? My preference is larger pull requests usually, so I would keep this in one piece, but I would go a bit further. Can we use this new clock in one of the tests (maybe one of the utxo* fuzz tests, haven't looked too closely yet). > Is btck_chainstate_manager_set_clock_time(chainman, now) a good kernel API? If we set it in the options instead, we can fake the time once per instance, if we set it like here, it can be moved any number of times. I think I prefer that, as it seems to overlap more closely with the current global mechanics. The only reservation I have with this is that there might be a case where the clock needs to be set during chainman construction. I don't think that is the case right now, but that might change in the future. > Are the changes to validation code ok? I would feel more confident if something (a test, a utility, etc.) was actually exercising the various instances where the chainman-scoped fake time is used now.
stickies-v commented 2026-06-23 14:44:41 +00:00 (Migrated from github.com)
  • Is btck_chainstate_manager_set_clock_time(chainman, now) a good kernel API? I think it is, giving applications control over time in a pretty granular way that doesn't get in the way of adding more control later.

This could get a bit annoying and potentially dangerous when users need to start managing clocks across multiple places. Perhaps a more ergonomic alternative could be to add a btck_Clock type which basically is just a callback that returns the current time? The callback can then be anything, e.g. a fixed value, the user's system time, a mocked clock, ...

That way, a single clock can be passed multiple times, and updated in a single place. Of course, it comes with its costs of increased API surface and lifetime concerns that a simple seconds setter doesn't have.

> * Is `btck_chainstate_manager_set_clock_time(chainman, now)` a good kernel API? I think it is, giving applications control over time in a pretty granular way that doesn't get in the way of adding more control later. This could get a bit annoying and potentially dangerous when users need to start managing clocks across multiple places. Perhaps a more ergonomic alternative could be to add a `btck_Clock` type which basically is just a callback that returns the current time? The callback can then be anything, e.g. a fixed value, the user's system time, a mocked clock, ... That way, a single clock can be passed multiple times, and updated in a single place. Of course, it comes with its costs of increased API surface and lifetime concerns that a simple seconds setter doesn't have.
seduless (Migrated from github.com) reviewed 2026-06-25 03:48:59 +00:00
seduless (Migrated from github.com) commented 2026-06-25 03:47:25 +00:00

In commit "test: Fix nondeterministic clock race in chainstatemanager_ibd_exit_after_loading_blocks" (f0fea79bdaabbea40f1177702ff2c169d82f62be)

Nice fix, thanks for cleaning this up.

Instead of bare SetMockTime calls, this is a good candidate for the RAII FakeNodeClock.

diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp
index b0176f641c..3c357844cd 100644
--- a/src/test/validation_chainstatemanager_tests.cpp
+++ b/src/test/validation_chainstatemanager_tests.cpp
@@ -18,2 +18,3 @@
 #include <test/util/setup_common.h>
+#include <test/util/time.h>
 #include <test/util/validation.h>
@@ -22,3 +23,2 @@
 #include <util/result.h>
-#include <util/time.h>
 #include <util/vector.h>
@@ -178,3 +178,3 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTe
     // causing a spurious failure in the tip_recent=true case.
-    SetMockTime(Now<NodeSeconds>());
+    FakeNodeClock clock{};

@@ -214,3 +214,2 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTe
     }
-    SetMockTime(0);
 }
In commit "test: Fix nondeterministic clock race in chainstatemanager_ibd_exit_after_loading_blocks" (f0fea79bdaabbea40f1177702ff2c169d82f62be) Nice fix, thanks for cleaning this up. Instead of bare `SetMockTime` calls, this is a good candidate for the RAII `FakeNodeClock`. ```diff diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index b0176f641c..3c357844cd 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -18,2 +18,3 @@ #include <test/util/setup_common.h> +#include <test/util/time.h> #include <test/util/validation.h> @@ -22,3 +23,2 @@ #include <util/result.h> -#include <util/time.h> #include <util/vector.h> @@ -178,3 +178,3 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTe // causing a spurious failure in the tip_recent=true case. - SetMockTime(Now<NodeSeconds>()); + FakeNodeClock clock{}; @@ -214,3 +214,2 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTe } - SetMockTime(0); } ```
DrahtBot commented 2026-06-29 01:08:25 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task Windows native, fuzz, VS: https://github.com/bitcoin/bitcoin/actions/runs/28340730799/job/83954616764
LLM reason (✨ experimental): CI failed because the fuzz test corpus runner hit an error in the utxo_total_supply target (fuzz.exe exit code 1 processing a specific input).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `Windows native, fuzz, VS`: https://github.com/bitcoin/bitcoin/actions/runs/28340730799/job/83954616764</sub> <sub>LLM reason (✨ experimental): CI failed because the fuzz test corpus runner hit an error in the `utxo_total_supply` target (fuzz.exe exit code 1 processing a specific input).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
ryanofsky (Migrated from github.com) reviewed 2026-06-29 01:30:16 +00:00
ryanofsky (Migrated from github.com) left a comment

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4763429282

I would keep this in one piece

Thanks, kept all commits for now and added new tests as suggested.

re: stickies-v https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4780411600

Perhaps a more ergonomic alternative could be to add a btck_Clock type which basically is just a callback that returns the current time?

This is an interesting idea. Implemented in 3d513c5d03ceb8d33dc8e19c07149e3337c64ac6 to see what it would look like.

The implementation barely even affects validation code so I think the main question is does it seem safer or easier to use for kernel applications?

My instinct is that it doesn't because if you're creating multiple chainstate managers or other objects that accept times, and your application needs artificial times instead of the current clock time, I wouldn't assume that multiple objects would want to have the same time set. Or if they did, I wouldn't assume that setting the time once on a shared clock object would be much easier than setting the time directly on the relevant objects.

But I also think this alternate API is clean, and would have no problem switching to this approach. It also seems like it wouldn't be hard to switch approaches later (with compatibly even).


Updated 37fb1b596e2006ffb1af82945f81a27c2bc1777a -> 1ae28a401368448cc6466aada72afe2101ae7d61 (pr/kclock.5 -> pr/kclock.6, compare) adding suggested tests

Updated 1ae28a401368448cc6466aada72afe2101ae7d61 -> 12a237f9d3570f96663dd84550f9fcc25ee53584 (pr/kclock.6 -> pr/kclock.7, compare) to fix utxo_total_supply fuzz g_used_system_time failure https://github.com/bitcoin/bitcoin/actions/runs/28340730799/job/83954616774

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4763429282 > I would keep this in one piece Thanks, kept all commits for now and added new tests as suggested. re: stickies-v https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4780411600 > Perhaps a more ergonomic alternative could be to add a btck_Clock type which basically is just a callback that returns the current time? This is an interesting idea. Implemented in 3d513c5d03ceb8d33dc8e19c07149e3337c64ac6 to see what it would look like. The implementation barely even affects validation code so I think the main question is does it seem safer or easier to use for kernel applications? My instinct is that it doesn't because if you're creating multiple chainstate managers or other objects that accept times, and your application needs artificial times instead of the current clock time, I wouldn't assume that multiple objects would want to have the same time set. Or if they did, I wouldn't assume that setting the time once on a shared clock object would be much easier than setting the time directly on the relevant objects. But I also think this alternate API is clean, and would have no problem switching to this approach. It also seems like it wouldn't be hard to switch approaches later (with compatibly even). --- <!-- begin push-6 --> Updated 37fb1b596e2006ffb1af82945f81a27c2bc1777a -> 1ae28a401368448cc6466aada72afe2101ae7d61 ([`pr/kclock.5`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.5) -> [`pr/kclock.6`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.6), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.5..pr/kclock.6))<!-- end --> adding suggested tests <!-- begin push-7 --> Updated 1ae28a401368448cc6466aada72afe2101ae7d61 -> 12a237f9d3570f96663dd84550f9fcc25ee53584 ([`pr/kclock.6`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.6) -> [`pr/kclock.7`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.7), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.6..pr/kclock.7))<!-- end --> to fix utxo_total_supply fuzz g_used_system_time failure https://github.com/bitcoin/bitcoin/actions/runs/28340730799/job/83954616774
ryanofsky (Migrated from github.com) commented 2026-06-29 00:11:39 +00:00

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3471733323

Instead of bare SetMockTime calls, this is a good candidate for the RAII FakeNodeClock.

Thanks! Applied your patch and switched to FakeNodeClock

re: https://github.com/bitcoin/bitcoin/pull/35557#discussion_r3471733323 > Instead of bare `SetMockTime` calls, this is a good candidate for the RAII `FakeNodeClock`. Thanks! Applied your patch and switched to FakeNodeClock
DrahtBot commented 2026-07-24 18:57:50 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
maflcko commented 2026-08-17 08:16:51 +00:00 (Migrated from github.com)

This is an alternative to #35496, and one of several alternatives discussed in that PR

That one was merged, so is this here still relevant?

Also, this conflicts with https://github.com/bitcoin/bitcoin/pull/35906#discussion_r3726539086

> This is an alternative to #35496, and one of several alternatives discussed in that PR That one was merged, so is this here still relevant? Also, this conflicts with https://github.com/bitcoin/bitcoin/pull/35906#discussion_r3726539086
DrahtBot commented 2026-08-21 08:25:38 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-08-31 16:54:00 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
First-time contributor

2 additional verified findings in the full report.

Earlier concerns remain unresolved; see the full report for their current assessments.

🟠 Minor · Clarify whether the clock-time override is fixed or advancing

validation.h:1027 Now() returns the stored timestamp unchanged until the caller resets it. The public set_clock_time doc only says it 'overrides the current time' and never says the value stays fixed, so a long-running caller must keep advancing it.

Location: src/validation.h:1027 (head).

🔵 Suggestion · Options-clock test does not protect load-time clock initialization

The test only observes header validation after construction, so installing the options clock after startup verification would still satisfy its assertions.

Location: src/test/kernel/test_kernel.cpp:1607 (head).

🔵 Suggestion · Clock documentation omits override precedence and maintenance timers

Zero falls back to the global mock when set, nonzero overrides that mock, and periodic flush scheduling remains outside the scoped clock despite the broader documentation.

Location: src/kernel/bitcoinkernel.h:1353 (head).

🔵 Suggestion · Clock tests do not check isolation between managers

The single-manager future-time assertions would also pass with shared clock storage, leaving the API's per-manager isolation unprotected.

Location: src/test/kernel/test_kernel.cpp:1571 (head).

🔵 Suggestion · Fuzz clock comment names an absent helper and omits template creation

The retained global mock covers direct NodeClock reads in both flushing and block-template creation, not the nonexistent _now_nondet() helper.

Location: src/test/fuzz/utxo_total_supply.cpp:39 (head).

🔵 Suggestion · Scoped-clock tests miss IBD, reset and callback-progress regressions

The IBD test does not reliably distinguish clock sources, and the kernel tests leave reset fallback and scoped callback progress unasserted.

Location: src/test/validation_chainstatemanager_tests.cpp:192 (head).

Review coverage was incomplete.


Reviewed: 2f6b902f980a · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:055d1cd96b176486294e0a3d052a7e506a00c1f0c7c35fbf579b09cfe6d615cd:237cc81563f53f26a84ba90acce49f0597b0e28b0a77aa425401db6e90431c0b --> <!-- ralph-base:5bf3bc5e263407a252c14a17afe04fc3bf24a889 --> <!-- ralph-head:2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad --> <!-- ralph-case:fea274ddbe2cba66c4c7b535b645cc101acc384d699cc7922a14881537599fac --> [2 additional verified findings in the full report](<https://review.fish.foo/traces/35557-2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad-2344-4.html>). Earlier concerns remain unresolved; see the full report for their current assessments. **🟠 Minor · Clarify whether the clock-time override is fixed or advancing** validation.h:1027 Now() returns the stored timestamp unchanged until the caller resets it. The public set_clock_time doc only says it 'overrides the current time' and never says the value stays fixed, so a long-running caller must keep advancing it. Location: [`src/validation.h:1027`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/validation.h#L1027>) (head). **🔵 Suggestion · Options-clock test does not protect load-time clock initialization** The test only observes header validation after construction, so installing the options clock after startup verification would still satisfy its assertions. Location: [`src/test/kernel/test_kernel.cpp:1607`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/test/kernel/test_kernel.cpp#L1607>) (head). **🔵 Suggestion · Clock documentation omits override precedence and maintenance timers** Zero falls back to the global mock when set, nonzero overrides that mock, and periodic flush scheduling remains outside the scoped clock despite the broader documentation. Location: [`src/kernel/bitcoinkernel.h:1353`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/kernel/bitcoinkernel.h#L1353>) (head). **🔵 Suggestion · Clock tests do not check isolation between managers** The single-manager future-time assertions would also pass with shared clock storage, leaving the API's per-manager isolation unprotected. Location: [`src/test/kernel/test_kernel.cpp:1571`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/test/kernel/test_kernel.cpp#L1571>) (head). **🔵 Suggestion · Fuzz clock comment names an absent helper and omits template creation** The retained global mock covers direct NodeClock reads in both flushing and block-template creation, not the nonexistent `_now_nondet()` helper. Location: [`src/test/fuzz/utxo_total_supply.cpp:39`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/test/fuzz/utxo_total_supply.cpp#L39>) (head). **🔵 Suggestion · Scoped-clock tests miss IBD, reset and callback-progress regressions** The IBD test does not reliably distinguish clock sources, and the kernel tests leave reset fallback and scoped callback progress unasserted. Location: [`src/test/validation_chainstatemanager_tests.cpp:192`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad/src/test/validation_chainstatemanager_tests.cpp#L192>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`2f6b902f980a`](<https://git.fish.foo/bitcoin/bitcoin/commit/2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad>) · [Full review report](<https://review.fish.foo/traces/35557-2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad-2344-4.html>) · [Previous reports](<https://review.fish.foo/traces/35557/>) [permalink](<https://review.fish.foo/traces/35557-2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad-2344-4.html>)
DrahtBot commented 2026-10-07 12:41:31 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
sedited commented 2026-10-26 12:04:33 +00:00 (Migrated from github.com)

That one was merged, so is this here still relevant?

I'm not sure. Some of the test changes seem fine, but not sure if this is worth the squeeze. Maybe @stringintech can chime in?

> That one was merged, so is this here still relevant? I'm not sure. Some of the test changes seem fine, but not sure if this is worth the squeeze. Maybe @stringintech can chime in?
ryanofsky (Migrated from github.com) reviewed 2026-11-07 00:28:42 +00:00
ryanofsky (Migrated from github.com) left a comment

Thanks for the reviews, and sorry for the slow update. I made this PR smaller and added a way to set the time before the chainstate manager is created, which answers sedited's construction-time question. Replies to each comment are inline.

Updated 0e777d8b30 -> 2f6b902f98 (pr/kclock.11 -> pr/kclock.12, compare) adding btck_chainstate_manager_options_set_clock_time(), using time point types for clock times, and moving the mempool refactor and the kernel now() check to https://github.com/ryanofsky/bitcoin/commits/pr/kclock2

Thanks for the reviews, and sorry for the slow update. I made this PR smaller and added a way to set the time before the chainstate manager is created, which answers sedited's construction-time question. Replies to each comment are inline. <!-- begin push-12 --> Updated 0e777d8b302d66360000ad55b6ac1232ac9b62ac -> 2f6b902f980af4f9dcf885c6fc4a5eb6a2abb1ad ([`pr/kclock.11`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.11) -> [`pr/kclock.12`](https://github.com/ryanofsky/bitcoin/commits/pr/kclock.12), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/kclock.11..pr/kclock.12))<!-- end --> adding `btck_chainstate_manager_options_set_clock_time()`, using time point types for clock times, and moving the mempool refactor and the kernel `now()` check to https://github.com/ryanofsky/bitcoin/commits/pr/kclock2
Lines 1370-1372
ryanofsky (Migrated from github.com) commented 2026-11-06 23:25:56 +00:00

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-6415550075

In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d3114)

Some of the test changes seem fine, but not sure if this is worth the squeeze.

Thanks, that's fair, and the PR was trying to do too much. I made it smaller: it now has the test race fix, the btck_chainstate_manager_set_clock_time API (now with an options setter too, see the separate comment about construction time), and the two tests using the new clock (chainstatemanager_ibd_exit_after_loading_blocks and the utxo_total_supply fuzz target you suggested). The mempool time point refactor and the commit making NodeClock::now() a link error in kernel code are now in a follow-up branch based on this one: https://github.com/ryanofsky/bitcoin/commits/pr/kclock2. I know you suggested keeping this in one piece, so I can open the follow-up as a PR whenever that would help review.

On maflcko's question in https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-5313546704: I think this is still relevant. #35496 sets the time for the whole process, and this sets it for one chainstate manager, so a time set for one test, run, or thread can't leak into another.

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-6415550075 In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d311435af967c75998d6eac04c1a8691758) > Some of the test changes seem fine, but not sure if this is worth the squeeze. Thanks, that's fair, and the PR was trying to do too much. I made it smaller: it now has the test race fix, the `btck_chainstate_manager_set_clock_time` API (now with an options setter too, see [the separate comment about construction time](https://github.com/bitcoin/bitcoin/pull/35557#discussion_r4498695811)), and the two tests using the new clock (`chainstatemanager_ibd_exit_after_loading_blocks` and the `utxo_total_supply` fuzz target you suggested). The mempool time point refactor and the commit making `NodeClock::now()` a link error in kernel code are now in a follow-up branch based on this one: https://github.com/ryanofsky/bitcoin/commits/pr/kclock2. I know you suggested keeping this in one piece, so I can open the follow-up as a PR whenever that would help review. On maflcko's question in https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-5313546704: I think this is still relevant. #35496 sets the time for the whole process, and this sets it for one chainstate manager, so a time set for one test, run, or thread can't leak into another.
ryanofsky (Migrated from github.com) commented 2026-11-06 23:25:59 +00:00

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4763429282

In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d3114)

there might be a case where the clock needs to be set during chainman construction

Looking again, there is one already: btck_chainstate_manager_create loads the chainstate, which runs this future-tip check and can latch the IBD status off, before the caller can call btck_chainstate_manager_set_clock_time. So I added btck_chainstate_manager_options_set_clock_time, which sets the time before the chainstate manager is created, and kept btck_chainstate_manager_set_clock_time for changing it afterwards, so the time can still be moved any number of times, as you preferred.

re: sedited https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4763429282 In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d311435af967c75998d6eac04c1a8691758) > there might be a case where the clock needs to be set during chainman construction Looking again, there is one already: `btck_chainstate_manager_create` loads the chainstate, which runs this future-tip check and can latch the IBD status off, before the caller can call `btck_chainstate_manager_set_clock_time`. So I added `btck_chainstate_manager_options_set_clock_time`, which sets the time before the chainstate manager is created, and kept `btck_chainstate_manager_set_clock_time` for changing it afterwards, so the time can still be moved any number of times, as you preferred.
ryanofsky (Migrated from github.com) commented 2026-11-06 23:25:57 +00:00

re: maflcko https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-5313546704

In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d3114)

The conflict with #35906 is only textual, and the two changes fit together: #35906 makes ContextualCheckBlockHeader take the time as a parameter, and with this PR its callers would pass chainman.Now() instead of NodeClock::now(). I'd suggest #35906 go first since it has ACKs, and I'll rebase on it.

re: maflcko https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-5313546704 In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d311435af967c75998d6eac04c1a8691758) The conflict with #35906 is only textual, and the two changes fit together: #35906 makes `ContextualCheckBlockHeader` take the time as a parameter, and with this PR its callers would pass `chainman.Now()` instead of `NodeClock::now()`. I'd suggest #35906 go first since it has ACKs, and I'll rebase on it.
Lines 1031-1035
ryanofsky (Migrated from github.com) commented 2026-11-06 23:25:58 +00:00

re: stickies-v https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4780411600

In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d3114)

One argument for your callback idea that I missed before: stringintech pointed out in https://github.com/bitcoin/bitcoin/pull/35496#issuecomment-5207535775 that a long call like btck_chainstate_manager_process_block may need to read the time several times, for example when the IBD status is re-checked after each connected block. A fixed time set beforehand can't advance during the call, and a callback can. My prototype in 3d513c5d03ceb8d33dc8e19c07149e3337c64ac6 only made the time a shared settable value, so it didn't really test your suggestion. I still think a setter is simpler to start with, since a fixed time is what deterministic tests and replays want, but I'm happy to switch to a callback if you or others think it's worth the extra API surface.

re: stickies-v https://github.com/bitcoin/bitcoin/pull/35557#issuecomment-4780411600 In commit "kernel, validation: scope clock time to ChainstateManager" (98bb3d311435af967c75998d6eac04c1a8691758) One argument for your callback idea that I missed before: stringintech pointed out in https://github.com/bitcoin/bitcoin/pull/35496#issuecomment-5207535775 that a long call like `btck_chainstate_manager_process_block` may need to read the time several times, for example when the IBD status is re-checked after each connected block. A fixed time set beforehand can't advance during the call, and a callback can. My prototype in 3d513c5d03ceb8d33dc8e19c07149e3337c64ac6 only made the time a shared settable value, so it didn't really test your suggestion. I still think a setter is simpler to start with, since a fixed time is what deterministic tests and replays want, but I'm happy to switch to a callback if you or others think it's worth the extra API surface.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35557
No description provided.