Skip to content

Commit faa54e3

Browse files
author
MarcoFalke
committed
Move pblocktree global to BlockManager
1 parent fa27f03 commit faa54e3

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ bool TxIndex::Init()
204204
// Attempt to migrate txindex from the old database to the new one. Even if
205205
// chain_tip is null, the node could be reindexing and we still want to
206206
// delete txindex records in the old database.
207-
if (!m_db->MigrateData(*pblocktree, m_chainstate->m_chain.GetLocator())) {
207+
if (!m_db->MigrateData(*m_chainstate->m_blockman.m_block_tree_db, m_chainstate->m_chain.GetLocator())) {
208208
return false;
209209
}
210210

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ void Shutdown(NodeContext& node)
264264
chainstate->ResetCoinsViews();
265265
}
266266
}
267-
pblocktree.reset();
268267
}
269268
for (const auto& client : node.chain_clients) {
270269
client->stop();
@@ -1355,6 +1354,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13551354

13561355
UnloadBlockIndex(node.mempool.get(), chainman);
13571356

1357+
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
13581358
// new CBlockTreeDB tries to delete the existing file, which
13591359
// fails if it's still open from the previous loop. Close it first:
13601360
pblocktree.reset();

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
518518
}
519519
nFile++;
520520
}
521-
pblocktree->WriteReindexing(false);
521+
WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
522522
fReindex = false;
523523
LogPrintf("Reindexing finished\n");
524524
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):

src/test/util/setup_common.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,11 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
141141
m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
142142
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
143143

144-
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
145-
146144
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
147145
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
148146

149147
m_node.chainman = std::make_unique<ChainstateManager>();
148+
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
150149

151150
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.
152151
constexpr int script_check_threads = 2;
@@ -169,7 +168,6 @@ ChainTestingSetup::~ChainTestingSetup()
169168
m_node.scheduler.reset();
170169
m_node.chainman->Reset();
171170
m_node.chainman.reset();
172-
pblocktree.reset();
173171
}
174172

175173
TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)

src/test/validation_chainstate_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup)
2020
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
2121
{
2222
ChainstateManager manager;
23+
WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true));
2324
CTxMemPool mempool;
2425

2526
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.

src/validation.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo
170170
return chain.Genesis();
171171
}
172172

173-
std::unique_ptr<CBlockTreeDB> pblocktree;
174-
175173
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
176174
const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore,
177175
bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
@@ -2075,7 +2073,7 @@ bool CChainState::FlushStateToDisk(
20752073
if (!setFilesToPrune.empty()) {
20762074
fFlushForPrune = true;
20772075
if (!fHavePruned) {
2078-
pblocktree->WriteFlag("prunedblockfiles", true);
2076+
m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
20792077
fHavePruned = true;
20802078
}
20812079
}
@@ -2127,7 +2125,7 @@ bool CChainState::FlushStateToDisk(
21272125
vBlocks.push_back(*it);
21282126
setDirtyBlockIndex.erase(it++);
21292127
}
2130-
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2128+
if (!m_blockman.m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
21312129
return AbortNode(state, "Failed to write to block index database");
21322130
}
21332131
}
@@ -3700,11 +3698,11 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash)
37003698

37013699
bool BlockManager::LoadBlockIndex(
37023700
const Consensus::Params& consensus_params,
3703-
CBlockTreeDB& blocktree,
37043701
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
37053702
{
3706-
if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }))
3703+
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
37073704
return false;
3705+
}
37083706

37093707
// Calculate nChainWork
37103708
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
@@ -3767,22 +3765,22 @@ void BlockManager::Unload() {
37673765
bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates)
37683766
{
37693767
if (!LoadBlockIndex(
3770-
::Params().GetConsensus(), *pblocktree,
3768+
::Params().GetConsensus(),
37713769
setBlockIndexCandidates)) {
37723770
return false;
37733771
}
37743772

37753773
// Load block file info
3776-
pblocktree->ReadLastBlockFile(nLastBlockFile);
3774+
m_block_tree_db->ReadLastBlockFile(nLastBlockFile);
37773775
vinfoBlockFile.resize(nLastBlockFile + 1);
37783776
LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
37793777
for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
3780-
pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
3778+
m_block_tree_db->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
37813779
}
37823780
LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
37833781
for (int nFile = nLastBlockFile + 1; true; nFile++) {
37843782
CBlockFileInfo info;
3785-
if (pblocktree->ReadBlockFileInfo(nFile, info)) {
3783+
if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
37863784
vinfoBlockFile.push_back(info);
37873785
} else {
37883786
break;
@@ -3807,13 +3805,13 @@ bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkCompar
38073805
}
38083806

38093807
// Check whether we have ever pruned block & undo files
3810-
pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
3808+
m_block_tree_db->ReadFlag("prunedblockfiles", fHavePruned);
38113809
if (fHavePruned)
38123810
LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
38133811

38143812
// Check whether we need to continue reindexing
38153813
bool fReindexing = false;
3816-
pblocktree->ReadReindexing(fReindexing);
3814+
m_block_tree_db->ReadReindexing(fReindexing);
38173815
if(fReindexing) fReindex = true;
38183816

38193817
return true;

src/validation.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ class BlockManager
446446
*/
447447
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
448448

449+
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
450+
449451
bool LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
450452

451453
/**
@@ -458,7 +460,6 @@ class BlockManager
458460
*/
459461
bool LoadBlockIndex(
460462
const Consensus::Params& consensus_params,
461-
CBlockTreeDB& blocktree,
462463
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
463464
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
464465

@@ -1047,9 +1048,6 @@ class ChainstateManager
10471048
}
10481049
};
10491050

1050-
/** Global variable that points to the active block tree (protected by cs_main) */
1051-
extern std::unique_ptr<CBlockTreeDB> pblocktree;
1052-
10531051
using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
10541052

10551053
/** Dump the mempool to disk. */

0 commit comments

Comments
 (0)