rpc: enable filtering getnodeaddresses by network

pull/826/head
Jon Atack 4 years ago
parent 80ba294854
commit 6c98c09991
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

@ -853,6 +853,7 @@ static RPCHelpMan getnodeaddresses()
"\nReturn known addresses, which can potentially be used to find new nodes in the network.\n", "\nReturn known addresses, which can potentially be used to find new nodes in the network.\n",
{ {
{"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The maximum number of addresses to return. Specify 0 to return all known addresses."}, {"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The maximum number of addresses to return. Specify 0 to return all known addresses."},
{"network", RPCArg::Type::STR, RPCArg::DefaultHint{"all networks"}, "Return only addresses of the specified network. Can be one of: " + Join(GetNetworkNames(), ", ") + "."},
}, },
RPCResult{ RPCResult{
RPCResult::Type::ARR, "", "", RPCResult::Type::ARR, "", "",
@ -869,7 +870,10 @@ static RPCHelpMan getnodeaddresses()
}, },
RPCExamples{ RPCExamples{
HelpExampleCli("getnodeaddresses", "8") HelpExampleCli("getnodeaddresses", "8")
+ HelpExampleRpc("getnodeaddresses", "8") + HelpExampleCli("getnodeaddresses", "4 \"i2p\"")
+ HelpExampleCli("-named getnodeaddresses", "network=onion count=12")
+ HelpExampleRpc("getnodeaddresses", "8")
+ HelpExampleRpc("getnodeaddresses", "4, \"i2p\"")
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
@ -879,8 +883,13 @@ static RPCHelpMan getnodeaddresses()
const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()}; const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()};
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range"); if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
const std::optional<Network> network{request.params[1].isNull() ? std::nullopt : std::optional<Network>{ParseNetwork(request.params[1].get_str())}};
if (network == NET_UNROUTABLE) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Network not recognized: %s", request.params[1].get_str()));
}
// returns a shuffled list of CAddress // returns a shuffled list of CAddress
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)}; const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, network)};
UniValue ret(UniValue::VARR); UniValue ret(UniValue::VARR);
for (const CAddress& addr : vAddr) { for (const CAddress& addr : vAddr) {

Loading…
Cancel
Save