merge bitcoin#21594: add network field to getnodeaddresses

This commit is contained in:
Kittywhiskers Van Gogh 2024-04-12 16:37:49 +00:00
parent ce992f7a3a
commit 51edeb082c
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 17 additions and 13 deletions

View File

@ -0,0 +1,5 @@
RPC changes
-----------------------
- The `getnodeaddresses` RPC now returns a "network" field indicating the
network type (ipv4, ipv6, onion, or i2p) for each address.

View File

@ -901,7 +901,7 @@ static RPCHelpMan setnetworkactive()
static RPCHelpMan getnodeaddresses() static RPCHelpMan getnodeaddresses()
{ {
return RPCHelpMan{"getnodeaddresses", return 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, /* default */ "1", "The maximum number of addresses to return. Specify 0 to return all known addresses."}, {"count", RPCArg::Type::NUM, /* default */ "1", "The maximum number of addresses to return. Specify 0 to return all known addresses."},
}, },
@ -910,10 +910,11 @@ static RPCHelpMan getnodeaddresses()
{ {
{RPCResult::Type::OBJ, "", "", {RPCResult::Type::OBJ, "", "",
{ {
{RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " of when the node was last seen"}, {RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
{RPCResult::Type::NUM, "services", "The services offered"}, {RPCResult::Type::NUM, "services", "The services offered by the node"},
{RPCResult::Type::STR, "address", "The address of the node"}, {RPCResult::Type::STR, "address", "The address of the node"},
{RPCResult::Type::NUM, "port", "The port of the node"}, {RPCResult::Type::NUM, "port", "The port number of the node"},
{RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") the node connected through"},
}}, }},
} }
}, },
@ -928,15 +929,11 @@ static RPCHelpMan getnodeaddresses()
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
} }
int count = 1; const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()};
if (!request.params[0].isNull()) { if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
count = request.params[0].get_int();
if (count < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
}
}
// returns a shuffled list of CAddress // returns a shuffled list of CAddress
std::vector<CAddress> vAddr = node.connman->GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt); const std::vector<CAddress> vAddr{node.connman->GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)};
UniValue ret(UniValue::VARR); UniValue ret(UniValue::VARR);
for (const CAddress& addr : vAddr) { for (const CAddress& addr : vAddr) {
@ -945,6 +942,7 @@ static RPCHelpMan getnodeaddresses()
obj.pushKV("services", (uint64_t)addr.nServices); obj.pushKV("services", (uint64_t)addr.nServices);
obj.pushKV("address", addr.ToStringIP()); obj.pushKV("address", addr.ToStringIP());
obj.pushKV("port", addr.GetPort()); obj.pushKV("port", addr.GetPort());
obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
ret.push_back(obj); ret.push_back(obj);
} }
return ret; return ret;

View File

@ -212,7 +212,7 @@ class NetTest(DashTestFramework):
for i in range(10000): for i in range(10000):
first_octet = i >> 8 first_octet = i >> 8
second_octet = i % 256 second_octet = i % 256
a = "{}.{}.1.1".format(first_octet, second_octet) a = "{}.{}.1.1".format(first_octet, second_octet) # IPV4
imported_addrs.append(a) imported_addrs.append(a)
self.nodes[0].addpeeraddress(a, 8333) self.nodes[0].addpeeraddress(a, 8333)
@ -229,6 +229,7 @@ class NetTest(DashTestFramework):
assert_equal(a["services"], NODE_NETWORK) assert_equal(a["services"], NODE_NETWORK)
assert a["address"] in imported_addrs assert a["address"] in imported_addrs
assert_equal(a["port"], 8333) assert_equal(a["port"], 8333)
assert_equal(a["network"], "ipv4")
node_addresses = self.nodes[0].getnodeaddresses(1) node_addresses = self.nodes[0].getnodeaddresses(1)
assert_equal(len(node_addresses), 1) assert_equal(len(node_addresses), 1)