Skip to content

Commit b071ad9

Browse files
committed
introduce and use the generalized node::Warnings interface
Instead of having separate warning functions (and globals) for each different warning that can be raised, encapsulate this logic into a single class and allow to (un)set any number of warnings. Introduces behaviour change: - the `-alertnotify` command is executed for all `KernelNotifications::warningSet` calls, which now also covers the `LARGE_WORK_INVALID_CHAIN` warning. - previously, warnings were returned based on a predetermined order, e.g. with the "pre-release test build" warning always first. This is no longer the case, and Warnings::GetMessages() will return messages sorted by the id of the warning. Removes warnings.cpp from kernel.
1 parent 20e616f commit b071ad9

15 files changed

+211
-79
lines changed

doc/release-notes-30058.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- When running with -alertnotify, an alert can now be raised multiple
2+
times instead of just once. Previously, it was only raised when unknown
3+
new consensus rules were activated, whereas the scope has now been
4+
increased to include all kernel warnings. Specifically, alerts will now
5+
also be raised when an invalid chain with a large amount of work has
6+
been detected. Additional warnings may be added in the future.
7+
(#30058)

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ BITCOIN_CORE_H = \
196196
kernel/messagestartchars.h \
197197
kernel/notifications_interface.h \
198198
kernel/validation_cache_sizes.h \
199+
kernel/warning.h \
199200
key.h \
200201
key_io.h \
201202
logging.h \
@@ -996,8 +997,7 @@ libbitcoinkernel_la_SOURCES = \
996997
util/tokenpipe.cpp \
997998
validation.cpp \
998999
validationinterface.cpp \
999-
versionbits.cpp \
1000-
warnings.cpp
1000+
versionbits.cpp
10011001

10021002
# Required for obj/build.h to be generated first.
10031003
# More details: https://www.gnu.org/software/automake/manual/html_node/Built-Sources-Example.html

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ BITCOIN_TESTS =\
118118
test/net_peer_eviction_tests.cpp \
119119
test/net_tests.cpp \
120120
test/netbase_tests.cpp \
121+
test/node_warnings_tests.cpp \
121122
test/orphanage_tests.cpp \
122123
test/peerman_tests.cpp \
123124
test/pmt_tests.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <kernel/checks.h>
1717
#include <kernel/context.h>
1818
#include <kernel/validation_cache_sizes.h>
19+
#include <kernel/warning.h>
1920

2021
#include <consensus/validation.h>
2122
#include <core_io.h>
@@ -28,6 +29,7 @@
2829
#include <util/fs.h>
2930
#include <util/signalinterrupt.h>
3031
#include <util/task_runner.h>
32+
#include <util/translation.h>
3133
#include <validation.h>
3234
#include <validationinterface.h>
3335

@@ -86,9 +88,13 @@ int main(int argc, char* argv[])
8688
{
8789
std::cout << "Progress: " << title.original << ", " << progress_percent << ", " << resume_possible << std::endl;
8890
}
89-
void warning(const bilingual_str& warning) override
91+
void warningSet(kernel::Warning id, const bilingual_str& message) override
9092
{
91-
std::cout << "Warning: " << warning.original << std::endl;
93+
std::cout << "Warning " << static_cast<int>(id) << " set: " << message.original << std::endl;
94+
}
95+
void warningUnset(kernel::Warning id) override
96+
{
97+
std::cout << "Warning " << static_cast<int>(id) << " unset" << std::endl;
9298
}
9399
void flushError(const bilingual_str& message) override
94100
{

src/kernel/notifications_interface.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace kernel {
1616

1717
//! Result type for use with std::variant to indicate that an operation should be interrupted.
1818
struct Interrupted{};
19+
enum class Warning;
20+
1921

2022
//! Simple result type for functions that need to propagate an interrupt status and don't have other return values.
2123
using InterruptResult = std::variant<std::monostate, Interrupted>;
@@ -38,7 +40,8 @@ class Notifications
3840
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) { return {}; }
3941
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
4042
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
41-
virtual void warning(const bilingual_str& warning) {}
43+
virtual void warningSet(Warning id, const bilingual_str& message) {}
44+
virtual void warningUnset(Warning id) {}
4245

4346
//! The flush error notification is sent to notify the user that an error
4447
//! occurred while flushing block data to disk. Kernel code may ignore flush

src/kernel/warning.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2024-present 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+
#ifndef BITCOIN_KERNEL_WARNING_H
6+
#define BITCOIN_KERNEL_WARNING_H
7+
8+
namespace kernel {
9+
enum class Warning {
10+
UNKNOWN_NEW_RULES_ACTIVATED,
11+
LARGE_WORK_INVALID_CHAIN,
12+
};
13+
} // namespace kernel
14+
#endif // BITCOIN_KERNEL_WARNING_H

src/node/abort.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212

1313
#include <atomic>
1414
#include <cstdlib>
15-
#include <string>
1615

1716
namespace node {
1817

1918
void AbortNode(util::SignalInterrupt* shutdown, std::atomic<int>& exit_status, const bilingual_str& message)
2019
{
21-
SetMiscWarning(message);
20+
g_warnings.Set(Warning::FATAL_INTERNAL_ERROR, message);
2221
InitError(_("A fatal internal error occurred, see debug.log for details: ") + message);
2322
exit_status.store(EXIT_FAILURE);
2423
if (shutdown && !(*shutdown)()) {

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class NodeImpl : public Node
9393
explicit NodeImpl(NodeContext& context) { setContext(&context); }
9494
void initLogging() override { InitLogging(args()); }
9595
void initParameterInteraction() override { InitParameterInteraction(args()); }
96-
bilingual_str getWarnings() override { return Join(GetWarnings(), Untranslated("<hr />")); }
96+
bilingual_str getWarnings() override { return Join(node::g_warnings.GetMessages(), Untranslated("<hr />")); }
9797
int getExitStatus() override { return Assert(m_context)->exit_status.load(); }
9898
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
9999
bool baseInitialize() override

src/node/kernel_notifications.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <common/args.h>
1111
#include <common/system.h>
1212
#include <kernel/context.h>
13+
#include <kernel/warning.h>
1314
#include <logging.h>
1415
#include <node/abort.h>
1516
#include <node/interface_ui.h>
@@ -46,16 +47,6 @@ static void AlertNotify(const std::string& strMessage)
4647
#endif
4748
}
4849

49-
static void DoWarning(const bilingual_str& warning)
50-
{
51-
static bool fWarned = false;
52-
node::SetMiscWarning(warning);
53-
if (!fWarned) {
54-
AlertNotify(warning.original);
55-
fWarned = true;
56-
}
57-
}
58-
5950
namespace node {
6051

6152
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
@@ -80,9 +71,16 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc
8071
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
8172
}
8273

83-
void KernelNotifications::warning(const bilingual_str& warning)
74+
void KernelNotifications::warningSet(kernel::Warning id, const bilingual_str& message)
75+
{
76+
if (node::g_warnings.Set(id, message)) {
77+
AlertNotify(message.original);
78+
}
79+
}
80+
81+
void KernelNotifications::warningUnset(kernel::Warning id)
8482
{
85-
DoWarning(warning);
83+
g_warnings.Unset(id);
8684
}
8785

8886
void KernelNotifications::flushError(const bilingual_str& message)

src/node/kernel_notifications.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class CBlockIndex;
1515
enum class SynchronizationState;
1616
struct bilingual_str;
1717

18+
namespace kernel {
19+
enum class Warning;
20+
} // namespace kernel
21+
1822
namespace util {
1923
class SignalInterrupt;
2024
} // namespace util
@@ -34,7 +38,9 @@ class KernelNotifications : public kernel::Notifications
3438

3539
void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override;
3640

37-
void warning(const bilingual_str& warning) override;
41+
void warningSet(kernel::Warning id, const bilingual_str& message) override;
42+
43+
void warningUnset(kernel::Warning id) override;
3844

3945
void flushError(const bilingual_str& message) override;
4046

0 commit comments

Comments
 (0)