MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / addconnection

Function addconnection

src/rpc/net.cpp:389–450  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

387}
388
389static RPCMethod addconnection()
390{
391 return RPCMethod{
392 "addconnection",
393 "Open an outbound connection to a specified node. This RPC is for testing only.\n",
394 {
395 {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address and port to attempt connecting to."},
396 {"connection_type", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of connection to open (\"outbound-full-relay\", \"block-relay-only\", \"addr-fetch\" or \"feeler\")."},
397 {"v2transport", RPCArg::Type::BOOL, RPCArg::Optional::NO, "Attempt to connect using BIP324 v2 transport protocol"},
398 },
399 RPCResult{
400 RPCResult::Type::OBJ, "", "",
401 {
402 { RPCResult::Type::STR, "address", "Address of newly added connection." },
403 { RPCResult::Type::STR, "connection_type", "Type of connection opened." },
404 }},
405 RPCExamples{
406 HelpExampleCli("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\" true")
407 + HelpExampleRpc("addconnection", "\"192.168.0.6:8333\" \"outbound-full-relay\" true")
408 },
409 [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
410{
411 if (Params().GetChainType() != ChainType::REGTEST) {
412 throw std::runtime_error("addconnection is for regression testing (-regtest mode) only.");
413 }
414
415 const std::string address = request.params[0].get_str();
416 auto conn_type_in{util::TrimStringView(self.Arg<std::string_view>("connection_type"))};
417 ConnectionType conn_type{};
418 if (conn_type_in == "outbound-full-relay") {
419 conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
420 } else if (conn_type_in == "block-relay-only") {
421 conn_type = ConnectionType::BLOCK_RELAY;
422 } else if (conn_type_in == "addr-fetch") {
423 conn_type = ConnectionType::ADDR_FETCH;
424 } else if (conn_type_in == "feeler") {
425 conn_type = ConnectionType::FEELER;
426 } else {
427 throw JSONRPCError(RPC_INVALID_PARAMETER, self.ToString());
428 }
429 bool use_v2transport{self.Arg<bool>("v2transport")};
430
431 NodeContext& node = EnsureAnyNodeContext(request.context);
432 CConnman& connman = EnsureConnman(node);
433
434 if (use_v2transport && !(connman.GetLocalServices() & NODE_P2P_V2)) {
435 throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: Adding v2transport connections requires -v2transport init flag to be set.");
436 }
437
438 const bool success = connman.AddConnection(address, conn_type, use_v2transport);
439 if (!success) {
440 throw JSONRPCError(RPC_CLIENT_NODE_CAPACITY_REACHED, "Error: Already at capacity for specified connection type.");
441 }
442
443 UniValue info(UniValue::VOBJ);
444 info.pushKV("address", address);
445 info.pushKV("connection_type", conn_type_in);
446

Callers

nothing calls this directly

Calls 10

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
TrimStringViewFunction · 0.85
JSONRPCErrorFunction · 0.85
GetLocalServicesMethod · 0.80
AddConnectionMethod · 0.80
pushKVMethod · 0.80
ParamsClass · 0.50
GetChainTypeMethod · 0.45
ToStringMethod · 0.45

Tested by

no test coverage detected