Skip to content

Commit 447761c

Browse files
committed
kernel: Add notification interface
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. Define a new kernel notification class with virtual methods for notifying about internal kernel events. Create a new file in the node library for defining a function creating the default set of notification methods such that these do not need to be re-defined all over the codebase. As a first step, add a `blockTip` method, wrapping `uiInterface.NotifyBlockTip`.
1 parent 0f8c95d commit 447761c

13 files changed

+103
-2
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ BITCOIN_CORE_H = \
186186
kernel/mempool_limits.h \
187187
kernel/mempool_options.h \
188188
kernel/mempool_persist.h \
189+
kernel/notifications_interface.h \
189190
kernel/validation_cache_sizes.h \
190191
key.h \
191192
key_io.h \
@@ -214,6 +215,7 @@ BITCOIN_CORE_H = \
214215
node/database_args.h \
215216
node/eviction.h \
216217
node/interface_ui.h \
218+
node/kernel_notifications.h \
217219
node/mempool_args.h \
218220
node/mempool_persist_args.h \
219221
node/miner.h \
@@ -408,6 +410,7 @@ libbitcoin_node_a_SOURCES = \
408410
node/eviction.cpp \
409411
node/interface_ui.cpp \
410412
node/interfaces.cpp \
413+
node/kernel_notifications.cpp \
411414
node/mempool_args.cpp \
412415
node/mempool_persist_args.cpp \
413416
node/miner.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// It is part of the libbitcoinkernel project.
1313

1414
#include <kernel/chainparams.h>
15+
#include <kernel/chainstatemanager_opts.h>
1516
#include <kernel/checks.h>
1617
#include <kernel/context.h>
1718
#include <kernel/validation_cache_sizes.h>
@@ -34,6 +35,7 @@
3435
#include <filesystem>
3536
#include <functional>
3637
#include <iosfwd>
38+
#include <memory>
3739

3840
int main(int argc, char* argv[])
3941
{
@@ -80,12 +82,22 @@ int main(int argc, char* argv[])
8082

8183
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
8284

85+
class KernelNotifications : public kernel::Notifications
86+
{
87+
public:
88+
void blockTip(SynchronizationState, CBlockIndex&) override
89+
{
90+
std::cout << "Block tip changed" << std::endl;
91+
}
92+
};
93+
auto notifications = std::make_unique<KernelNotifications>();
8394

8495
// SETUP: Chainstate
8596
const ChainstateManager::Options chainman_opts{
8697
.chainparams = *chainparams,
8798
.datadir = gArgs.GetDataDirNet(),
8899
.adjusted_time_callback = NodeClock::now,
100+
.notifications = *notifications,
89101
};
90102
const node::BlockManager::Options blockman_opts{
91103
.chainparams = chainman_opts.chainparams,

src/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <node/chainstatemanager_args.h>
4646
#include <node/context.h>
4747
#include <node/interface_ui.h>
48+
#include <node/kernel_notifications.h>
4849
#include <node/mempool_args.h>
4950
#include <node/mempool_persist_args.h>
5051
#include <node/miner.h>
@@ -124,6 +125,7 @@ using node::DEFAULT_PERSIST_MEMPOOL;
124125
using node::DEFAULT_PRINTPRIORITY;
125126
using node::fReindex;
126127
using node::g_indexes_ready_to_sync;
128+
using node::KernelNotifications;
127129
using node::LoadChainstate;
128130
using node::MempoolPath;
129131
using node::NodeContext;
@@ -1019,9 +1021,11 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10191021

10201022
// Also report errors from parsing before daemonization
10211023
{
1024+
KernelNotifications notifications{};
10221025
ChainstateManager::Options chainman_opts_dummy{
10231026
.chainparams = chainparams,
10241027
.datadir = args.GetDataDirNet(),
1028+
.notifications = notifications,
10251029
};
10261030
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
10271031
return InitError(*error);
@@ -1427,12 +1431,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14271431

14281432
// ********************************************************* Step 7: load block chain
14291433

1434+
node.notifications = std::make_unique<KernelNotifications>();
14301435
fReindex = args.GetBoolArg("-reindex", false);
14311436
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
14321437
ChainstateManager::Options chainman_opts{
14331438
.chainparams = chainparams,
14341439
.datadir = args.GetDataDirNet(),
14351440
.adjusted_time_callback = GetAdjustedTime,
1441+
.notifications = *node.notifications,
14361442
};
14371443
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14381444

src/kernel/chainstatemanager_opts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
77

8+
#include <kernel/notifications_interface.h>
9+
810
#include <arith_uint256.h>
911
#include <dbwrapper.h>
1012
#include <txdb.h>
@@ -42,6 +44,7 @@ struct ChainstateManagerOpts {
4244
DBOptions block_tree_db{};
4345
DBOptions coins_db{};
4446
CoinsViewOptions coins_view{};
47+
Notifications& notifications;
4548
};
4649

4750
} // namespace kernel

src/kernel/notifications_interface.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2023 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_NOTIFICATIONS_INTERFACE_H
6+
#define BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H
7+
8+
class CBlockIndex;
9+
enum class SynchronizationState;
10+
11+
namespace kernel {
12+
13+
/**
14+
* A base class defining functions for notifying about certain kernel
15+
* events.
16+
*/
17+
class Notifications
18+
{
19+
public:
20+
virtual ~Notifications(){};
21+
22+
virtual void blockTip(SynchronizationState state, CBlockIndex& index) {}
23+
};
24+
} // namespace kernel
25+
26+
#endif // BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <net.h>
1212
#include <net_processing.h>
1313
#include <netgroup.h>
14+
#include <node/kernel_notifications.h>
1415
#include <policy/fees.h>
1516
#include <scheduler.h>
1617
#include <txmempool.h>

src/node/context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class WalletLoader;
3030
} // namespace interfaces
3131

3232
namespace node {
33+
class KernelNotifications;
34+
3335
//! NodeContext struct containing references to chain state and connection
3436
//! state.
3537
//!
@@ -62,6 +64,7 @@ struct NodeContext {
6264
interfaces::WalletLoader* wallet_loader{nullptr};
6365
std::unique_ptr<CScheduler> scheduler;
6466
std::function<void()> rpc_interruption_point = [] {};
67+
std::unique_ptr<KernelNotifications> notifications;
6568

6669
//! Declare default constructor and destructor that are not inline, so code
6770
//! instantiating the NodeContext struct doesn't need to #include class

src/node/kernel_notifications.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2023 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+
#include <node/kernel_notifications.h>
6+
7+
#include <node/interface_ui.h>
8+
9+
namespace node {
10+
11+
void KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
12+
{
13+
uiInterface.NotifyBlockTip(state, &index);
14+
}
15+
16+
} // namespace node

src/node/kernel_notifications.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2023 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_NODE_KERNEL_NOTIFICATIONS_H
6+
#define BITCOIN_NODE_KERNEL_NOTIFICATIONS_H
7+
8+
#include <kernel/notifications_interface.h>
9+
10+
class CBlockIndex;
11+
enum class SynchronizationState;
12+
13+
namespace node {
14+
class KernelNotifications : public kernel::Notifications
15+
{
16+
public:
17+
void blockTip(SynchronizationState state, CBlockIndex& index) override;
18+
};
19+
} // namespace node
20+
21+
#endif // BITCOIN_NODE_KERNEL_NOTIFICATIONS_H

src/test/util/setup_common.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <node/blockstorage.h>
2424
#include <node/chainstate.h>
2525
#include <node/context.h>
26+
#include <node/kernel_notifications.h>
2627
#include <node/mempool_args.h>
2728
#include <node/miner.h>
2829
#include <node/validation_cache_args.h>
@@ -64,6 +65,7 @@ using node::ApplyArgsManOptions;
6465
using node::BlockAssembler;
6566
using node::BlockManager;
6667
using node::CalculateCacheSizes;
68+
using node::KernelNotifications;
6769
using node::LoadChainstate;
6870
using node::RegenerateCommitments;
6971
using node::VerifyLoadedChainstate;
@@ -182,11 +184,14 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
182184

183185
m_cache_sizes = CalculateCacheSizes(m_args);
184186

187+
m_node.notifications = std::make_unique<KernelNotifications>();
188+
185189
const ChainstateManager::Options chainman_opts{
186190
.chainparams = chainparams,
187191
.datadir = m_args.GetDataDirNet(),
188192
.adjusted_time_callback = GetAdjustedTime,
189193
.check_block_index = true,
194+
.notifications = *m_node.notifications,
190195
};
191196
const BlockManager::Options blockman_opts{
192197
.chainparams = chainman_opts.chainparams,

0 commit comments

Comments
 (0)