Skip to content

Commit bbb62c8

Browse files
committed
cli: modify -addrinfo to use getaddrmaninfo RPC endpoint
1 parent 33303b2 commit bbb62c8

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

src/bitcoin-cli.cpp

+11-26
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void SetupCliArgs(ArgsManager& argsman)
8484
"RPC generatetoaddress nblocks and maxtries arguments. Example: bitcoin-cli -generate 4 1000",
8585
DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES),
8686
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
87-
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::OPTIONS);
87+
argsman.AddArg("-addrinfo", "Get the number of addresses known to the node, per network and total.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
8888
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::OPTIONS);
8989
argsman.AddArg("-netinfo", "Get network peer connection information from the remote server. An optional integer argument from 0 to 4 can be passed for different peers listings (default: 0). Pass \"help\" for detailed help documentation.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
9090

@@ -255,46 +255,31 @@ class BaseRequestHandler
255255
/** Process addrinfo requests */
256256
class AddrinfoRequestHandler : public BaseRequestHandler
257257
{
258-
private:
259-
int8_t NetworkStringToId(const std::string& str) const
260-
{
261-
for (size_t i = 0; i < NETWORKS.size(); ++i) {
262-
if (str == NETWORKS[i]) return i;
263-
}
264-
return UNKNOWN_NETWORK;
265-
}
266-
267258
public:
268259
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
269260
{
270261
if (!args.empty()) {
271262
throw std::runtime_error("-addrinfo takes no arguments");
272263
}
273-
UniValue params{RPCConvertValues("getnodeaddresses", std::vector<std::string>{{"0"}})};
274-
return JSONRPCRequestObj("getnodeaddresses", params, 1);
264+
return JSONRPCRequestObj("getaddrmaninfo", NullUniValue, 1);
275265
}
276266

277267
UniValue ProcessReply(const UniValue& reply) override
278268
{
279269
if (!reply["error"].isNull()) return reply;
280-
const std::vector<UniValue>& nodes{reply["result"].getValues()};
281-
if (!nodes.empty() && nodes.at(0)["network"].isNull()) {
282-
throw std::runtime_error("-addrinfo requires bitcoind server to be running v22.0 and up");
283-
}
284-
// Count the number of peers known to our node, by network.
285-
std::array<uint64_t, NETWORKS.size()> counts{{}};
286-
for (const UniValue& node : nodes) {
287-
std::string network_name{node["network"].get_str()};
288-
const int8_t network_id{NetworkStringToId(network_name)};
289-
if (network_id == UNKNOWN_NETWORK) continue;
290-
++counts.at(network_id);
270+
const std::vector<std::string>& network_types{reply["result"].getKeys()};
271+
const std::vector<UniValue>& addrman_counts{reply["result"].getValues()};
272+
if (network_types.empty()) {
273+
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");
291274
}
275+
292276
// Prepare result to return to user.
293277
UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ};
294278
uint64_t total{0}; // Total address count
295-
for (size_t i = 1; i < NETWORKS.size() - 1; ++i) {
296-
addresses.pushKV(NETWORKS[i], counts.at(i));
297-
total += counts.at(i);
279+
for (size_t i = 0; i < network_types.size(); ++i) {
280+
uint64_t addr_count = addrman_counts[i]["total"].getInt<int>();
281+
addresses.pushKV(network_types[i], addr_count);
282+
total += addr_count;
298283
}
299284
addresses.pushKV("total", total);
300285
result.pushKV("addresses_known", addresses);

0 commit comments

Comments
 (0)