Repository navigation
Primitives: Combine assignments - #35994
purpleKarrot wants to merge 8 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
58e4280 to
da83f70
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
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. |
| Txid hash; | ||
| uint32_t n; | ||
| CONSTRUCTOR_ARGUMENT(1, 2) Txid hash; | ||
| CONSTRUCTOR_ARGUMENT(2, 2) uint32_t n; |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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 #35994 (comment)).
| t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4); | ||
| t1.vin[2].prevout.hash = dummyTransactions[1].GetHash(); | ||
| t1.vin[2].prevout.n = 1; | ||
| t1.vin[2].prevout = COutPoint(dummyTransactions[1].GetHash(), 1); |
There was a problem hiding this comment.
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.
| t1.vin[2].prevout = COutPoint(dummyTransactions[1].GetHash(), 1); | |
| t1.vin[2].prevout = COutPoint{dummyTransactions[1].GetHash(), 1}; |
There was a problem hiding this comment.
Yes, there are a few places where aggregate initialization would not compile, unfortunately.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I tried switching the fix-it to emit {}. From what I can tell everything that then fails to compile is in test/bench.
| public: | ||
| void addCheckFactories(clang::tidy::ClangTidyCheckFactories& CheckFactories) override | ||
| { | ||
| CheckFactories.registerCheck<CombineAssignments>("bitcoin-combine-assignments"); |
There was a problem hiding this comment.
| CheckFactories.registerCheck<CombineAssignments>("bitcoin-combine-assignments"); | |
| CheckFactories.registerCheck<CombineAssignments>("bitcoin-prefer-ctor"); |
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 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: bitcoin/src/bench/ccoins_caching.cpp Lines 35 to 51 in 6751a32 could be rewritten (assuming the necessary constructors are made available) as: const CTransaction tx_1{
std::vector{
CTxIn{COutPoint{dummyTransactions[0].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0)},
CTxIn{COutPoint{dummyTransactions[1].GetHash(), 0}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
CTxIn{COutPoint{dummyTransactions[1].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
},
std::vector{
CTxOut{90 * COIN, CScript{} << OP_1},
CTxOut{},
},
};In this approach, #35994 (comment) can be applied directly, with explicit conversions where necessary. |
|
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 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 #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? #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 #35994 (comment)), 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? |
alexanderwiederin
left a comment
There was a problem hiding this comment.
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.
| txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL; | ||
| txSpend.vout[0].scriptPubKey = CScript(); | ||
| txSpend.vout[0].nValue = txCredit.vout[0].nValue; | ||
| txSpend.vin[0] = CTxIn(COutPoint(txCredit.GetHash(), 0), scriptSig, CTxIn::SEQUENCE_FINAL); |
There was a problem hiding this comment.
Does this drop the witness?
They do belong into the same bucket. As I wrote in #35569 (comment):
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 |
…ssion 950bdb7 bench: Construct CTxOut and COutPoint in a single expression (Alexander Wiederin) Pull request description: Replaces field-by-field mutation of `CTxOut` and `COutPoint` in two bench files with brace initialisation, which requires the `size_t` conversions to be made explicit. Noticed while looking at #35994, where switching the proposed fix-it to `{}` surfaces implicit narrowing conversions like these. The constructed values are unchanged. *Note: Only the sites where a conversion is involved are included in this PR; the remaining field-by-field construction in `bench/` would be covered by #35994's follow-ups.* ACKs for top commit: l0rinc: code review ACK 950bdb7 maflcko: review ACK 950bdb7 🐯 Tree-SHA512: c664d41eeeb9241d381e5fa27689f18d9445a942055734463978498f60d0f2c495a0d7a58131493bb63267371bb6fe2d45ac124d910c919c8905322948d17a23
Add a check to bitcoin-tidy that detects multiple consecutive assignments to data members of the same base and suggests a constructor call as a replacement.
da83f70 to
95ea8fc
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
| coinbaseTx.vout.resize(1); | ||
| coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB; | ||
| coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus()); | ||
| coinbaseTx.vout[0] = CTxOut(GetBlockSubsidy(nHeight, chainparams.GetConsensus()), SCRIPT_PUB); |
There was a problem hiding this comment.
should probably use init-list, like #36019 (comment)
|
🐙 This pull request conflicts with the target branch and needs rebase. |
…context-dependent cases bdf58dd test: construct transaction_tests test_Get inputs/outputs declaratively (Alexander Wiederin) 24d00c4 bench: construct ccoins_caching inputs/outputs declaratively (Alexander Wiederin) a45478a test: construct sigopcount_tests BuildTxs inputs/outputs declaratively (Alexander Wiederin) 524d554 test: construct CreateBlockChain coinbase declaratively (Alexander Wiederin) 60be093 test: construct transaction_utils inputs/outputs declaratively (Alexander Wiederin) Pull request description: Part of #36692 (previously discussed in #35994): replace procedural, field-by-field construction of `COutPoint`, `CTxIn` and `CTxOut` in test code with constructor calls. This PR covers cases that are context-dependent and need manual changes, independent of a scripted-diff for the trivial bulk: - `test/util/transaction_utils`: in `BuildSpendingTransaction`, the `scriptWitness` assignment has to be moved after `vin` is replaced, otherwise the witness is dropped. - `test/util/mining`: in `CreateBlockChain`, the coinbase input's fields were split by the output assignments, with comments that are now kept next to the constructor arguments. - `sigopcount_tests`: `BuildTxs` has the same witness ordering issue as `BuildSpendingTransaction`. - `bench/ccoins_caching`: The `scriptSig`s are built with `<<` appends, and the second output was created by `vout.resize(2)` but never assigned, so it is kept as a null `CTxOut{}`. - `transaction_tests`: `test_Get` builds the same transaction as `bench/ccoins_caching`, with the same `<<` appends and null second output. **This change is test- and bench-only and does not change the constructed transactions.** Notes for reviewers: - `CTxIn` has no witness constructor argument, so `scriptWitness` is assigned after `vin` is replaced. - `BuildTxs` is called repeatedly on the same transaction objects. Previously, `resize(1)` kept the existing inputs and only overwrote some of their fields; now they are replaced entirely. The fields that were left untouched by `BuildTxs` are never modified by the test, so the transactions are unchanged. - The null second output in `ccoins_caching` is copied from `test_Get` in `transaction_tests` and looks like an oversight. It is kept here to avoid behaviour changes. ACKs for top commit: maflcko: review ACK bdf58dd 🍠 purpleKarrot: But even when the approach does not follow the steps 1 and 2, it is a step in the right direction, hence ACK bdf58dd Tree-SHA512: af4c7489ddc553eda466b7ac4ca33cff400a495264b8cb2d869929e108fab8dac08f3cc164be79a3505b7c8b461514fc6f87f5afd42919f0712dd1d325d824b7
|
CI is red and this draft is inactive for months. I guess it can be closed? |

#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.