Skip to content

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

Open
davidgumberg wants to merge 4 commits into
bitcoin:masterfrom
davidgumberg:2026-11-5-cache-serialized-cmpctblock-msg
Open

davidgumberg wants to merge 4 commits into
bitcoin:masterfrom
davidgumberg:2026-11-5-cache-serialized-cmpctblock-msg

Conversation

@davidgumberg

Copy link
Copy Markdown
Contributor

This PR extends the optimization added in #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 #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

DrahtBot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

davidgumberg and others added 3 commits November 5, 2026 17:45
This commit is drawn from the closed bitcoin#26755.
(bitcoin#26755)

This also changes `std::launch::deferred` to `std::launch::async` since
we are likely going to use this result very soon if someone has
requested HB blocks from us, and if no one has, then performance here is
not that critical.

Co-authored-by: Andrew Toth <[email protected]>
Also one non-refactor change which is setting the `pIndexBestHeaderSent`
on the `SendMessages` CMPCTBLOCK announcement fallback.
Noticed while making changes to compact blocks code, right now
`PeerHasHeader` relies on callers to never make the mistake of calling
it with a nullptr, `state` can be trivially made into a reference, and
we can add an Assume for pindex being nullptr, and fall back to returning
false.
@davidgumberg
davidgumberg force-pushed the 2026-11-5-cache-serialized-cmpctblock-msg branch from 9285ade to d84a57a Compare November 6, 2026 01:45
@davidgumberg davidgumberg changed the title cmpctblock: Cache serialized cmpctblock msg for relay cmpctblock: Cache serialized cmpctblock msg for relay [part of prefill series] Nov 6, 2026
@DrahtBot DrahtBot removed the CI failed label Nov 6, 2026
Comment thread src/net_processing.cpp
const std::shared_future<CSerializedNetMsg> lazy_ser{
std::async(std::launch::deferred, [&] { return NetMsg::Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
std::shared_future<CSerializedNetMsg> lazy_ser{
std::async(std::launch::async, [pblock = pblock] {

@maflcko maflcko Nov 6, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/net_processing.cpp
});
}

LogDebug(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

w0xlt commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

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.

5 participants