2023-05-10 22:29:17 +02:00
|
|
|
// Copyright (c) 2023 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <node/kernel_notifications.h>
|
|
|
|
|
2023-05-08 14:49:37 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2023-07-07 12:53:31 -04:00
|
|
|
#include <chain.h>
|
2023-05-08 14:49:37 +02:00
|
|
|
#include <common/args.h>
|
2023-05-08 11:32:13 +02:00
|
|
|
#include <common/system.h>
|
2023-05-09 11:15:46 +02:00
|
|
|
#include <kernel/context.h>
|
|
|
|
#include <logging.h>
|
|
|
|
#include <node/abort.h>
|
2023-05-10 22:29:17 +02:00
|
|
|
#include <node/interface_ui.h>
|
2023-05-09 11:15:46 +02:00
|
|
|
#include <util/check.h>
|
2023-05-08 14:49:37 +02:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/string.h>
|
2023-05-10 22:36:04 +02:00
|
|
|
#include <util/translation.h>
|
2023-05-08 14:49:37 +02:00
|
|
|
#include <warnings.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
static void AlertNotify(const std::string& strMessage)
|
|
|
|
{
|
|
|
|
uiInterface.NotifyAlertChanged();
|
|
|
|
#if HAVE_SYSTEM
|
|
|
|
std::string strCmd = gArgs.GetArg("-alertnotify", "");
|
|
|
|
if (strCmd.empty()) return;
|
|
|
|
|
|
|
|
// Alert text should be plain ascii coming from a trusted source, but to
|
|
|
|
// be safe we first strip anything not in safeChars, then add single quotes around
|
|
|
|
// the whole string before passing it to the shell:
|
|
|
|
std::string singleQuote("'");
|
|
|
|
std::string safeStatus = SanitizeString(strMessage);
|
|
|
|
safeStatus = singleQuote+safeStatus+singleQuote;
|
|
|
|
ReplaceAll(strCmd, "%s", safeStatus);
|
|
|
|
|
|
|
|
std::thread t(runCommand, strCmd);
|
|
|
|
t.detach(); // thread runs free
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DoWarning(const bilingual_str& warning)
|
|
|
|
{
|
|
|
|
static bool fWarned = false;
|
|
|
|
SetMiscWarning(warning);
|
|
|
|
if (!fWarned) {
|
|
|
|
AlertNotify(warning.original);
|
|
|
|
fWarned = true;
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 22:29:17 +02:00
|
|
|
|
|
|
|
namespace node {
|
|
|
|
|
2023-07-07 12:53:31 -04:00
|
|
|
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
|
2023-05-10 22:29:17 +02:00
|
|
|
{
|
|
|
|
uiInterface.NotifyBlockTip(state, &index);
|
2023-07-07 12:53:31 -04:00
|
|
|
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
|
2023-07-07 17:32:54 -04:00
|
|
|
if (!m_shutdown()) {
|
|
|
|
LogPrintf("Error: failed to send shutdown signal after reaching stop height\n");
|
|
|
|
}
|
2023-07-07 12:53:31 -04:00
|
|
|
return kernel::Interrupted{};
|
|
|
|
}
|
|
|
|
return {};
|
2023-05-10 22:29:17 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 22:35:49 +02:00
|
|
|
void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
|
|
|
|
{
|
|
|
|
uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:36:04 +02:00
|
|
|
void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
|
|
|
|
{
|
|
|
|
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
|
|
|
|
}
|
|
|
|
|
2023-05-08 14:49:37 +02:00
|
|
|
void KernelNotifications::warning(const bilingual_str& warning)
|
|
|
|
{
|
|
|
|
DoWarning(warning);
|
|
|
|
}
|
|
|
|
|
2023-06-15 23:09:37 +02:00
|
|
|
void KernelNotifications::flushError(const std::string& debug_message)
|
|
|
|
{
|
2023-07-07 17:32:54 -04:00
|
|
|
AbortNode(&m_shutdown, m_exit_status, debug_message);
|
2023-05-09 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KernelNotifications::fatalError(const std::string& debug_message, const bilingual_str& user_message)
|
|
|
|
{
|
2023-07-07 17:32:54 -04:00
|
|
|
node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr,
|
|
|
|
m_exit_status, debug_message, user_message);
|
2023-06-15 23:09:37 +02:00
|
|
|
}
|
|
|
|
|
2023-07-07 12:53:31 -04:00
|
|
|
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
|
|
|
|
{
|
|
|
|
if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:29:17 +02:00
|
|
|
} // namespace node
|