-
Notifications
You must be signed in to change notification settings - Fork 37.7k
[p2p] Add Peer struct for per-peer data in net processing #19607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you @MarcoFalke @promag @jonatack @dongcarl @ariard @theuni @fjahr @hebasto @troygiorshev for reviewing the prerequisite PRs! This is the first PR in the cs_main_split sequence where we start getting benefit by reducing cs_main scope in net processing. It also introduces the new structure and locking model, so it's the PR that requires the most conceptual/approach review in the sequence. If this gets merged, then it's a long procession of PRs to slowly move data into the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK 3fa130d, only some style-nits
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tACK 3fa130d with some questions and suggestions.
- Is the plan to lock every member of
Peer
behind its own mutex? Why not give eachPeer
its own mutex instead? Misbehaving
'scs_main
locks can be removed fromdenialofservice_tests.cpp
: around here. I looked and couldn't find any further places.clang-format
complains a bit on each commit.
Currently the plan is to have related members behind a single mutex, eg all tx relay fields behind the same mutex. The main reason is that that model maps most closely to what exists already (look at the various locks inside CNode). Once everything has moved to
Fixed. Thanks!
Can you expand a bit. What am I supposed to be looking for here? |
8f8d5b0
to
123bba3
Compare
ok, I've taken clang-format's suggestions, except one that joins an initializer list onto the same line as the function and parameter list (which makes a really long line), and one which would break the scripted-diff linter. |
@jonatack oops. Fixed. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK afe3604
Some questions below, feel free to ignore.
Thanks for the review @jonatack and @MarcoFalke . All your latest review comments are addressed. |
reACK 9510938 |
re-ACK 9510938 per |
Code review ACK 9510938 Still testing a bit but everything looks good! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK 9510938
I really like the approach to gather members by logical usage and progressively swap their cs_main guard by a new per-structure one. I thought at first, it would add new lock dependencies if we moved everything from CNodeState
directly into Peer
as a quick read of related issue description made it thought but in fact no.
Perhaps #19398 should laid out more the lock refactoring strategy and compare with any concurrent one (wasn't this discussed on IRC a while ago ?). It may help to take a decision here.
I glanced back on #18963, it adds also a CPeerState
, aiming also to dry-up CNodeState
but as branch tip was only used to signal end of validation processing back to source peer and resume its message processing. I think this current approach is somehow concurrent to #18963 but not incompatible with its end-goal of introducing asynchronous headers/block processing. Once this refactoring is done, it would be easier to reason on adding a new async "validation" thread, still challenging but easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 8e35bf5 keeping in mind Cory's comment (#19607 (comment)) for the follow-up
Concept ACK. |
…rocessing 8e35bf5 scripted-diff: rename misbehavior members (John Newbery) 1f96d2e [net processing] Move misbehavior tracking state to Peer (John Newbery) 7cd4159 [net processing] Add Peer (John Newbery) aba0335 [net processing] Remove CNodeState.name (John Newbery) Pull request description: We currently have two structures for per-peer data: - `CNode` in net, which should just contain connection layer data (eg socket, send/recv buffers, etc), but currently also contains some application layer data (eg tx/block inventory). - `CNodeState` in net processing, which contains p2p application layer data, but requires cs_main to be locked for access. This PR adds a third struct `Peer`, which is for p2p application layer data, and doesn't require cs_main. Eventually all application layer data from `CNode` should be moved to `Peer`, and any data that doesn't strictly require cs_main should be moved from `CNodeState` to `Peer` (probably all of `CNodeState` eventually). `Peer` objects are stored as shared pointers in a net processing global map `g_peer_map`, which is protected by `g_peer_mutex`. To use a `Peer` object, `g_peer_mutex` is locked, a copy of the shared pointer is taken, and the lock is released. Individual members of `Peer` are protected by different mutexes that guard related data. The lifetime of the `Peer` object is managed by the shared_ptr refcount. This PR adds the `Peer` object and moves the misbehaving data from `CNodeState` to `Peer`. This allows us to immediately remove 15 `LOCK(cs_main)` instances. For more motivation see bitcoin#19398 ACKs for top commit: laanwj: Code review ACK 8e35bf5 troygiorshev: reACK 8e35bf5 via `git range-diff master 9510938 8e35bf5` theuni: ACK 8e35bf5. jonatack: ACK 8e35bf5 keeping in mind Cory's comment (bitcoin#19607 (comment)) for the follow-up Tree-SHA512: ad84a92b78fb34c9f43813ca3dfbc7282c887d55300ea2ce0994d134da3e0c7dbc44d54380e00b13bb75a57c28857ac3236bea9135467075d78026767a19e4b1
bb6a32c [net processing] Move Misbehaving() to PeerManager (John Newbery) aa114b1 [net_processing] Move SendBlockTransactions into PeerManager (John Newbery) 3115e00 [net processing] Move MaybePunishPeerForTx to PeerManager (John Newbery) e662e2d [net processing] Move ProcessOrphanTx to PeerManager (John Newbery) b70cd89 [net processing] Move MaybePunishNodeForBlock into PeerManager (John Newbery) d777835 [net processing] Move ProcessHeadersMessage to PeerManager (John Newbery) 64f6162 [whitespace] tidy up indentation after scripted diff (John Newbery) 58bd369 scripted-diff: [net processing] Rename PeerLogicValidation to PeerManager (John Newbery) 2297b26 [net_processing] Pass chainparams to PeerLogicValidation constructor (John Newbery) 824bbd1 [move only] Collect all private members of PeerLogicValidation together (John Newbery) Pull request description: Continues the work of moving net_processing logic into PeerLogicValidation. See bitcoin/bitcoin#19704 and bitcoin/bitcoin#19607 (comment) for motivation. This PR also renames `PeerLogicValidation` to `PeerManager` as suggested in bitcoin/bitcoin#10756 (review). ACKs for top commit: MarcoFalke: re-ACK bb6a32c only change is rebase due to conflict in struct NodeContext and variable rename 🤸 hebasto: re-ACK bb6a32c, only rebased, and added renaming `s/peer_logic/peerman/` into scripted-diff since my [previous](bitcoin/bitcoin#19791 (review)) review (verified with `git range-diff`). Tree-SHA512: a2de4a521688fd25125b401e5575402c52b328a0fa27b3010567008d4f596b960aabbd02b2d81f42658f88f4365443fadb1008150a62fbcea123fb42d85a2c21
…t_processing 3002b4a [net processing] Guard m_continuation_block with m_block_inv_mutex (John Newbery) 184557e [net processing] Move hashContinue to net processing (John Newbery) c853ef0 scripted-diff: rename vBlockHashesToAnnounce and vInventoryBlockToSend (John Newbery) 53b7ac1 [net processing] Move block inventory data to Peer (John Newbery) 78040f9 [net processing] Rename nStartingHeight to m_starting_height (John Newbery) 77a2c2f [net processing] Move nStartingHeight to Peer (John Newbery) 717a374 [net processing] Improve documentation for Peer destruction/locking (John Newbery) Pull request description: This continues the work of moving application layer data into net_processing, by moving all block inventory state into the new Peer object added in bitcoin#19607. For motivation, see bitcoin#19398. ACKs for top commit: jonatack: re-ACK 3002b4a per `git diff 9aad3e4 3002b4a` Sjors: Code review re-ACK 3002b4a MarcoFalke: re-ACK 3002b4a 🌓 Tree-SHA512: eb2b474b73b025791ee3e6e41809926b332b48468763219f31638ca390f427632f05902dfc6a2c6bdc1ce47b215782f67874ddbf05b97d77d5897b7e2abfe4d9
Summary: ``` We currently have two structures for per-peer data: CNode in net, which should just contain connection layer data (eg socket, send/recv buffers, etc), but currently also contains some application layer data (eg tx/block inventory). CNodeState in net processing, which contains p2p application layer data, but requires cs_main to be locked for access. This PR adds a third struct Peer, which is for p2p application layer data, and doesn't require cs_main. Eventually all application layer data from CNode should be moved to Peer, and any data that doesn't strictly require cs_main should be moved from CNodeState to Peer (probably all of CNodeState eventually). Peer objects are stored as shared pointers in a net processing global map g_peer_map, which is protected by g_peer_mutex. To use a Peer object, g_peer_mutex is locked, a copy of the shared pointer is taken, and the lock is released. Individual members of Peer are protected by different mutexes that guard related data. The lifetime of the Peer object is managed by the shared_ptr refcount. This PR adds the Peer object and moves the misbehaving data from CNodeState to Peer. This allows us to immediately remove 15 LOCK(cs_main) instances. For more motivation see #19398 ``` Backport of core [[bitcoin/bitcoin#19607 | PR19607]]. Depends on D8793. Includes changes from our codebase that don't exist in core. Test Plan: With Clang and Debug: ninja all check-all Reviewers: #bitcoin_abc, majcosta Reviewed By: #bitcoin_abc, majcosta Subscribers: majcosta Differential Revision: https://reviews.bitcoinabc.org/D8794
a5e15ae scripted-diff: rename ping members (John Newbery) 45dcf22 [net processing] Move ping data fields to net processing (John Newbery) dd2646d [net processing] Move ping timeout logic to net processing (John Newbery) 0b43b81 [net processing] Move send ping message logic into function (John Newbery) 1a07600 [net] Add RunInactivityChecks() (John Newbery) f8b3058 [net processing] Add Peer& arg to MaybeDiscourageAndDisconnect() (John Newbery) Pull request description: This continues the work of moving application layer data into net_processing, by moving all ping data into the new Peer object added in bitcoin#19607. For motivation, see bitcoin#19398. ACKs for top commit: glozow: reACK bitcoin@a5e15ae MarcoFalke: review ACK a5e15ae 🥉 amitiuttarwar: ACK a5e15ae Tree-SHA512: fb84241613d6a6e1f2832fa5378030b5877a02e8308188f57ab545a6eaf2ab731a93abb7dcd3a7f7285bb66700f938096378a8e90cd6a3e6f3309f81d85a344e
0829516 [refactor] Remove unused ForEachNodeThen() template (John Newbery) 09cc66c scripted-diff: rename address relay fields (John Newbery) 76568a3 [net processing] Move addr relay data and logic into net processing (John Newbery) caba7ae [net processing] Make RelayAddress() a member function of PeerManagerImpl (John Newbery) 86acc96 [net processing] Take NodeId instead of CNode* as originator for RelayAddress() (John Newbery) Pull request description: This continues the work of moving application layer data into net_processing, by moving all addr data into the new Peer object added in #19607. For motivation, see #19398. ACKs for top commit: laanwj: Code review ACK 0829516 mzumsande: ACK 0829516, reviewed the code and ran tests. sipa: utACK 0829516 hebasto: re-ACK 0829516 Tree-SHA512: efe0410fac288637f203eb37d1999910791e345872d37e1bd5cde50e25bb3cb1c369ab86b3a166ffd5e06ee72e4508aa2c46d658be6a54e20b4f220d2f57d0a6
1066d10 scripted-diff: rename TxRelay members (John Newbery) 575bbd0 [net processing] Move tx relay data to Peer (John Newbery) 785f55f [net processing] Move m_wtxid_relay to Peer (John Newbery) 3634670 [net] Add CNode.m_relays_txs and CNode.m_bloom_filter_loaded (John Newbery) Pull request description: This continues the work of moving application layer data into net_processing, by moving all tx data into the new Peer object added in #19607. For motivation, see #19398. ACKs for top commit: dergoegge: ACK 1066d10 - This is a good layer separation improvement with no behavior changes. glozow: utACK 1066d10 Tree-SHA512: 0c9d6b8a0a05e2d816b6d6588b7df133842ec960ae67667813422aa7bd8eb5308599c714f3822a98ddbdf364ffab9050b055079277ba4aff24092557ff99ebcc
We currently have two structures for per-peer data:
CNode
in net, which should just contain connection layer data (eg socket, send/recv buffers, etc), but currently also contains some application layer data (eg tx/block inventory).CNodeState
in net processing, which contains p2p application layer data, but requires cs_main to be locked for access.This PR adds a third struct
Peer
, which is for p2p application layer data, and doesn't require cs_main. Eventually all application layer data fromCNode
should be moved toPeer
, and any data that doesn't strictly require cs_main should be moved fromCNodeState
toPeer
(probably all ofCNodeState
eventually).Peer
objects are stored as shared pointers in a net processing global mapg_peer_map
, which is protected byg_peer_mutex
. To use aPeer
object,g_peer_mutex
is locked, a copy of the shared pointer is taken, and the lock is released. Individual members ofPeer
are protected by different mutexes that guard related data. The lifetime of thePeer
object is managed by the shared_ptr refcount.This PR adds the
Peer
object and moves the misbehaving data fromCNodeState
toPeer
. This allows us to immediately remove 15LOCK(cs_main)
instances.For more motivation see #19398