diff --git a/src/Makefile.test_util.include b/src/Makefile.test_util.include index 749d3a5f1d62..5eae384ebfdc 100644 --- a/src/Makefile.test_util.include +++ b/src/Makefile.test_util.include @@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \ TEST_UTIL_H = \ test/util/blockfilter.h \ test/util/chainstate.h \ + test/util/coins.h \ test/util/json.h \ test/util/index.h \ test/util/llmq_tests.h \ @@ -17,6 +18,7 @@ TEST_UTIL_H = \ test/util/mining.h \ test/util/net.h \ test/util/poolresourcetester.h \ + test/util/random.h \ test/util/script.h \ test/util/setup_common.h \ test/util/str.h \ @@ -31,6 +33,7 @@ libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libtest_util_a_SOURCES = \ test/util/blockfilter.cpp \ test/util/index.cpp \ + test/util/coins.cpp \ test/util/json.cpp \ test/util/logging.cpp \ test/util/mining.cpp \ diff --git a/src/logging/timer.h b/src/logging/timer.h index d954e4630178..f4114597cba7 100644 --- a/src/logging/timer.h +++ b/src/logging/timer.h @@ -12,6 +12,7 @@ #include #include +#include #include @@ -28,14 +29,14 @@ class Timer std::string prefix, std::string end_msg, BCLog::LogFlags log_category = BCLog::LogFlags::ALL, - bool msg_on_completion = true) : - m_prefix(std::move(prefix)), - m_title(std::move(end_msg)), - m_log_category(log_category), - m_message_on_completion(msg_on_completion) + bool msg_on_completion = true) + : m_prefix(std::move(prefix)), + m_title(std::move(end_msg)), + m_log_category(log_category), + m_message_on_completion(msg_on_completion) { this->Log(strprintf("%s started", m_title)); - m_start_t = GetTime(); + m_start_t = std::chrono::steady_clock::now(); } ~Timer() @@ -60,24 +61,25 @@ class Timer std::string LogMsg(const std::string& msg) { - const auto end_time = GetTime() - m_start_t; - if (m_start_t.count() <= 0) { + const auto end_time{std::chrono::steady_clock::now()}; + if (!m_start_t) { return strprintf("%s: %s", m_prefix, msg); } + const auto duration{end_time - *m_start_t}; if constexpr (std::is_same::value) { - return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count()); + return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks(duration)); } else if constexpr (std::is_same::value) { - return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001); + return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks(duration)); } else if constexpr (std::is_same::value) { - return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001); + return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks(duration)); } else { static_assert(ALWAYS_FALSE, "Error: unexpected time type"); } } private: - std::chrono::microseconds m_start_t{}; + std::optional m_start_t{}; //! Log prefix; usually the name of the function this was created in. const std::string m_prefix; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 2e75a19f3bcc..bdbf43a56b26 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -1125,6 +1125,55 @@ static RPCHelpMan setmnthreadactive() }; } +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(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[]{ @@ -1146,6 +1195,7 @@ void RegisterNetRPCCommands(CRPCTable &t) {"hidden", &addpeeraddress}, {"hidden", &sendmsgtopeer}, {"hidden", &setmnthreadactive}, + {"hidden", &getaddrmaninfo}, }; for (const auto& c : commands) { t.appendCommand(c.name, &c); diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp index 2cd1ae9510d2..af2e87eedf4e 100644 --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -7,6 +7,7 @@ #include #include #include // for InsecureRand* +#include #include #include diff --git a/src/test/bip324_tests.cpp b/src/test/bip324_tests.cpp index 7d651d72b38d..5d6bef2ec7eb 100644 --- a/src/test/bip324_tests.cpp +++ b/src/test/bip324_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index c7ed320cf9d2..e5ccccfe2dc3 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index 47728ba30a17..9be0e52e1e55 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/src/test/checkqueue_tests.cpp b/src/test/checkqueue_tests.cpp index a73b4c252642..a338511c9fad 100644 --- a/src/test/checkqueue_tests.cpp +++ b/src/test/checkqueue_tests.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 793c2d84a179..6a6d030d460c 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -7,6 +7,7 @@ #include