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

Function addnode

src/rpc/net.cpp:319–387  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

317}
318
319static RPCMethod addnode()
320{
321 return RPCMethod{
322 "addnode",
323 "Attempts to add or remove a node from the addnode list.\n"
324 "Or try a connection to a node once.\n"
325 "Nodes added using addnode (or -connect) are protected from DoS disconnection and are not required to be\n"
326 "full nodes/support SegWit as other outbound peers are (though such peers will not be synced from).\n" +
327 strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
328 " and are counted separately from the -maxconnections limit.\n",
329 {
330 {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address/hostname optionally followed by :port of the peer to connect to"},
331 {"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once"},
332 {"v2transport", RPCArg::Type::BOOL, RPCArg::DefaultHint{"set by -v2transport"}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
333 },
334 RPCResult{RPCResult::Type::NONE, "", ""},
335 RPCExamples{
336 HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
337 + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\" true")
338 },
339 [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
340{
341 const auto command{self.Arg<std::string_view>("command")};
342 if (command != "onetry" && command != "add" && command != "remove") {
343 throw std::runtime_error(
344 self.ToString());
345 }
346
347 NodeContext& node = EnsureAnyNodeContext(request.context);
348 CConnman& connman = EnsureConnman(node);
349
350 const auto node_arg{self.Arg<std::string_view>("node")};
351 bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
352 bool use_v2transport = self.MaybeArg<bool>("v2transport").value_or(node_v2transport);
353
354 if (use_v2transport && !node_v2transport) {
355 throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
356 }
357
358 if (command == "onetry")
359 {
360 CAddress addr;
361 connman.OpenNetworkConnection(/*addrConnect=*/addr,
362 /*fCountFailure=*/false,
363 /*grant_outbound=*/{},
364 /*pszDest=*/std::string{node_arg}.c_str(),
365 /*conn_type=*/ConnectionType::MANUAL,
366 /*use_v2transport=*/use_v2transport,
367 /*proxy_override=*/std::nullopt);
368 return UniValue::VNULL;
369 }
370
371 if (command == "add")
372 {
373 if (!connman.AddNode({std::string{node_arg}, use_v2transport})) {
374 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
375 }
376 }

Callers

nothing calls this directly

Calls 9

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
JSONRPCErrorFunction · 0.85
GetLocalServicesMethod · 0.80
OpenNetworkConnectionMethod · 0.80
AddNodeMethod · 0.80
RemoveAddedNodeMethod · 0.80
ToStringMethod · 0.45
value_orMethod · 0.45

Tested by

no test coverage detected