CTransaction: Add constructor taking inputs and outputs #36696

Open
purpleKarrot wants to merge 3 commits from declarative-test-data into master AGit
purpleKarrot commented 2026-10-29 16:13:13 +00:00 (Migrated from github.com)

Part of #36692. Please view this PR in context of changes like https://github.com/bitcoin/bitcoin/pull/36694.

CTransaction gains a constructor that allows direct declarative construction as an alternative to building through CMutableTransaction. CTxIn's constructors gain an optional argument to pass the script witness.

Part of #36692. Please view this PR in context of changes like https://github.com/bitcoin/bitcoin/pull/36694. `CTransaction` gains a constructor that allows direct declarative construction as an alternative to building through `CMutableTransaction`. `CTxIn`'s constructors gain an optional argument to pass the script witness.
DrahtBot commented 2026-10-29 16:13:23 +00:00 (Migrated from github.com)

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.

Type Reviewers
ACK hodlinator, janb84, yuvicc, alexanderwiederin, optout21
Concept ACK sedited

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36696 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36696.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36696#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [hodlinator](https://github.com/bitcoin/bitcoin/pull/36696#pullrequestreview-6127199587), [janb84](https://github.com/bitcoin/bitcoin/pull/36696#pullrequestreview-6129311611), [yuvicc](https://github.com/bitcoin/bitcoin/pull/36696#issuecomment-7313956260), [alexanderwiederin](https://github.com/bitcoin/bitcoin/pull/36696#issuecomment-7314105191), [optout21](https://github.com/bitcoin/bitcoin/pull/36696#pullrequestreview-6132650201) | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/36696#issuecomment-6631621595) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
maflcko (Migrated from github.com) reviewed 2026-10-29 16:28:07 +00:00
maflcko (Migrated from github.com) commented 2026-10-29 16:28:07 +00:00

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.

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.
First-time contributor

Earlier concerns neither republished nor resolved: 2 suggestions. See the Run archive linked from the Review report for their assessments.

🔵 Suggestion · Redundant BuildTxs call in the P2WPKH coinbase check

The third BuildTxs call 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 CTransaction defaults lack direct coverage

The new tests always supply version 1 and never assert locktime, so they do not check the value constructor's CURRENT_VERSION and zero-locktime defaults.

Location: src/primitives/transaction.h:314 (head).

Review coverage was incomplete.


Reviewed: 3ffffa071c2e · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:1ea904d983f8841c28cb8240c576e46ac2efe529 --> <!-- ralph-head:3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec --> <!-- ralph-case:96e88f4d197060664b869baeebbd3800c66d2ac3a40a63229042ebe1accdc599 --> Earlier concerns neither republished nor resolved: 2 suggestions. See the Run archive linked from the Review report for their assessments. **🔵 Suggestion · Redundant `BuildTxs` call in the P2WPKH coinbase check** The third `BuildTxs` call recreates the first pair, which remains in scope, adding construction and cache work without changing the test. Location: [`src/test/sigopcount_tests.cpp:164`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec/src/test/sigopcount_tests.cpp#L164>) (head). **🔵 Suggestion · New `CTransaction` defaults lack direct coverage** The new tests always supply version `1` and never assert locktime, so they do not check the value constructor's `CURRENT_VERSION` and zero-locktime defaults. Location: [`src/primitives/transaction.h:314`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec/src/primitives/transaction.h#L314>) (head). Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`3ffffa071c2e`](<https://git.fish.foo/bitcoin/bitcoin/commit/3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec>) · [Review report](<https://review.fish.foo/report/36696-3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec-3372-9.html>) · [Previous reports](<https://review.fish.foo/traces/36696/>) [permalink](<https://review.fish.foo/report/36696-3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec-3372-9.html>)
purpleKarrot (Migrated from github.com) reviewed 2026-10-29 18:47:40 +00:00
purpleKarrot (Migrated from github.com) commented 2026-10-29 18:47:40 +00:00

I added a few cases to show the new constructor in action. If the constructor would accept std::vector arguments, a transaction without inputs could be constructed as:

auto tx = CTransaction{{}, {
    CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG},
    CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG},
}};

It is certainly less verbose, but I don't think it does a service to readability.

I added a few cases to show the new constructor in action. If the constructor would accept `std::vector` arguments, a transaction without inputs could be constructed as: ```cpp auto tx = CTransaction{{}, { CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG}, CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG}, }}; ``` It is certainly less verbose, but I don't think it does a service to readability.
alexanderwiederin (Migrated from github.com) reviewed 2026-10-29 18:49:08 +00:00
alexanderwiederin (Migrated from github.com) left a comment

The CTxIn witness 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.

The `CTxIn` witness 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.
alexanderwiederin (Migrated from github.com) commented 2026-10-29 18:43:46 +00:00
    explicit CTransaction(In&& in, Out&& out, uint32_t ver = CURRENT_VERSION, uint32_t lt = 0)

Should this be explicit like the existing ones?

```suggestion explicit CTransaction(In&& in, Out&& out, uint32_t ver = CURRENT_VERSION, uint32_t lt = 0) ``` Should this be explicit like the existing ones?
alexanderwiederin (Migrated from github.com) commented 2026-10-29 18:40:08 +00:00
    if constexpr (std::is_lvalue_reference_v<R> || std::ranges::view<std::remove_cvref_t<R>>) {

I think std::span{inputs} would still move the elements out of inputs. Copying for views avoids that. What do you think?

```suggestion if constexpr (std::is_lvalue_reference_v<R> || std::ranges::view<std::remove_cvref_t<R>>) { ``` I think `std::span{inputs}` would still move the elements out of `inputs`. Copying for views avoids that. What do you think?
purpleKarrot (Migrated from github.com) reviewed 2026-10-29 19:21:54 +00:00
purpleKarrot (Migrated from github.com) commented 2026-10-29 19:21:54 +00:00

That is a good point. I think we should just invoke std::ranges::copy unconditionally. In C++23, we can remove this helper and use tag dispatching with std::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.

That is a good point. I think we should just invoke `std::ranges::copy` unconditionally. In C++23, we can remove this helper and use tag dispatching with `std::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.
purpleKarrot (Migrated from github.com) reviewed 2026-10-29 19:22:45 +00:00
purpleKarrot (Migrated from github.com) commented 2026-10-29 19:22:45 +00:00

Implicit conversion cannot happen because the constructor requires at least two arguments.

Implicit conversion cannot happen because the constructor requires at least two arguments.
purpleKarrot (Migrated from github.com) reviewed 2026-10-30 07:42:41 +00:00
purpleKarrot (Migrated from github.com) commented 2026-10-30 07:42:41 +00:00

Fixed it. Instead of moving when R is an rvalue, we want to move when R yields rvalues. std::ranges::copy already does that and so does std::from_range.

Fixed it. Instead of moving when `R` is an rvalue, we want to move when R *yields* rvalues. `std::ranges::copy` already does that and so does `std::from_range`.
purpleKarrot commented 2026-10-30 08:21:43 +00:00 (Migrated from github.com)

I have replaced the commits that exemplify the constructor in action with one that shows multiple things in a single place:

  • It shows the new constructor of CTransaction in action.
  • It shows the new constructor of CTxIn in action.
  • It shows how CMutableTransaction is not necessary in many places, not just for building, but also as function argument.
  • It reveals GetTxSigOpCost as an example test that contains multiple blocks where each should become its own unit test.
  • It reveals potential test dependencies: Each test setup modifies the CCoinsViewCache. It is not clear whether a test might depend on the modifications of tests run earlier or not.
  • It shows that CTransaction could benefit from applying best practices (never use const data members, maintain immutability by not providing write access): In return {std::move(creationTx), std::move(spendingTx)};, the compiler provided move constructor of CTransaction invokes the move constructors of all its data members, which are const, so they fall back to copying (this does not fire performance-move-const-arg, because the individual data members fall back, not the tx object).
  • It reveals the annoyance that CTransaction does not provide regular semantics: It is not possible to replace the existing values with std::tie(creationTx, spendingTx) = BuildTxs(...);, because CTransaction is 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).
  • It also provides an example where code could be much less verbose if the constructor accepted std::vector and explicit types were not required at the call site, and why I claim this to be less readable:
CTransaction creationTx{{{{}}}, {{1, scriptPubKey}}, 1};
CTransaction spendingTx{{{{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, {{1, {}}}, 1};
I have replaced the commits that exemplify the constructor in action with one that shows multiple things in a single place: - It shows the new constructor of `CTransaction` in action. - It shows the new constructor of `CTxIn` in action. - It shows how `CMutableTransaction` is not necessary in many places, not just for building, but also as function argument. - It reveals `GetTxSigOpCost` as an example test that contains multiple blocks where each should become its own unit test. - It reveals potential test dependencies: Each test setup modifies the `CCoinsViewCache`. It is not clear whether a test might depend on the modifications of tests run earlier or not. - It shows that `CTransaction` could benefit from applying best practices (never use `const` data members, maintain immutability by not providing write access): In `return {std::move(creationTx), std::move(spendingTx)};`, the compiler provided move constructor of `CTransaction` invokes the move constructors of all its data members, which are `const`, so they fall back to copying (this does not fire `performance-move-const-arg`, because the individual data members fall back, not the tx object). - It reveals the annoyance that `CTransaction` does not provide regular semantics: It is not possible to replace the existing values with `std::tie(creationTx, spendingTx) = BuildTxs(...);`, because `CTransaction` is 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). - It also provides an example where code could be much less verbose if the constructor accepted `std::vector` and explicit types were not required at the call site, and why I claim this to be less readable: ```cpp CTransaction creationTx{{{{}}}, {{1, scriptPubKey}}, 1}; CTransaction spendingTx{{{{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, {{1, {}}}, 1}; ```
maflcko (Migrated from github.com) reviewed 2026-10-30 09:27:20 +00:00
maflcko (Migrated from github.com) commented 2026-10-30 09:27:20 +00:00

Sure, but my thinking is that init-lists for CTransaction must always end in a trailing comma:

auto tx = CTransaction{
    {},
    {
        CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG},
        CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG},
    },
};

But again, up to other reviewers to pick the side here 😅

Sure, but my thinking is that init-lists for CTransaction must always end in a trailing comma: ```cpp auto tx = CTransaction{ {}, { CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG}, CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG}, }, }; ``` But again, up to other reviewers to pick the side here :sweat_smile:
sedited commented 2026-11-04 13:16:14 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
maflcko (Migrated from github.com) reviewed 2026-11-04 13:28:15 +00:00
maflcko (Migrated from github.com) commented 2026-11-04 13:28:15 +00:00

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):

src/test/sigopcount_tests.cpp: CTransaction{..., ..., 1}
src/test/sigopcount_tests.cpp: CTransaction{..., ..., 1}
src/test/sigopcount_tests.cpp: AddCoins(coins, creationTx, 0)
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):` src/test/sigopcount_tests.cpp: CTransaction{..., ..., 1} src/test/sigopcount_tests.cpp: CTransaction{..., ..., 1} src/test/sigopcount_tests.cpp: AddCoins(coins, creationTx, 0)
purpleKarrot (Migrated from github.com) reviewed 2026-11-06 09:40:47 +00:00
purpleKarrot (Migrated from github.com) commented 2026-11-06 09:40:47 +00:00

Added the /*version=*/ and /*nHeight=*/ comments.

Added the `/*version=*/` and `/*nHeight=*/` comments.
alexanderwiederin (Migrated from github.com) reviewed 2026-11-10 12:41:51 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-11-10 09:37:06 +00:00

nit: CScript{} curly braces.

nit: `CScript{}` curly braces.
alexanderwiederin (Migrated from github.com) commented 2026-11-10 10:35:23 +00:00

Small correction: implicit conversion is still possible I believe: f({ins, outs}) or return {ins, outs};. That copies all inputs/outputs and computes hashes. I would keep it explicit. Not a blocker though.

Small correction: implicit conversion is still possible I believe: `f({ins, outs})` or `return {ins, outs};`. That copies all inputs/outputs and computes hashes. I would keep it `explicit`. Not a blocker though.
yuvicc commented 2026-11-12 08:55:14 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
alexanderwiederin (Migrated from github.com) reviewed 2026-11-17 14:58:30 +00:00
alexanderwiederin (Migrated from github.com) left a 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

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
alexanderwiederin (Migrated from github.com) commented 2026-11-17 14:55:58 +00:00

The motivating range example from #36692 doesn’t compile with this PR

note: because 'is_convertible_v<COutPoint, CTxIn>' evaluated to false
The motivating range example from [#36692](https://github.com/bitcoin/bitcoin/issues/36692#issuecomment-6682329398) doesn’t compile with this PR ``` note: because 'is_convertible_v<COutPoint, CTxIn>' evaluated to false ```
janb84 (Migrated from github.com) reviewed 2026-11-24 18:28:49 +00:00
janb84 (Migrated from github.com) left a 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.

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<CTxIn>, 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: ```cpp // 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.
purpleKarrot commented 2026-11-24 19:42:10 +00:00 (Migrated from github.com)

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.

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.
janb84 (Migrated from github.com) reviewed 2026-11-25 09:38:57 +00:00
janb84 (Migrated from github.com) left a comment

Thanks for changing it to a by-value vector constructor.
Below are some NIT fixes that are also suggested by Ralph.

Thanks for changing it to a by-value vector constructor. Below are some NIT fixes that are also suggested by Ralph.
janb84 (Migrated from github.com) commented 2026-11-25 09:25:20 +00:00

NIT: With the change to a vector constructor, this can be simplified.

    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};
NIT: With the change to a vector constructor, this can be simplified. ```suggestion 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}; ```
janb84 (Migrated from github.com) commented 2026-11-25 09:35:30 +00:00

NIT: Also a simplification opportunity, re-use the spendingTx instead of creating a new spendingTx.

        // The witness of a coinbase transaction is not taken into account.
        CMutableTransaction coinbaseSpend{spendingTx};
        coinbaseSpend.vin[0].prevout.SetNull();
        assert(GetTransactionSigOpCost(CTransaction{coinbaseSpend}, coins, flags) == 0);
NIT: Also a simplification opportunity, re-use the spendingTx instead of creating a new spendingTx. ```suggestion // The witness of a coinbase transaction is not taken into account. CMutableTransaction coinbaseSpend{spendingTx}; coinbaseSpend.vin[0].prevout.SetNull(); assert(GetTransactionSigOpCost(CTransaction{coinbaseSpend}, coins, flags) == 0); ```
purpleKarrot (Migrated from github.com) reviewed 2026-11-25 09:42:54 +00:00
purpleKarrot (Migrated from github.com) commented 2026-11-25 09:42:54 +00:00

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.

> 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.
janb84 (Migrated from github.com) reviewed 2026-11-25 09:51:39 +00:00
janb84 (Migrated from github.com) commented 2026-11-25 09:51:39 +00:00

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.

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.
purpleKarrot (Migrated from github.com) reviewed 2026-11-25 09:56:00 +00:00
purpleKarrot (Migrated from github.com) commented 2026-11-25 09:56:00 +00:00

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.

How far do we want to go? just drop the explicit `std::vector` or also `CTxIn`, `COutPoint`, etc? ```cpp auto creationTx = CTransaction{ /*inputs=*/ {{{}}}, /*outputs=*/ {{1, scriptPubKey}}, /*version=*/ 1, }; ``` I would rather keep them all.
janb84 (Migrated from github.com) reviewed 2026-11-25 10:01:45 +00:00
janb84 (Migrated from github.com) commented 2026-11-25 10:01:45 +00:00

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?

> 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?
purpleKarrot (Migrated from github.com) reviewed 2026-11-25 10:12:32 +00:00
purpleKarrot (Migrated from github.com) commented 2026-11-25 10:12:32 +00:00

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.

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.
janb84 (Migrated from github.com) reviewed 2026-11-25 12:45:46 +00:00
janb84 (Migrated from github.com) commented 2026-11-25 12:45:46 +00:00

Given the future context of making the const members private it is more logical to keep the moves indeed.

Given the future context of making the `const` members `private` it is more logical to keep the moves indeed.
janb84 (Migrated from github.com) reviewed 2026-11-25 12:47:45 +00:00
janb84 (Migrated from github.com) left a comment

ACK 92f366aa70

Happy 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 92f366aa70c920e5bafd01ded67e63d4cc181e4c Happy 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.
alexanderwiederin commented 2026-11-27 14:29:32 +00:00 (Migrated from github.com)

ACK 92f366aa70

Built test_bitcoin and ran sigopcount_tests and transaction_tests locally.

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 CMutableTransaction to constructing CTransaction directly.

ACK 92f366aa70c Built `test_bitcoin` and ran `sigopcount_tests` and `transaction_tests` locally. 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 `CMutableTransaction` to constructing `CTransaction` directly.
hodlinator (Migrated from github.com) approved these changes 2026-11-30 10:22:26 +00:00
hodlinator (Migrated from github.com) left a comment

ACK 92f366aa70

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

ACK 92f366aa70c920e5bafd01ded67e63d4cc181e4c Nice use of forwarding constructors. Good choice of unit test to clean up! Happy you switched to accepting movable `std::vector`s for now. Some subjective divergence in style, most prominent being the one clang-format agrees with me on (see inline comment).
hodlinator (Migrated from github.com) commented 2026-11-30 09:48:06 +00:00

Agree on defaulting to explicit as per our developer-notes:

By default, declare constructors explicit

Agree on defaulting to `explicit` as per our developer-notes: > By default, declare constructors `explicit`
hodlinator (Migrated from github.com) commented 2026-11-30 10:06:11 +00:00

Would prefer less tokens, especially since there's no alignment benefit from using auto in this function:

    CTransaction creationTx{
Would prefer less tokens, especially since there's no alignment benefit from using `auto` in this function: ```suggestion CTransaction creationTx{ ```
hodlinator (Migrated from github.com) commented 2026-11-30 10:02:23 +00:00

Would prefer adhering to the majority style of not having an extra space before the value.

₿ git grep "\=\*/[^\n^ ]" |wc -l
2721

₿ git grep "\=\*/ " | wc -l
219

Clang-format agrees.

git diff -U0 HEAD~3.. | ./contrib/devtools/clang-format-diff.py -p1 -i -v
--- a/src/test/sigopcount_tests.cpp
+++ b/src/test/sigopcount_tests.cpp
@@ -88,15 +88,15 @@ static std::pair<CTransaction, CTransaction> BuildTxs(CCoinsViewCache& coins, co
     auto creationTx = CTransaction{
         std::vector{CTxIn{COutPoint{}}},
         std::vector{CTxOut{1, scriptPubKey}},
-        /*version=*/ 1,
+        /*version=*/1,
     };
     auto spendingTx = CTransaction{
         std::vector{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}},
         std::vector{CTxOut{1, CScript{}}},
-        /*version=*/ 1,
+        /*version=*/1,
     };
 
-    AddCoins(coins, creationTx, /*nHeight=*/ 0);
+    AddCoins(coins, creationTx, /*nHeight=*/0);
     return {std::move(creationTx), std::move(spendingTx)};
 }
Would prefer adhering to the majority style of not having an extra space before the value. ```sh ₿ git grep "\=\*/[^\n^ ]" |wc -l 2721 ₿ git grep "\=\*/ " | wc -l 219 ``` Clang-format agrees. ``` git diff -U0 HEAD~3.. | ./contrib/devtools/clang-format-diff.py -p1 -i -v ``` ```diff --- a/src/test/sigopcount_tests.cpp +++ b/src/test/sigopcount_tests.cpp @@ -88,15 +88,15 @@ static std::pair<CTransaction, CTransaction> BuildTxs(CCoinsViewCache& coins, co auto creationTx = CTransaction{ std::vector{CTxIn{COutPoint{}}}, std::vector{CTxOut{1, scriptPubKey}}, - /*version=*/ 1, + /*version=*/1, }; auto spendingTx = CTransaction{ std::vector{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}}, std::vector{CTxOut{1, CScript{}}}, - /*version=*/ 1, + /*version=*/1, }; - AddCoins(coins, creationTx, /*nHeight=*/ 0); + AddCoins(coins, creationTx, /*nHeight=*/0); return {std::move(creationTx), std::move(spendingTx)}; } ```
hodlinator (Migrated from github.com) commented 2026-11-30 09:53:08 +00:00

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.

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.
purpleKarrot commented 2026-12-01 07:41:10 +00:00 (Migrated from github.com)

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. https://github.com/bitcoin/bitcoin/pull/36696#discussion_r4693359321

It is still my preference to always have the type in front of the brace. But, meh. Changed the code to @hodlinator's preference.

> 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. https://github.com/bitcoin/bitcoin/pull/36696#discussion_r4693359321 It is still my preference to always have the type in front of the brace. But, meh. Changed the code to @hodlinator's preference.
hodlinator (Migrated from github.com) approved these changes 2026-12-01 08:10:37 +00:00
hodlinator (Migrated from github.com) left a comment

re-ACK 3ffffa071c

Thanks for incorporating my suggestions. Also resolves conflict with merged #36895.

re-ACK 3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec Thanks for incorporating my suggestions. Also resolves conflict with merged #36895.
hodlinator (Migrated from github.com) commented 2026-12-01 08:04:24 +00:00

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,
     };
 
Nanonits, feel free to ignore: * CTx*In* and CTx*Out* already indicate inputs/outputs, so the comments seems slightly redundant. * Could also remove redundant `COutPoint{}`. ```diff --- 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, }; ```
janb84 (Migrated from github.com) reviewed 2026-12-01 10:19:00 +00:00
janb84 (Migrated from github.com) commented 2026-12-01 10:19:00 +00:00

I would opt to keep it.

I would opt to keep it.
janb84 (Migrated from github.com) reviewed 2026-12-01 10:22:06 +00:00
janb84 (Migrated from github.com) left a comment

re-ACK 3ffffa071c

changes sinds last ACK;

  • small style changes
re-ACK 3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec changes sinds last ACK; - small style changes
yuvicc commented 2026-12-01 14:15:35 +00:00 (Migrated from github.com)

cr-ACK 3ffffa071c

The value ctor for CTransaction and the scriptWitness parameter on CTxIn are small additions that don't change behaviour, and the existing CMutableTransaction ctors now delegate to the new one. The test shows the benefits as well.

cr-ACK 3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec The value ctor for `CTransaction` and the `scriptWitness` parameter on `CTxIn` are small additions that don't change behaviour, and the existing `CMutableTransaction` ctors now delegate to the new one. The test shows the benefits as well.
alexanderwiederin commented 2026-12-01 14:20:59 +00:00 (Migrated from github.com)
ACK 3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec
optout21 (Migrated from github.com) reviewed 2026-12-01 14:24:13 +00:00
optout21 (Migrated from github.com) left a comment

ACK 3ffffa071c

New CTransaction constructor created for tests, but it's useful in general. Left some minor comments.

ACK 3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec New `CTransaction` constructor created for tests, but it's useful in general. Left some minor comments.
optout21 (Migrated from github.com) commented 2026-12-01 14:02:16 +00:00

5d48cd3 CTransaction: Add a value constructor:

I suggest making this constructor explicit as well (all the other changes are compatible with it).

> [5d48cd3](https://github.com/bitcoin/bitcoin/pull/36696/changes/5d48cd381b4cf5afdf5f86917bdce3712d6fa55b) _CTransaction: Add a value constructor_: I suggest making this constructor `explicit` as well (all the other changes are compatible with it).
optout21 (Migrated from github.com) commented 2026-12-01 14:22:14 +00:00

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.

> [5d48cd3](https://github.com/bitcoin/bitcoin/pull/36696/changes/5d48cd381b4cf5afdf5f86917bdce3712d6fa55b) _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.
optout21 (Migrated from github.com) commented 2026-12-01 14:08:28 +00:00

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.

> [3ffffa0](https://github.com/bitcoin/bitcoin/pull/36696/changes/3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec) _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.
optout21 (Migrated from github.com) commented 2026-12-01 13:54:10 +00:00

3ffffa0 test: Demonstrate new CTransaction constructor in sigopcount_tests:

Nit: creationTx2 is unused, also creationTx3.

> [3ffffa0](https://github.com/bitcoin/bitcoin/pull/36696/changes/3ffffa071c2eeaa61ad4ff111a8ab53cdbc9cbec) _test: Demonstrate new CTransaction constructor in sigopcount_tests_: Nit: `creationTx2` is unused, also `creationTx3`.
yuvicc (Migrated from github.com) reviewed 2026-12-01 14:29:02 +00:00
yuvicc (Migrated from github.com) commented 2026-12-01 14:29:02 +00:00
Please look above: https://github.com/bitcoin/bitcoin/pull/36696#discussion_r4416488577
optout21 (Migrated from github.com) reviewed 2026-12-01 14:37:05 +00:00
optout21 (Migrated from github.com) commented 2026-12-01 14:37:05 +00:00

Thanks, sorry for the duplication.

Thanks, sorry for the duplication.
purpleKarrot (Migrated from github.com) reviewed 2026-12-01 14:39:47 +00:00
purpleKarrot (Migrated from github.com) commented 2026-12-01 14:39:47 +00:00

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.

`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.
optout21 (Migrated from github.com) reviewed 2026-12-01 15:17:14 +00:00
optout21 (Migrated from github.com) commented 2026-12-01 15:17:14 +00:00

Accepted

Accepted
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36696
No description provided.