cmpctblock: Cache serialized cmpctblock msg for relay [part of prefill series] #36787

Open
davidgumberg wants to merge 4 commits from 2026-11-5-cache-serialized-cmpctblock-msg into master AGit
davidgumberg commented 2026-11-06 01:36:13 +00:00 (Migrated from github.com)

This PR extends the optimization added in https://github.com/bitcoin/bitcoin/pull/23880 which applies to peers that have requested HB blocks from us to also cache the serialized blocks for low-bandwidth peers. The core change here comes from the closed #26755 made by andrewtoth, but also includes other refactors and type safety improvements.

Performance is less critical for low-bandwidth peers, so this change on its own should have little impact on block propagation, but these refactors are useful as part of https://github.com/bitcoin/bitcoin/pull/35558 which adds prefilling of compact blocks, and where we run the risk of potentially having to serialize more than one cmpctblock_msg (one prefilled and one not-prefilled).

This PR extends the optimization added in https://github.com/bitcoin/bitcoin/pull/23880 which applies to peers that have requested HB blocks from us to also cache the serialized blocks for low-bandwidth peers. The core change here comes from the closed #26755 made by andrewtoth, but also includes other refactors and type safety improvements. Performance is less critical for low-bandwidth peers, so this change on its own should have little impact on block propagation, but these refactors are useful as part of https://github.com/bitcoin/bitcoin/pull/35558 which adds prefilling of compact blocks, and where we run the risk of potentially having to serialize more than one cmpctblock_msg (one prefilled and one not-prefilled).
DrahtBot commented 2026-11-06 01:36:20 +00:00 (Migrated from github.com)

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.

Type Reviewers
Concept ACK w0xlt

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:

  • #36955 (p2p: Use separate compact block nonce per network by mzumsande)
  • #36790 (p2p: ignore historical blocks when counting blocks in flight by sashaphmn)
  • #36372 (p2p: replace full outbound peers that don't support tx relay (take 2) by fjahr)
  • #35561 (net: move some CNodeState fields to Peer by Crypt-iQ)
  • #35522 (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc)
  • #35502 (refactor: extract per-message helpers from ProcessMessage (move-only) by w0xlt)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36787 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36787.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36787#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/36787#issuecomment-6806506058) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36955](https://github.com/bitcoin/bitcoin/pull/36955) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36955.svg"></sub> (p2p: Use separate compact block nonce per network by mzumsande) * [#36790](https://github.com/bitcoin/bitcoin/pull/36790) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36790.svg"></sub> (p2p: ignore historical blocks when counting blocks in flight by sashaphmn) * [#36372](https://github.com/bitcoin/bitcoin/pull/36372) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36372.svg"></sub> (p2p: replace full outbound peers that don't support tx relay (take 2) by fjahr) * [#35561](https://github.com/bitcoin/bitcoin/pull/35561) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35561.svg"></sub> (net: move some CNodeState fields to Peer by Crypt-iQ) * [#35522](https://github.com/bitcoin/bitcoin/pull/35522) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35522.svg"></sub> (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc) * [#35502](https://github.com/bitcoin/bitcoin/pull/35502) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35502.svg"></sub> (refactor: extract per-message helpers from ProcessMessage (move-only) by w0xlt) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🟠 Minor · Thread-launch failure interrupts block acceptance before storage

Under local thread-resource exhaustion, the forced async launch can interrupt acceptance of a new tip-extending block before storage, although a retry can recover.

Location: src/net_processing.cpp:2253 (head).

🟠 Minor · Cached getdata replies overwrite header-announcement state

A cached compact-block request can replace the peer's new-branch header state with a stale-branch block, causing an extra inv/getdata round trip when its recorded best-known block does not cover the new branch.

Location: src/net_processing.cpp:2312 (head).

🔵 Suggestion · The fallback comment describes an impossible cache-update window

The new SendMessages comment attributes a cache miss to a block connection after vHeaders assembly, but cs_main prevents the cache from changing during that interval.

Location: src/net_processing.cpp:6269 (head).

Review coverage was incomplete.


Reviewed: d84a57a9f78c · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:fcf76ea549f8b6124ffd52526565cf736033b9be --> <!-- ralph-head:d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8 --> <!-- ralph-case:0aa4e90e45cfe4376723284c325a35cfbe41b5ddbc97c4e13b202d3c84d12d19 --> **🟠 Minor · Thread-launch failure interrupts block acceptance before storage** Under local thread-resource exhaustion, the forced async launch can interrupt acceptance of a new tip-extending block before storage, although a retry can recover. Location: [`src/net_processing.cpp:2253`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8/src/net_processing.cpp#L2253>) (head). **🟠 Minor · Cached getdata replies overwrite header-announcement state** A cached compact-block request can replace the peer's new-branch header state with a stale-branch block, causing an extra inv/getdata round trip when its recorded best-known block does not cover the new branch. Location: [`src/net_processing.cpp:2312`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8/src/net_processing.cpp#L2312>) (head). **🔵 Suggestion · The fallback comment describes an impossible cache-update window** The new SendMessages comment attributes a cache miss to a block connection after vHeaders assembly, but cs_main prevents the cache from changing during that interval. Location: [`src/net_processing.cpp:6269`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8/src/net_processing.cpp#L6269>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d84a57a9f78c`](<https://git.fish.foo/bitcoin/bitcoin/commit/d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8>) · [Full review report](<https://review.fish.foo/traces/36787-d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8-2281-2.html>) · [Previous reports](<https://review.fish.foo/traces/36787/>) [permalink](<https://review.fish.foo/traces/36787-d84a57a9f78cb1a9b10d17e1dbbb74a59ff892a8-2281-2.html>)
maflcko (Migrated from github.com) reviewed 2026-11-06 09:13:27 +00:00
maflcko (Migrated from github.com) commented 2026-11-06 09:13:27 +00:00

for reference, the reason I didn't pick async was that this would create an unnamed/global/unscoped/hidden thread pool. I guess this should be fine, but the commit msg could say that this will introduce a new thread(pool) beside the named threads:

  myprogram    # main
  myprogram    # std::async worker, usually unnamed
  initload ,...    # named TraceThreads/ named Pools
edit: Also ralph says this, while it is likely ai slop, it highlights that this is not a trivial change and reviewers would have to look at the error handling path in this line
🟠 Minor · Thread-launch failure interrupts block acceptance before storage

Under local thread-resource exhaustion, the forced async launch can interrupt acceptance of a new tip-extending block before storage, although a retry can recover.
for reference, the reason I didn't pick async was that this would create an unnamed/global/unscoped/hidden thread pool. I guess this should be fine, but the commit msg could say that this will introduce a new thread(pool) beside the named threads: ``` myprogram # main myprogram # std::async worker, usually unnamed initload ,... # named TraceThreads/ named Pools ``` <details><summary>edit: Also ralph says this, while it is likely ai slop, it highlights that this is not a trivial change and reviewers would have to look at the error handling path in this line</summary> ``` 🟠 Minor · Thread-launch failure interrupts block acceptance before storage Under local thread-resource exhaustion, the forced async launch can interrupt acceptance of a new tip-extending block before storage, although a retry can recover. ``` </details>
maflcko (Migrated from github.com) reviewed 2026-11-06 09:16:22 +00:00
maflcko (Migrated from github.com) commented 2026-11-06 09:16:23 +00:00

nit: While changing the log output, could remove all the fluent/redundant __func__ or reword them to something hard-coded and unique. (Also, drop the trailing newlines, while touching.

nit: While changing the log output, could remove all the fluent/redundant `__func__` or reword them to something hard-coded and unique. (Also, drop the trailing newlines, while touching.
w0xlt commented 2026-11-11 20:31:05 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
andrewtoth (Migrated from github.com) reviewed 2026-11-27 18:34:40 +00:00
andrewtoth (Migrated from github.com) commented 2026-11-27 18:34:40 +00:00

Now that we have ThreadPool, could we instantiate one owned by, say, PeerManager, with a single thread that is then used here instead? That would alleviate the concerns about implicit thread lifecycle handling, and let us explicitly handle errors if we fail to submit the task.

Now that we have `ThreadPool`, could we instantiate one owned by, say, `PeerManager`, with a single thread that is then used here instead? That would alleviate the concerns about implicit thread lifecycle handling, and let us explicitly handle errors if we fail to submit the task.
maflcko (Migrated from github.com) reviewed 2026-11-28 14:09:34 +00:00
maflcko (Migrated from github.com) commented 2026-11-28 14:09:34 +00:00
Looks like https://github.com/bitcoin/bitcoin/pull/36955/changes replaces this anyway
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36787
No description provided.