Skip to content

Commit

Permalink
[RPC] add network field to getnodeaddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Aug 12, 2021
1 parent b4832b2 commit 67e2c2f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/netaddress.cpp
Expand Up @@ -636,7 +636,7 @@ uint32_t CNetAddr::GetLinkedIPv4() const
assert(false);
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/netaddress.h
Expand Up @@ -188,7 +188,7 @@ class CNetAddr
std::string ToStringIP() const;
uint64_t GetHash() const;
bool GetInAddr(struct in_addr* pipv4Addr) const;
uint32_t GetNetClass() const;
Network GetNetClass() const;

//! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
uint32_t GetLinkedIPv4() const;
Expand Down
20 changes: 9 additions & 11 deletions src/rpc/net.cpp
Expand Up @@ -572,10 +572,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
"\nResult:\n"
"[\n"
" {\n"
" \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) keeping track of when the node was last seen\n"
" \"services\": n, (numeric) The services offered\n"
" \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) when the node was last seen\n"
" \"services\": n, (numeric) The services offered by the node\n"
" \"address\": \"host\", (string) The address of the node\n"
" \"port\": n (numeric) The port of the node\n"
" \"port\": n, (numeric) The port number of the node\n"
" \"network\": \"xxxx\" (string) The network (ipv4, ipv6, onion) the node connected through\n"
" }\n"
" ,...\n"
"]\n"
Expand All @@ -589,15 +590,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
}

int count = 1;
if (!request.params[0].isNull()) {
count = request.params[0].get_int();
if (count < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
}
}
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");

// returns a shuffled list of CAddress
std::vector<CAddress> vAddr = g_connman->GetAddresses(count, /* max_pct */ 0);
const std::vector<CAddress> vAddr{g_connman->GetAddresses(count, /* max_pct */ 0)};
UniValue ret(UniValue::VARR);

for (const CAddress& addr : vAddr) {
Expand All @@ -606,6 +603,7 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request)
obj.pushKV("services", (uint64_t)addr.nServices);
obj.pushKV("address", addr.ToStringIP());
obj.pushKV("port", addr.GetPort());
obj.pushKV("network", GetNetworkName(addr.GetNetClass()));
ret.push_back(obj);
}
return ret;
Expand Down
3 changes: 2 additions & 1 deletion test/functional/rpc_net.py
Expand Up @@ -107,7 +107,7 @@ def _test_getnodeaddresses(self):
for i in range(10000):
first_octet = i >> 8
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)
self.nodes[0].addpeeraddress(a, 51472)

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

node_addresses = self.nodes[0].getnodeaddresses(1)
assert_equal(len(node_addresses), 1)
Expand Down

0 comments on commit 67e2c2f

Please sign in to comment.