Skip to content

Commit 951850b

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22371: Move pblocktree global to BlockManager
faa54e3 Move pblocktree global to BlockManager (MarcoFalke) fa27f03 Move LoadBlockIndexDB to BlockManager (MarcoFalke) Pull request description: The block tree db is used within BlockManager to write and read the block index, so make the db global a member variable of BlockManager. ACKs for top commit: jamesob: ACK faa54e3 ([`jamesob/ackr/22371.1.MarcoFalke.move_pblocktree_global_t`](https://github.com/jamesob/bitcoin/tree/ackr/22371.1.MarcoFalke.move_pblocktree_global_t)) theStack: re-ACK faa54e3 🥧 ryanofsky: Code review ACK faa54e3. I was thinking this looked like a change Carl would like, so no surprised he [Mega-acked](bitcoin/bitcoin#22371 (review)) Tree-SHA512: 1b7badbf503d53f5d4dbd9ed8f2e5c1ebfe48102665197048cc9e37bc87b5cec5f2277f3aae9f73a1095bfe879b19d288286ca3daa28031f5f1b64b1184439a9
2 parents 9faa4b6 + faa54e3 commit 951850b

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
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: 14 additions & 16 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;
@@ -3764,25 +3762,25 @@ void BlockManager::Unload() {
37643762
m_block_index.clear();
37653763
}
37663764

3767-
bool CChainState::LoadBlockIndexDB()
3765+
bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates)
37683766
{
3769-
if (!m_blockman.LoadBlockIndex(
3770-
m_params.GetConsensus(), *pblocktree,
3767+
if (!LoadBlockIndex(
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;
@@ -3792,7 +3790,7 @@ bool CChainState::LoadBlockIndexDB()
37923790
// Check presence of blk files
37933791
LogPrintf("Checking all blk files are present...\n");
37943792
std::set<int> setBlkDataFiles;
3795-
for (const std::pair<const uint256, CBlockIndex*>& item : m_blockman.m_block_index) {
3793+
for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
37963794
CBlockIndex* pindex = item.second;
37973795
if (pindex->nStatus & BLOCK_HAVE_DATA) {
37983796
setBlkDataFiles.insert(pindex->nFile);
@@ -3807,13 +3805,13 @@ bool CChainState::LoadBlockIndexDB()
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;
@@ -4114,7 +4112,7 @@ bool ChainstateManager::LoadBlockIndex()
41144112
// Load block index from databases
41154113
bool needs_init = fReindex;
41164114
if (!fReindex) {
4117-
bool ret = ActiveChainstate().LoadBlockIndexDB();
4115+
bool ret = m_blockman.LoadBlockIndexDB(ActiveChainstate().setBlockIndexCandidates);
41184116
if (!ret) return false;
41194117
needs_init = m_blockman.m_block_index.empty();
41204118
}

src/validation.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ 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+
451+
bool LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
452+
449453
/**
450454
* Load the blocktree off disk and into memory. Populate certain metadata
451455
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
@@ -456,7 +460,6 @@ class BlockManager
456460
*/
457461
bool LoadBlockIndex(
458462
const Consensus::Params& consensus_params,
459-
CBlockTreeDB& blocktree,
460463
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
461464
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
462465

@@ -798,8 +801,6 @@ class CChainState
798801
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
799802
void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
800803

801-
bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
802-
803804
//! Indirection necessary to make lock annotations work with an optional mempool.
804805
RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
805806
{
@@ -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)