net: move some CNodeState fields to Peer #35561

Open
Crypt-iQ wants to merge 3 commits from 06082026/cnodestate into master AGit
Crypt-iQ commented 2026-06-18 11:03:43 +00:00 (Migrated from github.com)

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)
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)
DrahtBot commented 2026-06-18 11:03:50 +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
ACK w0xlt
Concept ACK davidgumberg, mzumsande, instagibbs
Stale ACK dergoegge

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:

  • #35940 (net: allow selecting BIP152 high-bandwidth peers with -addnode by w0xlt)
  • #35522 (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.

<!--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/35561 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35561.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35561#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 | | ---- | --------- | | ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/35561#pullrequestreview-6176974649) | | Concept ACK | [davidgumberg](https://github.com/bitcoin/bitcoin/pull/35561#issuecomment-4747307175), [mzumsande](https://github.com/bitcoin/bitcoin/pull/35561#pullrequestreview-4938314585), [instagibbs](https://github.com/bitcoin/bitcoin/pull/35561#issuecomment-7350493119) | | Stale ACK | [dergoegge](https://github.com/bitcoin/bitcoin/pull/35561#pullrequestreview-4600905548) | 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: * [#35940](https://github.com/bitcoin/bitcoin/pull/35940) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35940.svg"></sub> (net: allow selecting BIP152 high-bandwidth peers with `-addnode` by w0xlt) * [#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) 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-->
davidgumberg commented 2026-06-19 00:47:44 +00:00 (Migrated from github.com)
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.
Crypt-iQ (Migrated from github.com) reviewed 2026-06-22 21:00:12 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-06-22 21:00:12 +00:00

This scoping can be removed now that the lock is gone, will fix the next time I push up.

This scoping can be removed now that the lock is gone, will fix the next time I push up.
dergoegge (Migrated from github.com) reviewed 2026-06-30 13:48:46 +00:00
dergoegge (Migrated from github.com) left a comment

Code review ACK 765f65971aa4fc6c7c0bdf971b4cdc771a5a296e

The changes themselves look fine/safe to me but I think it might be useful to provide more motivation for this change.

The m_num_preferred_download_peers change also seems correct to me, but I found the previous version with a shared lock (i.e. cs_main) a little easier to reason about wrt "at anytime m_num_preferred_download_peers reflects the number of CNodeStates with fPreferredDownload equals true".

Code review ACK 765f65971aa4fc6c7c0bdf971b4cdc771a5a296e The changes themselves look fine/safe to me but I think it might be useful to provide more motivation for this change. The `m_num_preferred_download_peers` change also seems correct to me, but I found the previous version with a shared lock (i.e. `cs_main`) a little easier to reason about wrt "at anytime `m_num_preferred_download_peers` reflects the number of `CNodeState`s with `fPreferredDownload` equals true".
dergoegge (Migrated from github.com) commented 2026-06-30 13:48:02 +00:00

nit: could scripted diff this to m_preferred_download

nit: could scripted diff this to `m_preferred_download`
Crypt-iQ commented 2026-07-02 15:54:20 +00:00 (Migrated from github.com)

The changes themselves look fine/safe to me but I think it might be useful to provide more motivation for this change.

I started doing this to see how complicated the change would be to remove CNodeState and the cs_main locking it requires, I have more branches that move some of the other fields. I think tracking multiple states (CNodeState & Peer) is more confusing to reason about than just having one state and I think? it's been a goal to reduce the scope of cs_main (i.e. https://github.com/bitcoin/bitcoin/issues/27502). I have a private branch that removes some other fields, though there are some gotchas with locking.

I found the previous version with a shared lock (i.e. cs_main) a little easier to reason about

Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it.

> The changes themselves look fine/safe to me but I think it might be useful to provide more motivation for this change. I started doing this to see how complicated the change would be to remove CNodeState and the cs_main locking it requires, I have more branches that move some of the other fields. I think tracking multiple states (CNodeState & Peer) is more confusing to reason about than just having one state and I think? it's been a goal to reduce the scope of cs_main (i.e. https://github.com/bitcoin/bitcoin/issues/27502). I have a private branch that removes some other fields, though there are some gotchas with locking. > I found the previous version with a shared lock (i.e. cs_main) a little easier to reason about Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it.
mzumsande (Migrated from github.com) reviewed 2026-08-14 17:02:09 +00:00
mzumsande (Migrated from github.com) left a comment

Concept ACK

Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it.

to be fair, we do have the same pattern with m_wtxid_relay / m_wtxid_relay_peers which are also two atomics, so no strong opinion.

Concept ACK > Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it. to be fair, we do have the same pattern with `m_wtxid_relay` / `m_wtxid_relay_peers` which are also two atomics, so no strong opinion.
mzumsande (Migrated from github.com) commented 2026-08-14 14:41:24 +00:00

should NewPoWValidBlock get a !m_peer_mutex annotation, now that we call GetPeerRef from it? I think it may be missed by the compiler due to the lambda it is used in.

should `NewPoWValidBlock` get a `!m_peer_mutex` annotation, now that we call `GetPeerRef` from it? I think it may be missed by the compiler due to the lambda it is used in.
Crypt-iQ (Migrated from github.com) reviewed 2026-08-15 16:00:41 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-08-15 16:00:41 +00:00

It is indeed missed by the compiler because of the lambda. Should the annotation be added if the compiler silently skips it?

It is indeed missed by the compiler because of the lambda. Should the annotation be added if the compiler silently skips it?
mzumsande (Migrated from github.com) reviewed 2026-08-17 12:32:43 +00:00
mzumsande (Migrated from github.com) commented 2026-08-17 12:32:43 +00:00

I'd say yes, since it would be incorrect to call the function with m_peer_mutex held, and I don't think the compiler would complain about something it may see as a "false positive".

I'd say yes, since it would be incorrect to call the function with `m_peer_mutex` held, and I don't think the compiler would complain about something it may see as a "false positive".
DrahtBot commented 2026-08-26 01:44:28 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task Windows native, fuzz, VS: https://github.com/bitcoin/bitcoin/actions/runs/32914333828/job/98014730803
LLM reason (✨ experimental): CI failed because the fuzz target rpc crashed (Windows exit code 3221225477 / access violation).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `Windows native, fuzz, VS`: https://github.com/bitcoin/bitcoin/actions/runs/32914333828/job/98014730803</sub> <sub>LLM reason (✨ experimental): CI failed because the fuzz target `rpc` crashed (Windows exit code 3221225477 / access violation).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
Crypt-iQ commented 2026-08-26 11:15:24 +00:00 (Migrated from github.com)

765f659 to 3861ab4: rebased since #32606 added another callsite using m_provides_cmpctblocks. Also added a commit for scripted diff. CI failures are https://github.com/bitcoin/bitcoin/pull/36088.

765f659 to 3861ab4: rebased since #32606 added another callsite using `m_provides_cmpctblocks`. Also added a commit for scripted diff. CI failures are https://github.com/bitcoin/bitcoin/pull/36088.
Crypt-iQ (Migrated from github.com) reviewed 2026-08-26 11:15:51 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-08-26 11:15:51 +00:00

added in 1648713efe

added in 1648713efe8aa29f292d7c52f720393892633300
Crypt-iQ (Migrated from github.com) reviewed 2026-08-26 11:16:15 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-08-26 11:16:15 +00:00

added in 3861ab49ec

added in 3861ab49ec77674356ddf3fc1bd090b4b25c2fde
w0xlt (Migrated from github.com) reviewed 2026-09-08 21:00:25 +00:00
w0xlt (Migrated from github.com) left a comment
ACK 3861ab49ec77674356ddf3fc1bd090b4b25c2fde
DrahtBot commented 2026-09-14 15:51:41 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Crypt-iQ commented 2026-09-23 14:35:02 +00:00 (Migrated from github.com)

3861ab4..9686707 rebased after new field added to CNodeState 😅

3861ab4..9686707 rebased after new field added to `CNodeState` 😅
First-time contributor

I found no actionable issues in this static review.


Reviewed: 4b02684629ce · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e1a29e5e677c47d383756f0fcf8f3760725c91d1104dd3ccb2f333b6217eba26:47880f5569fd2047d249266f337c4f2a509adf7d96c3ccb1c7644c2c2a650bff --> <!-- ralph-base:2f3f6397bb92beccaccce16742f0574d1617fb3e --> <!-- ralph-head:4b02684629ce2533a6c174d0a44c3429eca1bb81 --> <!-- ralph-case:92cd39dadace8e11f663c1bb9e5ba080697102a097f55f791f301542c8acd828 --> I found no actionable issues in this static review. <!-- Review tier: sensitive --> --- Reviewed: [`4b02684629ce`](<https://git.fish.foo/bitcoin/bitcoin/commit/4b02684629ce2533a6c174d0a44c3429eca1bb81>) · [Review report](<https://review.fish.foo/report/35561-4b02684629ce2533a6c174d0a44c3429eca1bb81-3496-4.html>) · [Previous reports](<https://review.fish.foo/traces/35561/>) [permalink](<https://review.fish.foo/report/35561-4b02684629ce2533a6c174d0a44c3429eca1bb81-3496-4.html>)
DrahtBot commented 2026-11-01 03:33:31 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
fanquake commented 2026-12-01 11:24:04 +00:00 (Migrated from github.com)

Seems like there is agreement to do this. Maybe one more rebase to get it over the line? Also cc @instagibbs

Seems like there is agreement to do this. Maybe one more rebase to get it over the line? Also cc @instagibbs
instagibbs commented 2026-12-02 15:32:15 +00:00 (Migrated from github.com)

ACK once rebased

ACK once rebased
w0xlt (Migrated from github.com) reviewed 2026-12-03 19:29:50 +00:00
w0xlt (Migrated from github.com) left a comment

reACK 4b02684629

reACK 4b02684629ce2533a6c174d0a44c3429eca1bb81
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!35561
No description provided.