mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
CTransaction: Add constructor taking inputs and outputs #36696
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!36696
Loading…
Reference in a new issue
No description provided.
Delete branch "declarative-test-data"
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?
Part of #36692. Please view this PR in context of changes like https://github.com/bitcoin/bitcoin/pull/36694.
CTransactiongains a constructor that allows direct declarative construction as an alternative to building throughCMutableTransaction.CTxIn's constructors gain an optional argument to pass the script witness.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
No conflicts as of last run.
nit: I'd still prefer the easier C++11 std::vector here (https://github.com/bitcoin/bitcoin/issues/36692#issuecomment-6501722895) but I'll leave it to other reviewers to decide.
Earlier concerns neither republished nor resolved: 2 suggestions. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion · Redundant
BuildTxscall in the P2WPKH coinbase checkThe third
BuildTxscall recreates the first pair, which remains in scope, adding construction and cache work without changing the test.Location:
src/test/sigopcount_tests.cpp:164(head).🔵 Suggestion · New
CTransactiondefaults lack direct coverageThe new tests always supply version
1and never assert locktime, so they do not check the value constructor'sCURRENT_VERSIONand zero-locktime defaults.Location:
src/primitives/transaction.h:314(head).Review coverage was incomplete.
Reviewed:
3ffffa071c2e· Review report · Previous reportspermalink
I added a few cases to show the new constructor in action. If the constructor would accept
std::vectorarguments, a transaction without inputs could be constructed as:It is certainly less verbose, but I don't think it does a service to readability.
The
CTxInwitness argument is a nice addition. It would remove the “assign the witness after replacing vin” workaround I needed in several places in #36606 and #36694.Should this be explicit like the existing ones?
I think
std::span{inputs}would still move the elements out ofinputs. Copying for views avoids that. What do you think?That is a good point. I think we should just invoke
std::ranges::copyunconditionally. In C++23, we can remove this helper and use tag dispatching withstd::from_range. All as long as we actually want range support, so I will leave this open until https://github.com/bitcoin/bitcoin/pull/36696#discussion_r4415029088 is resolved.Implicit conversion cannot happen because the constructor requires at least two arguments.
Fixed it. Instead of moving when
Ris an rvalue, we want to move when R yields rvalues.std::ranges::copyalready does that and so doesstd::from_range.I have replaced the commits that exemplify the constructor in action with one that shows multiple things in a single place:
CTransactionin action.CTxInin action.CMutableTransactionis not necessary in many places, not just for building, but also as function argument.GetTxSigOpCostas an example test that contains multiple blocks where each should become its own unit test.CCoinsViewCache. It is not clear whether a test might depend on the modifications of tests run earlier or not.CTransactioncould benefit from applying best practices (never useconstdata members, maintain immutability by not providing write access): Inreturn {std::move(creationTx), std::move(spendingTx)};, the compiler provided move constructor ofCTransactioninvokes the move constructors of all its data members, which areconst, so they fall back to copying (this does not fireperformance-move-const-arg, because the individual data members fall back, not the tx object).CTransactiondoes not provide regular semantics: It is not possible to replace the existing values withstd::tie(creationTx, spendingTx) = BuildTxs(...);, becauseCTransactionis only sort-of copyable (due to not following ... I repeat myself. This is not a big deal here, as tests should be refactored for independence anyway).std::vectorand explicit types were not required at the call site, and why I claim this to be less readable:Sure, but my thinking is that init-lists for CTransaction must always end in a trailing comma:
But again, up to other reviewers to pick the side here 😅
Concept ACK
not sure about this. Previously .version=1 was clear from reading the code, now it is impossible to tell from just reading the line if this it locktime or version or something else. Also the LLM:
Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):Added the
/*version=*/and/*nHeight=*/comments.nit:
CScript{}curly braces.Small correction: implicit conversion is still possible I believe:
f({ins, outs})orreturn {ins, outs};. That copies all inputs/outputs and computes hashes. I would keep itexplicit. Not a blocker though.Concept ACK
The PR description still says that elements are copied or moved, while it has been changed to always copy.
nit: commit messages could use a short body with motivation of #36692
The motivating range example from #36692 doesn’t compile with this PR
Concept ACK
After going back and forward on this PR, I have to agree with @maflcko and prefer the by-value std::vector constructor as the public interface.
If i'm correct the braced lists are the common case in the tests which this constructor is meant to clean up. A range template can't take those, so every such call site would still need a
std::arraywrapper.On the txpackage_tests example: as written it doesn't compile against this PR. CTxIn(COutPoint) is explicit, so a range of COutPoint doesn't satisfy RangeOf, and package has to stay a real Package because the test shuffles it and appends the child.
So if i fix the example, the two versions are:
So the range version saves one line in C++20, and nothing once
std::ranges::tois available. And in the corrected version the readability diff is weak imho.My strong preference currently (if I have not missed a angle) is to land the by-value vector constructor here and add the range overload as a follow-up once there's a real view-shaped caller that needs it.
Changed constructors to take the types of the corresponding members by value and move into place.
I agree that this is the most straightforward approach.
Thanks for changing it to a by-value vector constructor.
Below are some NIT fixes that are also suggested by Ralph.
NIT: With the change to a vector constructor, this can be simplified.
NIT: Also a simplification opportunity, re-use the spendingTx instead of creating a new spendingTx.
This goes against the motivation of making unit tests not depend on modifications of other tests.
Fair enough. My thinking was that spendingTx is now an immutable value and it's in the same block so not really an other test. Lets keep it as is.
How far do we want to go? just drop the explicit
std::vectoror alsoCTxIn,COutPoint, etc?I would rather keep them all.
Well i'm only suggesting to remove the explicit vector and make them const. Happy to drop the const but wondering what keeping the explicit
std::vectoris providing us?Making the local variables
constwould cause them to be copied in the return statement.Keeping
std::vectoris just for consistency, so you don't have to decide where to put a type in front of a brace and where you can omit it. Just consistently use a type always. Henceautowith the type on the right. But it is my personal preference. I can also change it.Given the future context of making the
constmembersprivateit is more logical to keep the moves indeed.ACK
92f366aa70Happy with the current state of this PR, (see discussions above). I think it is in a good state and shows enough value to get merged.
ACK
92f366aa70Built
test_bitcoinand ransigopcount_testsandtransaction_testslocally.Non-blocking nit: consider making the constructor explicit, to match the CMutableTransaction constructors. That would stop things like
f({vin, vout})from quietly building and hashing a transaction.I plan to open follow-ups that convert more tests from
CMutableTransactionto constructingCTransactiondirectly.ACK
92f366aa70Nice use of forwarding constructors.
Good choice of unit test to clean up!
Happy you switched to accepting movable
std::vectors for now.Some subjective divergence in style, most prominent being the one clang-format agrees with me on (see inline comment).
Agree on defaulting to
explicitas per our developer-notes:Would prefer less tokens, especially since there's no alignment benefit from using
autoin this function:Would prefer adhering to the majority style of not having an extra space before the value.
Clang-format agrees.
For some reason I also prefer omitting the explicit
std::vector, it's a beloved but boring type, making the initializer-list braces feel sufficient. But agree it's subjective.It is still my preference to always have the type in front of the brace. But, meh. Changed the code to @hodlinator's preference.
re-ACK
3ffffa071cThanks for incorporating my suggestions. Also resolves conflict with merged #36895.
Nanonits, feel free to ignore:
COutPoint{}.I would opt to keep it.
re-ACK
3ffffa071cchanges sinds last ACK;
cr-ACK
3ffffa071cThe value ctor for
CTransactionand thescriptWitnessparameter onCTxInare small additions that don't change behaviour, and the existingCMutableTransactionctors now delegate to the new one. The test shows the benefits as well.ACK
3ffffa071cACK
3ffffa071cNew
CTransactionconstructor created for tests, but it's useful in general. Left some minor comments.I suggest making this constructor
explicitas well (all the other changes are compatible with it).The
CURRENT_VERSIONdefault value forversionis taken over fromCMutableTransaction. But I think it's better ifversionhad no default; theCURRENTmay change; 3 already exists; the version is quite important and it's better if specified explicitly; less confusion with the other the locktime parameter with default.If the default value is removed, the rest of the changeset builds without further modifications.
Nit:
std::moveofCTransactionis a copy, due to the const members. Themove's here could be removed, as they don't really end up in a move, but copy.Nit:
creationTx2is unused, alsocreationTx3.Please look above: https://github.com/bitcoin/bitcoin/pull/36696#discussion_r4416488577
Thanks, sorry for the duplication.
CTransactionhas a move constructor, which invokes the move constructor of each member. Because the members areconst, they fall back to being copied. The fallback is not obvious to the client, otherwise clang-tidy would complain. As a user ofCTransaction, you should not need to know about its members at all.std::moveis semantically correct here, I'd say.Accepted