kernel_notifications.cpp raw

   1  // Copyright (c) 2023 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <node/kernel_notifications.h>
   6  
   7  #include <limenka-build-config.h> // IWYU pragma: keep
   8  
   9  #include <chain.h>
  10  #include <common/args.h>
  11  #include <common/system.h>
  12  #include <kernel/context.h>
  13  #include <kernel/warning.h>
  14  #include <logging.h>
  15  #include <node/abort.h>
  16  #include <node/interface_ui.h>
  17  #include <node/warnings.h>
  18  #include <util/check.h>
  19  #include <util/signalinterrupt.h>
  20  #include <util/strencodings.h>
  21  #include <util/string.h>
  22  #include <util/translation.h>
  23  
  24  #include <cstdint>
  25  #include <string>
  26  #include <thread>
  27  
  28  using util::ReplaceAll;
  29  
  30  static void AlertNotify(const std::string& strMessage)
  31  {
  32  #if HAVE_SYSTEM
  33      if (!gArgs.IsArgSet("-alertnotify")) return;
  34  
  35      // Alert text should be plain ascii coming from a trusted source, but to
  36      // be safe we first strip anything not in safeChars, then add single quotes around
  37      // the whole string before passing it to the shell:
  38      std::string singleQuote("\"");
  39      std::string safeStatus = SanitizeString(strMessage);
  40      safeStatus = singleQuote+safeStatus+singleQuote;
  41  
  42      for (std::string command : gArgs.GetArgs("-alertnotify")) {
  43          ReplaceAll(command, "%s", safeStatus);
  44  
  45          std::thread t(runCommand, command);
  46          t.detach(); // thread runs free
  47      }
  48  #endif
  49  }
  50  
  51  namespace node {
  52  
  53  kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
  54  {
  55      {
  56          LOCK(m_tip_block_mutex);
  57          Assume(index.GetBlockHash() != uint256::ZERO);
  58          m_tip_block = index.GetBlockHash();
  59          m_tip_block_cv.notify_all();
  60      }
  61  
  62      uiInterface.NotifyBlockTip(state, &index);
  63      if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
  64          if (!m_shutdown_request()) {
  65              LogError("Failed to send shutdown signal after reaching stop height\n");
  66          }
  67          return kernel::Interrupted{};
  68      }
  69      return {};
  70  }
  71  
  72  void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
  73  {
  74      uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
  75  }
  76  
  77  void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
  78  {
  79      uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
  80  }
  81  
  82  void KernelNotifications::warningSet(kernel::Warning id, const bilingual_str& message, const bool update)
  83  {
  84      if (m_warnings.Set(id, message, update)) {
  85          AlertNotify(message.original);
  86      }
  87  }
  88  
  89  void KernelNotifications::warningUnset(kernel::Warning id)
  90  {
  91      m_warnings.Unset(id);
  92  }
  93  
  94  void KernelNotifications::flushError(const bilingual_str& message)
  95  {
  96      AbortNode(m_shutdown_request, m_exit_status, message, &m_warnings);
  97  }
  98  
  99  void KernelNotifications::fatalError(const bilingual_str& message)
 100  {
 101      node::AbortNode(m_shutdown_on_fatal_error ? m_shutdown_request : nullptr,
 102                      m_exit_status, message, &m_warnings);
 103  }
 104  
 105  std::optional<uint256> KernelNotifications::TipBlock()
 106  {
 107      AssertLockHeld(m_tip_block_mutex);
 108      return m_tip_block;
 109  };
 110  
 111  
 112  void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
 113  {
 114      if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
 115  }
 116  
 117  } // namespace node
 118