mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-23 22:19:39 +01:00
introduce and use the generalized node::Warnings interface
Instead of having separate warning functions (and globals) for each different warning that can be raised, encapsulate this logic into a single class and allow to (un)set any number of warnings. Introduces behaviour change: - the `-alertnotify` command is executed for all `KernelNotifications::warningSet` calls, which now also covers the `LARGE_WORK_INVALID_CHAIN` warning. - previously, warnings were returned based on a predetermined order, e.g. with the "pre-release test build" warning always first. This is no longer the case, and Warnings::GetMessages() will return messages sorted by the id of the warning. Removes warnings.cpp from kernel.
This commit is contained in:
@@ -6,26 +6,86 @@
|
||||
#ifndef BITCOIN_NODE_WARNINGS_H
|
||||
#define BITCOIN_NODE_WARNINGS_H
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <sync.h>
|
||||
|
||||
#include <map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
class UniValue;
|
||||
struct bilingual_str;
|
||||
|
||||
namespace kernel {
|
||||
enum class Warning;
|
||||
} // namespace kernel
|
||||
|
||||
namespace node {
|
||||
void SetMiscWarning(const bilingual_str& warning);
|
||||
void SetfLargeWorkInvalidChainFound(bool flag);
|
||||
/** Pass std::nullopt to disable the warning */
|
||||
void SetMedianTimeOffsetWarning(std::optional<bilingual_str> warning);
|
||||
/** Return potential problems detected by the node. */
|
||||
std::vector<bilingual_str> GetWarnings();
|
||||
enum class Warning {
|
||||
CLOCK_OUT_OF_SYNC,
|
||||
PRE_RELEASE_TEST_BUILD,
|
||||
FATAL_INTERNAL_ERROR,
|
||||
};
|
||||
|
||||
/**
|
||||
* RPC helper function that wraps GetWarnings. Returns a UniValue::VSTR
|
||||
* with the latest warning if use_deprecated is set to true, or a
|
||||
* UniValue::VARR with all warnings otherwise.
|
||||
* @class Warnings
|
||||
* @brief Manages warning messages within a node.
|
||||
*
|
||||
* The Warnings class provides mechanisms to set, unset, and retrieve
|
||||
* warning messages.
|
||||
*
|
||||
* This class is designed to be non-copyable to ensure warnings
|
||||
* are managed centrally.
|
||||
*/
|
||||
class Warnings
|
||||
{
|
||||
typedef std::variant<kernel::Warning, node::Warning> warning_type;
|
||||
|
||||
mutable Mutex m_mutex;
|
||||
std::map<warning_type, bilingual_str> m_warnings GUARDED_BY(m_mutex);
|
||||
|
||||
public:
|
||||
Warnings();
|
||||
//! A warnings instance should always be passed by reference, never copied.
|
||||
Warnings(const Warnings&) = delete;
|
||||
Warnings& operator=(const Warnings&) = delete;
|
||||
/**
|
||||
* @brief Set a warning message. If a warning with the specified
|
||||
* `id` is already active, false is returned and the new
|
||||
* warning is ignored. If `id` does not yet exist, the
|
||||
* warning is set, and true is returned.
|
||||
*
|
||||
* @param[in] id Unique identifier of the warning.
|
||||
* @param[in] message Warning message to be shown.
|
||||
*
|
||||
* @returns true if the warning was indeed set (i.e. there is no
|
||||
* active warning with this `id`), otherwise false.
|
||||
*/
|
||||
bool Set(warning_type id, bilingual_str message) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
||||
/**
|
||||
* @brief Unset a warning message. If a warning with the specified
|
||||
* `id` is active, it is unset, and true is returned.
|
||||
* Otherwise, no warning is unset and false is returned.
|
||||
*
|
||||
* @param[in] id Unique identifier of the warning.
|
||||
*
|
||||
* @returns true if the warning was indeed unset (i.e. there is an
|
||||
* active warning with this `id`), otherwise false.
|
||||
*/
|
||||
bool Unset(warning_type id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
||||
/** Return potential problems detected by the node, sorted by the
|
||||
* warning_type id */
|
||||
std::vector<bilingual_str> GetMessages() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
||||
};
|
||||
|
||||
/**
|
||||
* RPC helper function that wraps g_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;
|
||||
} // namespace node
|
||||
|
||||
#endif // BITCOIN_NODE_WARNINGS_H
|
||||
|
||||
Reference in New Issue
Block a user