mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
Instead of having a single NodeContext::shutdown member that is used both to request shutdowns and check if they have been requested, use separate members for each. Benefits of this change: 1. Should make code a little clearer and easier to search because it is easier to see which parts of code are triggering shutdowns and which parts are just checking to see if they were triggered. 2. Makes it possible for init.cpp to specify additional code to run when a shutdown is requested, like signalling the m_tip_block_cv condition variable. Motivation for this change was to remove hacky NodeContext argument and m_tip_block_cv access from the StopRPC function, so StopRPC can just be concerned with RPC functionality, not other node functionality.
107 lines
3.1 KiB
C++
107 lines
3.1 KiB
C++
// 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>
|
|
|
|
#include <config/bitcoin-config.h> // IWYU pragma: keep
|
|
|
|
#include <chain.h>
|
|
#include <common/args.h>
|
|
#include <common/system.h>
|
|
#include <kernel/context.h>
|
|
#include <kernel/warning.h>
|
|
#include <logging.h>
|
|
#include <node/abort.h>
|
|
#include <node/interface_ui.h>
|
|
#include <node/warnings.h>
|
|
#include <util/check.h>
|
|
#include <util/signalinterrupt.h>
|
|
#include <util/strencodings.h>
|
|
#include <util/string.h>
|
|
#include <util/translation.h>
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
using util::ReplaceAll;
|
|
|
|
static void AlertNotify(const std::string& strMessage)
|
|
{
|
|
#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
|
|
}
|
|
|
|
namespace node {
|
|
|
|
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
|
|
{
|
|
{
|
|
LOCK(m_tip_block_mutex);
|
|
m_tip_block = index.GetBlockHash();
|
|
m_tip_block_cv.notify_all();
|
|
}
|
|
|
|
uiInterface.NotifyBlockTip(state, &index);
|
|
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
|
|
if (!m_shutdown_request()) {
|
|
LogError("Failed to send shutdown signal after reaching stop height\n");
|
|
}
|
|
return kernel::Interrupted{};
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void KernelNotifications::headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
|
|
{
|
|
uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
|
|
}
|
|
|
|
void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
|
|
{
|
|
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
|
|
}
|
|
|
|
void KernelNotifications::warningSet(kernel::Warning id, const bilingual_str& message)
|
|
{
|
|
if (m_warnings.Set(id, message)) {
|
|
AlertNotify(message.original);
|
|
}
|
|
}
|
|
|
|
void KernelNotifications::warningUnset(kernel::Warning id)
|
|
{
|
|
m_warnings.Unset(id);
|
|
}
|
|
|
|
void KernelNotifications::flushError(const bilingual_str& message)
|
|
{
|
|
AbortNode(m_shutdown_request, m_exit_status, message, &m_warnings);
|
|
}
|
|
|
|
void KernelNotifications::fatalError(const bilingual_str& message)
|
|
{
|
|
node::AbortNode(m_shutdown_on_fatal_error ? m_shutdown_request : nullptr,
|
|
m_exit_status, message, &m_warnings);
|
|
}
|
|
|
|
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
|
|
{
|
|
if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;
|
|
}
|
|
|
|
} // namespace node
|