Skip to content

Commit 4487b80

Browse files
committed
[rpc/net] Allow v2 p2p support in addconnection
This test-only RPC is required when a TestNode initiates an outbound v2 p2p connection. Add a new arg `v2transport` so that the node can attempt v2 connections.
1 parent 16b5b4b commit 4487b80

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
18331833
RandAddEvent((uint32_t)id);
18341834
}
18351835

1836-
bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type)
1836+
bool CConnman::AddConnection(const std::string& address, ConnectionType conn_type, bool use_v2transport = false)
18371837
{
18381838
AssertLockNotHeld(m_unused_i2p_sessions_mutex);
18391839
std::optional<int> max_connections;
@@ -1866,7 +1866,7 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
18661866
CSemaphoreGrant grant(*semOutbound, true);
18671867
if (!grant) return false;
18681868

1869-
OpenNetworkConnection(CAddress(), false, std::move(grant), address.c_str(), conn_type, /*use_v2transport=*/false);
1869+
OpenNetworkConnection(CAddress(), false, std::move(grant), address.c_str(), conn_type, /*use_v2transport=*/use_v2transport);
18701870
return true;
18711871
}
18721872

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,14 @@ class CConnman
11841184
* @param[in] address Address of node to try connecting to
11851185
* @param[in] conn_type ConnectionType::OUTBOUND, ConnectionType::BLOCK_RELAY,
11861186
* ConnectionType::ADDR_FETCH or ConnectionType::FEELER
1187+
* @param[in] use_v2transport Set to true if node attempts to connect using BIP 324 v2 transport protocol.
11871188
* @return bool Returns false if there are no available
11881189
* slots for this connection:
11891190
* - conn_type not a supported ConnectionType
11901191
* - Max total outbound connection capacity filled
11911192
* - Max connection capacity for type is filled
11921193
*/
1193-
bool AddConnection(const std::string& address, ConnectionType conn_type) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1194+
bool AddConnection(const std::string& address, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
11941195

11951196
size_t GetNodeCount(ConnectionDirection) const;
11961197
uint32_t GetMappedAS(const CNetAddr& addr) const;

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
302302
{ "sendmsgtopeer", 0, "peer_id" },
303303
{ "stop", 0, "wait" },
304304
{ "addnode", 2, "v2transport" },
305+
{ "addconnection", 2, "v2transport" },
305306
};
306307
// clang-format on
307308

src/rpc/net.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static RPCHelpMan addconnection()
370370
{
371371
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
372372
{"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\", \"addr-fetch\" or \"feeler\")."},
373+
{"v2transport", RPCArg::Type::BOOL, RPCArg::Default{false}, "Attempt to connect using BIP324 v2 transport protocol"},
373374
},
374375
RPCResult{
375376
RPCResult::Type::OBJ, "", "",
@@ -378,8 +379,8 @@ static RPCHelpMan addconnection()
378379
{ RPCResult::Type::STR, "connection_type", "Type of connection opened." },
379380
}},
380381
RPCExamples{
381-
HelpExampleCli("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\"")
382-
+ HelpExampleRpc("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\"")
382+
HelpExampleCli("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\" true")
383+
+ HelpExampleRpc("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\" true")
383384
},
384385
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
385386
{
@@ -401,11 +402,16 @@ static RPCHelpMan addconnection()
401402
} else {
402403
throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
403404
}
405+
bool use_v2transport = !request.params[2].isNull() && request.params[2].get_bool();
404406

405407
NodeContext& node = EnsureAnyNodeContext(request.context);
406408
CConnman& connman = EnsureConnman(node);
407409

408-
const bool success = connman.AddConnection(address, conn_type);
410+
if (use_v2transport && !(connman.GetLocalServices() & NODE_P2P_V2)) {
411+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Adding v2transport connections requires -v2transport init flag to be set.");
412+
}
413+
414+
const bool success = connman.AddConnection(address, conn_type, use_v2transport);
409415
if (!success) {
410416
throw JSONRPCError(RPC_CLIENT_NODE_CAPACITY_REACHED, "Error: Already at capacity for specified connection type.");
411417
}

0 commit comments

Comments
 (0)