Review report

bitcoin/bitcoin: PR #36521

2 major findings · 1 minor finding · 4 suggestions

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

Findings

Link

[major] · std_node still expects validation errors for empty transactions

The standard-node submission of OutputMissing now fails its debug-log assertion, stopping p2p_invalid_tx.py before the remaining transaction and orphan tests.

Location: test/functional/p2p_invalid_tx.py:81 (head).

Details and suggested fix

Only the first submission gives p2p_deserialization_error precedence. std_node still selects std_reject_reason or reject_reason, so OutputMissing expects bad-txns-vout-empty and InputMissing expects bad-txns-vin-empty. Both nodes deserialize tx messages with reject_empty = true, independently of -acceptnonstdtxn, and log Transaction without outputs or Transaction without inputs before reaching validation. send_txs_and_test asserts the requested log string after synchronizing with a ping, so the first template fails. Give the deserialization error precedence for the standard node too: std_reject = template.p2p_deserialization_error or template.std_reject_reason or template.reject_reason.


Link

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

External kernel consumers enumerating a large witness through Items() now perform O(n²) prefix decoding, and the witness C API exposes no linear traversal alternative.

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

Details and suggested fix

Items() dereferences through GetItem(index), which now calls CScriptWitness::operator[]. Each lookup except the last starts at begin(), so enumeration requires about n²/2 CompactSize decodes instead of base's constant-time lookup per item. btck_transaction_create accepts caller-supplied bytes through unbudgeted TX_WITH_WITNESS; the input/output-count limits do not bound the witness element count. A witness with nearly 4M empty elements therefore entails about 8×10¹² iterator steps to enumerate, a static operation count rather than a timing measurement. Add a single-pass C traversal based on CScriptWitness::Iterator and use it to provide linear enumeration in Items(), while documenting indexed-access complexity. No in-tree caller enumerating a large witness was found, so practical exposure depends on external kernel consumers.


Link

[minor] · Witness validation expands stacks before it needs ownership

ToVectors() runs before version and size dispatch, rebuilding every element even on nonexecuting paths and adding avoidable allocations to Taproot key-path verification.

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

Details and suggested fix

ToVectors() reserves one vector object per element and copies every element before checks that base performed through a span. Wrong-count P2WPKH, inactive Taproot, unknown witness versions and P2A do not need an owned execution stack; Taproot key-path verification only needs the signature span. A mined, consensus-valid unknown-version or P2A spend with nearly 4M empty elements can consequently recreate about 96 MB of vector metadata on a 64-bit target. This transient, block-weight-bounded allocation roughly restores the footprint base already retained, reducing the validation-time memory saving without changing acceptance. Perform version, count and commitment checks through witness spans, and construct only the remaining execution elements immediately before ExecuteWitnessScript. Preserve the existing error order and OP_SUCCESS precedence. If the annex becomes a span, explicitly retain its CompactSize length prefix when hashing it, since span serialization writes only the raw bytes.


Optional improvements

Link

[suggestion] · Stricter decoding and P2P handling lack a release note

Already-invalid transactions with impossible input or output counts now fail RPC decoding, and some invalid P2P messages are ignored before validation, but neither behavior change has a release note.

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

Details and suggested fix

The count limits apply regardless of serialization parameters: transactions claiming more than 24390 inputs or 111111 outputs now fail decoding. For example, mempool_accept.py now expects -22 'TX decode failed' rather than a validation reject reason. Separately, P2P transactions without inputs or outputs, and messages whose transactions exceed the weight budget, fail deserialization before the former validation path. This changes transaction rejection-filter handling and can avoid peer disconnection for invalid blocks. Add a PR-specific release note covering both changes, making clear that the affected transactions were already consensus-invalid.


Link

[suggestion] · Deserialization benchmarks omit P2P weight accounting

The new benchmarks do not measure the per-input, per-output and per-witness accounting performed by P2P block deserialization.

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

Details and suggested fix

TX_WITH_WITNESS leaves weight_left null, so the timed loop skips the conditional GetSerializeSize calls and budget updates used by TxWithWitnessP2P(weight_left). Add a budgeted variant using the same synthetic blocks, resetting weight_left to MAX_BLOCK_WEIGHT - WITNESS_SCALE_FACTOR * (80 + 1) inside every timed iteration. This would measure the P2P path, particularly for shapes with many inputs, outputs or transactions. Keep the existing runs as the unrestricted disk/RPC baseline; the source alone does not establish how much the accounting costs.


Link

[suggestion] · Compact-message budget test has no witness-bearing transaction

WeightLimitedDeserializationTest uses only witness-free transactions, leaving witness charging and witness-parameter propagation through the compact-message wrappers untested.

Location: src/test/blockencodings_tests.cpp:464 (head).

Details and suggested fix

BuildBlockTestCase derives all three transactions from BuildTransactionTestCase, which never sets a witness. Consequently, the exact-budget, byte-identical round-trip and one-WU-short assertions do not depend on extended serialization or witness weight. Both wrappers now inherit transaction parameters from the stream, so parsing them with witnesses disabled, or omitting witness charging, would still satisfy this fixture. Give at least one transaction a non-empty CScriptWitness before computing txs_weight and constructing the two containers. The existing assertions would then cover those wrapper-specific regressions as well.


Link

[suggestion] · Intermediate-only helpers remain in the final API

SpanReader::Remaining() and CScriptWitness::ToSpans() have no remaining production callers after later commits replace their intended uses.

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

Details and suggested fix

Witness element parsing now uses DecodeCompactSize, and policy checks iterate directly over the witness. Only streams_span_reader_remaining calls SpanReader::Remaining(), and only script_witness_representation calls ToSpans(); the HTTP server's similarly named calls are on LineReader. Remove both unused helpers and their dedicated test assertions from the final series unless a concrete caller needs them. Reorder or squash the intermediate commits as necessary so each commit still builds.


Coverage details

Earlier concerns

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