Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#26515: rpc: skip getpeerinfo for a peer without…
Browse files Browse the repository at this point in the history
… CNodeStateStats

6fefd49 rpc: Require NodeStateStats object in getpeerinfo (Martin Zumsande)

Pull request description:

  The objects `CNode`, `CNodeState` and `Peer` store different info about a peer - `InitializeNode()` and `FinalizeNode()` make sure that for the duration of a connection, we should always have one of each for a peer.

  Therefore, there is no situation in which, as part of getpeerinfo RPC,  `GetNodeStateStats()` (which requires a `CNodeState` and a `Peer` entry for a `NodeId` to succeed)  could fail for a legitimate reason while the peer is connected - this can only happen if there is a race condition between peer disconnection and the `getpeerinfo` processing (see also a more detailed description of this in bitcoin/bitcoin#26457 (review)).

  But in this case I think it's better to just not include the newly disconnected peer in the response instead of returning just parts of its data.

  An earlier version of this PR also made the affected `CNodeStateStats` fields non-optional (see mzumsande/bitcoin@5f900e2). Since this conflicts with #25923 and should be a separate discussion, I removed that commit from this PR.

ACKs for top commit:
  dergoegge:
    Approach ACK 6fefd49
  MarcoFalke:
    review ACK 6fefd49 👒

Tree-SHA512: 89c8f7318df4634c1630415de9c8350e6dc2d14d9d07e039e5b180c51bfd3ee2ce99eeac4f9f858af7de846f7a6b48fcae96ebac08495b30e431a5d2d4660532
  • Loading branch information
MarcoFalke committed Dec 19, 2022
2 parents 65f5cfd + 6fefd49 commit 3d97496
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ static RPCHelpMan getpeerinfo()
UniValue obj(UniValue::VOBJ);
CNodeStateStats statestats;
bool fStateStats = peerman.GetNodeStateStats(stats.nodeid, statestats);
// GetNodeStateStats() requires the existence of a CNodeState and a Peer object
// to succeed for this peer. These are created at connection initialisation and
// exist for the duration of the connection - except if there is a race where the
// peer got disconnected in between the GetNodeStats() and the GetNodeStateStats()
// calls. In this case, the peer doesn't need to be reported here.
if (!fStateStats) {
continue;
}
obj.pushKV("id", stats.nodeid);
obj.pushKV("addr", stats.m_addr_name);
if (stats.addrBind.IsValid()) {
Expand Down

0 comments on commit 3d97496

Please sign in to comment.