Repository navigation
CTransaction: Add constructor taking inputs and outputs - #36696
purpleKarrot wants to merge 3 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 ConflictsNo conflicts as of last run. |
7e66b30 to
172feb0
Compare
4491755 to
705d5d5
Compare
|
I have replaced the commits that exemplify the constructor in action with one that shows multiple things in a single place:
CTransaction creationTx{{{{}}}, {{1, scriptPubKey}}, 1};
CTransaction spendingTx{{{{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, {{1, {}}}, 1}; |
|
Concept ACK |
…vely c723c37 test: construct multisig_tests inputs/outputs declaratively (yuvicc) Pull request description: Part of #36692. This replaces the field-by-field construction of transaction inputs and outputs in `multisig_tests` with `COutPoint`, `CTxIn` and `CTxOut` constructor calls. It keeps `CMutableTransaction` as the builder, since a `CTransaction` constructor taking inputs and outputs directly is still under discussion (#36696). If one is added, the `vin`/`vout` lists here can be passed to it directly. This change is test-only and does not change the constructed transactions: - The funding outputs were previously created by `vout.resize(3)` and only had their `scriptPubKey` assigned, leaving `nValue` at its null value (`-1`). The `-1` is now spelled out explicitly, which makes this previously hidden value visible - The loop index is changed from `int` to `uint32_t` to match `COutPoint::n` ACKs for top commit: purpleKarrot: ACK c723c37 maflcko: review ACK c723c37 🍯 alexanderwiederin: ACK c723c37 Tree-SHA512: 3122762dae1e4fbc1a4ad79ce67128e83d29da965b3c8b6009f075c39310b8f983a96e3c4f30844a57af2faca4c658149808176aa4bb67d940ebeec147031676
705d5d5 to
06c8215
Compare
…ively b7abd88 test: construct mempool_tests inputs/outputs declaratively (Alexander Wiederin) Pull request description: Part of #36692. This replaces the field-by-field construction of transaction inputs and outputs in `mempool_tests` with `COutPoint`, `CTxIn` and `CTxOut` constructor calls. It keeps `CMutableTransaction` as the builder, since a `CTransaction` constructor taking inputs and outputs directly is still under discussion (#36696). If one is added, the `vin`/`vout` lists here can be passed to it directly. Either way, the declarative form makes the constructed transactions easier to read. This change is test-only and does not change the constructed transactions. The `mempool_eviction` bench and `rbf_tests` changes from the earlier version of this PR have been dropped, as they conflict with #36521. They can follow once that is resolved. ACKs for top commit: sedited: ACK b7abd88 yuvicc: lgtm ACK b7abd88 janb84: tACK b7abd88 Tree-SHA512: 5d1ebdad6d5029a310434415cb0bcbe0bdaddfcee0fd3381eb03b9189a851187e302e794e74b81b7bb193ab14425a40dd531f0eea14baf774171fc168748eec5
…txpackage, duplicate_inputs and p2sh tests a4a2647 test: construct script_p2sh_tests inputs/outputs declaratively (yuvicc) 7fc7d26 bench: construct duplicate_inputs inputs/outputs declaratively (yuvicc) 5336eaa test: construct txpackage_tests inputs/outputs declaratively (yuvicc) Pull request description: Part of #36692. Follow-up to #36750 Replaces field-by-field construction of inputs and outputs with `COutPoint`, `CTxIn` and `CTxOut` ctor calls in `txpackage_tests`, `bench/duplicate_inputs` and `script_p2sh_tests`. `CMutableTransaction` is kept as the builder while a `CTransaction` constructor is discussed in #36696. Witnesses are still assigned after `vin`, since `CTxIn` has no witness argument yet. Test-only; the constructed transactions are unchanged. - `script_p2sh_tests`: two outputs created by `vout.resize(1)` never had a value, so their null `nValue` (`-1`) is now explicit. - Intentional mutations are left as they are: the `scriptSig` swap in `sign`, the dummy signing in `ValidateInputsStandardness`, and the witness-only copies in `txpackage_tests` (`mtx_child2`, `mtx_parent2_v2`). ACKs for top commit: alexanderwiederin: ACK a4a2647 purpleKarrot: ACK a4a2647 maflcko: review ACK a4a2647 👒 janb84: ACK a4a2647 jpk68: ACK a4a2647 Tree-SHA512: ca452697a5bcccfbed2778785906f75c22e95fea04a1e79fb2d6fdae80ebe4badf5d8cb49a8b6add6e80a480b4826436caaee43ec534d4e6aae22f6584935b86
|
Concept ACK |
alexanderwiederin
left a comment
There was a problem hiding this comment.
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
janb84
left a comment
There was a problem hiding this comment.
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::array wrapper.
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:
// range constructor
const CTransaction child{
package | std::views::transform([](const CTransactionRef& parent) { return CTxIn{COutPoint{parent->GetHash(), 0}}; }),
std::array{CTxOut{47 * COIN, spk2}}, // a braced list doesn't compile next to a view
};
// vector constructor
std::vector<CTxIn> inputs;
for (const auto& parent : package) inputs.emplace_back(COutPoint{parent->GetHash(), 0});
const CTransaction child{std::move(inputs), {CTxOut{47 * COIN, spk2}}};So the range version saves one line in C++20, and nothing once std::ranges::to is 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.
ac38485 to
92f366a
Compare
|
Changed constructors to take the types of the corresponding members by value and move into place. |
janb84
left a comment
There was a problem hiding this comment.
Thanks for changing it to a by-value vector constructor.
Below are some NIT fixes that are also suggested by Ralph.
| auto creationTx = CTransaction{ | ||
| std::vector{CTxIn{COutPoint{}}}, | ||
| std::vector{CTxOut{1, scriptPubKey}}, | ||
| /*version=*/ 1, | ||
| }; | ||
| creationTx.vout = { | ||
| CTxOut{1, scriptPubKey}, | ||
| auto spendingTx = CTransaction{ | ||
| std::vector{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, | ||
| std::vector{CTxOut{1, CScript{}}}, | ||
| /*version=*/ 1, | ||
| }; | ||
|
|
||
| spendingTx.version = 1; | ||
| spendingTx.vin = { | ||
| CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig}, | ||
| }; | ||
| spendingTx.vin[0].scriptWitness = witness; | ||
| spendingTx.vout = { | ||
| CTxOut{1, CScript{}}, | ||
| }; | ||
|
|
||
| AddCoins(coins, CTransaction(creationTx), 0); | ||
| AddCoins(coins, creationTx, /*nHeight=*/ 0); | ||
| return {std::move(creationTx), std::move(spendingTx)}; |
There was a problem hiding this comment.
NIT: With the change to a vector constructor, this can be simplified.
| auto creationTx = CTransaction{ | |
| std::vector{CTxIn{COutPoint{}}}, | |
| std::vector{CTxOut{1, scriptPubKey}}, | |
| /*version=*/ 1, | |
| }; | |
| creationTx.vout = { | |
| CTxOut{1, scriptPubKey}, | |
| auto spendingTx = CTransaction{ | |
| std::vector{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, | |
| std::vector{CTxOut{1, CScript{}}}, | |
| /*version=*/ 1, | |
| }; | |
| spendingTx.version = 1; | |
| spendingTx.vin = { | |
| CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig}, | |
| }; | |
| spendingTx.vin[0].scriptWitness = witness; | |
| spendingTx.vout = { | |
| CTxOut{1, CScript{}}, | |
| }; | |
| AddCoins(coins, CTransaction(creationTx), 0); | |
| AddCoins(coins, creationTx, /*nHeight=*/ 0); | |
| return {std::move(creationTx), std::move(spendingTx)}; | |
| const CTransaction creationTx{ | |
| {CTxIn{COutPoint{}}}, | |
| {CTxOut{1, scriptPubKey}}, | |
| /*version=*/ 1, | |
| }; | |
| const CTransaction spendingTx{ | |
| {CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, | |
| {CTxOut{1, CScript{}}}, | |
| /*version=*/ 1, | |
| }; | |
| AddCoins(coins, creationTx, /*nHeight=*/ 0); | |
| return {creationTx, spendingTx}; |
There was a problem hiding this comment.
How far do we want to go? just drop the explicit std::vector or also CTxIn, COutPoint, etc?
auto creationTx = CTransaction{
/*inputs=*/ {{{}}},
/*outputs=*/ {{1, scriptPubKey}},
/*version=*/ 1,
};I would rather keep them all.
There was a problem hiding this comment.
How far do we want to go?
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::vector is providing us?
There was a problem hiding this comment.
Making the local variables const would cause them to be copied in the return statement.
Keeping std::vector is 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. Hence auto with the type on the right. But it is my personal preference. I can also change it.
There was a problem hiding this comment.
Given the future context of making the const members private it is more logical to keep the moves indeed.
There was a problem hiding this comment.
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.
| scriptPubKey[0] = 0x00; | ||
| BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness); | ||
| const auto [creationTx3, spendingTx3] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness); | ||
|
|
||
| // The witness of a coinbase transaction is not taken into account. | ||
| spendingTx.vin[0].prevout.SetNull(); | ||
| assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0); | ||
| CMutableTransaction spendingTx4{spendingTx3}; | ||
| spendingTx4.vin[0].prevout.SetNull(); | ||
| assert(GetTransactionSigOpCost(CTransaction{spendingTx4}, coins, flags) == 0); |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
ACK 92f366a Built Non-blocking nit: consider making the constructor explicit, to match the CMutableTransaction constructors. That would stop things like I plan to open follow-ups that convert more tests from |
hodlinator
left a comment
There was a problem hiding this comment.
ACK 92f366a
Nice 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).
| auto creationTx = CTransaction{ | ||
| std::vector{CTxIn{COutPoint{}}}, | ||
| std::vector{CTxOut{1, scriptPubKey}}, | ||
| /*version=*/ 1, | ||
| }; | ||
| creationTx.vout = { | ||
| CTxOut{1, scriptPubKey}, | ||
| auto spendingTx = CTransaction{ | ||
| std::vector{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, | ||
| std::vector{CTxOut{1, CScript{}}}, | ||
| /*version=*/ 1, | ||
| }; | ||
|
|
||
| spendingTx.version = 1; | ||
| spendingTx.vin = { | ||
| CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig}, | ||
| }; | ||
| spendingTx.vin[0].scriptWitness = witness; | ||
| spendingTx.vout = { | ||
| CTxOut{1, CScript{}}, | ||
| }; | ||
|
|
||
| AddCoins(coins, CTransaction(creationTx), 0); | ||
| AddCoins(coins, creationTx, /*nHeight=*/ 0); | ||
| return {std::move(creationTx), std::move(spendingTx)}; |
There was a problem hiding this comment.
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.
92f366a to
e63704e
Compare
It is still my preference to always have the type in front of the brace. But, meh. Changed the code to @hodlinator's preference. |
e63704e to
3ffffa0
Compare
| CTransaction creationTx{ | ||
| /*inputs=*/{CTxIn{COutPoint{}}}, | ||
| /*outputs=*/{CTxOut{1, scriptPubKey}}, | ||
| /*version=*/1, |
There was a problem hiding this comment.
Nanonits, feel free to ignore:
- CTxIn and CTxOut already indicate inputs/outputs, so the comments seems slightly redundant.
- Could also remove redundant
COutPoint{}.
--- a/src/test/sigopcount_tests.cpp
+++ b/src/test/sigopcount_tests.cpp
@@ -86,13 +86,13 @@ static ScriptError VerifyWithFlag(const CTransaction& output, const CTransaction
static std::pair<CTransaction, CTransaction> BuildTxs(CCoinsViewCache& coins, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
{
CTransaction creationTx{
- /*inputs=*/{CTxIn{COutPoint{}}},
- /*outputs=*/{CTxOut{1, scriptPubKey}},
+ {CTxIn{}},
+ {CTxOut{1, scriptPubKey}},
/*version=*/1,
};
CTransaction spendingTx{
- /*inputs=*/{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}},
- /*outputs=*/{CTxOut{1, CScript{}}},
+ {CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}},
+ {CTxOut{1, CScript{}}},
/*version=*/1,
};
|
cr-ACK 3ffffa0 The value ctor for |
|
ACK 3ffffa0 |
| scriptPubKey[0] = 0x51; | ||
| BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness); | ||
| assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0); | ||
| const auto [creationTx2, spendingTx2] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness); |
There was a problem hiding this comment.
3ffffa0 test: Demonstrate new CTransaction constructor in sigopcount_tests:
Nit: creationTx2 is unused, also creationTx3.
| bool ComputeHasWitness() const; | ||
|
|
||
| public: | ||
| CTransaction(std::vector<CTxIn> inputs, std::vector<CTxOut> outputs, uint32_t version = CURRENT_VERSION, uint32_t locktime = 0); |
There was a problem hiding this comment.
5d48cd3 CTransaction: Add a value constructor:
I suggest making this constructor explicit as well (all the other changes are compatible with it).
There was a problem hiding this comment.
Thanks, sorry for the duplication.
|
|
||
| AddCoins(coins, CTransaction(creationTx), 0); | ||
| AddCoins(coins, creationTx, /*nHeight=*/0); | ||
| return {std::move(creationTx), std::move(spendingTx)}; |
There was a problem hiding this comment.
3ffffa0 test: Demonstrate new CTransaction constructor in sigopcount_tests:
Nit: std::move of CTransaction is a copy, due to the const members. The move's here could be removed, as they don't really end up in a move, but copy.
There was a problem hiding this comment.
CTransaction has a move constructor, which invokes the move constructor of each member. Because the members are const, they fall back to being copied. The fallback is not obvious to the client, otherwise clang-tidy would complain. As a user of CTransaction, you should not need to know about its members at all. std::move is semantically correct here, I'd say.
| bool ComputeHasWitness() const; | ||
|
|
||
| public: | ||
| CTransaction(std::vector<CTxIn> inputs, std::vector<CTxOut> outputs, uint32_t version = CURRENT_VERSION, uint32_t locktime = 0); |
There was a problem hiding this comment.
5d48cd3 CTransaction: Add a value constructor:
The CURRENT_VERSION default value for version is taken over from CMutableTransaction. But I think it's better if version had no default; the CURRENT may 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.

Part of #36692. Please view this PR in context of changes like #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.