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

pull davidgumberg wants to merge 4 commits into bitcoin:master from davidgumberg:2026-11-5-cache-serialized-cmpctblock-msg changing 1 files +81 −54
  1. davidgumberg commented at 1:36 AM on November 6, 2026: 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).

  2. p2p: refactor: Stuff m_most_recent* into a struct 7c5c232da6
  3. DrahtBot commented at 1:36 AM on November 6, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #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)
    • #36521 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36521.svg"></sub> (Keep witness stacks in serialized form in CScriptWitness by sipa)
    • #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)
    • #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)

    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-->

  4. p2p: Cache serialized `cmpct_block_msg` for relay.
    This commit is drawn from the closed #26755.
    (https://github.com/bitcoin/bitcoin/pull/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 <andrewstoth@gmail.com>
    42a992816a
  5. net: refactor: Move common logic into SendCompactBlock
    Also one non-refactor change which is setting the `pIndexBestHeaderSent`
    on the `SendMessages` CMPCTBLOCK announcement fallback.
    ea51d1eaa9
  6. qa: Make PeerHasHeader more robust against nullptr
    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.
    d84a57a9f7
  7. davidgumberg force-pushed on Nov 6, 2026
  8. DrahtBot added the label CI failed on Nov 6, 2026
  9. davidgumberg renamed this:
    cmpctblock: Cache serialized cmpctblock msg for relay
    cmpctblock: Cache serialized cmpctblock msg for relay [part of prefill series]
    on Nov 6, 2026
  10. DrahtBot removed the label CI failed on Nov 6, 2026
  11. in src/net_processing.cpp:2253 in d84a57a9f7
    2248 | @@ -2240,25 +2249,29 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
    2249 |  
    2250 |      if (!DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) return;
    2251 |  
    2252 | -    uint256 hashBlock(pblock->GetHash());
    2253 | -    const std::shared_future<CSerializedNetMsg> lazy_ser{
    2254 | -        std::async(std::launch::deferred, [&] { return NetMsg::Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
    2255 | +    std::shared_future<CSerializedNetMsg> lazy_ser{
    2256 | +        std::async(std::launch::async, [pblock = pblock]  {
    


    maflcko commented at 9:13 AM on November 6, 2026:

    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>

  12. in src/net_processing.cpp:2301 in ea51d1eaa9
    2302 | +            return false;
    2303 |          }
    2304 | -    });
    2305 | +    }
    2306 | +
    2307 | +    LogDebug(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
    


    maflcko commented at 9:16 AM on November 6, 2026:

    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.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-08 19:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me