Skip to content

Commit 3a4d1a2

Browse files
committed
net: merge AlreadyConnectedToAddress() and FindNode(CNetAddr)
`CConnman::AlreadyConnectedToAddress()` is the only caller of `CConnman::FindNode(CNetAddr)`, so merge the two in one function. The unit test that checked whether `AlreadyConnectedToAddress()` ignores the port is now unnecessary because now the function takes a `CNetAddr` argument. It has no access to the port.
1 parent d8fe258 commit 3a4d1a2

File tree

4 files changed

+8
-27
lines changed

4 files changed

+8
-27
lines changed

src/net.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,6 @@ bool IsLocal(const CService& addr)
331331
return mapLocalHost.count(addr) > 0;
332332
}
333333

334-
CNode* CConnman::FindNode(const CNetAddr& ip)
335-
{
336-
LOCK(m_nodes_mutex);
337-
for (CNode* pnode : m_nodes) {
338-
if (static_cast<CNetAddr>(pnode->addr) == ip) {
339-
return pnode;
340-
}
341-
}
342-
return nullptr;
343-
}
344-
345334
CNode* CConnman::FindNode(const std::string& addrName)
346335
{
347336
LOCK(m_nodes_mutex);
@@ -364,9 +353,10 @@ CNode* CConnman::FindNode(const CService& addr)
364353
return nullptr;
365354
}
366355

367-
bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
356+
bool CConnman::AlreadyConnectedToAddress(const CNetAddr& addr) const
368357
{
369-
return FindNode(static_cast<CNetAddr>(addr));
358+
LOCK(m_nodes_mutex);
359+
return std::ranges::any_of(m_nodes, [&addr](CNode* node) { return node->addr == addr; });
370360
}
371361

372362
bool CConnman::CheckIncomingNonce(uint64_t nonce)

src/net.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,15 +1365,13 @@ class CConnman
13651365

13661366
uint64_t CalculateKeyedNetGroup(const CNetAddr& ad) const;
13671367

1368-
CNode* FindNode(const CNetAddr& ip);
13691368
CNode* FindNode(const std::string& addrName);
13701369
CNode* FindNode(const CService& addr);
13711370

13721371
/**
1373-
* Determine whether we're already connected to a given address, in order to
1374-
* avoid initiating duplicate connections.
1372+
* Determine whether we're already connected to a given address.
13751373
*/
1376-
bool AlreadyConnectedToAddress(const CAddress& addr);
1374+
bool AlreadyConnectedToAddress(const CNetAddr& addr) const;
13771375

13781376
bool AttemptToEvictConnection();
13791377
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);

src/test/net_peer_connection_tests.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,8 @@ BOOST_FIXTURE_TEST_CASE(test_addnode_getaddednodeinfo_and_connection_detection,
151151
}
152152

153153
BOOST_TEST_MESSAGE("\nCheck that all connected peers are correctly detected as connected");
154-
for (auto node : connman->TestNodes()) {
155-
BOOST_CHECK(connman->AlreadyConnectedPublic(node->addr));
156-
}
157-
158-
BOOST_TEST_MESSAGE("\nCheck that peers with the same addresses as connected peers but different ports are detected as connected.");
159-
for (auto node : connman->TestNodes()) {
160-
uint16_t changed_port = node->addr.GetPort() + 1;
161-
CService address_with_changed_port{node->addr, changed_port};
162-
BOOST_CHECK(connman->AlreadyConnectedPublic(CAddress{address_with_changed_port, NODE_NONE}));
154+
for (const auto& node : connman->TestNodes()) {
155+
BOOST_CHECK(connman->AlreadyConnectedToAddressPublic(node->addr));
163156
}
164157

165158
// Clean up

src/test/util/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct ConnmanTestMsg : public CConnman {
8989
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const;
9090
void FlushSendBuffer(CNode& node) const;
9191

92-
bool AlreadyConnectedPublic(const CAddress& addr) { return AlreadyConnectedToAddress(addr); };
92+
bool AlreadyConnectedToAddressPublic(const CNetAddr& addr) { return AlreadyConnectedToAddress(addr); };
9393

9494
CNode* ConnectNodePublic(PeerManager& peerman, const char* pszDest, ConnectionType conn_type)
9595
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);

0 commit comments

Comments
 (0)