mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 16:23:37 +00:00
WIP: validation, net: Process blocks asynchronously and reduce cs_main contention #36244
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36244
Loading…
Reference in a new issue
No description provided.
Delete branch "validation/async-block-processing"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The shared P2P message thread currently waits for block processing to finish before it can process messages from other peers.
This PR moves block processing to a dedicated worker after the initial checks, following the approach of #18963 and #16324:
cs_main, the chain lock.cs_mainis busy,SendMessages()defers work such as requesting headers, blocks or transactions and announcing new blocks or transactions. This allows the P2P message thread to move on to other peers.A controlled 10,000-block IBD benchmark showed these response-time improvements, measured as the median of four per-run medians:
With four download connections, the median time to download and validate the 10,000 tested blocks during IBD fell by approximately 5%, from 473.6 s to 450.3 s.
The comparison used matching Clang 18 Release builds of baseline
4519933391ddand this branch, mainnet blocks 910,489 - 920,488, one local source node, and-assumevalid=0 -blocksonly=1. Each setup ran four times per build. PINGs ran at 10/s; the block probe requested the same previously validated block after warmup, at most once per second, timing receipt of the full block.These results apply to this controlled IBD workload; they do not establish gains for other services, hardware, or network conditions. Performance outside IBD was not measured.
Tests and test infrastructure account for approximately 67% of the diff. Production code and build integration account for 1,132 changed lines (883 additions and 249 deletions).
I’m opening this PR as a draft primarily to gather feedback on the overall approach.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
-Wunusedby fanquake)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.
func(x, /*named_arg=*/0)in C++, andfunc(x, named_arg=0)in Python):ProcessNewBlock(block, state, true, true)insrc/node/block_template_manager.cppProcessNewBlock(block, state, true, true)insrc/test/util/setup_common.cppProcessNewBlock(block, state, true, true)insrc/test/util/mining.cppProcessNewBlock(block, state, true, true)insrc/test/baseindex_tests.cppProcessNewBlock(block, state, true, true)insrc/test/blockfilter_index_tests.cppProcessNewBlock(block, state, true, true)insrc/test/coinstatsindex_tests.cppProcessNewBlock(block, state, true, true)insrc/test/txindex_tests.cppProcessNewBlock(block, state, true, true)insrc/test/txospenderindex_tests.cppProcessNewBlock(..., true, true)insrc/test/validation_chainstate_tests.cppProcessNewBlock(..., true, true)insrc/test/validation_block_tests.cppProcessNewBlock(..., true, true)insrc/test/validation_tests.cppProcessNewBlock(..., true, true)insrc/test/peerman_tests.cppPossible places where comparison-specific test macros should replace generic comparisons:
BOOST_CHECK_THROW(queue.Start(), std::logic_error);-> considerBOOST_CHECK_EXCEPTION(queue.Start(), std::logic_error, /*predicate on message*/)so the test verifies the specific failure reason, not just the exception type.BOOST_CHECK_THROW(stopped.Start(), std::logic_error);-> considerBOOST_CHECK_EXCEPTION(stopped.Start(), std::logic_error, /*predicate on message*/)to assert the expected error detail.2026-10-03 07:29:49
🐙 This pull request conflicts with the target branch and needs rebase.
The PR description describes IBD speedup. But, wouldn't the point of something like this be that we could process messages while validating at steady-state, so a block that takes a long time to validate does not hold up responding to compact block transaction requests or other messages? And eventually we could also process competing sibling blocks in parallel?
@andrewtoth Yes, keeping other peers responsive during block validation, including at the tip, is the main motivation. I used IBD as a repeatable workload with sustained validation to measure that benefit. The shorter IBD time was a secondary observation; the main measured gains were faster PING/PONG and block-serving responses, as shown in the table.
Your compact-block example fits.
getblocktxnalready has a path that serves missing transactions from the recent block cache without takingcs_main. This PR moves block processing onto a separate worker, allowing that handler to respond to other peers while validation continues. Other handlers that needcs_maincan still wait.Parallel validation of competing sibling blocks would require further work on shared chainstate and synchronization. This PR keeps a single worker processing blocks serially.
I see this separation of networking and block processing, along with fewer unnecessary waits for cs_main, as a starting point for further concurrency improvements.
🐙 This pull request conflicts with the target branch and needs rebase.
Base:
c8e852d3a02a73d473228232148163cdbc8f7bb0Head:
28aa6cc77dc2561f86ddbc16d1b3001bdbd0a96e💡 Suggestion
Bound memory retained by queued blocks (src/validation_queue.cpp:90, head)
Each enqueue retains the full block until a worker processes it, with no global job or byte limit. If admissions outpace processing, blocks from multiple peers can accumulate. The default 200-connection setting and 4,000,000-byte serialized block limit allow roughly 800 MB of block payload, before object overhead; accepted work also survives source disconnection. Consider a global byte or job budget that defers further submissions without blocking the shared message thread or dropping accepted work. This preserves FIFO behavior but trades some download throughput for bounded memory. No queue high-water or peak-memory measurements were supplied, so practical exposure is unquantified.
Review coverage was incomplete. The archaeology review had incomplete evidence. The independent review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete. The build review had incomplete evidence.
Full review report
🐙 This pull request conflicts with the target branch and needs rebase.
Concept ACK
I think it would be great to finally get this rolling. However, I'm not sure if this should be one big pull request. Reviewing the pure validation changes with the net processing changes at once seems a bit much. A good candidate for a first pull request seems to me changing AcceptBlock to write blocks without holding cs_main in the meantime.