Skip to content

Commit c8eb8da

Browse files
committed
rpc: Introduce getaddrmaninfo for count of addresses stored in new/tried table
1 parent 8371914 commit c8eb8da

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/rpc/net.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,55 @@ static RPCHelpMan sendmsgtopeer()
10161016
};
10171017
}
10181018

1019+
static RPCHelpMan getaddrmaninfo()
1020+
{
1021+
return RPCHelpMan{"getaddrmaninfo",
1022+
"\nProvides information about the node's address manager by returning the number of "
1023+
"addresses in the `new` and `tried` tables and their sum for all networks.\n"
1024+
"This RPC is for testing only.\n",
1025+
{},
1026+
RPCResult{
1027+
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
1028+
{
1029+
{RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")",
1030+
{
1031+
{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."},
1032+
{RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
1033+
{RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
1034+
}},
1035+
}
1036+
},
1037+
RPCExamples{
1038+
HelpExampleCli("getaddrmaninfo", "")
1039+
+ HelpExampleRpc("getaddrmaninfo", "")
1040+
},
1041+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1042+
{
1043+
NodeContext& node = EnsureAnyNodeContext(request.context);
1044+
if (!node.addrman) {
1045+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
1046+
}
1047+
1048+
UniValue ret(UniValue::VOBJ);
1049+
for (int n = 0; n < NET_MAX; ++n) {
1050+
enum Network network = static_cast<enum Network>(n);
1051+
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
1052+
UniValue obj(UniValue::VOBJ);
1053+
obj.pushKV("new", node.addrman->Size(network, true));
1054+
obj.pushKV("tried", node.addrman->Size(network, false));
1055+
obj.pushKV("total", node.addrman->Size(network));
1056+
ret.pushKV(GetNetworkName(network), obj);
1057+
}
1058+
UniValue obj(UniValue::VOBJ);
1059+
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
1060+
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
1061+
obj.pushKV("total", node.addrman->Size());
1062+
ret.pushKV("all_networks", obj);
1063+
return ret;
1064+
},
1065+
};
1066+
}
1067+
10191068
void RegisterNetRPCCommands(CRPCTable& t)
10201069
{
10211070
static const CRPCCommand commands[]{
@@ -1035,6 +1084,7 @@ void RegisterNetRPCCommands(CRPCTable& t)
10351084
{"hidden", &addconnection},
10361085
{"hidden", &addpeeraddress},
10371086
{"hidden", &sendmsgtopeer},
1087+
{"hidden", &getaddrmaninfo},
10381088
};
10391089
for (const auto& c : commands) {
10401090
t.appendCommand(c.name, &c);

src/test/fuzz/rpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
110110
"generate",
111111
"generateblock",
112112
"getaddednodeinfo",
113+
"getaddrmaninfo",
113114
"getbestblockhash",
114115
"getblock",
115116
"getblockchaininfo",

0 commit comments

Comments
 (0)