Skip to content

Comments

block connection test#179

Open
willcl-ark wants to merge 6 commits intomasterfrom
block-connector
Open

block connection test#179
willcl-ark wants to merge 6 commits intomasterfrom
block-connector

Conversation

@willcl-ark
Copy link

connect more blocks, faster

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.
@github-actions
Copy link

Benchmark Results

Comparison to nightly master:

  • 450 MB: 67 min (nightly: 62 min, 2026-02-11) → -8.1% slower
  • 32000 MB: 52 min (nightly: 47 min, 2026-02-11) → -12.3% slower

View detailed results
View nightly trend chart

@willcl-ark willcl-ark force-pushed the master branch 6 times, most recently from 764baeb to aaf12e4 Compare February 20, 2026 03:28
@willcl-ark willcl-ark force-pushed the master branch 2 times, most recently from d6be3ec to 1bc1f6c Compare February 22, 2026 03:32
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.

1 participant