Skip to content

Conversation

jnewbery
Copy link
Contributor

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

@jnewbery
Copy link
Contributor Author

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 Peer object.

Copy link
Member

@maflcko maflcko left a 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

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 28, 2020

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

Conflicts

Reviewers, 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.

Copy link
Contributor

@troygiorshev troygiorshev left a 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 each Peer its own mutex instead?
  • Misbehaving's cs_main locks can be removed from denialofservice_tests.cpp: around here. I looked and couldn't find any further places.
  • clang-format complains a bit on each commit.

@jnewbery
Copy link
Contributor Author

Is the plan to lock every member of Peer behind its own mutex? Why not give each Peer its own mutex instead?

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 Peer then it should be much easier to change the locking model, but I didn't want to add a debate about what the ideal locking model is to this project.

Misbehaving's cs_main locks can be removed from denialofservice_tests.cpp: around here. I looked and couldn't find any further places.

Fixed. Thanks!

clang-format complains a bit on each commit.

Can you expand a bit. What am I supposed to be looking for here?

@jnewbery jnewbery force-pushed the 2020-07-peer branch 2 times, most recently from 8f8d5b0 to 123bba3 Compare July 29, 2020 13:47
@jnewbery
Copy link
Contributor Author

clang-format complains a bit on each commit.

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
Copy link
Member

@jnewbery it looks like there is an incomplete rebase-in-progress in the last commit 123bba3; will review on next push.

@jnewbery
Copy link
Contributor Author

@jonatack oops. Fixed. Thanks!

Copy link
Member

@jonatack jonatack left a 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.

@jnewbery
Copy link
Contributor Author

Thanks for the review @jonatack and @MarcoFalke . All your latest review comments are addressed.

@troygiorshev
Copy link
Contributor

reACK 9510938

@laanwj
Copy link
Member

laanwj commented Jul 30, 2020

Code review ACK 8e35bf5
9510938

@theuni As you made the CNode and CNodeState separation originally can you take a look here please.

@jonatack
Copy link
Member

re-ACK 9510938 per git diff afe3604 9510938

@fjahr
Copy link
Contributor

fjahr commented Jul 30, 2020

Code review ACK 9510938

Still testing a bit but everything looks good!

@jnewbery
Copy link
Contributor Author

Note for maintainers: please don't merge this yet. It has 4 ACKs, but going down this path is quite an important design decision. I'd like @theuni, @sipa or @sdaftuar to weight in before we move forward.

Copy link

@ariard ariard left a 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.

@DrahtBot DrahtBot mentioned this pull request Aug 2, 2020
18 tasks
Copy link
Member

@jonatack jonatack left a 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

@sipa
Copy link
Member

sipa commented Aug 28, 2020

Concept ACK.

@laanwj laanwj merged commit 1cf73fb into bitcoin:master Aug 28, 2020
@jnewbery jnewbery deleted the 2020-07-peer branch August 28, 2020 19:22
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Aug 28, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 28, 2020
…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
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 2, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 3, 2020
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Sep 7, 2020
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
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 7, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 8, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 28, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Sep 29, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Oct 19, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Nov 19, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Dec 14, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Dec 19, 2020
jnewbery added a commit to jnewbery/bitcoin that referenced this pull request Dec 19, 2020
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Dec 22, 2020
…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
mjdietzx pushed a commit to mjdietzx/bitcoin that referenced this pull request Dec 26, 2020
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 6, 2021
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
nolim1t pushed a commit to nolim1t/bitcoin-upstream that referenced this pull request Feb 16, 2021
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
fanquake added a commit that referenced this pull request May 24, 2021
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
fanquake added a commit that referenced this pull request Mar 25, 2022
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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.