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.
This commit is contained in:
Sergi Delgado Segura 2023-07-25 15:47:36 -04:00
parent 94e8882d82
commit 34b9ef443b
4 changed files with 20 additions and 16 deletions

View file

@ -2804,7 +2804,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
return ret; return ret;
} }
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo(bool include_connected) const
{ {
std::vector<AddedNodeInfo> ret; std::vector<AddedNodeInfo> ret;
@ -2839,6 +2839,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
// strAddNode is an IP:port // strAddNode is an IP:port
auto it = mapConnected.find(service); auto it = mapConnected.find(service);
if (it != mapConnected.end()) { if (it != mapConnected.end()) {
if (!include_connected) {
continue;
}
addedNode.resolvedAddress = service; addedNode.resolvedAddress = service;
addedNode.fConnected = true; addedNode.fConnected = true;
addedNode.fInbound = it->second; addedNode.fInbound = it->second;
@ -2847,6 +2850,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
// strAddNode is a name // strAddNode is a name
auto it = mapConnectedByName.find(addr.m_added_node); auto it = mapConnectedByName.find(addr.m_added_node);
if (it != mapConnectedByName.end()) { if (it != mapConnectedByName.end()) {
if (!include_connected) {
continue;
}
addedNode.resolvedAddress = it->second.second; addedNode.resolvedAddress = it->second.second;
addedNode.fConnected = true; addedNode.fConnected = true;
addedNode.fInbound = it->second.first; addedNode.fInbound = it->second.first;
@ -2865,21 +2871,19 @@ void CConnman::ThreadOpenAddedConnections()
while (true) while (true)
{ {
CSemaphoreGrant grant(*semAddnode); CSemaphoreGrant grant(*semAddnode);
std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo(); std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo(/*include_connected=*/false);
bool tried = false; bool tried = false;
for (const AddedNodeInfo& info : vInfo) { for (const AddedNodeInfo& info : vInfo) {
if (!info.fConnected) { if (!grant) {
if (!grant) { // If we've used up our semaphore and need a new one, let's not wait here since while we are waiting
// If we've used up our semaphore and need a new one, let's not wait here since while we are waiting // the addednodeinfo state might change.
// the addednodeinfo state might change. break;
break;
}
tried = true;
CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
} }
tried = true;
CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
} }
// Retry every 60 seconds if a connection was attempted, otherwise two seconds // Retry every 60 seconds if a connection was attempted, otherwise two seconds
if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2))) if (!interruptNet.sleep_for(std::chrono::seconds(tried ? 60 : 2)))

View file

@ -1201,7 +1201,7 @@ public:
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex); bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex); bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
std::vector<AddedNodeInfo> GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex); std::vector<AddedNodeInfo> GetAddedNodeInfo(bool include_connected) const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
/** /**
* Attempts to open a connection. Currently only used from tests. * Attempts to open a connection. Currently only used from tests.

View file

@ -487,7 +487,7 @@ static RPCHelpMan getaddednodeinfo()
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
const CConnman& connman = EnsureConnman(node); const CConnman& connman = EnsureConnman(node);
std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo(); std::vector<AddedNodeInfo> vInfo = connman.GetAddedNodeInfo(/*include_connected=*/true);
if (!request.params[0].isNull()) { if (!request.params[0].isNull()) {
bool found = false; bool found = false;

View file

@ -121,7 +121,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool()); connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
}); });
} }
(void)connman.GetAddedNodeInfo(); (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
(void)connman.GetExtraFullOutboundCount(); (void)connman.GetExtraFullOutboundCount();
(void)connman.GetLocalServices(); (void)connman.GetLocalServices();
(void)connman.GetMaxOutboundTarget(); (void)connman.GetMaxOutboundTarget();