Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35657. ReviewsSee the guideline and AI policy for information on the review process. ConflictsNo conflicts as of last run. LLM Linter (✨ experimental)Possible typos and grammar issues:
2026-07-04 11:53:50 |
a38f455 to
f03a13a
Compare
Exercise the case where a transaction spends an outpoint that is entirely absent from the coins view. AccessCoin returns a spent placeholder Coin for it, so it must be rejected the same way as an already-spent input: bad-txns-inputs-missingorspent.
f03a13a to
5b02e4b
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
l0rinc
left a comment
There was a problem hiding this comment.
I'm not necessarily against this change; we've argued this for years now, but as noted in the description, this changes invalid block reason reporting.
Not sure how critical that would be (in l0rinc#137 I deliberately tried avoiding that), but ultimately the effect is small, and after #35215 it is expected to be even smaller.
|
Thank you @l0rinc for your comments!!
Sorry, I wasn't aware, I tried to do some research before doing the pr but it looks like I missed something. I am reading the code, profiling and running benchmarks to find things to optimize, but I am not finding that much. I would be glad if you could point me to any issue or problem that has already been discussed but no one is working on :D
Personally, I think the impact is minimal. I would say (correct me if I am wrong) the mismatch is only visible locally, in However im totally open to do any changes or explore a solution to that matter if it is really that relevant! |
Not necessarily, some similar discussion happened a long time ago e.g. #14837
We're working hard to make sure you don't find that much :p
As mentioned, I'm not against it, but I'm personally not prioritizing it (especially while #35295 is still open). |
|
Could turn into draft while CI is red? |
|
Changed to draft because DrahtBot requested it. Based on some previous comments about the behavioral change, I ran an demo on regtest comparing Scenarios
Results
Scenarios 3 and 4 showed the correct/expected error, since each only had a single failure present, no behavior change in either case. |
|
Note regarding the last "Edit" section of the description: I have checked the current version of #35187 (7bb468db884c885db5af5ab7abc6580b416f650e), and it does not touch this part (file |
|
@arejula27, please check if this still makes sense now that #35215 was merged. |
I will try to re-run the IBD focused on that section of the chain, but it may take a while. My hardware is modest and struggles with the validation, it's an external SSD, and sometimes the chain gets corrupted, so I have to start from zero again. That said, I don't expect this PR to show impact in that way. Last time, it only showed gains in the micro-benchmarks. So if this PR needs IBD-level impact to be accepted, I'd say there's a high chance it gets dismissed. |
Every change is a risk, so we need to be convinced to spend time reviewing this thoroughly to be confident that it's safe to do this. Microbenchmarks are usually enough if the change is trivial otherwise, which arguably isn't the case here. |
|
Yes, I totally understand and make sense, will come back with data 🫡
Btw, found that |
There's one way to find out! Though note that we're planning on applying the savings from #35531 there as well (if this one gets in), so a future rewrite is not unlikely. |
|
Closing this. Re-measured on current master, after #35215 made
~10.9%, down from the ~14.6% in the description. The saving is one warm-cache lookup per input, never a read from the backing store, and Based on the feedback in this thread, I don't think this is worth spending more reviewer time on fr the follwoing reasons:
Thanks for the reviews and your time!! |
Consensus::CheckTxInputscurrently makes two UTXO lookups per input: first through aHaveInputs()pre-pass (which internally callsHaveCoin()for every input), and then throughAccessCoin()on the same outpoints in the main validation loop.HaveInputs()has only a single caller (CheckTxInputs), andAccessCoin()already returns a reference to a spent sentinelCoinfor any missing outpoint. This means the presence check can be folded into the existing loop without changing behavior.This PR removes the initial
HaveInputs()pre-pass and instead checkscoin.IsSpent()immediately afterAccessCoin()returns. As a result,CheckTxInputsperforms one lookup per input instead of two (2N → N, where N is the number of inputs) in the validation path shared by bothConnectBlockand mempool acceptance.HaveInputs()itself is not removed. It is still used by the coins-view fuzz harness, although this change leaves it with no callers in the validation hot path. Removing it would be a trivial follow-up.The only observable behavioral difference is which validation error is reported if one input is missing while another input in the same transaction fails for some unrelated reason (for example, coinbase immaturity).
ConnectBlock, all such failures are ultimately reported asBLOCK_CONSENSUS, so the specific failure reason or ordering has no consensus impact.MemPoolAccept::PreChecks), every input has already been checked withm_view.HaveCoin()beforeCheckTxInputs()is called. Missing inputs are handled there as orphan transactions, making the "missing input" path insideCheckTxInputs()unreachable during mempool validation.Benchmark (
bench_bitcoin -filter=CheckTxInputs)The benchmark below is intentionally designed to measure the cost of the redundant lookup itself, rather than end-to-end block validation.
A fresh
CCoinsViewCachewrappingCoinsTipis constructed for each measured batch (coins are added toCoinsTipitself, not to the benchmarked view), so each pass actually crosses the view/base boundary instead of hitting an already-warmed local cache. This closely matches how a fresh per-block view behaves in production.~14.6% faster. This is smaller than a naive 2N → N lookup-count argument might suggest, because both lookups are
FetchCoincalls against the same already-resolved cache entry after the first one resolves it. The second lookup is just a cheap lookup in an already-warm cache, not a second trip to the backing store. The real saving is exactly one redundant cache lookup per input, which is what this benchmark isolates.As a sanity check, I also compared IBD over the same block range before and after this change and observed no measurable difference in synchronization time, which is expected:
CheckTxInputsaccounts for only a small fraction ofConnectBlock, where signature verification dominates.The optimization is therefore real but modest. Its value comes from removing a genuinely redundant lookup without changing consensus behavior, rather than from any meaningful IBD speedup.
Historical context
This pre-pass originally served a real purpose.
Before April 2017,
CheckTxInputs()used a per-txidCCoinscache whereAccessCoins(txid)returned a raw pointer that could beNULL, and a singleCCoinsentry could contain both spent and unspent outputs. In that design,HaveInputs()prevented both null-pointer dereferences and spending an already-spent output within an otherwise live entry.On 2017-04-25, two commits changed this:
0003911326("Introduce new per-txout CCoinsViewCache functions") introducedAccessCoin(), which returns a reference to a sentinel empty coin for missing outpoints.f68cdfe92b("Switch from per-tx to per-txout CCoinsViewCache methods in some places") updatedCheckTxInputs()to use the new API.From that point onward, missing and already-spent outputs were both represented by
coin.IsSpent()on the object returned byAccessCoin(), making theHaveInputs()pre-pass redundant. The validation loop has effectively remained unchanged since then, aside from the later scripted rename fromIsPruned()toIsSpent()(589827975f).This PR simply removes that leftover redundancy.
Edit: :When I opened this PR, I hadn't reviewed PR #35187, which modifies the same lines. While the primary goal of that PR is different, it also avoids the redundant lookup as a side effect.Although that PR was submitted earlier, it is significantly larger and includes more critical and potentially controversial changes (it has already received one concept NACK), so it may take longer to be merged.This PR is smaller, focused on a specific issue, and can be evaluated independently. It also doesn't block or conflict with the other PR, since #35187 can still be merged later on top of these changes without any issues.Not anymore