Skip to content

Commit 884bde5

Browse files
author
MarcoFalke
committed
Merge #20291: [net] Consolidate logic around calling CAddrMan::Connected()
0bfce9d [addrman] Fix Connected() comment (John Newbery) eefe194 [net] Consolidate logic around calling CAddrMan::Connected() (John Newbery) Pull request description: Currently, the logic around whether we called CAddrMan::Connected() for a peer is spread between verack processing (where we discard inbound peers) and FinalizeNode (where we discard misbehaving and block-relay-only peers). Consolidate that logic to a single place. Also remove the CNode.fCurrentlyConnected bool, which is now redundant. We can rely on CNode.fSuccessfullyConnected, since the two bools were only ever flipped to true in the same place. ACKs for top commit: mzumsande: Code review ACK 0bfce9d amitiuttarwar: code review ACK 0bfce9d. nice tidy, and bonus that we get to remove an unnecessary call to `cs_main` Tree-SHA512: 1ab74dae3bc12a6846da57c853033e546bb4f91caa39f4c50bf0cf7eca59cb917bdb2ef795da55363e7e9f70949cb28bb3be004cb3afa4389f970d2fe097d932
2 parents c4d1e24 + 0bfce9d commit 884bde5

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/addrman.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,18 @@ friend class CAddrManTest;
281281
//! Select several addresses at once.
282282
void GetAddr_(std::vector<CAddress> &vAddr, size_t max_addresses, size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs);
283283

284-
//! Mark an entry as currently-connected-to.
285-
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
284+
/** We have successfully connected to this peer. Calling this function
285+
* updates the CAddress's nTime, which is used in our IsTerrible()
286+
* decisions and gossiped to peers. Callers should be careful that updating
287+
* this information doesn't leak topology information to network spies.
288+
*
289+
* net_processing calls this function when it *disconnects* from a peer to
290+
* not leak information about currently connected peers.
291+
*
292+
* @param[in] addr The address of the peer we were connected to
293+
* @param[in] nTime The time that we were last connected to this peer
294+
*/
295+
void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
286296

287297
//! Update an entry's service bits.
288298
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -704,7 +714,7 @@ friend class CAddrManTest;
704714
return vAddr;
705715
}
706716

707-
//! Mark an entry as currently-connected-to.
717+
//! Outer function for Connected_()
708718
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
709719
{
710720
LOCK(cs);

src/net_processing.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,6 @@ namespace {
290290
struct CNodeState {
291291
//! The peer's address
292292
const CService address;
293-
//! Whether we have a fully established connection.
294-
bool fCurrentlyConnected;
295293
//! The best known block we know this peer has announced.
296294
const CBlockIndex *pindexBestKnownBlock;
297295
//! The hash of the last unknown block this peer has announced.
@@ -390,7 +388,6 @@ struct CNodeState {
390388
CNodeState(CAddress addrIn, bool is_inbound, bool is_manual)
391389
: address(addrIn), m_is_inbound(is_inbound), m_is_manual_connection(is_manual)
392390
{
393-
fCurrentlyConnected = false;
394391
pindexBestKnownBlock = nullptr;
395392
hashLastUnknownBlock.SetNull();
396393
pindexLastCommonBlock = nullptr;
@@ -857,8 +854,9 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
857854
if (state->fSyncStarted)
858855
nSyncStarted--;
859856

860-
if (misbehavior == 0 && state->fCurrentlyConnected && !node.IsBlockOnlyConn()) {
861-
// Note: we avoid changing visible addrman state for block-relay-only peers
857+
if (node.fSuccessfullyConnected && misbehavior == 0 &&
858+
!node.IsBlockOnlyConn() && !node.IsInboundConn()) {
859+
// Only change visible addrman state for outbound, full-relay peers
862860
fUpdateConnectionTime = true;
863861
}
864862

@@ -2488,9 +2486,6 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
24882486
if (pfrom.fSuccessfullyConnected) return;
24892487

24902488
if (!pfrom.IsInboundConn()) {
2491-
// Mark this node as currently connected, so we update its timestamp later.
2492-
LOCK(cs_main);
2493-
State(pfrom.GetId())->fCurrentlyConnected = true;
24942489
LogPrintf("New outbound peer connected: version: %d, blocks=%d, peer=%d%s (%s)\n",
24952490
pfrom.nVersion.load(), pfrom.nStartingHeight,
24962491
pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToString()) : ""),

0 commit comments

Comments
 (0)