From 6c98c099918bd20e2d3aa123643d6e3594e080e4 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 2 May 2021 17:15:25 +0200 Subject: rpc: enable filtering getnodeaddresses by network --- src/rpc/net.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/rpc') diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 18e4355e88..07b085ce74 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -853,6 +853,7 @@ static RPCHelpMan getnodeaddresses() "\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."}, + {"network", RPCArg::Type::STR, RPCArg::DefaultHint{"all networks"}, "Return only addresses of the specified network. Can be one of: " + Join(GetNetworkNames(), ", ") + "."}, }, RPCResult{ RPCResult::Type::ARR, "", "", @@ -869,7 +870,10 @@ static RPCHelpMan getnodeaddresses() }, RPCExamples{ 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 { @@ -879,8 +883,13 @@ static RPCHelpMan getnodeaddresses() 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"); + const std::optional network{request.params[1].isNull() ? std::nullopt : std::optional{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 - const std::vector vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)}; + const std::vector vAddr{connman.GetAddresses(count, /* max_pct */ 0, network)}; UniValue ret(UniValue::VARR); for (const CAddress& addr : vAddr) { -- cgit v1.2.3