Skip to content

net: move some CNodeState fields to Peer - #35561

Open
Crypt-iQ wants to merge 3 commits into
bitcoin:masterfrom
Crypt-iQ:06082026/cnodestate
Open

Crypt-iQ wants to merge 3 commits into
bitcoin:masterfrom
Crypt-iQ:06082026/cnodestate

Conversation

@Crypt-iQ

@Crypt-iQ Crypt-iQ commented Jun 18, 2026 •

Copy link
Copy Markdown
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)

@DrahtBot DrahtBot added the P2P label Jun 18, 2026
@DrahtBot

DrahtBot commented Jun 18, 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
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.

@davidgumberg

Copy link
Copy Markdown
Contributor

Concept ACK, Reviewed to 0954ae6.

Less cs_main is good, locks are scary, and can hurt performance, and are unnecessary for all of these fields. I need to review 765f659 carefully.

Comment thread src/net_processing.cpp Resolved

@dergoegge dergoegge left a comment

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.

Code review ACK 765f659

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

Comment thread src/net_processing.cpp Resolved
@DrahtBot
DrahtBot requested a review from davidgumberg June 30, 2026 13:48
@Crypt-iQ

Crypt-iQ commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

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. #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 mzumsande left a comment

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.

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.

Comment thread src/net_processing.cpp Resolved
@DrahtBot

Copy link
Copy Markdown
Contributor

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

@Crypt-iQ

Copy link
Copy Markdown
Contributor Author

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

@w0xlt w0xlt left a comment

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.

ACK 3861ab4

@Crypt-iQ

Copy link
Copy Markdown
Contributor Author

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

@fanquake

fanquake commented Dec 1, 2026

Copy link
Copy Markdown
Member

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

@instagibbs

Copy link
Copy Markdown
Member

ACK once rebased

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. Also, add a negative annotation to
NewPoWValidBlock for m_peer_mutex since it now acquires it.
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.
-BEGIN VERIFY SCRIPT-

 sed -i "s/fPreferredDownload/m_preferred_download/g" $( git grep -l fPreferredDownload )

-END VERIFY SCRIPT-

@w0xlt w0xlt left a comment

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.

reACK 4b02684

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants