Bitcoin Core  29.1.0
P2P Digital Currency
kernel_notifications.cpp
Go to the documentation of this file.
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 
6 
7 #include <bitcoin-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  std::string strCmd = gArgs.GetArg("-alertnotify", "");
34  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  std::string singleQuote("'");
40  std::string safeStatus = SanitizeString(strMessage);
41  safeStatus = singleQuote+safeStatus+singleQuote;
42  ReplaceAll(strCmd, "%s", safeStatus);
43 
44  std::thread t(runCommand, strCmd);
45  t.detach(); // thread runs free
46 #endif
47 }
48 
49 namespace node {
50 
52 {
53  {
55  Assume(index.GetBlockHash() != uint256::ZERO);
56  m_tip_block = index.GetBlockHash();
57  m_tip_block_cv.notify_all();
58  }
59 
60  uiInterface.NotifyBlockTip(state, &index);
61  if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
62  if (!m_shutdown_request()) {
63  LogError("Failed to send shutdown signal after reaching stop height\n");
64  }
65  return kernel::Interrupted{};
66  }
67  return {};
68 }
69 
70 void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
71 {
72  uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
73 }
74 
75 void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
76 {
77  uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
78 }
79 
81 {
82  if (m_warnings.Set(id, message)) {
83  AlertNotify(message.original);
84  }
85 }
86 
88 {
89  m_warnings.Unset(id);
90 }
91 
93 {
95 }
96 
98 {
100  m_exit_status, message, &m_warnings);
101 }
102 
103 std::optional<uint256> KernelNotifications::TipBlock()
104 {
106  return m_tip_block;
107 };
108 
109 
111 {
112  if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
113 }
114 
115 } // namespace node
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:11
AssertLockHeld(pool.cs)
std::optional< uint256 > TipBlock() EXCLUSIVE_LOCKS_REQUIRED(m_tip_block_mutex)
The block for which the last blockTip notification was received.
void ReadNotificationArgs(const ArgsManager &args, KernelNotifications &notifications)
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:85
CClientUIInterface uiInterface
Bilingual messages:
Definition: translation.h:24
std::variant< std::monostate, Interrupted > InterruptResult
Simple result type for functions that need to propagate an interrupt status and don&#39;t have other retu...
bool Unset(warning_type id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Unset a warning message.
Definition: warnings.cpp:36
const std::function< bool()> & m_shutdown_request
void progress(const bilingual_str &title, int progress_percent, bool resume_possible) override
std::string translated
Definition: translation.h:26
Warning
Definition: warning.h:9
void flushError(const bilingual_str &message) override
The flush error notification is sent to notify the user that an error occurred while flushing block d...
void warningUnset(kernel::Warning id) override
uint256 GetBlockHash() const
Definition: chain.h:243
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
bool Set(warning_type id, bilingual_str message) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Set a warning message.
Definition: warnings.cpp:29
ArgsManager & args
Definition: bitcoind.cpp:277
void fatalError(const bilingual_str &message) override
The fatal error notification is sent to notify the user when an error occurs in kernel code that can&#39;...
kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex &index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex)
#define LOCK(cs)
Definition: sync.h:257
static const uint256 ZERO
Definition: uint256.h:209
void warningSet(kernel::Warning id, const bilingual_str &message) override
#define Assume(val)
Assume is the identity function.
Definition: check.h:97
Definition: messages.h:20
ArgsManager gArgs
Definition: args.cpp:42
void AbortNode(const std::function< bool()> &shutdown_request, std::atomic< int > &exit_status, const bilingual_str &message, node::Warnings *warnings)
Definition: abort.cpp:18
std::string original
Definition: translation.h:25
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
#define LogError(...)
Definition: logging.h:358
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:482
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:457
Result type for use with std::variant to indicate that an operation should be interrupted.
std::atomic< int > & m_exit_status
int m_stop_at_height
Block height after which blockTip notification will return Interrupted{}, if >0.
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:153
static void AlertNotify(const std::string &strMessage)
bool m_shutdown_on_fatal_error
Useful for tests, can be set to false to avoid shutdown on fatal error.