Skip to content

Commit cb79cab

Browse files
committed
Merge #21594: rpc: add network field to getnodeaddresses
5c44678 rpc: improve getnodeaddresses help (Jon Atack) 1b91898 rpc: simplify/constify getnodeaddresses code (Jon Atack) 3bb6e7b rpc: add network field to rpc getnodeaddresses (Jon Atack) Pull request description: This patch adds a network field to RPC `getnodeaddresses`, which is useful on its own, particularly with the addition of new networks like I2P and others in the future, and which I also found helpful for adding a new CLI command as a follow-up to this pull that calls `getnodeaddresses` and needs to know the network of each address. While here, also improve the `getnodeaddresses` code and help. ``` $ bitcoin-cli -signet getnodeaddresses 3 [ { "time": 1611564659, "services": 1033, "address": "2600:1702:3c30:734f:8f2e:744b:2a51:dfa5", "port": 38333, "network": "ipv6" }, { "time": 1617531931, "services": 1033, "address": "153.126.143.201", "port": 38333, "network": "ipv4" }, { "time": 1617473058, "services": 1033, "address": "nsgyo7begau4yecc46ljfecaykyzszcseapxmtu6adrfagfrrzrlngyd.onion", "port": 38333, "network": "onion" } ] $ bitcoin-cli help getnodeaddresses getnodeaddresses ( count ) Return known addresses, which can potentially be used to find new nodes in the network. Arguments: 1. count (numeric, optional, default=1) The maximum number of addresses to return. Specify 0 to return all known addresses. Result: [ (json array) { (json object) "time" : xxx, (numeric) The UNIX epoch time when the node was last seen "services" : n, (numeric) The services offered by the node "address" : "str", (string) The address of the node "port" : n, (numeric) The port number of the node "network" : "str" (string) The network (ipv4, ipv6, onion, i2p) the node connected through }, ... ] ``` Future idea: allow passing `getnodeaddresses` a network (or networks) as an argument to return only addresses in that network. ACKs for top commit: laanwj: Tested ACK 5c44678 jarolrod: re-ACK 5c44678 promag: Code review ACK 5c44678. Tree-SHA512: ab0101f50c76d98c3204133b9f2ab6b7b17193ada31455ef706ad11afbf48f472fa3deb33e96028682369b35710ccd07d81863d2fd55c1485f32432f2b75efa8
2 parents aa69471 + 5c44678 commit cb79cab

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

doc/release-notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Changes to Wallet or GUI related settings can be found in the GUI or Wallet sect
126126

127127
- Passing an invalid `-rpcauth` argument now cause bitcoind to fail to start. (#20461)
128128

129+
- The `getnodeaddresses` RPC now returns a "network" field indicating the
130+
network type (ipv4, ipv6, onion, or i2p) for each address. (#21594)
131+
129132
Tools and Utilities
130133
-------------------
131134

src/rpc/net.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ static RPCHelpMan setnetworkactive()
835835
static RPCHelpMan getnodeaddresses()
836836
{
837837
return RPCHelpMan{"getnodeaddresses",
838-
"\nReturn known addresses which can potentially be used to find new nodes in the network\n",
838+
"\nReturn known addresses, which can potentially be used to find new nodes in the network.\n",
839839
{
840840
{"count", RPCArg::Type::NUM, /* default */ "1", "The maximum number of addresses to return. Specify 0 to return all known addresses."},
841841
},
@@ -844,10 +844,11 @@ static RPCHelpMan getnodeaddresses()
844844
{
845845
{RPCResult::Type::OBJ, "", "",
846846
{
847-
{RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " of when the node was last seen"},
848-
{RPCResult::Type::NUM, "services", "The services offered"},
847+
{RPCResult::Type::NUM_TIME, "time", "The " + UNIX_EPOCH_TIME + " when the node was last seen"},
848+
{RPCResult::Type::NUM, "services", "The services offered by the node"},
849849
{RPCResult::Type::STR, "address", "The address of the node"},
850-
{RPCResult::Type::NUM, "port", "The port of the node"},
850+
{RPCResult::Type::NUM, "port", "The port number of the node"},
851+
{RPCResult::Type::STR, "network", "The network (" + Join(GetNetworkNames(), ", ") + ") the node connected through"},
851852
}},
852853
}
853854
},
@@ -862,15 +863,11 @@ static RPCHelpMan getnodeaddresses()
862863
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
863864
}
864865

865-
int count = 1;
866-
if (!request.params[0].isNull()) {
867-
count = request.params[0].get_int();
868-
if (count < 0) {
869-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
870-
}
871-
}
866+
const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()};
867+
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
868+
872869
// returns a shuffled list of CAddress
873-
std::vector<CAddress> vAddr = node.connman->GetAddresses(count, /* max_pct */ 0);
870+
const std::vector<CAddress> vAddr{node.connman->GetAddresses(count, /* max_pct */ 0)};
874871
UniValue ret(UniValue::VARR);
875872

876873
for (const CAddress& addr : vAddr) {
@@ -879,6 +876,7 @@ static RPCHelpMan getnodeaddresses()
879876
obj.pushKV("services", (uint64_t)addr.nServices);
880877
obj.pushKV("address", addr.ToStringIP());
881878
obj.pushKV("port", addr.GetPort());
879+
obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
882880
ret.push_back(obj);
883881
}
884882
return ret;

test/functional/rpc_net.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_getnodeaddresses(self):
195195
for i in range(10000):
196196
first_octet = i >> 8
197197
second_octet = i % 256
198-
a = "{}.{}.1.1".format(first_octet, second_octet)
198+
a = "{}.{}.1.1".format(first_octet, second_octet) # IPV4
199199
imported_addrs.append(a)
200200
self.nodes[0].addpeeraddress(a, 8333)
201201

@@ -212,6 +212,7 @@ def test_getnodeaddresses(self):
212212
assert_equal(a["services"], NODE_NETWORK | NODE_WITNESS)
213213
assert a["address"] in imported_addrs
214214
assert_equal(a["port"], 8333)
215+
assert_equal(a["network"], "ipv4")
215216

216217
node_addresses = self.nodes[0].getnodeaddresses(1)
217218
assert_equal(len(node_addresses), 1)

0 commit comments

Comments
 (0)