mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:50:37 +00:00
refactor: keep duration calculations typed #35820
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!35820
Loading…
Reference in a new issue
No description provided.
Delete branch "l0rinc/compile-time-chrono-hardening"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem: Several fixed-duration calculations use raw integer time counts, leaving their units implicit and allowing unrelated integer values to be mixed.
Fix: Use
std::chronotypes through calculations where the surrounding APIs already support them, including GUI formatting, peer bans, scheduler forwarding, block timestamps, and target-spacing arithmetic.Where carrying chrono through an integer interface would broaden the change or make the result harder to read, this PR leaves the expression unchanged or keeps the conversion simply
duration / 1s.The remaining integer interfaces receive the same values, and characterization tests pin the PoW timing and inclusive assumevalid threshold.
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.
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:
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.
Concept ACK 7bb44a4b757c1d84091cd5518b5fa2abeb90168c
I like the clarity / readability this changes gives. It will even provide less proficient in C++ users to read consensus code.
Maybe split commit in kernel/consensus touching and the rest. In that way it's easy to ACK / review the consensus relevant changes.
Some NIT suggestions to use
std::chrono::<<duration>>can be done on more places but I have limited the NITS to hear the current thinking first :)NIT: why not go one step further for even more clarity ?
Note: not to use std::chrono::month{1};
Just to unify this section I would opt for using
std::chronofor every line. (not Blocking bike-shedding NIT)~0, some minor clarity improvements here and there but for the most part I don't see the point as this doesn't add any actual type safety or clarity. I don't find
14 * 24 * 60 * 60; // two weeksharder to read thanTicksSeconds(2 * 7 * 24h);Thanks for the review and feedback.
I agree that
14 * 24 * 60 * 60; // two weeksis readable in isolation - the main benefit is moving units from comments into expressions and (where possible), into types.E.g.
MIN_BLOCKS_TO_KEEP - 3600 / nPowTargetSpacingneeded a(1 hour)comment (proving that others didn't consider them trivial), whileMIN_BLOCKS_TO_KEEP - 1h / PowTargetSpacing()is self-describing and keeps both operands typed.Likewise,
8 * 60 * 60; // in secondsbecomesTicksSeconds(8h), making the conversion and units explicit.The branch is now split by risk, with exact-value coverage for the consensus-sensitive changes.
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/30320524467/job/90155255838LLM reason (✨ experimental): CI failed because IWYU detected missing/incorrect includes and forced an error (“Failure generated from IWYU” / exit status 1).
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.
Tend to agree with stickies. The changes as-is also violate the util/time.h docs.
This seems like a step in the wrong direction. The docstring of
TicksSecondsalso says that it should be avoided in code.the correct approach would be to write
constexpr auto ...{90min};and then use proper chrono types below:std::chrono::seconds secs{blockDate.secsTo(currentDate)};Also, the pull description seems wrong. I fail to see how any of the changes here are related to ODR?
They're not, I noticed these raw duration expressions while reviewing #35792's changes, which is why I originally described this as a follow-up.
Since that wording can read as grouping the two changes, I removed it from the PR description.
Thanks for the suggestion, I reworked the branch so durations remain typed where the surrounding APIs already support chrono, and I like it a lot better this way.
Where carrying chrono farther would broaden the change or make call sites harder to read, I left the expression unchanged or converted only where an existing interface still takes integer seconds.
not sure what exactly this is trying to enforce or harden. A plain
300 /* ms */seems more readable and direct than acount_milliseconds(300ms)or the alternative used in other parts of this pull:300ms / 1ms.I think the chrono types should only be used when the type adds some compile-time checks. And I don't think replacing a 300-raw-int literal with a 300-chrono-literal, and a cast to int is adding any checks.
Sure, I’ve reverted the ones that were arguably noisier than the original code. Thanks for the pushback!
I'd argue it's also valuable when we can remove comments that had to explain the values (e.g.
14 * 24 * 60 * 60; // two weeks) or when we can use the literals in range checks, e.g.if (delta_seconds <= 0 || delta_seconds > 3600) {.🐙 This pull request conflicts with the target branch and needs rebase.
Rebased after https://github.com/bitcoin/bitcoin/pull/35852
🐙 This pull request conflicts with the target branch and needs rebase.
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/36026083226/job/107722840641LLM reason (✨ experimental): CI failed because the IWYU check detected required include changes (edited
/src/rpc/node.cppand other file), causing it to exit non-zero.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.
1 suggestion
🔵 Suggestion
Assumevalid test does not pin the exact two-week threshold
Location:
test/functional/feature_assumevalid.py:190(head).The test catches a threshold below two weeks and a changed
<=comparison, but a larger threshold still passes.Details and suggested fix
Node 2 receives 2017 headers, placing block 1 exactly two weeks of equivalent work behind the best header. The test expects script verification to remain enabled, so it catches a lower threshold or changing
<=to<. But the nearest case that expects verification to be skipped places block 1 1,320,600 seconds behind; any threshold from two weeks up to that value still passes. For example, a mistaken 15-day threshold would go undetected. Such a mistake would make the node check more scripts, so this is a test-precision gap, not a safety problem. Send node 2 one additional header and check both sides of the boundary: block 1 should disable script verification at 2017 blocks behind, and block 2 should enable it at exactly 2016 blocks behind. Update thegetchaintipslookup toself.blocks[BURIED_HEADER_COUNT]; block 102 remains checked, so the existing rejection assertions still hold.Reviewed:
261fff951c5c· Full review report · Previous reportsThis has not received review for some time now and the response to the changes has been somewhat lukewarm. I'm also not entirely convinced that
std::chrono::weeks{2} / 1sis more readable/better than14 * 24 * 60 * 60; // two weeks. The division by1sis still something that a casual reader would have to read up on imo. Maybe this should be reconsidered?Yeah, I like the concept of moving toward chrono types, but if the casts are just "normalized" or "hidden" via
/1s, it seems more confusing than helpful. I think it is fine to have some casts, but they should be verbose and useTicksorTicksSinceEpoch, so that they either act as explicit documentation, or can be removed in the future.So I think stuff like this is fine to use:
However, this is not:
I'd be happy to review a pull with the first type of changes, but the second one can be left for now as-is.
Up for grabs