From 260f8da71a35232d859d7705861fc1a88bfbbe81 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Tue, 7 May 2024 14:21:35 +0100 Subject: refactor: remove warnings globals --- src/node/abort.cpp | 4 ++-- src/node/abort.h | 3 ++- src/node/context.cpp | 1 + src/node/context.h | 3 +++ src/node/interfaces.cpp | 3 ++- src/node/kernel_notifications.cpp | 8 ++++---- src/node/kernel_notifications.h | 5 ++++- src/node/timeoffsets.cpp | 4 ++-- src/node/timeoffsets.h | 10 ++++++++++ src/node/warnings.cpp | 16 +++++++--------- src/node/warnings.h | 8 +++----- 11 files changed, 40 insertions(+), 25 deletions(-) (limited to 'src/node') diff --git a/src/node/abort.cpp b/src/node/abort.cpp index 68c09f0ef7..8a17c41fd2 100644 --- a/src/node/abort.cpp +++ b/src/node/abort.cpp @@ -15,9 +15,9 @@ namespace node { -void AbortNode(util::SignalInterrupt* shutdown, std::atomic& exit_status, const bilingual_str& message) +void AbortNode(util::SignalInterrupt* shutdown, std::atomic& exit_status, const bilingual_str& message, node::Warnings* warnings) { - g_warnings.Set(Warning::FATAL_INTERNAL_ERROR, message); + if (warnings) warnings->Set(Warning::FATAL_INTERNAL_ERROR, message); InitError(_("A fatal internal error occurred, see debug.log for details: ") + message); exit_status.store(EXIT_FAILURE); if (shutdown && !(*shutdown)()) { diff --git a/src/node/abort.h b/src/node/abort.h index 1092279142..c881af4634 100644 --- a/src/node/abort.h +++ b/src/node/abort.h @@ -14,7 +14,8 @@ class SignalInterrupt; } // namespace util namespace node { -void AbortNode(util::SignalInterrupt* shutdown, std::atomic& exit_status, const bilingual_str& message); +class Warnings; +void AbortNode(util::SignalInterrupt* shutdown, std::atomic& exit_status, const bilingual_str& message, node::Warnings* warnings); } // namespace node #endif // BITCOIN_NODE_ABORT_H diff --git a/src/node/context.cpp b/src/node/context.cpp index e32d21b383..da05fde6ee 100644 --- a/src/node/context.cpp +++ b/src/node/context.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/src/node/context.h b/src/node/context.h index a7d92989dd..77838ea99b 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -39,6 +39,7 @@ class SignalInterrupt; namespace node { class KernelNotifications; +class Warnings; //! NodeContext struct containing references to chain state and connection //! state. @@ -81,6 +82,8 @@ struct NodeContext { //! Issues calls about blocks and transactions std::unique_ptr validation_signals; std::atomic exit_status{EXIT_SUCCESS}; + //! Manages all the node warnings + std::unique_ptr warnings; //! Declare default constructor and destructor that are not inline, so code //! instantiating the NodeContext struct doesn't need to #include class diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 9ea3f63fdf..090dd83096 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -93,7 +93,7 @@ public: explicit NodeImpl(NodeContext& context) { setContext(&context); } void initLogging() override { InitLogging(args()); } void initParameterInteraction() override { InitParameterInteraction(args()); } - bilingual_str getWarnings() override { return Join(node::g_warnings.GetMessages(), Untranslated("
")); } + bilingual_str getWarnings() override { return Join(Assert(m_context->warnings)->GetMessages(), Untranslated("
")); } int getExitStatus() override { return Assert(m_context)->exit_status.load(); } uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); } bool baseInitialize() override @@ -101,6 +101,7 @@ public: if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false; if (!AppInitParameterInteraction(args())) return false; + m_context->warnings = std::make_unique(); m_context->kernel = std::make_unique(); m_context->ecc_context = std::make_unique(); if (!AppInitSanityChecks(*m_context->kernel)) return false; diff --git a/src/node/kernel_notifications.cpp b/src/node/kernel_notifications.cpp index c1a9e351b9..b6d31ab492 100644 --- a/src/node/kernel_notifications.cpp +++ b/src/node/kernel_notifications.cpp @@ -72,25 +72,25 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc void KernelNotifications::warningSet(kernel::Warning id, const bilingual_str& message) { - if (node::g_warnings.Set(id, message)) { + if (m_warnings.Set(id, message)) { AlertNotify(message.original); } } void KernelNotifications::warningUnset(kernel::Warning id) { - g_warnings.Unset(id); + m_warnings.Unset(id); } void KernelNotifications::flushError(const bilingual_str& message) { - AbortNode(&m_shutdown, m_exit_status, message); + AbortNode(&m_shutdown, m_exit_status, message, &m_warnings); } void KernelNotifications::fatalError(const bilingual_str& message) { node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr, - m_exit_status, message); + m_exit_status, message, &m_warnings); } void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications) diff --git a/src/node/kernel_notifications.h b/src/node/kernel_notifications.h index 96f09be453..e37f4d4e1e 100644 --- a/src/node/kernel_notifications.h +++ b/src/node/kernel_notifications.h @@ -25,12 +25,14 @@ class SignalInterrupt; namespace node { +class Warnings; static constexpr int DEFAULT_STOPATHEIGHT{0}; class KernelNotifications : public kernel::Notifications { public: - KernelNotifications(util::SignalInterrupt& shutdown, std::atomic& exit_status) : m_shutdown(shutdown), m_exit_status{exit_status} {} + KernelNotifications(util::SignalInterrupt& shutdown, std::atomic& exit_status, node::Warnings& warnings) + : m_shutdown(shutdown), m_exit_status{exit_status}, m_warnings{warnings} {} [[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override; @@ -53,6 +55,7 @@ public: private: util::SignalInterrupt& m_shutdown; std::atomic& m_exit_status; + node::Warnings& m_warnings; }; void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications); diff --git a/src/node/timeoffsets.cpp b/src/node/timeoffsets.cpp index d06a07b15a..002c00d245 100644 --- a/src/node/timeoffsets.cpp +++ b/src/node/timeoffsets.cpp @@ -48,7 +48,7 @@ bool TimeOffsets::WarnIfOutOfSync() const // when median == std::numeric_limits::min(), calling std::chrono::abs is UB auto median{std::max(Median(), std::chrono::seconds(std::numeric_limits::min() + 1))}; if (std::chrono::abs(median) <= WARN_THRESHOLD) { - node::g_warnings.Unset(node::Warning::CLOCK_OUT_OF_SYNC); + m_warnings.Unset(node::Warning::CLOCK_OUT_OF_SYNC); return false; } @@ -61,6 +61,6 @@ bool TimeOffsets::WarnIfOutOfSync() const "RPC methods to get more info." ), Ticks(WARN_THRESHOLD))}; LogWarning("%s\n", msg.original); - node::g_warnings.Set(node::Warning::CLOCK_OUT_OF_SYNC, msg); + m_warnings.Set(node::Warning::CLOCK_OUT_OF_SYNC, msg); return true; } diff --git a/src/node/timeoffsets.h b/src/node/timeoffsets.h index 2b12584e12..eba706ac1e 100644 --- a/src/node/timeoffsets.h +++ b/src/node/timeoffsets.h @@ -11,8 +11,16 @@ #include #include +namespace node { +class Warnings; +} // namespace node + class TimeOffsets { +public: + TimeOffsets(node::Warnings& warnings) : m_warnings{warnings} {} + +private: //! Maximum number of timeoffsets stored. static constexpr size_t MAX_SIZE{50}; //! Minimum difference between system and network time for a warning to be raised. @@ -23,6 +31,8 @@ class TimeOffsets * positive offset means our peer's clock is ahead of our local clock. */ std::deque m_offsets GUARDED_BY(m_mutex){}; + node::Warnings& m_warnings; + public: /** Add a new time offset sample. */ void Add(std::chrono::seconds offset) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); diff --git a/src/node/warnings.cpp b/src/node/warnings.cpp index 4e7fd3941c..b99c845900 100644 --- a/src/node/warnings.cpp +++ b/src/node/warnings.cpp @@ -17,8 +17,6 @@ #include namespace node { -Warnings g_warnings; - Warnings::Warnings() { // Pre-release build warning @@ -54,17 +52,17 @@ std::vector Warnings::GetMessages() const return messages; } -UniValue GetWarningsForRpc(bool use_deprecated) +UniValue GetWarningsForRpc(const Warnings& warnings, bool use_deprecated) { if (use_deprecated) { - const auto all_warnings{g_warnings.GetMessages()}; - return all_warnings.empty() ? "" : all_warnings.back().original; + const auto all_messages{warnings.GetMessages()}; + return all_messages.empty() ? "" : all_messages.back().original; } - UniValue warnings{UniValue::VARR}; - for (auto&& warning : g_warnings.GetMessages()) { - warnings.push_back(std::move(warning.original)); + UniValue messages{UniValue::VARR}; + for (auto&& message : warnings.GetMessages()) { + messages.push_back(std::move(message.original)); } - return warnings; + return messages; } } // namespace node diff --git a/src/node/warnings.h b/src/node/warnings.h index b7ff74ecf4..24aeb8a922 100644 --- a/src/node/warnings.h +++ b/src/node/warnings.h @@ -7,13 +7,13 @@ #define BITCOIN_NODE_WARNINGS_H #include +#include #include #include #include class UniValue; -struct bilingual_str; namespace kernel { enum class Warning; @@ -79,14 +79,12 @@ public: }; /** - * RPC helper function that wraps g_warnings.GetMessages(). + * RPC helper function that wraps warnings.GetMessages(). * * Returns a UniValue::VSTR with the latest warning if use_deprecated is * set to true, or a UniValue::VARR with all warnings otherwise. */ -UniValue GetWarningsForRpc(bool use_deprecated); - -extern Warnings g_warnings; +UniValue GetWarningsForRpc(const Warnings& warnings, bool use_deprecated); } // namespace node #endif // BITCOIN_NODE_WARNINGS_H -- cgit v1.2.3