net: move some CNodeState fields to Peer #35561

pull Crypt-iQ wants to merge 2 commits into bitcoin:master from Crypt-iQ:06082026/cnodestate changing 1 files +33 −29
  1. Crypt-iQ commented at 11:03 AM on June 18, 2026: contributor

    The first commit moves m_requested_hb_cmpctblocks and m_provides_cmpctblocks from CNodeState to Peer and also makes the two fields atomic.

    The second commit moves fPreferredDownload to Peer and makes it atomic. Since m_num_preferred_download_peers is a counter based on this field, it can also be atomic. This also lets us drop cs_main usage in a few places. I found this commit harder to reason about, specifically whether the asserts in FinalizeNode were still safe for m_num_preferred_download_peers if another thread could modify the value before/after the m_num_preferred_download_peers -= peer->fPreferredDownload line and trigger the assert. Afaict, these asserts are still safe because ProcessMessage cannot run for the same peer as the peer being cleaned up in FinalizeNode. I am also happy to drop this second commit or explain my reasoning in more detail.

    For reference:

    • m_requested_hb_cmpctblocks can be accessed from:

      • NewPoWValidBlock (read) (b-msghand, http, ipc? threads)
      • ProcessMessage (write) (b-msghand)
      • SendMessages (read) (b-msghand)
    • m_provides_cmpctblocks can be accessed from:

      • MaybeSetPeerAsAnnouncingHeaderAndIDs (read) (b-msghand, http, ipc?)
      • HeadersDirectFetchBlocks (read) (b-msghand)
      • ProcessMessage (write) (b-msghand)
    • fPreferredDownload can be accessed from:

      • ProcessMessage (read/write) (b-msghand)
      • SendMessages (read) (b-msghand)
      • FinalizeNode (read) (bitcoind, b-net)
    • m_num_preferred_download_peers can be accessed from:

      • ProcessMessage (write) (b-msghand)
      • SendMessages (read) (b-msghand)
      • FinalizeNode (read/write) (bitcoind, b-net)
  2. net: move cmpctblocks fields from CNodeState to Peer
    Both m_requested_hb_cmpctblocks & m_provides_cmpctblocks were
    protected by cs_main which is unnecessary. Instead convert them
    to std::atomic<bool> as Mutex is not needed. This way we don't
    need to worry about potential lock inversion in the future or
    thread safety annotations.
    0954ae6696
  3. net: move fPreferredDownload to Peer, make atomic
    Also make the m_num_preferred_download_peers counter atomic and add
    an assert in FinalizeNode. The asserts for m_num_preferred_download_peers
    in FinalizeNode are still safe even without cs_main locking because of
    the implicit guarantee that ProcessMessage may not run for a peer
    at the same time as FinalizeNode. This has the nice benefit of being
    able to remove cs_main usage in some places.
    765f65971a
  4. DrahtBot added the label P2P on Jun 18, 2026
  5. DrahtBot commented at 11:03 AM on June 18, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35561.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK davidgumberg

    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:

    • #35558 (p2p: Prefill compact blocks by davidgumberg)
    • #35522 (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc)
    • #35315 (refactor: Use NodeClock::time_point in more places by maflcko)
    • #34743 (p2p: don't disconnect manual peers for block stalling by willcl-ark)
    • #34565 (refactor: extract BlockDownloadManager from PeerManagerImpl 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-->

  6. davidgumberg commented at 12:47 AM on June 19, 2026: contributor

    Concept ACK, Reviewed to https://github.com/bitcoin/bitcoin/pull/35561/changes/0954ae669683a3179a362dd47cb6176cda914eb2.

    Less cs_main is good, locks are scary, and can hurt performance, and are unnecessary for all of these fields. I need to review https://github.com/bitcoin/bitcoin/pull/35561/commits/765f65971aa4fc6c7c0bdf971b4cdc771a5a296e carefully.

Labels

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-06-20 23:51 UTC

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