HomePhabricator

[p2p] Add Peer struct for per-peer data in net processing

Description

[p2p] Add Peer struct for per-peer data in net processing

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

Details

Provenance
John Newbery <john@johnnewbery.com>Authored on Jul 29 2020, 07:57
FabienCommitted on Jan 6 2021, 12:42
FabienPushed on Jan 6 2021, 12:43
Reviewer
Restricted Project
Differential Revision
D8794: [p2p] Add Peer struct for per-peer data in net processing
Parents
rABCafc426328870: rpc: Relock wallet only if most recent callback
Branches
Unknown
Tags
Unknown