0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-15 11:36:00 -05:00

rpc: getaddrmaninfo followups

- make `getaddrmaninfo` RPC public since it's not for development
  purposes only and regular users might find it useful
- add missing `all_networks` key to RPC help
- use clang format spacing
This commit is contained in:
stratospher 2023-10-03 09:04:40 +05:30
parent 97f756b12c
commit 3931e6abc3
2 changed files with 37 additions and 49 deletions

View file

@ -1033,50 +1033,43 @@ static RPCHelpMan sendmsgtopeer()
static RPCHelpMan getaddrmaninfo() static RPCHelpMan getaddrmaninfo()
{ {
return RPCHelpMan{"getaddrmaninfo", return RPCHelpMan{
"\nProvides information about the node's address manager by returning the number of " "getaddrmaninfo",
"addresses in the `new` and `tried` tables and their sum for all networks.\n" "\nProvides information about the node's address manager by returning the number of "
"This RPC is for testing only.\n", "addresses in the `new` and `tried` tables and their sum for all networks.\n",
{}, {},
RPCResult{ RPCResult{
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", {
{ {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ", all_networks)", {
{RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")", {RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
{ {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
{RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."}, {RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
{RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."}, }},
{RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"}, }},
}}, RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
} [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
}, NodeContext& node = EnsureAnyNodeContext(request.context);
RPCExamples{ if (!node.addrman) {
HelpExampleCli("getaddrmaninfo", "") throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
+ HelpExampleRpc("getaddrmaninfo", "") }
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
NodeContext& node = EnsureAnyNodeContext(request.context);
if (!node.addrman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
}
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
for (int n = 0; n < NET_MAX; ++n) { for (int n = 0; n < NET_MAX; ++n) {
enum Network network = static_cast<enum Network>(n); enum Network network = static_cast<enum Network>(n);
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue; if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("new", node.addrman->Size(network, true)); obj.pushKV("new", node.addrman->Size(network, true));
obj.pushKV("tried", node.addrman->Size(network, false)); obj.pushKV("tried", node.addrman->Size(network, false));
obj.pushKV("total", node.addrman->Size(network)); obj.pushKV("total", node.addrman->Size(network));
ret.pushKV(GetNetworkName(network), obj); ret.pushKV(GetNetworkName(network), obj);
} }
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("new", node.addrman->Size(std::nullopt, true)); obj.pushKV("new", node.addrman->Size(std::nullopt, true));
obj.pushKV("tried", node.addrman->Size(std::nullopt, false)); obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
obj.pushKV("total", node.addrman->Size()); obj.pushKV("total", node.addrman->Size());
ret.pushKV("all_networks", obj); ret.pushKV("all_networks", obj);
return ret; return ret;
}, },
}; };
} }
@ -1164,10 +1157,10 @@ void RegisterNetRPCCommands(CRPCTable& t)
{"network", &clearbanned}, {"network", &clearbanned},
{"network", &setnetworkactive}, {"network", &setnetworkactive},
{"network", &getnodeaddresses}, {"network", &getnodeaddresses},
{"network", &getaddrmaninfo},
{"hidden", &addconnection}, {"hidden", &addconnection},
{"hidden", &addpeeraddress}, {"hidden", &addpeeraddress},
{"hidden", &sendmsgtopeer}, {"hidden", &sendmsgtopeer},
{"hidden", &getaddrmaninfo},
{"hidden", &getrawaddrman}, {"hidden", &getrawaddrman},
}; };
for (const auto& c : commands) { for (const auto& c : commands) {

View file

@ -371,11 +371,6 @@ class NetTest(BitcoinTestFramework):
self.log.info("Test getaddrmaninfo") self.log.info("Test getaddrmaninfo")
node = self.nodes[1] node = self.nodes[1]
self.log.debug("Test that getaddrmaninfo is a hidden RPC")
# It is hidden from general help, but its detailed help may be called directly.
assert "getaddrmaninfo" not in node.help()
assert "getaddrmaninfo" in node.help("getaddrmaninfo")
# current count of ipv4 addresses in addrman is {'new':1, 'tried':1} # current count of ipv4 addresses in addrman is {'new':1, 'tried':1}
self.log.info("Test that count of addresses in addrman match expected values") self.log.info("Test that count of addresses in addrman match expected values")
res = node.getaddrmaninfo() res = node.getaddrmaninfo()