#35561 net: move some CNodeState fields to Peer
https://github.com/bitcoin/bitcoin/pull/35561 · · +42/-42 in 4 files, 3 commits · labels: P2P, Needs rebase
Goal
- Reduce lock contention on the main engine lock during peer download and block negotiation
- Decouple peer state tracking from internal node validation locks
Moves `m_requested_hb_cmpctblocks`, `m_provides_cmpctblocks`, and `fPreferredDownload` (renamed `m_preferred_download`) from `CNodeState` to `Peer` as atomic variables. Also makes `m_num_preferred_download_peers` an atomic integer, allowing several call sites in `net_processing.cpp` to drop `cs_main` locking.
Problem: Protecting peer download and compact block negotiation flags with `cs_main` introduces unnecessary lock contention, thread safety annotations, and split state across `CNodeState` and `Peer`.
Category: P2P (#52 of 65)
P3 · cleanup
- P3 because it incrementally reduces main lock contention without resolving an active bottleneck or bug
- Provides small leverage toward decoupling network processing from the main lock
Reduces cs_main locking scope in net_processing by moving three peer state booleans and an integer counter to atomics. While it serves the ongoing goal of decoupling net_processing from cs_main, it addresses no active bug or major performance bottleneck.
Membership: Modifies peer state structures and lock usage inside src/net_processing.cpp.
Factors: security/stability 0, bug 0, performance 1, user value 0, leverage 1
Reviewability: Stale: Needs rebase
- Needs rebase due to merge conflicts with master
The PR has merge conflicts against master and is marked with the 'Needs rebase' label.
Author status: active; last push addressed review feedback and rebased, but currently silent for 22 days while conflicting with master
Resolved concerns:
- dergoegge noted that reasoning about `m_num_preferred_download_peers` without a shared lock was harder, but agreed the atomics are correct; mzumsande noted precedent with `m_wtxid_relay_peers`.
- mzumsande suggested adding a negative lock annotation `!m_peer_mutex` to `NewPoWValidBlock`, which the author added.
Agreement: Strong
- Strong support for reducing main lock scope for peer state tracking (davidgumberg, mzumsande)
- Confirmed the atomic implementation is safe and matches existing patterns (dergoegge, mzumsande)
- Code approval on the latest head commit (w0xlt)
Strong support for dropping cs_main and migrating state to Peer; all reviewer questions resolved
Reviewers agreed with the direction to reduce cs_main locking scope and approved the atomic implementation across iterations without objections.
- davidgumberg Concept ACKed: 'Less cs_main is good, locks are scary, and can hurt performance, and are unnecessary for all of these fields.'
- dergoegge reviewed and ACKed an earlier revision, confirming the changes look fine and safe.
- mzumsande Concept ACKed, pointing out consistency with existing atomic patterns in net_processing.
- w0xlt ACKed the latest head commit.
Review verdicts (DrahtBot): 1 (+1)
- ACK: w0xlt
- Stale ACK: dergoegge
- Concept ACK: davidgumberg, mzumsande
Files
4 lines under test/bench/ci.
- src/net_processing.cpp +39/-39
- src/node/txdownloadman_impl.cpp +1/-1
- test/functional/test_framework/test_framework.py +1/-1
- test/functional/wallet_listsinceblock.py +1/-1
Card
PR 35561 moves compact block relay flags and preferred download status from CNodeState to Peer as atomic variables, and converts m_num_preferred_download_peers into an atomic integer. This removes the need for cs_main locking across several peer processing paths in net_processing.cpp. The change is internal cleanup that incrementally reduces cs_main lock contention and simplifies peer state tracking. The PR has strong review support with an ACK on the latest push, but is currently stale pending a rebase.