Review report

core: PR #36696

1 minor finding · 4 suggestions

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive for their assessments.

Findings

Link

[minor] · ToVector moves from elements of temporary views and transfers nested buffers

A temporary view over mutable inputs is moved through element references, emptying scripts and witnesses; moving owning inputs can also leave aliases to transaction data, though no current caller is affected.

Location: src/util/vector.h:31 (head).

Details and suggested fix

ToVector moves whenever R is not an lvalue reference. A temporary view over a mutable container, such as std::span{inputs}, std::views::all(inputs), or inputs | std::views::take(n), is an rvalue whose elements are CTxIn&, so it enters the move branch and moves from inputs. The prevout and sequence remain, but the scriptSig and witness stack are moved out. Outputs passed this way lose their scriptPubKey, so reusing them can silently produce a different transaction. This also differs from the std::ranges::to<std::vector> named in the comment, which copies from range references.

For owning rvalue ranges, moving a CTxIn can transfer its witness buffer and any scriptSig buffer longer than prevector's 36-byte inline capacity. A reference taken into those buffers before construction can therefore still refer to data owned by the transaction. Writing through such a reference would leave the cached wtxid stale.

Every checked-in caller passes prvalue std::arrays, so no current call site is affected. Copy unconditionally with std::ranges::copy to avoid both effects and match std::ranges::to. Excluding views from the move branch addresses only the first.


Optional improvements

Link

[suggestion] · The range constructor has no direct tests for hashes, witnesses, or copy and move behavior

Without direct coverage, a regression in the constructor could change txid, wtxid, or HasWitness() without a test detecting it.

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

Details and suggested fix

The constructor's only callers are the benchmark and SetupDummyInputs; both pass prvalue std::arrays without witnesses and use the default version and locktime. Tests that call SetupDummyInputs use GetHash() only to spend those same transactions, so an incorrect txid could remain self-consistent. The new scriptWitnessIn argument has no caller, and ToVector has no test.

Add a transaction_tests case that constructs the same transaction through CMutableTransaction and through CTransaction{ins, outs, ver, lt}. Include a non-empty witness passed through the new CTxIn argument, and non-default version and locktime; compare GetHash(), GetWitnessHash(), and HasWitness(). A ToVector case in util_tests using the existing Tracker type, as test_tracked_vector does, would cover copy and move behavior for lvalue and rvalue ranges. A short constructor comment could also explain that elements are placed in transaction-owned storage rather than adopting the caller's container.


Link

[suggestion] · util/vector.h relies on transitive includes for ranges and concepts facilities

The helper uses facilities from <ranges> and <concepts> without including them directly, contrary to the developer notes; the IWYU CI job does not check this header.

Location: src/util/vector.h:20 (head).

Details and suggested fix

RangeOf and ToVector use std::ranges::input_range, std::ranges::range_reference_t, and std::ranges::sized_range, declared by <ranges>, as well as std::convertible_to, declared by <concepts>. The header adds only <algorithm> and <iterator>. Current standard libraries likely expose these names transitively, so no build failure is established. However, doc/developer-notes.md requires each header to include what it uses directly. The IWYU CI job enforces this only for .cpp files, and util/vector.h has no .cpp. Add #include <concepts> and #include <ranges>. primitives/transaction.h now includes this header, so most translation units would then include <ranges>.


Link

[suggestion] · SetupDummyInputs copies transactions unnecessarily before AddCoins

The CTransaction(...) wrapper used to convert from CMutableTransaction; now it performs a redundant copy.

Location: src/test/util/transaction_utils.cpp:68 (head).

Details and suggested fix

dummyTransactions now holds CTransactions, and AddCoins takes a const CTransaction&. Thus CTransaction(dummyTransactions[0]) calls the implicit copy constructor, copying vin, vout, and the cached hashes into a temporary that is discarded after the call. Behavior does not change. Pass dummyTransactions[0] and dummyTransactions[1] directly.


Link

[suggestion] · CTxIn copies witness data when given an rvalue

The new parameter is const CScriptWitness&, so a moved or helper-returned witness is copied item by item; no caller currently passes a witness.

Location: src/primitives/transaction.cpp:26 (head).

Details and suggested fix

Both CTxIn constructors take const CScriptWitness& and assign scriptWitnessIn to scriptWitness. Passing std::move(witness) or a witness returned from a helper therefore copies the outer vector and every stack item. CScriptWitness has only a defaulted default constructor, so callers must build a named witness or call a helper first, making an rvalue a natural way to pass it. Take the witness by value and move it into the member. Lvalue callers would still pay one copy, while rvalue callers would avoid copying. scriptSigIn is also taken by value but copy-assigned, so moving it would help in the same way.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.