warnings.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-present The Bitcoin Core developers
   3  // Distributed under the MIT software license, see the accompanying
   4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  #ifndef BITCOIN_NODE_WARNINGS_H
   7  #define BITCOIN_NODE_WARNINGS_H
   8  
   9  #include <sync.h>
  10  #include <util/translation.h>
  11  
  12  #include <map>
  13  #include <variant>
  14  #include <vector>
  15  
  16  class UniValue;
  17  
  18  namespace kernel {
  19  enum class Warning;
  20  } // namespace kernel
  21  
  22  namespace node {
  23  enum class Warning {
  24      CLOCK_OUT_OF_SYNC,
  25      PRE_RELEASE_TEST_BUILD,
  26      FATAL_INTERNAL_ERROR,
  27  };
  28  
  29  /**
  30   * @class Warnings
  31   * @brief Manages warning messages within a node.
  32   *
  33   * The Warnings class provides mechanisms to set, unset, and retrieve
  34   * warning messages. It updates the GUI when warnings are changed.
  35   *
  36   * This class is designed to be non-copyable to ensure warnings
  37   * are managed centrally.
  38   */
  39  class Warnings
  40  {
  41      typedef std::variant<kernel::Warning, node::Warning> warning_type;
  42  
  43      mutable Mutex m_mutex;
  44      std::map<warning_type, bilingual_str> m_warnings GUARDED_BY(m_mutex);
  45  
  46  public:
  47      Warnings();
  48      //! A warnings instance should always be passed by reference, never copied.
  49      Warnings(const Warnings&) = delete;
  50      Warnings& operator=(const Warnings&) = delete;
  51      /**
  52       * @brief Set a warning message. If a warning with the specified
  53       *        `id` is already active, false is returned and the new
  54       *        warning is ignored. If `id` does not yet exist, the
  55       *        warning is set, the UI is updated, and true is returned.
  56       *
  57       * @param[in]   id  Unique identifier of the warning.
  58       * @param[in]   message Warning message to be shown.
  59       *
  60       * @returns true if the warning was indeed set (i.e. there is no
  61       *          active warning with this `id`), otherwise false.
  62       */
  63      bool Set(warning_type id, bilingual_str message) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  64      /**
  65       * @brief Unset a warning message. If a warning with the specified
  66       *        `id` is active, it is unset, the UI is updated, and true
  67       *        is returned. Otherwise, no warning is unset and false is
  68       *        returned.
  69       *
  70       * @param[in]   id  Unique identifier of the warning.
  71       *
  72       * @returns true if the warning was indeed unset (i.e. there is an
  73       *          active warning with this `id`), otherwise false.
  74       */
  75      bool Unset(warning_type id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  76      /** Return potential problems detected by the node, sorted by the
  77       * warning_type id */
  78      std::vector<bilingual_str> GetMessages() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  79  };
  80  
  81  /**
  82   * RPC helper function that wraps warnings.GetMessages().
  83   *
  84   * Returns a UniValue::VSTR with the latest warning if use_deprecated is
  85   * set to true, or a UniValue::VARR with all warnings otherwise.
  86   */
  87  UniValue GetWarningsForRpc(const Warnings& warnings, bool use_deprecated);
  88  } // namespace node
  89  
  90  #endif // BITCOIN_NODE_WARNINGS_H
  91