Skip to content

Commit

Permalink
[p2p] Add Peer struct for per-peer data in net processing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jnewbery authored and Fabcien committed Jan 6, 2021
1 parent afc4263 commit b6a215d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 99 deletions.
3 changes: 0 additions & 3 deletions src/avalanche/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ bool Processor::registerVotes(NodeId nodeid, const Response &response,
auto w = queries.getWriteView();
auto it = w->find(std::make_tuple(nodeid, response.getRound()));
if (it == w.end()) {
LOCK(cs_main);
Misbehaving(nodeid, 2, "unexpcted-ava-response");
return false;
}
Expand All @@ -308,14 +307,12 @@ bool Processor::registerVotes(NodeId nodeid, const Response &response,
const std::vector<Vote> &votes = response.GetVotes();
size_t size = invs.size();
if (votes.size() != size) {
LOCK(cs_main);
Misbehaving(nodeid, 100, "invalid-ava-response-size");
return false;
}

for (size_t i = 0; i < size; i++) {
if (invs[i].hash != votes[i].GetHash()) {
LOCK(cs_main);
Misbehaving(nodeid, 100, "invalid-ava-response-content");
return false;
}
Expand Down
Loading

0 comments on commit b6a215d

Please sign in to comment.