Skip to content

Commit ff564c7

Browse files
committed
Merge bitcoin/bitcoin#27511: rpc: Add test-only RPC getaddrmaninfo for new/tried table address count
28bac81 test: add functional test for getaddrmaninfo (stratospher) c8eb8da rpc: Introduce getaddrmaninfo for count of addresses stored in new/tried table (stratospher) Pull request description: implements bitcoin/bitcoin#26907. split off from #26988 to keep RPC, CLI discussions separate. This PR introduces a new RPC `getaddrmaninfo`which returns the count of addresses in the new/tried table of a node's addrman broken down by network type. This would be useful for users who want to see the distribution of addresses from different networks across new/tried table in the addrman. ```jsx $ getaddrmaninfo Result: { (json object) json object with network type as keys "network" : { (json object) The network (ipv4, ipv6, onion, i2p, cjdns) "new" : n, (numeric) number of addresses in new table "tried" : n, (numeric) number of addresses in tried table "total" : n (numeric) total number of addresses in both new/tried tables from a network }, ... } ``` ### additional context from [original PR](bitcoin/bitcoin#26988) 1. network coverage tests were skipped because there’s a small chance that addresses from different networks could hash to the same bucket and cause count of different network addresses in the tests to fail. see bitcoin/bitcoin#26988 (comment). 2. #26988 uses this RPC in -addrinfo CLI. Slight preference for keeping the RPC hidden since this info will mostly be useful to only super users. see bitcoin/bitcoin#26988 (comment). ACKs for top commit: 0xB10C: ACK 28bac81 willcl-ark: reACK 28bac81 achow101: ACK 28bac81 brunoerg: reACK 28bac81 theStack: Code-review ACK 28bac81 Tree-SHA512: 346390167e1ebed7ca5c79328ea452633736aff8b7feefea77460e04d4489059334ae78a3f757f32f5fb7827b309d7186bebab3c3760b3dfb016d564a647371a
2 parents 3966b0a + 28bac81 commit ff564c7

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/rpc/net.cpp

+50
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

+1
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",

test/functional/rpc_net.py

+23
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def run_test(self):
6666
self.test_getnodeaddresses()
6767
self.test_addpeeraddress()
6868
self.test_sendmsgtopeer()
69+
self.test_getaddrmaninfo()
6970

7071
def test_connection_count(self):
7172
self.log.info("Test getconnectioncount")
@@ -360,6 +361,28 @@ def test_sendmsgtopeer(self):
360361
node.sendmsgtopeer(peer_id=0, msg_type="addr", msg=zero_byte_string.hex())
361362
self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)
362363

364+
def test_getaddrmaninfo(self):
365+
self.log.info("Test getaddrmaninfo")
366+
node = self.nodes[1]
367+
368+
self.log.debug("Test that getaddrmaninfo is a hidden RPC")
369+
# It is hidden from general help, but its detailed help may be called directly.
370+
assert "getaddrmaninfo" not in node.help()
371+
assert "getaddrmaninfo" in node.help("getaddrmaninfo")
372+
373+
# current count of ipv4 addresses in addrman is {'new':1, 'tried':1}
374+
self.log.info("Test that count of addresses in addrman match expected values")
375+
res = node.getaddrmaninfo()
376+
assert_equal(res["ipv4"]["new"], 1)
377+
assert_equal(res["ipv4"]["tried"], 1)
378+
assert_equal(res["ipv4"]["total"], 2)
379+
assert_equal(res["all_networks"]["new"], 1)
380+
assert_equal(res["all_networks"]["tried"], 1)
381+
assert_equal(res["all_networks"]["total"], 2)
382+
for net in ["ipv6", "onion", "i2p", "cjdns"]:
383+
assert_equal(res[net]["new"], 0)
384+
assert_equal(res[net]["tried"], 0)
385+
assert_equal(res[net]["total"], 0)
363386

364387
if __name__ == '__main__':
365388
NetTest().main()

0 commit comments

Comments
 (0)