mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 09:11:37 +00:00
WIP: Primitives: Combine assignments #35994
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!35994
Loading…
Reference in a new issue
No description provided.
Delete branch "combine-assignments"
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?
#35904 mentions that validation code does not require the data members of
COutPoint,CTxOut, andCTxInto be individually mutable. This requirement instead comes from test code being written in a procedural rather than declarative style.This PR presents a
bitcoin-tidycheck that identifies consecutive assignments to annotated data members of the same object and suggests rewriting them with a constructor invocation. This allows a mechanical refactoring of the majority of mutations and it paves the road for making the types fully immutable.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.
🚧 At least one of the CI tasks failed.
Task
32 bit ARM: https://github.com/bitcoin/bitcoin/actions/runs/32057506973/job/95470930565LLM reason (✨ experimental): CI failed at compile time because
transaction.his inconsistent with theCOutPoint/CTxIn/CTxOutclass definitions (missing fields likehash/n,prevout,scriptSig, etc.).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.
I understand that it's not always trivial to run every check locally, but we're getting notifications about every push and it doesn't really increase the confidence in changes like this if they don't even pass CI.
I usually push these to my own fork first to make sure the change makes sense before pushing here.
Also, the
Primitives: Combine assignmentsgive no clue about what this PR is about, but reading the code seems to make sense, concept ACK.Looks like this is mostly a test-only cleanup (modulo ~6 instances in real code).
Seems fine, as this should reduce test issues due to re-using dirty state accidentally (c.f. https://github.com/bitcoin/bitcoin/pull/35863 et al)
Is there a reason to not use the C++11 narrowing checks for new code? Other places use a
static_cast<uint32_t>()for the second arg, and I think this is useful to know about and be explicit.Is the annotation really required? I'd guess that clang-tidy should be able to iterator over all ctors with args itself and then derive this itself?
The benefit would be that there is only a single source of truth and this won't go stale. Also, manual marking wouldn't be needed. (If you want to limit this to a selected set of classes, it would be better to pass the list as an option to clang-tidy?)
Given that the was confusion about the name, maybe name it differently?
I pushed this both to back the claim that most mutations appear in test code, but also as a base for discussion about the approach.
One approach would be to submit this PR as is.
An alternative approach would be to focus on one class at a time, like "COutPoint: Make type immutable". Such a PR would then apply
bitcoin-prefer-ctoras introduced here,bitcoin-use-observersfrom #35569, some manual fixes, plus the change to make the data members private. In this approach, https://github.com/bitcoin/bitcoin/pull/35994#discussion_r3801299334 would be less of an issue, because the annotations would be temporary anyway.Yet another approach would be to just use this PR as a backlog to identify "places of interest" and then rewrite the code manually, one file at a time, in a declarative style. For example, this code:
github.com/bitcoin/bitcoin@6751a323c0/src/bench/ccoins_caching.cpp (L35-L51)could be rewritten (assuming the necessary constructors are made available) as:
In this approach, https://github.com/bitcoin/bitcoin/pull/35994#discussion_r3801307204 can be applied directly, with explicit conversions where necessary.
Yes, there are a few places where aggregate initialization would not compile, unfortunately.
I don't want to build too many heuristics into the clang-tidy check. But depending on how we approach the change, it may be less of an issue or not be needed at all (see https://github.com/bitcoin/bitcoin/pull/35994#issuecomment-5325013842).
Ok, I see. I wonder if it is worth it to update them manually (either by adding a static_cast, or by updating the outer type to
unsigned, or so)?Given that such a patch should be easy to review and test-only, with other benefits, it seems fine to submit it and I'd be happy to review it, but no strong opinion.
If i'm not mistaken, I read 3 options from the comment of purpleKarrot:
A. Merge this PR as is. The tooling lands in this PR but needs annotations to work. The drawback is that the
CONSTRUCTOR_ARGUMENTannotations stay inprimitives/transaction.hwith no guarantee that a follow-up removes them, and reviewers approve a large mechanical diff with the justification in a future PR.B. One class at a time, ending in "immutability". For example "COutPoint: Make type immutable": annotate, run the check, run use-observers from #35569, fix the rest by hand, make the members private, and delete the annotations in the same PR. Each PR is self contained and can be judged as a whole if the change is worth the churn.
C. Use the check as a backlog only. Never merge the annotations. Run the check locally to find candidate files and rewrite them by hand in declarative style.
--
Personally I would opt for option B, where a tracking issue can be used to identify and coordinate the effort, keep this bitcoin-tidy code etc. If i'm not mistaken (or mis-understood the comment) this will result in PR's that are self contained with no, or little, follow ups so that the benefits, churn and results are clearly visible in one PR.
I am not sure about throwing this in the same bucket as https://github.com/bitcoin/bitcoin/pull/35569. I think it is pretty clear that a CTransaction should not be mutable (otherwise there wouldn't be a MutableTransaction).
However, for other structs, is there much value/benefit? https://github.com/bitcoin/bitcoin/pull/35569 seems a bit controversial, so expanding it to stuff that has even less benefit may not be the best approach?
I'd leave this a mostly test-only cleanup (plus maybe https://github.com/bitcoin/bitcoin/pull/35994#discussion_r3803317221), but no strong opinion.
Concept ACK
Cleaning up the tests is a convention. It does nothing to stop the same buggy behaviour being repeated in the future. Can you explain what benefit you see to keeping the structs in this PR mutable?
I tried switching the fix-it to emit {}. From what I can tell everything that then fails to compile is in test/bench.
Concept ACK
Do the checks here need to be run to a fixed point or is a single pass sufficient? Might be worth adding the invocation in the commit messages, so the mechanical changes can be verified.
Does this drop the witness?
Indeed.
They do belong into the same bucket. As I wrote in https://github.com/bitcoin/bitcoin/pull/35569#issuecomment-5281383955:
Which is exactly the claim that I wanted to back with this draft.
Concerning the approach, I actually lean towards C.
That sounds reasonable to me. It means the surrounding context gets read, the narrowing conversions get made explicit and comments in the affected blocks survive.
I've started on a couple of these in
/bench. Happy to keep going if that's useful.🐙 This pull request conflicts with the target branch and needs rebase.
🚧 At least one of the CI tasks failed.
Task
tidy: https://github.com/bitcoin/bitcoin/actions/runs/32467526648/job/96727213621LLM reason (✨ experimental): Clang-tidy failed due to
bitcoin-combine-assignmentswarnings-as-errors intest/mempool_fee_estimator_tests.cpp(separate member assignments not combined).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.
should probably use init-list, like https://github.com/bitcoin/bitcoin/pull/36019#discussion_r3811388178
🐙 This pull request conflicts with the target branch and needs rebase.
ralph referenced this pull request2026-10-23 08:23:45 +00:00
CI is red and this draft is inactive for months. I guess it can be closed?
🔴 Major · The new check makes tidy CI fail on an unconverted test file
With
bitcoin-*enabled andWarningsAsErrors: '*', the untouchedMakeRandomTxassignments inmempool_fee_estimator_tests.cppshould make the tidy job fail; this is inferred from the code, not a CI run.Location:
contrib/devtools/bitcoin-tidy/bitcoin-tidy.cpp:15(head).🟠 Minor · BuildSpendingTransaction drops the supplied script witness
The helper returns an input without the caller's witness, although current tests still pass because
DoTestsupplies that witness directly toVerifyScript.Location:
src/test/util/transaction_utils.cpp:33(head).🟠 Minor · The fix-it can change side effects, dependencies, and repeated assignments
The rewrite is not always equivalent; in this change it also makes the order of RNG draws in
sighash_tests.cppunspecified.Location:
contrib/devtools/bitcoin-tidy/combine-assignments.cpp:169(head).🟠 Minor · Compound assignments are rewritten as plain assignments
A compound update followed by another annotated-field assignment can be rewritten to a constructor that drops the existing value and the compound operation; no current in-tree site with this shape was identified.
Location:
contrib/devtools/bitcoin-tidy/combine-assignments.cpp:72(head).🔵 Suggestion · Annotations reach ordinary Clang builds of hot transaction fields
Clang lowers accesses to annotated fields through
llvm.ptr.annotation, which can inhibit optimization; the impact on Bitcoin Core has not been measured.Location:
src/attributes.h:29(head).🔵 Suggestion · The fix-it deletes comments between combined statements
The mechanical rewrite silently removed explanatory comments from several tests in this change.
Location:
contrib/devtools/bitcoin-tidy/combine-assignments.cpp:174(head).Review coverage was incomplete.
Reviewed:
95ea8fca11e4· Full review report · Previous reports