Skip to content

Commit 67e2c2f

Browse files
committed
[RPC] add network field to getnodeaddresses
1 parent b4832b2 commit 67e2c2f

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ uint32_t CNetAddr::GetLinkedIPv4() const
636636
assert(false);
637637
}
638638

639-
uint32_t CNetAddr::GetNetClass() const
639+
Network CNetAddr::GetNetClass() const
640640
{
641641
// Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
642642

src/netaddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class CNetAddr
188188
std::string ToStringIP() const;
189189
uint64_t GetHash() const;
190190
bool GetInAddr(struct in_addr* pipv4Addr) const;
191-
uint32_t GetNetClass() const;
191+
Network GetNetClass() const;
192192

193193
//! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
194194
uint32_t GetLinkedIPv4() const;

src/rpc/net.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
572572
"\nResult:\n"
573573
"[\n"
574574
" {\n"
575-
" \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) keeping track of when the node was last seen\n"
576-
" \"services\": n, (numeric) The services offered\n"
575+
" \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) when the node was last seen\n"
576+
" \"services\": n, (numeric) The services offered by the node\n"
577577
" \"address\": \"host\", (string) The address of the node\n"
578-
" \"port\": n (numeric) The port of the node\n"
578+
" \"port\": n, (numeric) The port number of the node\n"
579+
" \"network\": \"xxxx\" (string) The network (ipv4, ipv6, onion) the node connected through\n"
579580
" }\n"
580581
" ,...\n"
581582
"]\n"
@@ -589,15 +590,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
589590
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
590591
}
591592

592-
int count = 1;
593-
if (!request.params[0].isNull()) {
594-
count = request.params[0].get_int();
595-
if (count < 0) {
596-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
597-
}
598-
}
593+
const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()};
594+
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
595+
599596
// returns a shuffled list of CAddress
600-
std::vector<CAddress> vAddr = g_connman->GetAddresses(count, /* max_pct */ 0);
597+
const std::vector<CAddress> vAddr{g_connman->GetAddresses(count, /* max_pct */ 0)};
601598
UniValue ret(UniValue::VARR);
602599

603600
for (const CAddress& addr : vAddr) {
@@ -606,6 +603,7 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
606603
obj.pushKV("services", (uint64_t)addr.nServices);
607604
obj.pushKV("address", addr.ToStringIP());
608605
obj.pushKV("port", addr.GetPort());
606+
obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
609607
ret.push_back(obj);
610608
}
611609
return ret;

test/functional/rpc_net.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _test_getnodeaddresses(self):
107107
for i in range(10000):
108108
first_octet = i >> 8
109109
second_octet = i % 256
110-
a = "{}.{}.1.1".format(first_octet, second_octet)
110+
a = "{}.{}.1.1".format(first_octet, second_octet) # IPv4
111111
imported_addrs.append(a)
112112
self.nodes[0].addpeeraddress(a, 51472)
113113

@@ -124,6 +124,7 @@ def _test_getnodeaddresses(self):
124124
assert_equal(a["services"], NODE_NETWORK)
125125
assert a["address"] in imported_addrs
126126
assert_equal(a["port"], 51472)
127+
assert_equal(a["network"], "ipv4")
127128

128129
node_addresses = self.nodes[0].getnodeaddresses(1)
129130
assert_equal(len(node_addresses), 1)

0 commit comments

Comments
 (0)