Skip to content

Commit 6c98c09

Browse files
committed
rpc: enable filtering getnodeaddresses by network
1 parent 80ba294 commit 6c98c09

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/rpc/net.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ static RPCHelpMan getnodeaddresses()
853853
"\nReturn known addresses, which can potentially be used to find new nodes in the network.\n",
854854
{
855855
{"count", RPCArg::Type::NUM, RPCArg::Default{1}, "The maximum number of addresses to return. Specify 0 to return all known addresses."},
856+
{"network", RPCArg::Type::STR, RPCArg::DefaultHint{"all networks"}, "Return only addresses of the specified network. Can be one of: " + Join(GetNetworkNames(), ", ") + "."},
856857
},
857858
RPCResult{
858859
RPCResult::Type::ARR, "", "",
@@ -869,7 +870,10 @@ static RPCHelpMan getnodeaddresses()
869870
},
870871
RPCExamples{
871872
HelpExampleCli("getnodeaddresses", "8")
872-
+ HelpExampleRpc("getnodeaddresses", "8")
873+
+ HelpExampleCli("getnodeaddresses", "4 \"i2p\"")
874+
+ HelpExampleCli("-named getnodeaddresses", "network=onion count=12")
875+
+ HelpExampleRpc("getnodeaddresses", "8")
876+
+ HelpExampleRpc("getnodeaddresses", "4, \"i2p\"")
873877
},
874878
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
875879
{
@@ -879,8 +883,13 @@ static RPCHelpMan getnodeaddresses()
879883
const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()};
880884
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
881885

886+
const std::optional<Network> network{request.params[1].isNull() ? std::nullopt : std::optional<Network>{ParseNetwork(request.params[1].get_str())}};
887+
if (network == NET_UNROUTABLE) {
888+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Network not recognized: %s", request.params[1].get_str()));
889+
}
890+
882891
// returns a shuffled list of CAddress
883-
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)};
892+
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, network)};
884893
UniValue ret(UniValue::VARR);
885894

886895
for (const CAddress& addr : vAddr) {

0 commit comments

Comments
 (0)