Review report

bitcoin/bitcoin: PR #36521

1 major finding · 1 minor finding · 3 suggestions

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

Findings

Link

[major] · Kernel witness Items() enumeration becomes quadratic

WitnessStackApi::Items() repeatedly scans packed prefixes, turning complete kernel witness traversal from O(n) into O(n²), with no single-pass witness API for consumers of large stacks.

Location: src/kernel/bitcoinkernel.cpp:875 (head).

Details and suggested fix

Items() dereferences through GetItem(index) and this C accessor. Each non-final lookup now starts at CScriptWitness::begin() and advances to the requested index, whereas base indexed the vector directly. The final-element shortcut leaves approximately n²/2 CompactSize decodes for a complete traversal. btck_transaction_create accepts caller-supplied bytes through unbudgeted TX_WITH_WITNESS, so millions of empty elements are reachable without consensus validation. Even a consensus-valid block can contain nearly four million empty witness elements in a spend of an unknown witness version, giving roughly 8×10¹² decode steps if enumerated this way. This is an operation-count estimate, not a measured runtime. Add a single-pass C API cursor backed by CScriptWitness::Iterator and route Items() through it, preserving compact storage without repeated scans. The only in-tree enumeration is the four-element kernel test; no production caller enumerating a large witness was found, so real-world exposure depends on external consumers.


Link

[minor] · Witness verification expands the whole stack before dispatch

VerifyWitnessProgram rebuilds owned vectors even for paths that never execute a witness script, losing avoidable memory and allocation savings from the packed representation.

Location: src/script/interpreter.cpp:1933 (head).

Details and suggested fix

witness.ToVectors() reserves an outer vector and copies every element before version or size dispatch. Base used a span and acquired ownership only inside ExecuteWitnessScript. The new copy therefore also covers future-version/P2A returns, Taproot key-path or disabled-Taproot paths, and wrong-count P2WPKH or P2WSH hash-mismatch rejections. Block flags do not discourage unknown witness versions, so uncached script validation of a mined, consensus-valid spend can reconstruct nearly four million empty vectors, approximately 96 MB of vector headers on a typical 64-bit build, then discard them. Nonempty elements also require allocations. These are static estimates of transient work comparable to what base retained, not a demonstrated peak-memory regression. Standard mempool policy applies the smaller 400k WU limit and additional P2WSH/annex checks. Perform dispatch and read-only checks using size(), back() and element spans, adapting VerifyTaprootCommitment to spans, and materialize only the remaining execution stack before ExecuteWitnessScript. Preserve the existing error order, OP_SUCCESS precedence over stack limits, and the CompactSize prefix when hashing an annex span.


Optional improvements

Link

[suggestion] · Weight-budget tests miss pre-allocation rejection

The new unit and fuzz checks can still pass if minimum-weight charges move after input/output allocation and reading, leaving the early-rejection property unprotected.

Location: src/test/transaction_tests.cpp:1568 (head).

Details and suggested fix

UnserializeTransaction correctly charges counts and minimum weights before resizing the vectors. The unit and compact-block budget checks serialize complete objects, while the fuzz budget loop runs only after normal parsing succeeds. The truncated count tests use no budget, so they protect a different guard. Add prefix-only cases with counts below the unconditional limits but above the remaining budget. A version followed by an input count of two requires 32 + 4 + 2×164 = 364 WU, so a 363-WU budget must produce Weight limit exceeded rather than end-of-data. Assert that the fresh destination's input vector remains empty, and add the corresponding output-count case after one minimal input.


Link

[suggestion] · Benchmarks skip weight-budgeted P2P deserialization

The synthetic-block benchmarks use only TX_WITH_WITNESS, so they do not time the additional per-input, per-output and per-witness weight accounting performed for incoming block messages.

Location: src/bench/block_deserialize.cpp:111 (head).

Details and suggested fix

Both the setup read and timed loop leave weight_left null, skipping the conditional GetSerializeSize charges. The incoming block handler instead uses TxWithWitnessP2P(weight_left). Add a separately labeled P2P variant over the same shapes, resetting a budget equivalent to MAX_BLOCK_TXS_WEIGHT inside every iteration. These fixtures have nonempty input/output vectors and fit that budget. Keep the existing run as the disk/RPC baseline. This would measure the additional path; no runtime regression is asserted.


Link

[suggestion] · SpanReader::Remaining() is left without a production caller

The final witness iterator no longer needs this newly added accessor, leaving only its dedicated unit-test caller.

Location: src/streams.h:104 (head).

Details and suggested fix

The intermediate witness iterator used SpanReader::Remaining(), but the final implementation decodes a span directly with DecodeCompactSize. At head, only streams_span_reader_remaining calls this accessor; the similarly named HTTP calls belong to LineReader. Remove the unused method and its dedicated test from the series, adjusting the intermediate commits so they still build, unless a concrete follow-up needs the API.


Coverage details

Earlier concerns

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