mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-06-04 00:12:33 +02:00
Merge 27511 via rpc_getaddrmaninfo-24^
This commit is contained in:
commit
0619a0108c
@ -971,6 +971,55 @@ static RPCHelpMan addpeeraddress()
|
||||
};
|
||||
}
|
||||
|
||||
static RPCHelpMan getaddrmaninfo()
|
||||
{
|
||||
return RPCHelpMan{"getaddrmaninfo",
|
||||
"\nProvides information about the node's address manager by returning the number of "
|
||||
"addresses in the `new` and `tried` tables and their sum for all networks.\n"
|
||||
"This RPC is for testing only.\n",
|
||||
{},
|
||||
RPCResult{
|
||||
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
|
||||
{
|
||||
{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, "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);
|
||||
if (!node.addrman) {
|
||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
|
||||
}
|
||||
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
for (int n = 0; n < NET_MAX; ++n) {
|
||||
enum Network network = static_cast<enum Network>(n);
|
||||
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("new", node.addrman->Size(network, true));
|
||||
obj.pushKV("tried", node.addrman->Size(network, false));
|
||||
obj.pushKV("total", node.addrman->Size(network));
|
||||
ret.pushKV(GetNetworkName(network), obj);
|
||||
}
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
|
||||
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
|
||||
obj.pushKV("total", node.addrman->Size());
|
||||
ret.pushKV("all_networks", obj);
|
||||
return ret;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
void RegisterNetRPCCommands(CRPCTable& t)
|
||||
{
|
||||
static const CRPCCommand commands[]{
|
||||
@ -989,6 +1038,7 @@ void RegisterNetRPCCommands(CRPCTable& t)
|
||||
{"network", &getnodeaddresses},
|
||||
{"hidden", &addconnection},
|
||||
{"hidden", &addpeeraddress},
|
||||
{"hidden", &getaddrmaninfo},
|
||||
};
|
||||
for (const auto& c : commands) {
|
||||
t.appendCommand(c.name, &c);
|
||||
|
@ -110,6 +110,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
|
||||
"generate",
|
||||
"generateblock",
|
||||
"getaddednodeinfo",
|
||||
"getaddrmaninfo",
|
||||
"getbestblockhash",
|
||||
"getblock",
|
||||
"getblockchaininfo",
|
||||
|
@ -65,6 +65,7 @@ class NetTest(BitcoinTestFramework):
|
||||
self.test_service_flags()
|
||||
self.test_getnodeaddresses()
|
||||
self.test_addpeeraddress()
|
||||
self.test_getaddrmaninfo()
|
||||
|
||||
def test_connection_count(self):
|
||||
self.log.info("Test getconnectioncount")
|
||||
@ -328,6 +329,28 @@ class NetTest(BitcoinTestFramework):
|
||||
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
|
||||
assert_equal(len(addrs), 2)
|
||||
|
||||
def test_getaddrmaninfo(self):
|
||||
self.log.info("Test getaddrmaninfo")
|
||||
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}
|
||||
self.log.info("Test that count of addresses in addrman match expected values")
|
||||
res = node.getaddrmaninfo()
|
||||
assert_equal(res["ipv4"]["new"], 1)
|
||||
assert_equal(res["ipv4"]["tried"], 1)
|
||||
assert_equal(res["ipv4"]["total"], 2)
|
||||
assert_equal(res["all_networks"]["new"], 1)
|
||||
assert_equal(res["all_networks"]["tried"], 1)
|
||||
assert_equal(res["all_networks"]["total"], 2)
|
||||
for net in ["ipv6", "onion", "i2p", "cjdns"]:
|
||||
assert_equal(res[net]["new"], 0)
|
||||
assert_equal(res[net]["tried"], 0)
|
||||
assert_equal(res[net]["total"], 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
NetTest().main()
|
||||
|
Loading…
Reference in New Issue
Block a user