net, rpc: Enable AddrFetch connections for functional testing

Co-authored-by: Amiti Uttarwar <amiti@uttarwar.org>
This commit is contained in:
Martin Zumsande 2021-05-31 22:49:42 +02:00
parent 533500d907
commit c34ad3309f
4 changed files with 23 additions and 8 deletions

View file

@ -1166,16 +1166,29 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type) bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type)
{ {
if (conn_type != ConnectionType::OUTBOUND_FULL_RELAY && conn_type != ConnectionType::BLOCK_RELAY) return false; std::optional<int> max_connections;
switch (conn_type) {
const int max_connections = conn_type == ConnectionType::OUTBOUND_FULL_RELAY ? m_max_outbound_full_relay : m_max_outbound_block_relay; case ConnectionType::INBOUND:
case ConnectionType::MANUAL:
case ConnectionType::FEELER:
return false;
case ConnectionType::OUTBOUND_FULL_RELAY:
max_connections = m_max_outbound_full_relay;
break;
case ConnectionType::BLOCK_RELAY:
max_connections = m_max_outbound_block_relay;
break;
// no limit for ADDR_FETCH because -seednode has no limit either
case ConnectionType::ADDR_FETCH:
break;
} // no default case, so the compiler can warn about missing cases
// Count existing connections // Count existing connections
int existing_connections = WITH_LOCK(cs_vNodes, int existing_connections = WITH_LOCK(cs_vNodes,
return std::count_if(vNodes.begin(), vNodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; });); return std::count_if(vNodes.begin(), vNodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; }););
// Max connections of specified type already exist // Max connections of specified type already exist
if (existing_connections >= max_connections) return false; if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
// Max total outbound connections already exist // Max total outbound connections already exist
CSemaphoreGrant grant(*semOutbound, true); CSemaphoreGrant grant(*semOutbound, true);

View file

@ -890,6 +890,7 @@ public:
* *
* @param[in] address Address of node to try connecting to * @param[in] address Address of node to try connecting to
* @param[in] conn_type ConnectionType::OUTBOUND or ConnectionType::BLOCK_RELAY * @param[in] conn_type ConnectionType::OUTBOUND or ConnectionType::BLOCK_RELAY
* or ConnectionType::ADDR_FETCH
* @return bool Returns false if there are no available * @return bool Returns false if there are no available
* slots for this connection: * slots for this connection:
* - conn_type not a supported ConnectionType * - conn_type not a supported ConnectionType

View file

@ -337,7 +337,7 @@ static RPCHelpMan addconnection()
"\nOpen an outbound connection to a specified node. This RPC is for testing only.\n", "\nOpen an outbound connection to a specified node. This RPC is for testing only.\n",
{ {
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."}, {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
{"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open, either \"outbound-full-relay\" or \"block-relay-only\"."}, {"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\" or \"addr-fetch\")."},
}, },
RPCResult{ RPCResult{
RPCResult::Type::OBJ, "", "", RPCResult::Type::OBJ, "", "",
@ -363,6 +363,8 @@ static RPCHelpMan addconnection()
conn_type = ConnectionType::OUTBOUND_FULL_RELAY; conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
} else if (conn_type_in == "block-relay-only") { } else if (conn_type_in == "block-relay-only") {
conn_type = ConnectionType::BLOCK_RELAY; conn_type = ConnectionType::BLOCK_RELAY;
} else if (conn_type_in == "addr-fetch") {
conn_type = ConnectionType::ADDR_FETCH;
} else { } else {
throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString()); throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
} }

View file

@ -557,9 +557,8 @@ class TestNode():
return p2p_conn return p2p_conn
def add_outbound_p2p_connection(self, p2p_conn, *, p2p_idx, connection_type="outbound-full-relay", **kwargs): def add_outbound_p2p_connection(self, p2p_conn, *, p2p_idx, connection_type="outbound-full-relay", **kwargs):
"""Add an outbound p2p connection from node. Either """Add an outbound p2p connection from node. Must be an
full-relay("outbound-full-relay") or "outbound-full-relay", "block-relay-only" or "addr-fetch" connection.
block-relay-only("block-relay-only") connection.
This method adds the p2p connection to the self.p2ps list and returns This method adds the p2p connection to the self.p2ps list and returns
the connection to the caller. the connection to the caller.