Review report

core: PR #36696

1 minor finding · 3 suggestions

Findings

Link

[minor] · ToVector moves elements from caller-owned containers through temporary views

Passing a temporary view over mutable inputs to the CTransaction range constructor moves from those inputs, although no current caller uses this pattern.

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

Details and suggested fix

ToVector chooses whether to copy or move based on whether the range argument is an lvalue. A temporary non-owning view over a mutable container is an rvalue, so the helper moves from the view's elements even though it does not own them. For example, passing std::span{inputs} moves from the caller's CTxIns: their scriptSig and witness become empty, while prevout and nSequence remain. Passing a view of outputs similarly empties their scriptPubKey. Reusing those objects to construct another transaction can therefore produce different contents. This also differs from the std::ranges::to<std::vector> behavior described in the comment, which copies from the range references in these cases. The only current caller passes prvalue std::arrays that own their elements, so no existing call site is affected. Move elements only when the rvalue range owns them, for example by excluding ranges that are views, and update the comment to describe the actual semantics.


Optional improvements

Link

[suggestion] · The range constructor's ownership behavior is undocumented and untested

The constructor's element-wise copy-or-move behavior is not documented or tested, so a future change could adopt a caller's container and undermine transaction immutability.

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

Details and suggested fix

The constructor creates fresh vectors through ToVector rather than adopting the caller's container. This prevents a retained CTxIn& from changing the transaction after construction and invalidating its cached txid or wtxid, but the code does not document that behavior. The only caller is the benchmark, which passes prvalue std::arrays with empty witnesses and default version and locktime, and checks only ValidateInputsStandardness. Add ToVector tests for lvalue and rvalue ranges, similar to the copy and move count checks in test_tracked_vector. A transaction test should also cover a non-empty witness and non-default version and locktime, and confirm that modifying a retained reference into an rvalue vector does not change the transaction. Document that elements are copied from lvalue ranges or moved from rvalue ranges into transaction-owned storage; limit this claim to element references, since moving an element can transfer its heap storage, such as witness stack items.


Link

[suggestion] · util/vector.h omits direct includes for range and concept facilities

The header relies on transitive includes for <ranges> and <concepts>; this has not been shown to break a build, and the current 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 facilities declared in <ranges> and std::convertible_to declared in <concepts>, but the header includes neither. Current standard libraries likely expose these declarations through <algorithm>, so no build failure has been established. However, the developer notes require headers to include what they use directly, and the IWYU CI job does not cover util/vector.h, which has no .cpp file. Add #include <concepts> and #include <ranges>. Since primitives/transaction.h includes this header, <ranges> will then be included by most translation units.


Link

[suggestion] · CTxIn copies witnesses even when callers pass rvalues

A moved or helper-returned witness is copied item by item by the CTxIn constructors; the cost falls mainly on test code, and no caller in this change passes a witness yet.

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

Details and suggested fix

Both CTxIn constructors take CScriptWitness by const reference and copy-assign it. Passing std::move(witness) or a witness returned from a helper therefore copies the outer vector and every stack item. Because CScriptWitness has no non-default constructor, callers must build a named witness or use a helper before passing it, making an rvalue a natural way to transfer it. Take the witness by value and move it into the member, so lvalue callers still pay one copy while rvalue callers pay none. Apply the same move assignment to scriptSigIn, which is already passed by value but currently copy-assigned.


Coverage details