LCOV - code coverage report
Current view: top level - src/node - kernel_notifications.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 62.5 % 40 25
Test Date: 2024-08-28 04:44:32 Functions: 77.8 % 9 7
Branches: 21.1 % 38 8

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023 The Bitcoin Core 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 <config/bitcoin-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                 :           1 : static void AlertNotify(const std::string& strMessage)
      31                 :             : {
      32                 :             : #if HAVE_SYSTEM
      33   [ +  -  +  - ]:           2 :     std::string strCmd = gArgs.GetArg("-alertnotify", "");
      34         [ +  - ]:           1 :     if (strCmd.empty()) return;
      35                 :             : 
      36                 :             :     // Alert text should be plain ascii coming from a trusted source, but to
      37                 :             :     // be safe we first strip anything not in safeChars, then add single quotes around
      38                 :             :     // the whole string before passing it to the shell:
      39         [ #  # ]:           0 :     std::string singleQuote("'");
      40         [ #  # ]:           0 :     std::string safeStatus = SanitizeString(strMessage);
      41   [ #  #  #  # ]:           0 :     safeStatus = singleQuote+safeStatus+singleQuote;
      42   [ #  #  #  # ]:           0 :     ReplaceAll(strCmd, "%s", safeStatus);
      43                 :             : 
      44         [ #  # ]:           0 :     std::thread t(runCommand, strCmd);
      45         [ #  # ]:           0 :     t.detach(); // thread runs free
      46                 :             : #endif
      47                 :           1 : }
      48                 :             : 
      49                 :             : namespace node {
      50                 :             : 
      51                 :        6801 : kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
      52                 :             : {
      53                 :        6801 :     uiInterface.NotifyBlockTip(state, &index);
      54   [ -  +  -  - ]:        6801 :     if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
      55         [ #  # ]:           0 :         if (!m_shutdown()) {
      56                 :           0 :             LogError("Failed to send shutdown signal after reaching stop height\n");
      57                 :             :         }
      58                 :           0 :         return kernel::Interrupted{};
      59                 :             :     }
      60                 :        6801 :     return {};
      61                 :             : }
      62                 :             : 
      63                 :        6539 : void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
      64                 :             : {
      65                 :        6539 :     uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
      66                 :        6539 : }
      67                 :             : 
      68                 :          36 : void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
      69                 :             : {
      70                 :          36 :     uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
      71                 :          36 : }
      72                 :             : 
      73                 :           7 : void KernelNotifications::warningSet(kernel::Warning id, const bilingual_str& message)
      74                 :             : {
      75   [ +  -  +  + ]:          14 :     if (m_warnings.Set(id, message)) {
      76                 :           1 :         AlertNotify(message.original);
      77                 :             :     }
      78                 :           7 : }
      79                 :             : 
      80                 :        6097 : void KernelNotifications::warningUnset(kernel::Warning id)
      81                 :             : {
      82                 :        6097 :     m_warnings.Unset(id);
      83                 :        6097 : }
      84                 :             : 
      85                 :           0 : void KernelNotifications::flushError(const bilingual_str& message)
      86                 :             : {
      87                 :           0 :     AbortNode(&m_shutdown, m_exit_status, message, &m_warnings);
      88                 :           0 : }
      89                 :             : 
      90                 :           1 : void KernelNotifications::fatalError(const bilingual_str& message)
      91                 :             : {
      92                 :           1 :     node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr,
      93         [ -  + ]:           1 :                     m_exit_status, message, &m_warnings);
      94                 :           1 : }
      95                 :             : 
      96                 :           0 : void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
      97                 :             : {
      98   [ #  #  #  # ]:           0 :     if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
      99                 :           0 : }
     100                 :             : 
     101                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1