This commit is contained in:
stratospher 2025-03-13 02:09:28 +01:00 committed by GitHub
commit 668974a67a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 26 deletions

View file

@ -0,0 +1,5 @@
Tools and Utilities
--------
- CLI -addrinfo previously (v22 - v28) returned addresses known to the node after filtering for quality and recency.
It now returns all the addresses known to the node. (#26988)

View file

@ -89,7 +89,7 @@ static void SetupCliArgs(ArgsManager& argsman)
"RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000", "RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000",
DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES),
ArgsManager::ALLOW_ANY, OptionsCategory::CLI_COMMANDS); 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("-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); 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);
@ -273,46 +273,31 @@ public:
/** Process addrinfo requests */ /** Process addrinfo requests */
class AddrinfoRequestHandler : public BaseRequestHandler class AddrinfoRequestHandler : public BaseRequestHandler
{ {
private:
int8_t NetworkStringToId(const std::string& str) const
{
for (size_t i = 0; i < NETWORKS.size(); ++i) {
if (str == NETWORKS[i]) return i;
}
return UNKNOWN_NETWORK;
}
public: public:
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
{ {
if (!args.empty()) { if (!args.empty()) {
throw std::runtime_error("-addrinfo takes no arguments"); throw std::runtime_error("-addrinfo takes no arguments");
} }
UniValue params{RPCConvertValues("getnodeaddresses", std::vector<std::string>{{"0"}})}; return JSONRPCRequestObj("getaddrmaninfo", NullUniValue, 1);
return JSONRPCRequestObj("getnodeaddresses", params, 1);
} }
UniValue ProcessReply(const UniValue& reply) override UniValue ProcessReply(const UniValue& reply) override
{ {
if (!reply["error"].isNull()) return reply; if (!reply["error"].isNull()) return reply;
const std::vector<UniValue>& nodes{reply["result"].getValues()}; const std::vector<std::string>& network_types{reply["result"].getKeys()};
if (!nodes.empty() && nodes.at(0)["network"].isNull()) { const std::vector<UniValue>& addrman_counts{reply["result"].getValues()};
throw std::runtime_error("-addrinfo requires bitcoind server to be running v22.0 and up"); if (network_types.empty()) {
} 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");
// Count the number of peers known to our node, by network.
std::array<uint64_t, NETWORKS.size()> 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);
} }
// Prepare result to return to user. // Prepare result to return to user.
UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ}; UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ};
uint64_t total{0}; // Total address count uint64_t total{0}; // Total address count
for (size_t i = 1; i < NETWORKS.size() - 1; ++i) { for (size_t i = 0; i < network_types.size() - 1; ++i) {
addresses.pushKV(NETWORKS[i], counts.at(i)); uint64_t addr_count = addrman_counts[i]["total"].getInt<int>();
total += counts.at(i); addresses.pushKV(network_types[i], addr_count);
total += addr_count;
} }
addresses.pushKV("total", total); addresses.pushKV("total", total);
result.pushKV("addresses_known", std::move(addresses)); result.pushKV("addresses_known", std::move(addresses));