From 3324250b9361dbff474b7f1504614bad537d929e Mon Sep 17 00:00:00 2001 From: stratospher <44024636+stratospher@users.noreply.github.com> Date: Sun, 29 Jan 2023 22:19:00 +0530 Subject: [PATCH 1/2] cli: modify -addrinfo to use getaddrmaninfo RPC endpoint Currently -addrinfo returns addresses known to the node after filtering for quality and recency. However, the node considers all known addresses (even the filtered out addresses) when selecting peers to connect to. Update -addrinfo to display the full set of known addresses for more useful node information. --- src/bitcoin-cli.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 14a0cb245fab0..468588b3c6e9d 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -89,7 +89,7 @@ static void SetupCliArgs(ArgsManager& argsman) "RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000", DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); - argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total, after filtering for quality and recency. The total number of addresses known to the node may be higher.", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); + argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total.", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); argsman.AddArg("-getinfo", "Get general information from the remote server. Note that unlike server-side RPC calls, the output of -getinfo is the result of multiple non-atomic requests. Some entries in the output may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)", ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); argsman.AddArg("-netinfo", strprintf("Get network peer connection information from the remote server. An optional argument from 0 to %d can be passed for different peers listings (default: 0). If a non-zero value is passed, an additional \"outonly\" (or \"o\") argument can be passed to see outbound peers only. Pass \"help\" (or \"h\") for detailed help documentation.", NETINFO_MAX_LEVEL), ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); @@ -279,31 +279,27 @@ struct AddrinfoRequestHandler : BaseRequestHandler { if (!args.empty()) { throw std::runtime_error("-addrinfo takes no arguments"); } - UniValue params{RPCConvertValues("getnodeaddresses", std::vector{{"0"}})}; - return JSONRPCRequestObj("getnodeaddresses", params, 1); + return JSONRPCRequestObj("getaddrmaninfo", NullUniValue, 1); } UniValue ProcessReply(const UniValue& reply) override { - if (!reply["error"].isNull()) return reply; - const std::vector& nodes{reply["result"].getValues()}; - if (!nodes.empty() && nodes.at(0)["network"].isNull()) { - throw std::runtime_error("-addrinfo requires bitcoind server to be running v22.0 and up"); - } - // Count the number of peers known to our node, by network. - std::array counts{{}}; - for (const UniValue& node : nodes) { - std::string network_name{node["network"].get_str()}; - const int8_t network_id{NetworkStringToId(network_name)}; - if (network_id == UNKNOWN_NETWORK) continue; - ++counts.at(network_id); + if (!reply["error"].isNull()) { + if (reply["error"]["code"].getInt() == RPC_METHOD_NOT_FOUND) { + throw std::runtime_error("-addrinfo requires bitcoind server to be running v26.0 and up. if using an earlier bitcoind server (v22.0 - v25.0), use the appropriate binary"); + } + return reply; } + const std::vector& network_types{reply["result"].getKeys()}; + const std::vector& addrman_counts{reply["result"].getValues()}; + // Prepare result to return to user. UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ}; uint64_t total{0}; // Total address count - for (size_t i = 1; i < NETWORKS.size() - 1; ++i) { - addresses.pushKV(NETWORKS[i], counts.at(i)); - total += counts.at(i); + for (size_t i = 0; i < network_types.size() - 1; ++i) { + uint64_t addr_count = addrman_counts[i]["total"].getInt(); + addresses.pushKV(network_types[i], addr_count); + total += addr_count; } addresses.pushKV("total", total); result.pushKV("addresses_known", std::move(addresses)); From 363d69be225f09fc8706b636bb46863901d08ea4 Mon Sep 17 00:00:00 2001 From: stratospher <44024636+stratospher@users.noreply.github.com> Date: Mon, 13 Mar 2023 13:14:54 +0530 Subject: [PATCH 2/2] doc: add release notes for #26988 --- doc/release-notes-26988.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 doc/release-notes-26988.md diff --git a/doc/release-notes-26988.md b/doc/release-notes-26988.md new file mode 100644 index 0000000000000..d85a9128c75d5 --- /dev/null +++ b/doc/release-notes-26988.md @@ -0,0 +1,6 @@ +Tools and Utilities +-------- + +- CLI -addrinfo previously (v22 - v29) returned addresses known to the node after filtering for quality and recency. + However, the node considers all known addresses (even the filtered addresses) when selecting peers to connect to. + Update -addrinfo to only display the full set of known addresses for more useful node information. (#26988)