Skip to content

Commit 34b9ef4

Browse files
committed
net/rpc: Makes CConnman::GetAddedNodeInfo able to return only non-connected address on request
`CConnman::GetAddedNodeInfo` is used both to get a list of addresses to manually connect to in `CConnman::ThreadOpenAddedConnections`, and to report about manually added connections in `getaddednodeinfo`. In both cases, all addresses added to `m_added_nodes` are returned, however the nodes we are already connected to are only relevant to the latter, in the former they are actively discarded. Parametrizes `CConnman::GetAddedNodeInfo` so we can ask for only addresses we are not connected to, to avoid passing useless information around.
1 parent 94e8882 commit 34b9ef4

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/net.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
28042804
return ret;
28052805
}
28062806

2807-
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
2807+
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo(bool include_connected) const
28082808
{
28092809
std::vector<AddedNodeInfo> ret;
28102810

@@ -2839,6 +2839,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
28392839
// strAddNode is an IP:port
28402840
auto it = mapConnected.find(service);
28412841
if (it != mapConnected.end()) {
2842+
if (!include_connected) {
2843+
continue;
2844+
}
28422845
addedNode.resolvedAddress = service;
28432846
addedNode.fConnected = true;
28442847
addedNode.fInbound = it->second;
@@ -2847,6 +2850,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
28472850
// strAddNode is a name
28482851
auto it = mapConnectedByName.find(addr.m_added_node);
28492852
if (it != mapConnectedByName.end()) {
2853+
if (!include_connected) {
2854+
continue;
2855+
}
28502856
addedNode.resolvedAddress = it->second.second;
28512857
addedNode.fConnected = true;
28522858
addedNode.fInbound = it->second.first;
@@ -2865,21 +2871,19 @@ void CConnman::ThreadOpenAddedConnections()
28652871
while (true)
28662872
{
28672873
CSemaphoreGrant grant(*semAddnode);
2868-
std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
2874+
std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo(/*include_connected=*/false);
28692875
bool tried = false;
28702876
for (const AddedNodeInfo& info : vInfo) {
2871-
if (!info.fConnected) {
2872-
if (!grant) {
2873-
// If we've used up our semaphore and need a new one, let's not wait here since while we are waiting
2874-
// the addednodeinfo state might change.
2875-
break;
2876-
}
2877-
tried = true;
2878-
CAddress addr(CService(), NODE_NONE);
2879-
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
2880-
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
2881-
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
2877+
if (!grant) {
2878+
// If we've used up our semaphore and need a new one, let's not wait here since while we are waiting
2879+
// the addednodeinfo state might change.
2880+
break;
28822881
}
2882+
tried = true;
2883+
CAddress addr(CService(), NODE_NONE);
2884+
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
2885+
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
2886+
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
28832887
}
28842888
// Retry every 60 seconds if a connection was attempted, otherwise two seconds
28852889
if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ class CConnman
12011201

12021202
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
12031203
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1204-
std::vector<AddedNodeInfo> GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1204+
std::vector<AddedNodeInfo> GetAddedNodeInfo(bool include_connected) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
12051205

12061206
/**
12071207
* Attempts to open a connection. Currently only used from tests.

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static RPCHelpMan getaddednodeinfo()
487487
NodeContext& node = EnsureAnyNodeContext(request.context);
488488
const CConnman& connman = EnsureConnman(node);
489489

490-
std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo();
490+
std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo(/*include_connected=*/true);
491491

492492
if (!request.params[0].isNull()) {
493493
bool found = false;

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
121121
connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
122122
});
123123
}
124-
(void)connman.GetAddedNodeInfo();
124+
(void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
125125
(void)connman.GetExtraFullOutboundCount();
126126
(void)connman.GetLocalServices();
127127
(void)connman.GetMaxOutboundTarget();

0 commit comments

Comments
 (0)