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 #26457#pullrequestreview-1181641835).
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 https://github.com/mzumsande/bitcoin/commit/5f900e27d0e5ceaa3b800a2eb5a8e8ff6bccad3b). Since this conflicts with #25923 and should be a separate discussion, I removed that commit from this PR.