← Back to Pull requests

blockchain: skip prevout fetch from disk on cache miss until checkpoint #1

Open
allocz opened this pull request with 478 lines diff across 8 files
View on GitHub
allocz allocz commented on Apr 27, 2026 11:19 UTC
Author

Motivation: In devices where the utxoset does not fit in ram, there's a lot of cache misses when performing IBD, this adds latency. This PR skips the fetch of prevouts until the last checkpoint block, significantly reducing IBD elapsed time by avoiding the disk lookups during IBD.

Trust assumptions: is assumed that all blocks until the checkpoint are valid, with this premise being truth, there's no risk of double spends or other consensus violations.

The full verification can still be enabled by passing the --nocheckpoints flag.

utxocache: 300MB
cpus: 4

  • disk: HDD
  • |implementation | blockStart | blockEnd | NsPerBlock | read | write |
    |------------------|------------|----------|-------------|--------------|--------------|
    |btcd_master | 944002 | 946000 | 20128628628 | 627172324352 | 58139002629 |
    |btcd_skip_prevout | 944002 | 946003 | 290854572 | 7701434880 | 9991956390 |
    |bitcoin_core_v31 | 944003 | 946000 | 16275913870 | 560042284032 | 118381513232 |

    As we can see, skip_prevout patch improved block processing speed in 68x,
    with 1.22% of total disk reads and 17.18% of total disk writes when compared to
    btcd_master.

    We also can see that btcd_master takes 23% more time to process each block
    when compared with bitcoin_core_v31, and btcd_skip_prevout takes 1.78% of
    the time that bitcoin_core_v31 takes per block.

    In summary, the benchmark started with btcd being 23% slower than Bitcoin Core,
    and now, btcd is 5495% faster than Bitcoin Core.

    The results suggest that in resource constrained computers, skipping the prevout
    check while processing assumed valid blocks does improve total IBD speed.

    UTXOSet hash was added to provide sanity check that in the end we have the expected utxoset.

    Diff: 478 lines (+429 / -49) across 8 files Reviewer: kcalvinalvin Review duration: 286255m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.57) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 478 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 4 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    87%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Real PR Conversation Timeline
    5 items
    allocz allocz commented on Apr 27, 2026 11:20 UTC
    Author

    This is a first draft just to validate the concept, the added metrics package will be removed later in a rebase.

    kcalvinalvin kcalvinalvin commented on code on May 02, 2026 05:17 UTC
    Code Review
    blockchain/utxocache.go:1
    @@ -287,20 +300,46 @@ func (s *utxoCache) fetchEntries(outpoints []wire.OutPoint) ([]*UtxoEntry, error
     		missingOpsIdx = append(missingOpsIdx, i)
     		missingOps = append(missingOps, outpoints[i])
     	}
    +	ta(len(outpoints) - len(missingOps))
     
    -	// Return early and don't attempt access the database if we don't have any
    -	// missing outpoints.
    +	// Return early and don't attempt access the database if we don't have 

    You've got a trailing whitespace here. Worth setting up something in your editor to automatically remove it

    kcalvinalvin kcalvinalvin commented on May 02, 2026 05:24 UTC
    Contributor

    Bit of background:

    For btcd, we'd ideally want to follow want bitcoin core is doing with the assumevalid. Right now we're using checkpoints because when things are not going wrong, it's essentially doing the same checks.

    So right now for btcd, we're also checking the inputs/outputs and that'd make it easier to change over to assumevalid. This change is going in the wrong direction.

    FWIW, I think for database optimizations, implementing swiftsync is a better way going forward.

    allocz allocz commented on May 02, 2026 16:50 UTC
    Author

    Thanks for the review. Do you see any new attack vectors enabled by this approach?

    Reviewers
    Assignees
    allocz allocz
    Labels
    risk: low confidence: 87.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57.2% (MEDIUM) · Higher = larger blast radius
    57.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.2% · Lower = rushed or shallow review
    87.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.3% · Safe to merge
    7.3%
    Repository