1 major finding · 1 minor finding · 2 suggestions
Earlier concerns neither republished nor resolved: 1 major finding · 1 minor finding · 2 suggestions. See the Run archive for their assessments.
Findings
[major] · Kernel witness enumeration takes quadratic time
Enumerating an n-item witness through the kernel API now takes about n²/2 element decodes; no checked-in caller does this, so production impact depends on consumers walking untrusted transactions or blocks.
Location: src/kernel/bitcoinkernel.cpp:892 (head).
Details and suggested fix
btck_witness_stack_get_item_at now uses CScriptWitness::operator[], which walks forward from the beginning for each requested index except the last. The C API exposes only a count and indexed accessor, and the C++ Items() range calls that accessor for every item. Enumerating an n-item witness therefore takes about n²/2 CompactSize decodes, whereas the previous vector-backed lookup was constant time.
A consensus-valid block can contain an input with roughly 4 million empty witness elements, such as one spending an unknown witness-version output. btck_transaction_create also accepts such a witness from caller-supplied bytes. Enumerating that witness would require on the order of 10¹³ iterator steps. The change notes that random access is O(n), but provides no linear-time way for kernel API consumers to enumerate items. Add sequential access, such as a callback-based traversal or cursor over the packed witness, and use it for Items(). No checked-in caller enumerates a large witness, so production impact depends on whether kernel consumers walk witness items from untrusted transactions or blocks.
[minor] · Witness verification copies the full stack before deciding whether it is needed
Some verification paths rebuild every witness element as a vector before returning or using only one item; a worst-case valid block would bring validation memory to roughly the previous level, but requires a mined nonstandard spend.
Location: src/script/interpreter.cpp:1933 (head).
Details and suggested fix
VerifyWitnessProgram calls witness.ToVectors() before checking the witness version. This reserves one 24-byte vector object per element and allocates storage for each non-empty element. The copy is unused for unknown witness versions and P2A, P2WPKH witnesses with the wrong item count, and Taproot control blocks with invalid sizes. Taproot key-path spends also copy the whole stack even though they only need front().
A mined block whose input carries millions of tiny witness elements while spending an unknown witness-version output would temporarily rebuild roughly the vector footprint that the packed representation removed from the deserialized block. This brings validation memory to roughly the previous level, not above it. Every Taproot key-path check also makes two small allocations that the previous implementation did not. Dispatch using size(), front(), and back(), and build the owned vector only immediately before each ExecuteWitnessScript call. If the annex becomes a span, keep its CompactSize prefix in m_annex_hash, because serializing a span omits it. Triggering the large case requires getting a nonstandard spend mined; no such block was identified.
Optional improvements
[suggestion] · Deserialization benchmarks omit the weight-budgeted P2P path
The benchmarks never time the per-input, per-output, and per-witness weight charging used for P2P transaction and block messages.
Location: src/bench/block_deserialize.cpp:111 (head).
Details and suggested fix
Every timed run deserializes with TX_WITH_WITNESS, but tx, block, cmpctblock, and blocktxn messages now use TxWithWitnessP2P(weight_left). With a budget set, UnserializeTransaction also calls GetSerializeSize for each input, output, and witness, and rejects empty transactions. None of that work is measured. Add a DeserializeBlock benchmark variant that resets weight_left inside each timed iteration to the same budget net processing uses: MAX_BLOCK_WEIGHT minus the header and a one-byte count. Keep the current runs as the disk/RPC baseline; the synthetic blocks do not need to change.
[suggestion] · New reader and witness APIs have no non-test callers
SpanReader::Remaining() and CScriptWitness::ToSpans() are used only by tests at head, leaving unused API surface; the two Remaining() methods also differ in meaning and return type.
Location: src/streams.h:104 (head).
Details and suggested fix
SpanReader::Remaining() was added in 74b1e69 so CScriptWitness could find its elements through SpanReader. Commit 0871946 switched element lookup to the inline DecodeCompactSize, leaving the streams_span_reader_remaining test as the method's only caller. The Remaining() calls in httpserver.cpp and util/string.cpp belong to LineReader, which returns a size_t; the same method name now has different meanings on the two reader types.
CScriptWitness::ToSpans() likewise lost its only production caller when 8072be3 rewrote IsWitnessStandard to iterate directly; only script_tests calls it now. Remove the Remaining() addition and its test. Either remove ToSpans() or give it a production caller, such as a sequential kernel witness-traversal API.
Earlier concerns
Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.
- [suggestion]: Reuse the witness-item buffer in benchmark setup
- [major]: The witness-size guard is tautological on 32-bit builds
- [minor]: The input-count cutoff admits an impossible boundary count
- [suggestion]: Document the stricter raw-transaction decoding behavior