Skip to content

Commit f871c69

Browse files
committed
kernel: Add warning method to notifications
This commit is part of the libbitcoinkernel project and seeks to remove the ChainstateManager's and, more generally, the kernel library's dependency on interface_ui with options methods in this and the following few commits. By removing interface_ui from the kernel library, its dependency on boost is reduced to just boost::multi_index. The DoWarning and AlertNotify functions are moved out of the validation.cpp file, which removes its dependency on interface_ui as well as util/system.
1 parent 4452707 commit f871c69

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ int main(int argc, char* argv[])
9999
{
100100
std::cout << "Progress: " << title.original << ", " << progress_percent << ", " << resume_possible << std::endl;
101101
}
102+
void warning(const bilingual_str& warning) override
103+
{
104+
std::cout << "Warning: " << warning.original << std::endl;
105+
}
102106
};
103107
auto notifications = std::make_unique<KernelNotifications>();
104108

src/kernel/notifications_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Notifications
2626
virtual void blockTip(SynchronizationState state, CBlockIndex& index) {}
2727
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
2828
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
29+
virtual void warning(const bilingual_str& warning) {}
2930
};
3031
} // namespace kernel
3132

src/node/kernel_notifications.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,51 @@
44

55
#include <node/kernel_notifications.h>
66

7+
#if defined(HAVE_CONFIG_H)
8+
#include <config/bitcoin-config.h>
9+
#endif
10+
11+
#include <common/args.h>
712
#include <node/interface_ui.h>
13+
#include <util/strencodings.h>
14+
#include <util/string.h>
15+
#include <util/system.h>
816
#include <util/translation.h>
17+
#include <warnings.h>
18+
19+
#include <cstdint>
20+
#include <string>
21+
#include <thread>
22+
23+
static void AlertNotify(const std::string& strMessage)
24+
{
25+
uiInterface.NotifyAlertChanged();
26+
#if HAVE_SYSTEM
27+
std::string strCmd = gArgs.GetArg("-alertnotify", "");
28+
if (strCmd.empty()) return;
29+
30+
// Alert text should be plain ascii coming from a trusted source, but to
31+
// be safe we first strip anything not in safeChars, then add single quotes around
32+
// the whole string before passing it to the shell:
33+
std::string singleQuote("'");
34+
std::string safeStatus = SanitizeString(strMessage);
35+
safeStatus = singleQuote+safeStatus+singleQuote;
36+
ReplaceAll(strCmd, "%s", safeStatus);
37+
38+
std::thread t(runCommand, strCmd);
39+
t.detach(); // thread runs free
40+
#endif
41+
}
42+
43+
static void DoWarning(const bilingual_str& warning)
44+
{
45+
static bool fWarned = false;
46+
SetMiscWarning(warning);
47+
if (!fWarned) {
48+
AlertNotify(warning.original);
49+
fWarned = true;
50+
}
51+
}
952

1053
namespace node {
1154

@@ -24,4 +67,9 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc
2467
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
2568
}
2669

70+
void KernelNotifications::warning(const bilingual_str& warning)
71+
{
72+
DoWarning(warning);
73+
}
74+
2775
} // namespace node

src/node/kernel_notifications.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class KernelNotifications : public kernel::Notifications
2323
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
2424

2525
void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override;
26+
27+
void warning(const bilingual_str& warning) override;
2628
};
2729
} // namespace node
2830

src/validation.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <logging.h>
2828
#include <logging/timer.h>
2929
#include <node/blockstorage.h>
30-
#include <node/interface_ui.h>
3130
#include <node/utxo_snapshot.h>
3231
#include <policy/policy.h>
3332
#include <policy/rbf.h>
@@ -53,7 +52,6 @@
5352
#include <util/moneystr.h>
5453
#include <util/rbf.h>
5554
#include <util/strencodings.h>
56-
#include <util/system.h>
5755
#include <util/time.h>
5856
#include <util/trace.h>
5957
#include <util/translation.h>
@@ -1641,26 +1639,6 @@ bool Chainstate::IsInitialBlockDownload() const
16411639
return false;
16421640
}
16431641

1644-
static void AlertNotify(const std::string& strMessage)
1645-
{
1646-
uiInterface.NotifyAlertChanged();
1647-
#if HAVE_SYSTEM
1648-
std::string strCmd = gArgs.GetArg("-alertnotify", "");
1649-
if (strCmd.empty()) return;
1650-
1651-
// Alert text should be plain ascii coming from a trusted source, but to
1652-
// be safe we first strip anything not in safeChars, then add single quotes around
1653-
// the whole string before passing it to the shell:
1654-
std::string singleQuote("'");
1655-
std::string safeStatus = SanitizeString(strMessage);
1656-
safeStatus = singleQuote+safeStatus+singleQuote;
1657-
ReplaceAll(strCmd, "%s", safeStatus);
1658-
1659-
std::thread t(runCommand, strCmd);
1660-
t.detach(); // thread runs free
1661-
#endif
1662-
}
1663-
16641642
void Chainstate::CheckForkWarningConditions()
16651643
{
16661644
AssertLockHeld(cs_main);
@@ -2601,16 +2579,6 @@ void Chainstate::PruneAndFlush()
26012579
}
26022580
}
26032581

2604-
static void DoWarning(const bilingual_str& warning)
2605-
{
2606-
static bool fWarned = false;
2607-
SetMiscWarning(warning);
2608-
if (!fWarned) {
2609-
AlertNotify(warning.original);
2610-
fWarned = true;
2611-
}
2612-
}
2613-
26142582
/** Private helper function that concatenates warning messages. */
26152583
static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
26162584
{
@@ -2677,7 +2645,7 @@ void Chainstate::UpdateTip(const CBlockIndex* pindexNew)
26772645
if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
26782646
const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
26792647
if (state == ThresholdState::ACTIVE) {
2680-
DoWarning(warning);
2648+
m_chainman.GetNotifications().warning(warning);
26812649
} else {
26822650
AppendWarning(warning_messages, warning);
26832651
}

0 commit comments

Comments
 (0)