Skip to content

Commit 22fa1f4

Browse files
committed
Merge bitcoin#28565: rpc: getaddrmaninfo followups
e6e444c refactor: add and use EnsureAnyAddrman in rpc (stratospher) bf589a5 doc: add release notes for bitcoin#27511 (stratospher) 3931e6a rpc: `getaddrmaninfo` followups (stratospher) Pull request description: - make `getaddrmaninfo` RPC public since it's not for development purposes only and regular users might find it useful. [bitcoin#26988 (comment)](bitcoin#26988 (comment)) - add missing `all_networks` key to RPC help. [bitcoin#27511 (comment)](bitcoin#27511 (comment)) - fix clang format spacing - add and use `EnsureAddrman` in RPC code. [bitcoin#27511 (comment)](bitcoin#27511 (comment)) ACKs for top commit: 0xB10C: Code Review re-ACK e6e444c theStack: Code-review ACK e6e444c pablomartin4btc: tested ACK e6e444c Tree-SHA512: c14090d5c64ff15e92d252578de2437bb2ce2e1e431d6698580241a29190f0a3528ae5b013c0ddb76a9ae538507191295c37cab7fd93469941cadbde44587072
2 parents ab2f531 + e6e444c commit 22fa1f4

File tree

5 files changed

+63
-62
lines changed

5 files changed

+63
-62
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
New RPCs
2+
--------
3+
4+
- A new RPC `getaddrmaninfo` has been added to view the distribution of addresses in the new and tried table of the
5+
node's address manager across different networks(ipv4, ipv6, onion, i2p, cjdns). The RPC returns count of addresses
6+
in new and tried table as well as their sum for all networks. (#27511)

src/rpc/net.cpp

+41-57
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,7 @@ static RPCHelpMan addpeeraddress()
949949
},
950950
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
951951
{
952-
NodeContext& node = EnsureAnyNodeContext(request.context);
953-
if (!node.addrman) {
954-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
955-
}
952+
AddrMan& addrman = EnsureAnyAddrman(request.context);
956953

957954
const std::string& addr_string{request.params[0].get_str()};
958955
const auto port{request.params[1].getInt<uint16_t>()};
@@ -968,11 +965,11 @@ static RPCHelpMan addpeeraddress()
968965
address.nTime = Now<NodeSeconds>();
969966
// The source address is set equal to the address. This is equivalent to the peer
970967
// announcing itself.
971-
if (node.addrman->Add({address}, address)) {
968+
if (addrman.Add({address}, address)) {
972969
success = true;
973970
if (tried) {
974971
// Attempt to move the address to the tried addresses table.
975-
node.addrman->Good(address);
972+
addrman.Good(address);
976973
}
977974
}
978975
}
@@ -1033,50 +1030,40 @@ static RPCHelpMan sendmsgtopeer()
10331030

10341031
static RPCHelpMan getaddrmaninfo()
10351032
{
1036-
return RPCHelpMan{"getaddrmaninfo",
1037-
"\nProvides information about the node's address manager by returning the number of "
1038-
"addresses in the `new` and `tried` tables and their sum for all networks.\n"
1039-
"This RPC is for testing only.\n",
1040-
{},
1041-
RPCResult{
1042-
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
1043-
{
1044-
{RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")",
1045-
{
1046-
{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."},
1047-
{RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
1048-
{RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
1049-
}},
1050-
}
1051-
},
1052-
RPCExamples{
1053-
HelpExampleCli("getaddrmaninfo", "")
1054-
+ HelpExampleRpc("getaddrmaninfo", "")
1055-
},
1056-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1057-
{
1058-
NodeContext& node = EnsureAnyNodeContext(request.context);
1059-
if (!node.addrman) {
1060-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
1061-
}
1062-
1063-
UniValue ret(UniValue::VOBJ);
1064-
for (int n = 0; n < NET_MAX; ++n) {
1065-
enum Network network = static_cast<enum Network>(n);
1066-
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
1067-
UniValue obj(UniValue::VOBJ);
1068-
obj.pushKV("new", node.addrman->Size(network, true));
1069-
obj.pushKV("tried", node.addrman->Size(network, false));
1070-
obj.pushKV("total", node.addrman->Size(network));
1071-
ret.pushKV(GetNetworkName(network), obj);
1072-
}
1073-
UniValue obj(UniValue::VOBJ);
1074-
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
1075-
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
1076-
obj.pushKV("total", node.addrman->Size());
1077-
ret.pushKV("all_networks", obj);
1078-
return ret;
1079-
},
1033+
return RPCHelpMan{
1034+
"getaddrmaninfo",
1035+
"\nProvides information about the node's address manager by returning the number of "
1036+
"addresses in the `new` and `tried` tables and their sum for all networks.\n",
1037+
{},
1038+
RPCResult{
1039+
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", {
1040+
{RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ", all_networks)", {
1041+
{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."},
1042+
{RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
1043+
{RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
1044+
}},
1045+
}},
1046+
RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
1047+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1048+
AddrMan& addrman = EnsureAnyAddrman(request.context);
1049+
1050+
UniValue ret(UniValue::VOBJ);
1051+
for (int n = 0; n < NET_MAX; ++n) {
1052+
enum Network network = static_cast<enum Network>(n);
1053+
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
1054+
UniValue obj(UniValue::VOBJ);
1055+
obj.pushKV("new", addrman.Size(network, true));
1056+
obj.pushKV("tried", addrman.Size(network, false));
1057+
obj.pushKV("total", addrman.Size(network));
1058+
ret.pushKV(GetNetworkName(network), obj);
1059+
}
1060+
UniValue obj(UniValue::VOBJ);
1061+
obj.pushKV("new", addrman.Size(std::nullopt, true));
1062+
obj.pushKV("tried", addrman.Size(std::nullopt, false));
1063+
obj.pushKV("total", addrman.Size());
1064+
ret.pushKV("all_networks", obj);
1065+
return ret;
1066+
},
10801067
};
10811068
}
10821069

@@ -1135,14 +1122,11 @@ static RPCHelpMan getrawaddrman()
11351122
+ HelpExampleRpc("getrawaddrman", "")
11361123
},
11371124
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1138-
NodeContext& node = EnsureAnyNodeContext(request.context);
1139-
if (!node.addrman) {
1140-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
1141-
}
1125+
AddrMan& addrman = EnsureAnyAddrman(request.context);
11421126

11431127
UniValue ret(UniValue::VOBJ);
1144-
ret.pushKV("new", AddrmanTableToJSON(node.addrman->GetEntries(false)));
1145-
ret.pushKV("tried", AddrmanTableToJSON(node.addrman->GetEntries(true)));
1128+
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
1129+
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
11461130
return ret;
11471131
},
11481132
};
@@ -1164,10 +1148,10 @@ void RegisterNetRPCCommands(CRPCTable& t)
11641148
{"network", &clearbanned},
11651149
{"network", &setnetworkactive},
11661150
{"network", &getnodeaddresses},
1151+
{"network", &getaddrmaninfo},
11671152
{"hidden", &addconnection},
11681153
{"hidden", &addpeeraddress},
11691154
{"hidden", &sendmsgtopeer},
1170-
{"hidden", &getaddrmaninfo},
11711155
{"hidden", &getrawaddrman},
11721156
};
11731157
for (const auto& c : commands) {

src/rpc/server_util.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,16 @@ PeerManager& EnsurePeerman(const NodeContext& node)
108108
}
109109
return *node.peerman;
110110
}
111+
112+
AddrMan& EnsureAddrman(const NodeContext& node)
113+
{
114+
if (!node.addrman) {
115+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
116+
}
117+
return *node.addrman;
118+
}
119+
120+
AddrMan& EnsureAnyAddrman(const std::any& context)
121+
{
122+
return EnsureAddrman(EnsureAnyNodeContext(context));
123+
}

src/rpc/server_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <any>
99

10+
class AddrMan;
1011
class ArgsManager;
1112
class CBlockPolicyEstimator;
1213
class CConnman;
@@ -31,5 +32,7 @@ CBlockPolicyEstimator& EnsureFeeEstimator(const node::NodeContext& node);
3132
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
3233
CConnman& EnsureConnman(const node::NodeContext& node);
3334
PeerManager& EnsurePeerman(const node::NodeContext& node);
35+
AddrMan& EnsureAddrman(const node::NodeContext& node);
36+
AddrMan& EnsureAnyAddrman(const std::any& context);
3437

3538
#endif // BITCOIN_RPC_SERVER_UTIL_H

test/functional/rpc_net.py

-5
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,6 @@ def test_getaddrmaninfo(self):
371371
self.log.info("Test getaddrmaninfo")
372372
node = self.nodes[1]
373373

374-
self.log.debug("Test that getaddrmaninfo is a hidden RPC")
375-
# It is hidden from general help, but its detailed help may be called directly.
376-
assert "getaddrmaninfo" not in node.help()
377-
assert "getaddrmaninfo" in node.help("getaddrmaninfo")
378-
379374
# current count of ipv4 addresses in addrman is {'new':1, 'tried':1}
380375
self.log.info("Test that count of addresses in addrman match expected values")
381376
res = node.getaddrmaninfo()

0 commit comments

Comments
 (0)