Motivation
net_processing.cpp is the largest file in the codebase (~6200 lines) and PeerManagerImpl mixes several largely independent subsystems into a single class: transaction relay, address gossip, headers sync, compactblocks, and block download. This makes the file difficult to review, test in isolation, and reason about lock ordering.
#30110 successfully extracted transaction download logic into TxDownloadManager. This PR applies the same approach to block download, continuing the incremental decomposition of PeerManagerImpl.
What this PR does
Extract all block download state and scheduling logic into a new BlockDownloadManager module:
Global state moved (from PeerManagerImpl): mapBlocksInFlight, mapBlockSource, nSyncStarted, m_block_stalling_timeout, m_last_tip_update, m_num_preferred_download_peers, m_peers_downloading_from.
Per-peer state moved (from CNodeState): pindexBestKnownBlock, hashLastUnknownBlock, pindexLastCommonBlock, pindexBestHeaderSent, fSyncStarted, vBlocksInFlight, m_downloading_since, m_stalling_since, fPreferredDownload.
Methods moved (from PeerManagerImpl): FindNextBlocksToDownload, TryDownloadingHistoricalBlocks, ProcessBlockAvailability, UpdateBlockAvailability, BlockRequested, RemoveBlockRequest, IsBlockRequested, IsBlockRequestedFromOutbound, TipMayBeStale.
The result:
net_processing.cppshrinks from 6193 to 5751 lines (−442 net)CNodeStateloses 10 fields, retaining only compact block relay and chain sync timeout state- Block download logic becomes unit-testable and fuzz-testable in isolation (8 unit test cases and 1 fuzz target added)
Design
Follows the TxDownloadManager pimpl pattern:
blockdownloadman.h— public interfaceblockdownloadman_impl.h— implementation class with per-peer stateblockdownloadman_impl.cpp— implementation
Unlike TxDownloadManager, no new mutex is introduced. Block download is inherently tied to chain state through CBlockIndex* pointers, so cs_main remains the synchronizing lock. The extraction is for code organization and testability, not lock granularity.
A fuzz target is included to exercise the extracted manager directly, covering peer lifecycle, availability updates, in-flight request tracking, scheduling, stalling state, and counter consistency.
Commits
- Add BlockDownloadManager class with pimpl skeleton: all new module files, no call sites changed
- Add unit tests: exercises the module in isolation
- Migrate call sites: ~150 call sites in
net_processing.cpprewired throughm_blockdownloadman, pure mechanical replacement - Add fuzz target: exercises
BlockDownloadManagerstate transitions and invariants through the new module boundary