1 minor finding · 4 suggestions
Findings
[minor] · Items() retains an undocumented quadratic traversal cost
Items() now takes Θ(n²) lookup work to traverse n witness elements; only test callers were found in-tree, so the impact on external consumers is unconfirmed.
Location: src/kernel/bitcoinkernel_wrapper.h:679 (head).
Details and suggested fix
Items() still dereferences through GetItem(i), which calls btck_witness_stack_get_item_at. That now uses CScriptWitness::operator[], restarting at begin() for every index, whereas the base indexed a vector. Adding IterateItems does not change existing range-for loops over Items(). Large witnesses make this consequential: unknown-version spends can contain many tiny elements in a consensus-valid block within its weight limit, despite standard mempool policy rejecting them. Typical two-to-four-item witnesses add little work. At minimum, document the O(index) lookup cost on btck_witness_stack_get_item_at and GetItem, and the O(n²) traversal cost on Items(), directing full scans to IterateItems.
Optional improvements
[suggestion] · VerifyWitnessProgram materializes witnesses on nonexecuting paths
VerifyWitnessProgram now allocates and copies every witness element before dispatch, including uncached Taproot key-path and unknown-version checks that need no execution stack.
Location: src/script/interpreter.cpp:1929 (head).
Details and suggested fix
ToVectors() reserves an outer vector and copies every payload before any version or shape check. Base used a non-owning span and copied only the remaining elements when calling ExecuteWitnessScript. Even a one-signature Taproot key-path spend therefore gains an outer-vector allocation and a signature copy. Unknown-version spends incur a full expansion before returning success under block flags, which omit SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM. This transient work is bounded by block weight, and deserialization already expands through vectors; it is not evidence of higher overall peak memory or a script-acceptance change.
Perform the version and shape checks and read-only annex/control/script preprocessing before materializing only the execution elements. Preserve the annex's CompactSize length prefix when hashing spans, the Tapscript budget based on GetSerializeSize(witness), and OP_SUCCESSx processing before execution-stack limits.
[suggestion] · CScriptWitness construction can violate its noexcept decoding invariant
CScriptWitness accepts vector elements above MAX_SIZE that later terminate in a noexcept accessor; no current caller producing such an oversized vector was found.
Location: src/script/script.cpp:312 (head).
Details and suggested fix
Assuming allocation succeeds, a single MAX_SIZE + 1 byte element fits Allocate's 32-bit total-size guard, and WriteCompactSize serializes it without a range check. Calling front() then reaches default-range-checked ReadCompactSize in Iterator::operator*, so the thrown std::ios_base::failure causes std::terminate. Pass range_check=false to the internal reads of this self-produced canonical serialization, while leaving external vector deserialization range-checked. Add a direct constructor boundary test: the new fuzz target starts with deserialization, and the representation unit test uses element sizes no larger than 65536 bytes.
[suggestion] · Kernel iteration tests miss exact callback-status forwarding
The new kernel tests would still pass if btck_witness_stack_iterate_items replaced a callback's status with a generic nonzero value, leaving its exact-return contract untested.
Location: src/test/kernel/test_kernel.cpp:591 (head).
Details and suggested fix
The C implementation correctly returns the callback's ret, but IterateItems only passes 0 or -1 through its bridge and checks != 0 before rethrowing. The exception test stops on the first item and checks the exception type and one call, so it cannot distinguish exact status propagation from normalization. Using the existing four-item P2WSH fixture, call the C function directly with a callback returning 42 on the third item. Assert return value 42 and exactly three visits. Keep the exception test, which covers the separate C++ exception-transport contract.
[suggestion] · Mempool witness-memory changes lack an operator-facing note
Serialized witness storage can reduce getmempoolinfo's usage and change eviction at a fixed -maxmempool, an operator-visible effect worth a release note; savings depend on witness composition and allocator estimates.
Location: src/core_memusage.h:22 (head).
Details and suggested fix
RecursiveDynamicUsage now charges one header-plus-serialization allocation instead of the outer witness vector and every element allocation. The per-entry estimate flows through cachedInnerUsage into CTxMemPool::DynamicMemoryUsage(), which the RPC reports and TrimToSize compares against the configured limit. Add a short release note explaining that witness-heavy mempools can occupy less of -maxmempool and retain more transactions before size-based eviction. This belongs with the representation change in #36895 rather than only with the kernel iteration API.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The kernel witness accessor was motivated by Silent Payments scanning: callers needed public keys from witness and scriptSig without independently parsing an entire raw transaction. A separate opaque WitnessStack type was adopted for logical layering and extensibility, including possible future annex access, with non-owning views and explicit owned-copy lifecycle operations.
The historical goal was usable, representation-independent access to input data, not exposure of the underlying vector. Serialized storage is consistent with that abstraction boundary. Indexed access and traversal still need to distinguish a borrowed witness handle from an independently owned copy.