mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
net: get AlreadyConnectedToAddress() to iterate the nodes itself
Previously `CConnman::AlreadyConnectedToAddress()` used two calls to `CConnman::FindNode()`. However that iterates the nodes two times which is inefficient and this was the only caller of `CConnman::FindNode(const CNetAddr&)`. So, drop that `FindNode()` method and iterate the nodes inside `CConnman::AlreadyConnectedToAddress()`.
This commit is contained in:
parent
06439a14c8
commit
2ed51a699c
2 changed files with 7 additions and 13 deletions
19
src/net.cpp
19
src/net.cpp
|
@ -332,17 +332,6 @@ bool IsLocal(const CService& addr)
|
||||||
return mapLocalHost.count(addr) > 0;
|
return mapLocalHost.count(addr) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CNode* CConnman::FindNode(const CNetAddr& ip)
|
|
||||||
{
|
|
||||||
LOCK(m_nodes_mutex);
|
|
||||||
for (CNode* pnode : m_nodes) {
|
|
||||||
if (static_cast<CNetAddr>(pnode->addr) == ip) {
|
|
||||||
return pnode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
CNode* CConnman::FindNode(const std::string& addrName)
|
CNode* CConnman::FindNode(const std::string& addrName)
|
||||||
{
|
{
|
||||||
LOCK(m_nodes_mutex);
|
LOCK(m_nodes_mutex);
|
||||||
|
@ -367,7 +356,13 @@ CNode* CConnman::FindNode(const CService& addr)
|
||||||
|
|
||||||
bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
|
bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
|
||||||
{
|
{
|
||||||
return FindNode(static_cast<CNetAddr>(addr)) || FindNode(addr.ToStringAddrPort());
|
const CNetAddr& net_addr{addr};
|
||||||
|
const std::string str_addr{addr.ToStringAddrPort()};
|
||||||
|
|
||||||
|
LOCK(m_nodes_mutex);
|
||||||
|
return std::ranges::any_of(m_nodes, [&net_addr, &str_addr](CNode* node) {
|
||||||
|
return node->addr == net_addr || node->m_addr_name == str_addr;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CConnman::CheckIncomingNonce(uint64_t nonce)
|
bool CConnman::CheckIncomingNonce(uint64_t nonce)
|
||||||
|
|
|
@ -1352,7 +1352,6 @@ private:
|
||||||
|
|
||||||
uint64_t CalculateKeyedNetGroup(const CNetAddr& ad) const;
|
uint64_t CalculateKeyedNetGroup(const CNetAddr& ad) const;
|
||||||
|
|
||||||
CNode* FindNode(const CNetAddr& ip);
|
|
||||||
CNode* FindNode(const std::string& addrName);
|
CNode* FindNode(const std::string& addrName);
|
||||||
CNode* FindNode(const CService& addr);
|
CNode* FindNode(const CService& addr);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue