Keep witness stacks in serialized form in CScriptWitness

bitcoin/bitcoin: PR #36895

6 suggestions

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.

Optional improvements

Link

[suggestion] · Items() makes kernel witness traversal quadratic

The kernel wrapper's Items() performs quadratic decoding through indexed lookups, with no sequential witness API or complexity warning; no external consumer's traversal pattern was identified.

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

Details and suggested fix

Each dereference calls GetItem(index), then btck_witness_stack_get_item_at, whose CScriptWitness::operator[] restarts at begin() and skips index elements. Traversing n elements therefore adds n(n-1)/2 skips; base indexed the vector directly. Since btck_transaction_create only deserializes, a consumer can inspect an unvalidated transaction containing one million empty witness elements, requiring roughly 500 billion skips for traversal. Add a cursor API backed by CScriptWitness::Iterator and use it for a forward Items() range, retaining compact storage and callback-based byte delivery. At minimum, document the indexed-access and full-traversal costs in bitcoinkernel.h and on Items().


Link

[suggestion] · IsWitnessStandard does avoidable work before cheap checks

In IsWitnessStandard, one-element native Taproot witnesses allocate an unused span vector, while oversized P2WSH witnesses are traversed before the count check; these bounded costs do not change policy results.

Location: src/policy/policy.cpp:328 (head).

Details and suggested fix

ToSpans() reserves and fills a heap vector before the Taproot branch reaches its one-element case, where no policy rules apply; base used a non-allocating std::span. Continue for size() == 1 before converting, since annex detection requires at least two elements. In the P2WSH branch, move the MAX_STANDARD_P2WSH_STACK_ITEMS check before the now-linear back(), then scan the first size() - 1 elements with the witness iterator instead of building a span vector. Standard mempool prechecks bound this work by MAX_STANDARD_TX_WEIGHT; the throughput effect is unmeasured.


Link

[suggestion] · DynamicMemoryUsage() lacks an upper-bound regression check

The witness tests permit per-element overhead, overcounting, and nonzero empty-witness usage, so they do not protect the compact memory-accounting invariant.

Location: src/test/fuzz/script_witness.cpp:91 (head).

Details and suggested fix

The fuzz assertion only requires usage to be at least the serialized size, or at least zero for an empty witness. Restoring per-element storage with accurate accounting would still satisfy it, and script_witness_representation checks no memory values. Add a separate deterministic unit case with 4096 empty elements and 4096 one-byte elements. Check zero usage for an empty witness and memusage::MallocUsage(4 + GetSerializeSize(witness)) for the non-empty cases after construction, deserialization, and copying. Keep these cases outside the exhaustive operator[] loop, which would be quadratic. This tests Core's accounting estimate, which feeds mempool usage, rather than measured allocator usage.


Link

[suggestion] · Vector-constructed witnesses can terminate on access above MAX_SIZE

Vector-constructed witnesses can terminate on access for counts or lengths above MAX_SIZE; no current caller producing those inputs was found.

Location: src/script/script.cpp:306 (head).

Details and suggested fix

The constructor caps total serialization at UINT32_MAX but does not limit element counts or lengths to MAX_SIZE, which is 32 MiB. For example, a single element of MAX_SIZE + 1 bytes can construct successfully, but front() reaches a range-checked ReadCompactSize inside a noexcept iterator and calls std::terminate. An excessive count similarly affects size() and begin(). Base's vector representation remained usable for these in-memory values. Pass range_check=false when parsing the class's own canonical serialization, while retaining the existing range checks during deserialization.


Link

[suggestion] · The representation unit test omits five-byte witness counts

The new representation unit test stops at 254 elements and never exercises size() or begin() with a five-byte count prefix, despite targeting witnesses with many tiny elements.

Location: src/test/script_tests.cpp:1741 (head).

Details and suggested fix

Large element lengths in SIZES exercise iterator length decoding, not the separate count-prefix handling in size() and begin(). Generic CompactSize tests cover the encoding, but not begin()'s removal of that prefix; the fuzz target can reach large counts without guaranteeing these cases. Add isolated witnesses with 65535 and 65536 empty or one-byte elements. Check the count, forward traversal distance, serialization round-trip, and a few selected indices. Do not add these counts to COUNTS, because its exhaustive operator[] loop would become quadratic.


Link

[suggestion] · Lower witness memory use has no release note

Compact witness storage lowers reported mempool usage and increases effective -maxmempool capacity, but the PR adds no release note explaining that operator-visible change.

Location: src/core_memusage.h:22 (head).

Details and suggested fix

RecursiveDynamicUsage(CTxIn) now accounts for one compact witness allocation instead of the outer vector and individual element allocations. This feeds CTxMemPoolEntry::nUsageSize, the getmempoolinfo memory estimate, and memory-based trimming. Add a short release note explaining that the same witness-heavy mempool uses less reported memory and that a fixed -maxmempool can retain more transactions. Describe this as a real storage reduction, not a correction to previously inaccurate accounting.


Coverage details

Earlier concerns

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