diff options
author | Jon Atack <jon@atack.com> | 2021-05-02 17:15:25 +0200 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2021-05-19 13:06:02 +0200 |
commit | 6c98c099918bd20e2d3aa123643d6e3594e080e4 (patch) | |
tree | 7d0cc8cc68ab1ad9e518c55248581787df732e3b /src | |
parent | 80ba294854e5025bcada58f1403858e6ea1d4380 (diff) |
rpc: enable filtering getnodeaddresses by network
Diffstat (limited to 'src')
-rw-r--r-- | src/rpc/net.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
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> 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 - 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); for (const CAddress& addr : vAddr) { |