Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-11-26 19:34:58 |
| template <class T> | ||
| void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent_outputs, bool force) | ||
| { | ||
| assert(!m_spent_outputs_ready); |
There was a problem hiding this comment.
I'm tending NACK here. Afaict this is just a hardening improvement, but I'm not sure if it really is one. We should never be hitting a code path where a re-initialization is possible, and this seems to relax that requirement. I think rather than messing with Init, we should seriously reconsider whether this data can be initialized at construction.
There was a problem hiding this comment.
I agree that resetting readiness flags inside Init() still leaves a reuse API around and makes the type responsible for defending against misuse after construction (I was hoping someone would object).
I’ll rework this so that PrecomputedTransactionData is always built for one transaction and has no public reinitialization path:
- Call sites that already have the transaction and spent outputs can construct it directly.
- Call sites that need to wait, such as validation before the script execution cache check, can store
std::optional<PrecomputedTransactionData>andemplace()once all required data has been gathered.
This also seems like a natural place to fold in #35663: validation already needs optional txdata storage, so the assumevalid path can leave the per-block txdata vector empty when script checks are skipped, while still resizing it before any queued checks can take pointers into it.
There was a problem hiding this comment.
Pushed: PrecomputedTransactionData is no longer default-constructible, public Init() is gone, and each object is built for one transaction.
ConnectBlock() also leaves txdata storage empty when script checks are skipped during assumevalid, so that path avoids the unused vector allocation.
This is indeed a lot nicer, thanks for the push. What do you think?
1279d5d to
586620e
Compare
586620e to
92470f7
Compare
92470f7 to
a5ac29b
Compare
|
Rebased after recent merges, ready for review again. I understood @theuni has a similar attempt before, if it's published somewhere, please let me know and I'll add him as a coauthor. |
sedited
left a comment
There was a problem hiding this comment.
ACK on the changes
I find the title a bit confusing. Which staleness are we exactly preventing with this change?
| private: | ||
| template <class T> | ||
| explicit PrecomputedTransactionData(const T& tx); | ||
| void Init(const T& tx, std::vector<CTxOut>&& spent_outputs, bool force); |
There was a problem hiding this comment.
Why not remove it entirely? The diff from doing so is small.
There was a problem hiding this comment.
Nice, I thought that should be a follow-up, but it was actually trivial. I removed Init() entirely and moved its body into the constructor - beautiful!
a5ac29b to
e7e202b
Compare
l0rinc
left a comment
There was a problem hiding this comment.
Updated the stack to remove the public Init() helper entirely and added a leading kernel API fix that avoids copying caller-supplied txdata on each verification call (had it in my backlog for a while, I could also push it separately if reviewers want).
@theuni, this touches the txdata lifetime area involved in the script-check issue you found , would you mind reviewing the approach and letting me know if it has your blessing?
| private: | ||
| template <class T> | ||
| explicit PrecomputedTransactionData(const T& tx); | ||
| void Init(const T& tx, std::vector<CTxOut>&& spent_outputs, bool force); |
There was a problem hiding this comment.
Nice, I thought that should be a follow-up, but it was actually trivial. I removed Init() entirely and moved its body into the constructor - beautiful!
e7e202b to
35a8964
Compare
|
tACK 35a8964 On which staleness this prevents: on the base (7988380), I compared 5961 BIP143 and BIP341 key path sighashes from a library I maintain with The kernel commit stops |
35a8964 to
245711d
Compare
|
Simple rebase after #29843 |
In the experimental kernel C API, the conditional expression mixes the caller-supplied lvalue with a fallback prvalue. Its result is a prvalue that copies the supplied PrecomputedTransactionData on every verification call with supplied data. Reproducer: https://godbolt.org/z/PzcKGW9da (GCC 15.2: one copy before, none after). Keep optional storage only for the fallback and bind directly to emplace(). Both conditional arms are now lvalues, so a supplied object is reused while the fallback retains the same lifetime.
Extend the transaction-only constructor to accept spent outputs and a `force` flag, then use it where those inputs are already available. This preserves each caller's precomputation behavior while removing default construction from the simple cases. The PSBT caller discards partial spent outputs when any input UTXO is missing, preserving its existing all-or-none precomputation. Cover forced and witness-detected constructor readiness before removing the initialization API.
SignTransaction() can provide spent outputs only when every input coin is available, so discard a partial collection when one is missing and construct txdata once with forced precomputation. The kernel creation API likewise gathers spent outputs before constructing and releasing its handle. The signature-cache fuzz checker does not read txdata, so its empty-transaction construction avoids unused hashing.
Script-cache hits return before validation gathers spent outputs or precomputes transaction data. Store txdata in `std::optional` and emplace it only after a cache miss to preserve that behavior. `ConnectBlock()` can skip script checks for blocks in the assumevalid period, so size its txdata storage only when checks can run.
Remove default construction and Init() so transaction-specific data is computed in the constructor. Make the earlier compile-time constructor check reject default construction. Existing callers already construct fresh data for each transaction, so validation and signing results are unchanged.
245711d to
241f55d
Compare

Problem:
PrecomputedTransactionDatacould be default-constructed and filled later withInit().That allowed an object to exist without transaction-specific data and left a public reuse path that was easy to get wrong.
The old
Init()guard only checkedm_spent_outputs_ready, which remained false after initialization without spent outputs even when the BIP143 prevout, sequence, and output hashes were already cached.No current caller reuses an object across transactions, so validation and signing results are unchanged.
btck_script_pubkey_verify()in the experimental kernel C API also copied caller-supplied txdata on each verification call.Fix: Remove default construction and
Init()so eachPrecomputedTransactionDataobject is built for one transaction.Callers that already have the transaction and optional spent outputs construct it directly.
Validation stores txdata in
std::optionaland emplaces it only after a script-cache miss, keeping precomputation lazy.ConnectBlock()also leaves txdata storage empty when assumevalid skips script checks, covering #35663.The kernel verification path binds directly to caller-supplied txdata and constructs a local fallback only when needed.