aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2018-09-18 19:49:24 -0400
committerMarcoFalke <falke.marco@gmail.com>2018-09-18 19:50:00 -0400
commitd26278988fbfc71dc558f7b2c60a184e8091450b (patch)
treea45ac6290c3c430b546c83a6aa5b7e51ffb0d3d7 /src
parent24f095df4506c599fe770309efd1c5b8048420e5 (diff)
parenta2eb6f540538d32725aecf678301a96247db6fd9 (diff)
downloadbitcoin-d26278988fbfc71dc558f7b2c60a184e8091450b.tar.xz
Merge #13152: [rpc] Add getnodeaddresses RPC command
a2eb6f5405 [rpc] Add getnodeaddresses RPC command (chris-belcher) Pull request description: Implements issue https://github.com/bitcoin/bitcoin/issues/9463 New getnodeaddresses call gives access via RPC to the peers known by the node. It may be useful for bitcoin wallets to broadcast their transactions over tor for improved privacy without using the centralized DNS seeds. getnodeaddresses is very similar to the getaddr p2p method. Please advise me on the best approach for writing an automated test. By my reading the getaddr p2p method also isn't really tested. Tree-SHA512: ad03abf518847476495b76a2f5394b8030aa86654429167fa618e21460abb505c10ef9817ec1b80472320d41d0aff5dc94a8efce023aaaaf5e81386aa92b852b
Diffstat (limited to 'src')
-rw-r--r--src/rpc/client.cpp1
-rw-r--r--src/rpc/net.cpp53
2 files changed, 54 insertions, 0 deletions
diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp
index f4d44fc809..649e222c39 100644
--- a/src/rpc/client.cpp
+++ b/src/rpc/client.cpp
@@ -163,6 +163,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "rescanblockchain", 0, "start_height"},
{ "rescanblockchain", 1, "stop_height"},
{ "createwallet", 1, "disable_private_keys"},
+ { "getnodeaddresses", 0, "count"},
};
// clang-format on
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index 099a7cf1da..846d90cd0a 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -626,6 +626,58 @@ static UniValue setnetworkactive(const JSONRPCRequest& request)
return g_connman->GetNetworkActive();
}
+static UniValue getnodeaddresses(const JSONRPCRequest& request)
+{
+ if (request.fHelp || request.params.size() > 1) {
+ throw std::runtime_error(
+ "getnodeaddresses ( count )\n"
+ "\nReturn known addresses which can potentially be used to find new nodes in the network\n"
+ "\nArguments:\n"
+ "1. \"count\" (numeric, optional) How many addresses to return. Limited to the smaller of " + std::to_string(ADDRMAN_GETADDR_MAX) +
+ " or " + std::to_string(ADDRMAN_GETADDR_MAX_PCT) + "% of all known addresses. (default = 1)\n"
+ "\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"
+ " \"address\": \"host\", (string) The address of the node\n"
+ " \"port\": n (numeric) The port of the node\n"
+ " }\n"
+ " ,....\n"
+ "]\n"
+ "\nExamples:\n"
+ + HelpExampleCli("getnodeaddresses", "8")
+ + HelpExampleRpc("getnodeaddresses", "8")
+ );
+ }
+ if (!g_connman) {
+ 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");
+ }
+ }
+ // returns a shuffled list of CAddress
+ std::vector<CAddress> vAddr = g_connman->GetAddresses();
+ UniValue ret(UniValue::VARR);
+
+ int address_return_count = std::min<int>(count, vAddr.size());
+ for (int i = 0; i < address_return_count; ++i) {
+ UniValue obj(UniValue::VOBJ);
+ const CAddress& addr = vAddr[i];
+ obj.pushKV("time", (int)addr.nTime);
+ obj.pushKV("services", (uint64_t)addr.nServices);
+ obj.pushKV("address", addr.ToStringIP());
+ obj.pushKV("port", addr.GetPort());
+ ret.push_back(obj);
+ }
+ return ret;
+}
+
// clang-format off
static const CRPCCommand commands[] =
{ // category name actor (function) argNames
@@ -642,6 +694,7 @@ static const CRPCCommand commands[] =
{ "network", "listbanned", &listbanned, {} },
{ "network", "clearbanned", &clearbanned, {} },
{ "network", "setnetworkactive", &setnetworkactive, {"state"} },
+ { "network", "getnodeaddresses", &getnodeaddresses, {"count"} },
};
// clang-format on