Skip to content

validation: avoid preallocating unused txdata during assumevalid - #35663

Closed
l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/validation-skip-txdata-precompute
Closed

l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/validation-skip-txdata-precompute

Conversation

@l0rinc

@l0rinc l0rinc commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem: During IBD/reindex, ConnectBlock() skips script verification for blocks in the assumevalid period.
However, it still performs one heap allocation for a fresh txsdata vector and default-constructs one PrecomputedTransactionData entry for every transaction in each connected block.
Because CheckInputScripts() is not called in that path, that work is unused and only adds allocator churn and avoidable heap fragmentation on large blocks.

Fix: Leave txsdata empty unless fScriptChecks is true, then resize it before queued checks can take pointers into the vector.
Update the lifetime comment and add an indexed-use assert that matches the real precondition.

Reproducer: On a synthetic ~7000-transaction block, the skipped-check path avoids the unused heap allocation and saves ~2MB memory per block.

ConnectBlockAllSchnorr mem usage: 7000 txs, num_schnorr=1, num_ecdsa=0, fScriptChecks=false
for C in 1835f2fcbf482035e1c11c72ae39cee4e24e4c00 832dc594f72bb6e8415674dce434684202073b12; do
  git reset --hard -q && git checkout -q "$C"
  perl -0pi -e 's/(num_txs = )1000/${1}7000/; s@(/\*num_schnorr=\*/)\d+, (/\*num_ecdsa=\*/)\d+@${1}1, ${2}0@; s/(fScriptChecks)\{[^}]+\};/${1}{false};/;' src/bench/connectblock.cpp src/validation.cpp
  cmake -B build -DBUILD_BENCH=ON >/dev/null 2>&1 && cmake --build build --target bench_bitcoin >/dev/null 2>&1
  /usr/bin/time -v build/bin/bench_bitcoin -filter=ConnectBlockAllSchnorr -sanity-check 2>&1 >/dev/null | grep 'Maximum resident'
done

1835f2f ... Maximum resident set size: 58704 kB
832dc594f7 ... Maximum resident set size: 56664 kB

During IBD and reindex, `ConnectBlock()` skips script verification for blocks in the `assumevalid` period, but still performs one heap allocation for a fresh `txsdata` vector and default-constructs one `PrecomputedTransactionData` entry for every transaction in each connected block.
Because `CheckInputScripts()` is not called in that path, that work is unused and only adds allocator churn and avoidable heap fragmentation on large blocks.

Leave `txsdata` empty unless `fScriptChecks` is true, then resize it before queued checks can take pointers into the vector.
Keep `txsdata` declared before `control` so queued script checks finish before the storage is destroyed.
@DrahtBot

DrahtBot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35663.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32575 (consensus: Remove special treatment for single threaded script checking by fjahr)

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.

@darosior darosior left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the big picture benefit of this change? On its face it seems unnecessarily risky especially as we just had a high-severity vulnerability related to this very data structure that none of all our eyeballs noticed for a decade.

@l0rinc

l0rinc commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

We're in agreement: while this is a tiny change with a modest effect, we have to be absolutely sure the risk is low.
I would argue that this may be exactly the right time to touch it, while the behavior is still fresh in our heads.

txsdata only needs storage when CheckInputScripts() can be called. When it can be called, the vector must already be fully sized before any pointers into it are taken, and it must outlive control. This PR tries to encode that invariant more directly.

If you think there are specific failure modes or missing coverage around this data structure, pointers would be appreciated. I would rather add coverage now, while we are looking at this area carefully, than leave it alone because it has historically been hard to reason about. It could help us encode our current understanding to make sure we're not afraid of it in the future.

@l0rinc

l0rinc commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #35662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants