Repository navigation
refactor: Extract per-message helpers from SendMessages() (move-only) - #35522
pablomartin4btc wants to merge 19 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
I don’t think this PR needs to be draft. Keeping it open is useful for review. If this, #34824, or #35502 lands first, rebasing should be straightforward. |
f69c587 to
06741a6
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
06741a6 to
03c5b2b
Compare
|
-Updates:
|
hodlinator
left a comment
There was a problem hiding this comment.
Concept ACK
Was recently reviewing some code here and felt it would have been nice to be able to refer to smaller parts of SendMessages() by name.
|
Converting to draft. I'll rebase once #36521 gets in. |
These four debug logs in SendMessages() use __func__ to prefix their output. The logging macros already capture the calling function, file, and line automatically via SourceLocation, available on demand with -logsourcelocations, so the manual __func__ prefix is redundant. It is also fragile under refactoring: relying on __func__ for a log line's identity means moving that log into a differently-named helper silently changes the printed output, even though nothing about what is being logged changed. Writing each line so it reads clearly without needing the surrounding function's name avoids that. No other change.
Extracts the periodic relay scheduling logic from the tx-relay section of SendMessages() into a self-contained helper. The helper determines whether it is time to trickle inventory to a peer and clears the pending inventory set if the peer has requested no transaction relay. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the BIP35 mempool-response block from the tx-relay section of SendMessages() into a self-contained helper. The m_send_mempool guard moves inside as an early return, keeping the call site a single line. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the regular tx-inventory relay selection block from the tx-relay section of SendMessages() into a self-contained helper. The helper covers candidate collection, heap-based fee/topology sorting, bloom/feerate filtering, and the m_last_inv_sequence update. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the initial headers-sync kick-off block from SendMessages() into a self-contained helper. The helper handles the fSyncStarted guard, the single-peer-or-close-to-tip condition, the getheaders locator construction, and the timeout initialisation. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the block-announcement section from SendMessages() into a self-contained helper. The helper covers header-relay candidate selection, compact-block sending (with cached-message optimisation), multi-header relay, and the inv fallback path. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the block-inv drain loop from SendMessages() into a self-contained helper. The helper flushes m_blocks_for_inv_relay into the caller-provided vInv vector so downstream tx-relay logic can continue appending to the same batch. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the stall/timeout checks from SendMessages() into a self-contained helper: block-download stall detection (with adaptive timeout backoff), per-block in-flight timeout, ping timeout, and headers-sync timeout. Returns true when the caller should return early, preserving identical semantics, including the manual-peer block-download cooldown. The ping timeout check was added to SendMessages() upstream after this branch's original base; it's included here as part of the same move, taking the now parameter needed for it. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the getdata section from SendMessages() into a self-contained helper. The helper covers block requests (FindNextBlocksToDownload and historical-block ranges), transaction download requests, and the final getdata flush. vGetData is local to the helper; the m_tx_download_mutex acquisition moves inside. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the compact-block/headers send path from MaybeSendBlockAnnouncements() into a self-contained helper. The helper chooses between a compact-block (with cached-message optimisation), a headers message, or sets fRevertToInv when neither format applies. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the inv-fallback path from MaybeSendBlockAnnouncements() into a self-contained helper. The helper inv-announces the chain tip when neither a headers nor a compact-block relay was possible. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the stall-detection block from CheckBlockSyncTimeouts() into a self-contained helper. The helper detects when the block-download window has stalled, pauses block requests to manual peers, and disconnects other peers. It applies adaptive timeout backoff when local bandwidth may be insufficient. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the per-block in-flight timeout check from CheckBlockSyncTimeouts() into a self-contained helper. The helper disconnects a peer when a requested block has been in flight longer than the threshold, compensating for the number of peers with validated downloads. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Co-authored-by: w0xlt <[email protected]>
Extracts the initial headers-sync timeout check from CheckBlockSyncTimeouts() into a self-contained helper. The helper disconnects (or resets sync state for NoBan peers) when initial headers sync has stalled, and resets the timeout once the chain has caught up. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Co-authored-by: w0xlt <[email protected]>
Extracts the block getdata request queuing logic from MaybeSendGetData() into a self-contained helper. The helper queues blocks via FindNextBlocksToDownload and TryDownloadingHistoricalBlocks, and marks a stalling peer when the in-flight queue runs empty. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the transaction getdata request queuing logic from MaybeSendGetData() into a self-contained helper. The helper acquires m_tx_download_mutex, iterates pending tx requests from m_txdownloadman, and flushes intermediate batches when the getdata size limit is reached. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Co-authored-by: w0xlt <[email protected]>
Extracts the sync-peer eligibility computation from the SendMessages() cs_main block into a self-contained helper. The helper initialises m_best_header if needed and returns whether blocks and headers should be synced from this peer based on preferred-download status and current in-flight state. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Extracts the tx-relay trickle and inventory dispatch block from the SendMessages() cs_main block into a self-contained helper. The helper acquires the per-peer tx_inventory_mutex, schedules the trickle interval, and conditionally sends the mempool response and tx inventory. This commit can be reviewed with the git options: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Co-authored-by: w0xlt <[email protected]>
Rename MaybeSendInitialGetheaders() to MaybeSendInitialGetHeaders() to match MaybeSendGetHeaders(), and normalize pointer/reference spacing and single-statement conditionals. Share the transaction relay scheduling guard, combine the bloom-filter and staller checks, use empty() for container checks, and simplify the timeout and sync-selection returns. Preserve evaluation order and lock scopes. Co-authored-by: w0xlt <[email protected]>
4d707f6 to
53e896e
Compare
|
-Updates:
|

PeerManagerImpl::SendMessages()is a ~500-line function handling every p2p conditional send message type inline, which makes it hard to navigate and review. This was reduced to less than 90 lines.I've identified this issue/ possible improvement while reviewing #34824.
-Notes:
First (prepare) commit removes the unnecessary
__func__prefix from the debug logs in SendMessages(), as suggested by maflcko. The logging macros already capture the calling function viaSourceLocation, available on demand with-logsourcelocations.Each of the following 17 commits represents a helper function extraction so it can be easily reviewed, it's only a pure code move, the only new lines are the declaration (with thread-safety annotations), the function signature, and the one-line call site, and they can be reviewed with the git options:
--color-moved=dimmed-zebra --color-moved-ws=ignore-all-space.One final cleanup commit normalises formatting and simplifies some of the extracted helpers, preserving existing control flow and lock scopes.