Open
Conversation
Change fChecked, m_checked_merkle_root, and m_checked_witness_commitment from mutable bool to mutable std::atomic<bool> with relaxed memory ordering. These are optimization caches, not synchronization primitives. This eliminates the data race that previously required CheckBlock() to run under cs_main. Since std::atomic is not copyable, add explicit copy/move constructors and assignment operators to CBlock. Update all reads to use load(memory_order_relaxed) and all writes to use store(true, memory_order_relaxed). Remove the stale race condition comment from TestBlockValidity.
Now that CBlock's cache flags are atomic, CheckBlock() no longer needs cs_main protection. Call it before acquiring the lock so only AcceptBlock() (which writes to the block index) holds cs_main. This reduces cs_main contention in ProcessNewBlock, allowing the message handler to begin context-free validation while other threads hold the lock.
During IBD, ActivateBestChain() (which calls ConnectBlock for script verification and UTXO updates) blocks the message handler thread, preventing new blocks from being received and stored. Add a background "blkconnect" thread to ChainstateManager that runs ActivateBestChain() in a loop during IBD. ProcessNewBlock now signals this thread instead of calling ActivateBestChain synchronously, allowing the message handler to immediately process the next block. After IBD completes, ProcessNewBlock reverts to synchronous ActivateBestChain with the pblock hint (avoiding disk re-reads). The thread is started during init before ImportBlocks, and cleanly shut down (interrupt + join) before chainstate destruction.
During IBD, the connector thread calls ActivateBestChain() without a pblock hint, forcing ConnectTip to re-read every block from disk even though ProcessNewBlock just had it in memory. This serialize-to-disk then deserialize-from-disk round-trip negates the parallelism benefit of the dedicated connector thread. Add a bounded cache (128 blocks) on ChainstateManager that ProcessNewBlock populates during IBD. ConnectTip checks the cache before falling back to a disk read, eliminating the redundant I/O for blocks that are still in memory.
With the connector thread processing blocks asynchronously during IBD, the message handler can store blocks much faster than they are validated. The existing BLOCK_DOWNLOAD_WINDOW (1024) and MAX_BLOCKS_IN_TRANSIT_PER_PEER (16) limits cause download stalls. During IBD, use BLOCK_DOWNLOAD_WINDOW_IBD (8192) and MAX_BLOCKS_IN_TRANSIT_PER_PEER_IBD (64) to keep the download pipeline full. After IBD completes, the original limits apply. The larger window means ~12GB of blocks stored ahead of validation, acceptable for modern hardware. Pruning is unaffected since blocks ahead of the validated tip cannot be pruned regardless.
During IBD, ProcessNewBlock holds cs_main for the entire AcceptBlock call, including the WriteBlock disk I/O. This serializes the message handler and connector thread on cs_main — they alternate rather than run in parallel. Move the WriteBlock call before the cs_main acquisition on the IBD path. A brief cs_main lock looks up the parent height, then the block is written to disk without holding cs_main. The resulting FlatFilePos is passed to AcceptBlock via the existing dbp parameter, so AcceptBlock only does a fast UpdateBlockInfo under the lock instead of the full disk write. This lets the message handler overlap disk writes with the connector thread's ConnectTip work, turning the pipeline from interleaved into genuinely parallel.
d68c7f0 to
82f5f60
Compare
Benchmark ResultsComparison to nightly master:
|
764baeb to
aaf12e4
Compare
d6be3ec to
1bc1f6c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
connect more blocks, faster