Skip to content

Commit ddf2ebd

Browse files
committed
Merge bitcoin/bitcoin#30058: Encapsulate warnings in generalized node::Warnings and remove globals
260f8da refactor: remove warnings globals (stickies-v) 9c4b0b7 node: update uiInterface whenever warnings updated (stickies-v) b071ad9 introduce and use the generalized `node::Warnings` interface (stickies-v) 20e616f move-only: move warnings from common to node (stickies-v) bed29c4 refactor: remove unnecessary AppendWarning helper function (stickies-v) Pull request description: This PR: - moves warnings from common to the node library and into the node namespace (as suggested in bitcoin/bitcoin#29845 (comment)) - generalizes the warnings interface to `Warnings::Set()` and `Warnings::Unset()` methods, instead of having a separate function and globals for each warning. As a result, this simplifies the `kernel::Notifications` interface. - removes warnings.cpp from the kernel library - removes warning globals - adds testing for the warning logic Behaviour change introduced: - the `-alertnotify` command is executed for all `KernelNotifications::warningSet` calls, which now also covers the `LARGE_WORK_INVALID_CHAIN` warning - the GUI is updated automatically whenever a warning is (un)set, covering some code paths where it previously wouldn't be, e.g. when `node::AbortNode()` is called, or for the `LARGE_WORK_INVALID_CHAIN` warning Some discussion points: - ~is `const std::string& id` the best way to refer to warnings? Enums are an obvious alternative, but since we need to define warnings across libraries, strings seem like a straightforward solution.~ _edit: updated approach to use `node::Warning` and `kernel::Warning` enums._ ACKs for top commit: achow101: ACK 260f8da ryanofsky: Code review ACK 260f8da. Only change since last review was rebasing TheCharlatan: Re-ACK 260f8da Tree-SHA512: a3fcedaee0d3ad64e9c111aeb30665162f98e0e72acd6a70b76ff2ddf4f0a34da4f97ce353c322a1668ca6ee4d8a81cc6e6d170c5bbeb7a43cffdaf66646b588
2 parents d97ddbe + 260f8da commit ddf2ebd

39 files changed

+361
-186
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: 4 additions & 4 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 \
@@ -239,6 +240,7 @@ BITCOIN_CORE_H = \
239240
node/types.h \
240241
node/utxo_snapshot.h \
241242
node/validation_cache_args.h \
243+
node/warnings.h \
242244
noui.h \
243245
outputtype.h \
244246
policy/v3_policy.h \
@@ -367,7 +369,6 @@ BITCOIN_CORE_H = \
367369
wallet/wallettool.h \
368370
wallet/walletutil.h \
369371
walletinitinterface.h \
370-
warnings.h \
371372
zmq/zmqabstractnotifier.h \
372373
zmq/zmqnotificationinterface.h \
373374
zmq/zmqpublishnotifier.h \
@@ -444,6 +445,7 @@ libbitcoin_node_a_SOURCES = \
444445
node/txreconciliation.cpp \
445446
node/utxo_snapshot.cpp \
446447
node/validation_cache_args.cpp \
448+
node/warnings.cpp \
447449
noui.cpp \
448450
policy/v3_policy.cpp \
449451
policy/fees.cpp \
@@ -721,7 +723,6 @@ libbitcoin_common_a_SOURCES = \
721723
script/sign.cpp \
722724
script/signingprovider.cpp \
723725
script/solver.cpp \
724-
warnings.cpp \
725726
$(BITCOIN_CORE_H)
726727
#
727728

@@ -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/bitcoind.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <kernel/context.h>
1818
#include <node/context.h>
1919
#include <node/interface_ui.h>
20+
#include <node/warnings.h>
2021
#include <noui.h>
2122
#include <util/check.h>
2223
#include <util/exception.h>
@@ -181,6 +182,8 @@ static bool AppInit(NodeContext& node)
181182
return false;
182183
}
183184

185+
node.warnings = std::make_unique<node::Warnings>();
186+
184187
node.kernel = std::make_unique<kernel::Context>();
185188
node.ecc_context = std::make_unique<ECC_Context>();
186189
if (!AppInitSanityChecks(*node.kernel))

src/index/base.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <util/thread.h>
1818
#include <util/translation.h>
1919
#include <validation.h> // For g_chainman
20-
#include <warnings.h>
2120

2221
#include <string>
2322
#include <utility>
@@ -31,7 +30,7 @@ template <typename... Args>
3130
void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
3231
{
3332
auto message = tfm::format(fmt, args...);
34-
node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message));
33+
node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
3534
}
3635

3736
CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14861486

14871487
// ********************************************************* Step 7: load block chain
14881488

1489-
node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status);
1489+
node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status, *Assert(node.warnings));
14901490
ReadNotificationArgs(args, *node.notifications);
14911491
ChainstateManager::Options chainman_opts{
14921492
.chainparams = chainparams,
@@ -1639,7 +1639,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16391639
assert(!node.peerman);
16401640
node.peerman = PeerManager::make(*node.connman, *node.addrman,
16411641
node.banman.get(), chainman,
1642-
*node.mempool, peerman_opts);
1642+
*node.mempool, *node.warnings,
1643+
peerman_opts);
16431644
validation_signals.RegisterValidationInterface(node.peerman.get());
16441645

16451646
// ********************************************************* Step 8: start indexers

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/net_processing.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <node/blockstorage.h>
2626
#include <node/timeoffsets.h>
2727
#include <node/txreconciliation.h>
28+
#include <node/warnings.h>
2829
#include <policy/fees.h>
2930
#include <policy/policy.h>
3031
#include <policy/settings.h>
@@ -489,7 +490,7 @@ class PeerManagerImpl final : public PeerManager
489490
public:
490491
PeerManagerImpl(CConnman& connman, AddrMan& addrman,
491492
BanMan* banman, ChainstateManager& chainman,
492-
CTxMemPool& pool, Options opts);
493+
CTxMemPool& pool, node::Warnings& warnings, Options opts);
493494

494495
/** Overridden from CValidationInterface. */
495496
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
@@ -790,7 +791,8 @@ class PeerManagerImpl final : public PeerManager
790791
/** Next time to check for stale tip */
791792
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
792793

793-
TimeOffsets m_outbound_time_offsets;
794+
node::Warnings& m_warnings;
795+
TimeOffsets m_outbound_time_offsets{m_warnings};
794796

795797
const Options m_opts;
796798

@@ -2042,14 +2044,14 @@ std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBl
20422044

20432045
std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
20442046
BanMan* banman, ChainstateManager& chainman,
2045-
CTxMemPool& pool, Options opts)
2047+
CTxMemPool& pool, node::Warnings& warnings, Options opts)
20462048
{
2047-
return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, opts);
2049+
return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, warnings, opts);
20482050
}
20492051

20502052
PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
20512053
BanMan* banman, ChainstateManager& chainman,
2052-
CTxMemPool& pool, Options opts)
2054+
CTxMemPool& pool, node::Warnings& warnings, Options opts)
20532055
: m_rng{opts.deterministic_rng},
20542056
m_fee_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}, m_rng},
20552057
m_chainparams(chainman.GetParams()),
@@ -2058,6 +2060,7 @@ PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
20582060
m_banman(banman),
20592061
m_chainman(chainman),
20602062
m_mempool(pool),
2063+
m_warnings{warnings},
20612064
m_opts{opts}
20622065
{
20632066
// While Erlay support is incomplete, it must be enabled explicitly via -txreconciliation.

0 commit comments

Comments
 (0)