warnings.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2021 The Limenka 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 LIMENKA_NODE_WARNINGS_H
   7  #define LIMENKA_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       *        If `update` is true, already active warnings will be
  57       *        updated with the new `message`, and this method will
  58       *        return true unless there has been no change (only the
  59       *        untranslated/original string is compared).
  60       *
  61       * @param[in]   id  Unique identifier of the warning.
  62       * @param[in]   message Warning message to be shown.
  63       * @param[in]   update  Whether an existing warning should be
  64       *              updated.
  65       *
  66       * @returns true if the warning was indeed set (i.e. there is no
  67       *          active warning with this `id`), otherwise false.
  68       */
  69      bool Set(warning_type id, bilingual_str message, bool update=false) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  70      /**
  71       * @brief Unset a warning message. If a warning with the specified
  72       *        `id` is active, it is unset, the UI is updated, and true
  73       *        is returned. Otherwise, no warning is unset and false is
  74       *        returned.
  75       *
  76       * @param[in]   id  Unique identifier of the warning.
  77       *
  78       * @returns true if the warning was indeed unset (i.e. there is an
  79       *          active warning with this `id`), otherwise false.
  80       */
  81      bool Unset(warning_type id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  82      /** Return potential problems detected by the node, sorted by the
  83       * warning_type id */
  84      std::vector<bilingual_str> GetMessages() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
  85  };
  86  
  87  /**
  88   * RPC helper function that wraps warnings.GetMessages().
  89   *
  90   * Returns a UniValue::VSTR with the latest warning if use_deprecated is
  91   * set to true, or a UniValue::VARR with all warnings otherwise.
  92   */
  93  UniValue GetWarningsForRpc(const Warnings& warnings, bool use_deprecated);
  94  } // namespace node
  95  
  96  #endif // LIMENKA_NODE_WARNINGS_H
  97