Skip to content

Commit edb55e2

Browse files
kernel: Pass interrupt reference to chainman
This and the following commit seek to decouple the libbitcoinkernel library from the shutdown code. As a library, it should it should have its own flexible interrupt infrastructure without relying on node-wide globals. The commit takes the first step towards this goal by de-globalising `ShutdownRequested` calls in kernel code. Co-authored-by: Russell Yanofsky <[email protected]> Co-authored-by: TheCharlatan <[email protected]>
1 parent e2d680a commit edb55e2

12 files changed

+55
-40
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int main(int argc, char* argv[])
113113
.chainparams = chainman_opts.chainparams,
114114
.blocks_dir = abs_datadir / "blocks",
115115
};
116-
ChainstateManager chainman{chainman_opts, blockman_opts};
116+
ChainstateManager chainman{kernel_context.interrupt, chainman_opts, blockman_opts};
117117

118118
node::CacheSizes cache_sizes;
119119
cache_sizes.block_tree_db = 2 << 20;

src/init.cpp

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

1465-
node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
1465+
node.chainman = std::make_unique<ChainstateManager>(node.kernel->interrupt, chainman_opts, blockman_opts);
14661466
ChainstateManager& chainman = *node.chainman;
14671467

14681468
node::ChainstateLoadOptions options;

src/kernel/mempool_persist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#include <logging.h>
1010
#include <primitives/transaction.h>
1111
#include <serialize.h>
12-
#include <shutdown.h>
1312
#include <streams.h>
1413
#include <sync.h>
1514
#include <txmempool.h>
1615
#include <uint256.h>
1716
#include <util/fs.h>
1817
#include <util/fs_helpers.h>
18+
#include <util/signalinterrupt.h>
1919
#include <util/time.h>
2020
#include <validation.h>
2121

@@ -95,7 +95,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
9595
} else {
9696
++expired;
9797
}
98-
if (ShutdownRequested())
98+
if (active_chainstate.m_chainman.m_interrupt)
9999
return false;
100100
}
101101
std::map<uint256, CAmount> mapDeltas;

src/node/blockstorage.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#include <logging.h>
1414
#include <pow.h>
1515
#include <reverse_iterator.h>
16-
#include <shutdown.h>
1716
#include <signet.h>
1817
#include <streams.h>
1918
#include <undo.h>
2019
#include <util/batchpriority.h>
2120
#include <util/fs.h>
21+
#include <util/signalinterrupt.h>
2222
#include <validation.h>
2323

2424
#include <map>
@@ -250,7 +250,8 @@ CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
250250

251251
bool BlockManager::LoadBlockIndex()
252252
{
253-
if (!m_block_tree_db->LoadBlockIndexGuts(GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
253+
if (!m_block_tree_db->LoadBlockIndexGuts(
254+
GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, m_interrupt)) {
254255
return false;
255256
}
256257

@@ -260,7 +261,7 @@ bool BlockManager::LoadBlockIndex()
260261
CBlockIndexHeightOnlyComparator());
261262

262263
for (CBlockIndex* pindex : vSortedByHeight) {
263-
if (ShutdownRequested()) return false;
264+
if (m_interrupt) return false;
264265
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
265266
pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
266267

@@ -890,8 +891,8 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
890891
}
891892
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
892893
chainman.ActiveChainstate().LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
893-
if (ShutdownRequested()) {
894-
LogPrintf("Shutdown requested. Exit %s\n", __func__);
894+
if (chainman.m_interrupt) {
895+
LogPrintf("Interrupt requested. Exit %s\n", __func__);
895896
return;
896897
}
897898
nFile++;
@@ -909,8 +910,8 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
909910
if (file) {
910911
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
911912
chainman.ActiveChainstate().LoadExternalBlockFile(file);
912-
if (ShutdownRequested()) {
913-
LogPrintf("Shutdown requested. Exit %s\n", __func__);
913+
if (chainman.m_interrupt) {
914+
LogPrintf("Interrupt requested. Exit %s\n", __func__);
914915
return;
915916
}
916917
} else {

src/node/blockstorage.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct FlatFilePos;
3333
namespace Consensus {
3434
struct Params;
3535
}
36+
namespace util {
37+
class SignalInterrupt;
38+
} // namespace util
3639

3740
namespace node {
3841

@@ -153,10 +156,12 @@ class BlockManager
153156
public:
154157
using Options = kernel::BlockManagerOpts;
155158

156-
explicit BlockManager(Options opts)
159+
explicit BlockManager(const util::SignalInterrupt& interrupt, Options opts)
157160
: m_prune_mode{opts.prune_target > 0},
158-
m_opts{std::move(opts)} {};
161+
m_opts{std::move(opts)},
162+
m_interrupt{interrupt} {};
159163

164+
const util::SignalInterrupt& m_interrupt;
160165
std::atomic<bool> m_importing{false};
161166

162167
BlockMap m_block_index GUARDED_BY(cs_main);

src/test/blockmanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2525
.chainparams = *params,
2626
.blocks_dir = m_args.GetBlocksDirPath(),
2727
};
28-
BlockManager blockman{blockman_opts};
28+
BlockManager blockman{m_node.kernel->interrupt, blockman_opts};
2929
CChain chain {};
3030
// simulate adding a genesis block normally
3131
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
197197
.chainparams = chainman_opts.chainparams,
198198
.blocks_dir = m_args.GetBlocksDirPath(),
199199
};
200-
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
200+
m_node.chainman = std::make_unique<ChainstateManager>(m_node.kernel->interrupt, chainman_opts, blockman_opts);
201201
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
202202
.path = m_args.GetDataDirNet() / "blocks" / "index",
203203
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),

src/test/validation_chainstatemanager_tests.cpp

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

src/txdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <logging.h>
1010
#include <pow.h>
1111
#include <random.h>
12-
#include <shutdown.h>
1312
#include <uint256.h>
13+
#include <util/signalinterrupt.h>
1414
#include <util/translation.h>
1515
#include <util/vector.h>
1616

@@ -291,15 +291,15 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
291291
return true;
292292
}
293293

294-
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
294+
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
295295
{
296296
AssertLockHeld(::cs_main);
297297
std::unique_ptr<CDBIterator> pcursor(NewIterator());
298298
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
299299

300300
// Load m_block_index
301301
while (pcursor->Valid()) {
302-
if (ShutdownRequested()) return false;
302+
if (interrupt) return false;
303303
std::pair<uint8_t, uint256> key;
304304
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
305305
CDiskBlockIndex diskindex;

src/txdb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class uint256;
2929
namespace Consensus {
3030
struct Params;
3131
};
32+
namespace util {
33+
class SignalInterrupt;
34+
} // namespace util
3235

3336
//! -dbcache default (MiB)
3437
static const int64_t nDefaultDbCache = 450;
@@ -98,7 +101,7 @@ class CBlockTreeDB : public CDBWrapper
98101
void ReadReindexing(bool &fReindexing);
99102
bool WriteFlag(const std::string &name, bool fValue);
100103
bool ReadFlag(const std::string &name, bool &fValue);
101-
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
104+
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
102105
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
103106
};
104107

0 commit comments

Comments
 (0)