Skip to content

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

Open
allocz wants to merge 2 commits into
masterfrom
checkpoint_no_prevout_disk_lookup
Open

blockchain: skip prevout fetch from disk on cache miss until checkpoint#1
allocz wants to merge 2 commits into
masterfrom
checkpoint_no_prevout_disk_lookup

Conversation

@allocz

@allocz allocz commented Apr 27, 2026

Copy link
Copy Markdown
Owner

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.

@allocz

allocz commented Apr 27, 2026

Copy link
Copy Markdown
Owner Author

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

Comment thread blockchain/utxocache.go Outdated

// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

@kcalvinalvin

Copy link
Copy Markdown

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 May 2, 2026

Copy link
Copy Markdown
Owner Author

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

@allocz allocz force-pushed the checkpoint_no_prevout_disk_lookup branch 5 times, most recently from fd262d0 to 3fc47a5 Compare May 8, 2026 18:00
@allocz allocz force-pushed the checkpoint_no_prevout_disk_lookup branch from 3fc47a5 to 6cccc58 Compare May 19, 2026 19:34
@allocz allocz force-pushed the checkpoint_no_prevout_disk_lookup branch from 6cccc58 to 78663d7 Compare July 12, 2026 14:51
Avoid fetching prevouts from disk on cache misses, and use hardcoded
utxoset hash to assert correct utxoset state. Doing this removes the
previous main bottleneck when executing IBD in machines constrained in
RAM and/or with slow disks.

The assumevalid checkpoints gives us the proof that the old blocks are
in a valid chain and the utxoset hash confirms that we reach the
expected UTXOState and that there's no software or hardware failures
causing a bad UTXOSet state.

The trust assumption is the code, because the UTXOSet hashes are
hardcoded and the full validation can still be performed by executing
btcd with `--nocheckpoints`.

SwiftSync also reduces disk I/O and goes further by enabling
more parallelization, but the current approach does not have the problem
of the distribution of the hint file, as all the data needed to assert
consistency is in the code itself.
@allocz allocz force-pushed the checkpoint_no_prevout_disk_lookup branch from 78663d7 to 1670298 Compare July 12, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants