Skip to content

Commit 73133c3

Browse files
committed
refactor: Add NodeContext::shutdown member
Add NodeContext::shutdown variable and start using it to replace the kernel::Context::interrupt variable. The latter can't easily be removed right away but will be removed later in this PR. Moving the interrupt object from the kernel context to the node context increases flexibility of the kernel API so it is possible to use multiple interrupt objects, or avoid creating one if one is not needed. It will also allow getting rid of the kernel::g_context global later in this PR, replacing it with a private SignalInterrupt instance in init.cpp There is no change in behavior in this commit outside of unit tests. In unit tests there should be no visible change either, but internally now each test has its own interrupt variable so the variable will be automatically reset between tests.
1 parent f4a8bd6 commit 73133c3

File tree

9 files changed

+12
-5
lines changed

9 files changed

+12
-5
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ int main(int argc, char* argv[])
125125
.blocks_dir = abs_datadir / "blocks",
126126
.notifications = chainman_opts.notifications,
127127
};
128-
ChainstateManager chainman{kernel_context.interrupt, chainman_opts, blockman_opts};
128+
util::SignalInterrupt interrupt;
129+
ChainstateManager chainman{interrupt, chainman_opts, blockman_opts};
129130

130131
node::CacheSizes cache_sizes;
131132
cache_sizes.block_tree_db = 2 << 20;

src/bitcoind.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static bool AppInit(NodeContext& node)
185185
}
186186

187187
node.kernel = std::make_unique<kernel::Context>();
188+
node.shutdown = &node.kernel->interrupt; // TEMPORARY: will go away when kernel->interrupt member is removed
188189
if (!AppInitSanityChecks(*node.kernel))
189190
{
190191
// InitError will have been called with detailed error, which ends up on console

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14851485
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
14861486
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
14871487

1488-
node.chainman = std::make_unique<ChainstateManager>(node.kernel->interrupt, chainman_opts, blockman_opts);
1488+
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown), chainman_opts, blockman_opts);
14891489
ChainstateManager& chainman = *node.chainman;
14901490

14911491
// This is defined and set here instead of inline in validation.h to avoid a hard

src/node/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct NodeContext {
5050
std::unique_ptr<kernel::Context> kernel;
5151
//! Init interface for initializing current process and connecting to other processes.
5252
interfaces::Init* init{nullptr};
53+
//! Interrupt object used to track whether node shutdown was requested.
54+
util::SignalInterrupt* shutdown{nullptr};
5355
std::unique_ptr<AddrMan> addrman;
5456
std::unique_ptr<CConnman> connman;
5557
std::unique_ptr<CTxMemPool> mempool;

src/node/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class NodeImpl : public Node
9999
if (!AppInitParameterInteraction(args())) return false;
100100

101101
m_context->kernel = std::make_unique<kernel::Context>();
102+
m_context->shutdown = &m_context->kernel->interrupt; // TEMPORARY: will go away when kernel->interrupt member is removed
102103
if (!AppInitSanityChecks(*m_context->kernel)) return false;
103104

104105
if (!AppInitLockDataDirectory()) return false;

src/test/blockmanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
3333
.blocks_dir = m_args.GetBlocksDirPath(),
3434
.notifications = notifications,
3535
};
36-
BlockManager blockman{m_node.kernel->interrupt, blockman_opts};
36+
BlockManager blockman{*Assert(m_node.shutdown), blockman_opts};
3737
// simulate adding a genesis block normally
3838
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
3939
// simulate what happens during reindex

src/test/util/setup_common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, const std::vecto
102102
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()},
103103
m_args{}
104104
{
105+
m_node.shutdown = &m_interrupt;
105106
m_node.args = &gArgs;
106107
std::vector<const char*> arguments = Cat(
107108
{
@@ -194,7 +195,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
194195
.blocks_dir = m_args.GetBlocksDirPath(),
195196
.notifications = chainman_opts.notifications,
196197
};
197-
m_node.chainman = std::make_unique<ChainstateManager>(m_node.kernel->interrupt, chainman_opts, blockman_opts);
198+
m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown), chainman_opts, blockman_opts);
198199
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<BlockTreeDB>(DBParams{
199200
.path = m_args.GetDataDirNet() / "blocks" / "index",
200201
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),

src/test/util/setup_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static constexpr CAmount CENT{1000000};
4747
* This just configures logging, data dir and chain parameters.
4848
*/
4949
struct BasicTestingSetup {
50+
util::SignalInterrupt m_interrupt;
5051
node::NodeContext m_node; // keep as first member to be destructed last
5152

5253
explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ struct SnapshotTestSetup : TestChain100Setup {
394394
// For robustness, ensure the old manager is destroyed before creating a
395395
// new one.
396396
m_node.chainman.reset();
397-
m_node.chainman = std::make_unique<ChainstateManager>(m_node.kernel->interrupt, chainman_opts, blockman_opts);
397+
m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown), chainman_opts, blockman_opts);
398398
}
399399
return *Assert(m_node.chainman);
400400
}

0 commit comments

Comments
 (0)