56fa33eac6 doc: add release note for the reduced memory usage of witnesses (Pieter Wuille)
150002be68 script: store witness stacks as their serialization (Pieter Wuille)
3fca9597f0 span: add SameSpan (Pieter Wuille)
519aa85b6b script: remove CScriptWitness's old interface (Pieter Wuille)
77f8bac8a0 test, bench, fuzz: build witness stacks before constructing a CScriptWitness (Pieter Wuille)
5fc4db08a8 refactor: use CScriptWitness's interface where that needs more than a rename (Pieter Wuille)
68b0ae9d9b policy: iterate over witness stacks directly in IsWitnessStandard (Pieter Wuille)
0f1253eb5a script: build the witness execution stack with ToVectors() (Pieter Wuille)
0e317e2ab5 script: let ExecuteWitnessScript take its initial stack by value (Pieter Wuille)
e294e2812a scripted-diff: replace witnesses as a whole instead of their stack (Pieter Wuille)
ce3c41bc3e scripted-diff: serialize CScriptWitness rather than its stack (Pieter Wuille)
afc3248f6b scripted-diff: read and compare CScriptWitness through its interface (Pieter Wuille)
a2230b147b scripted-diff: use CScriptWitness's size(), empty() and back() (Pieter Wuille)
4be6595998 test: check CScriptWitness against the vector of vectors it represents (Pieter Wuille)
fa34694a43 script: give CScriptWitness an interface that hides its representation (Pieter Wuille)
Pull request description:
This is split off from #36521. This PR focuses on reducing memory usage of transactions in memory, leaving performance improvements for other PRs.
`CScriptWitness` stores a witness stack as a `std::vector<std::vector<unsigned char>>`, which costs a 24-byte vector object and an allocation for every non-empty element. This makes it possible to construct transactions that take a large amount of memory after deserialization.
This PR first introduces encapsulation for `CScriptWitness`, hiding its representation. The last commit then changes the representation to be single `std::unique_ptr<byte[]>` which for non-empty witness stacks stores their serialization prefixed by a small length header. Witnesses can no longer be modified in place; they can only be replaced in their entirety. Access to the individual witness stack elements is done by on-the-fly deserialization instead.
`CScriptWitness` shrinks from 24 to 8 bytes, and `CTxIn` from 112 to 96 bytes. Worst-case allocated size for transactions kept in memory drops from roughly 28 bytes/WU to 1.6 bytes/WU.
This worsens deserialization and validation time, adding some 50-70 ms per block to the worst case for me. A follow-up #36918 with optimizations improves the worst case deserialization time significantly instead. Other minor degradations:
* libbitcoinkernel's witness stack element accessor becomes $\mathcal{O}(n)$ in the index of the witness element; using it to iterate a whole witness may therefore become $\mathcal{O}(n^2)$.
* `CScriptWitness::back()` is $\mathcal{O}(n)$ in the length of the witness stack.
Issue reported by furszy and 0xB10C.
ACKs for top commit:
instagibbs:
reACK 56fa33eac6
l0rinc:
reACK 56fa33eac6
brunoerg:
reACK 56fa33eac6
ajtowns:
ACK 56fa33eac6
w0xlt:
ACK 56fa33eac6
Tree-SHA512: b9786d96fe14641a8029d3cd5fcf440c6b60ee613d6cb0a19c9b9b765b43cdef8497cbd6a9a4dd8c757a621e1605b9966617649d147ad5cc67af9d2a448d44c7