Skip to content

Commit c2b4886

Browse files
MarcoFalkevijaydasmp
authored andcommitted
Merge 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#22371 (review)) Tree-SHA512: 1b7badbf503d53f5d4dbd9ed8f2e5c1ebfe48102665197048cc9e37bc87b5cec5f2277f3aae9f73a1095bfe879b19d288286ca3daa28031f5f1b64b1184439a9
1 parent ca5070a commit c2b4886

File tree

7 files changed

+22
-29
lines changed

7 files changed

+22
-29
lines changed

src/index/txindex.cpp

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

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ void PrepareShutdown(NodeContext& node)
338338
chainstate->ResetCoinsViews();
339339
}
340340
}
341-
pblocktree.reset();
342341
node.chain_helper.reset();
343342
if (node.mnhf_manager) {
344343
node.mnhf_manager->DisconnectManagers();
@@ -1992,6 +1991,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
19921991

19931992
UnloadBlockIndex(node.mempool.get(), chainman);
19941993

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

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman,
147147
}
148148
nFile++;
149149
}
150-
pblocktree->WriteReindexing(false);
150+
WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
151151
fReindex = false;
152152
LogPrintf("Reindexing finished\n");
153153
// 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
@@ -224,12 +224,11 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
224224
m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
225225
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
226226

227-
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
228-
229227
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
230228
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
231229

232230
m_node.chainman = std::make_unique<ChainstateManager>();
231+
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
233232

234233
m_node.connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests.
235234

@@ -264,7 +263,6 @@ ChainTestingSetup::~ChainTestingSetup()
264263
m_node.scheduler.reset();
265264
m_node.chainman->Reset();
266265
m_node.chainman.reset();
267-
pblocktree.reset();
268266
}
269267

270268
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
@@ -25,6 +25,7 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup)
2525
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
2626
{
2727
ChainstateManager manager;
28+
WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true));
2829
CTxMemPool mempool;
2930

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

src/validation.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo
197197
return chain.Genesis();
198198
}
199199

200-
std::unique_ptr<CBlockTreeDB> pblocktree;
201-
202200
bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
203201
static FILE* OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false);
204202
static FlatFileSeq BlockFileSeq();
@@ -2525,7 +2523,7 @@ bool CChainState::FlushStateToDisk(
25252523
if (!setFilesToPrune.empty()) {
25262524
fFlushForPrune = true;
25272525
if (!fHavePruned) {
2528-
pblocktree->WriteFlag("prunedblockfiles", true);
2526+
m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
25292527
fHavePruned = true;
25302528
}
25312529
}
@@ -2580,7 +2578,7 @@ bool CChainState::FlushStateToDisk(
25802578
vBlocks.push_back(*it);
25812579
setDirtyBlockIndex.erase(it++);
25822580
}
2583-
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2581+
if (!m_blockman.m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
25842582
return AbortNode(state, "Failed to write to block index database");
25852583
}
25862584
}
@@ -4477,11 +4475,11 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash)
44774475

44784476
bool BlockManager::LoadBlockIndex(
44794477
const Consensus::Params& consensus_params,
4480-
CBlockTreeDB& blocktree,
44814478
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
44824479
{
4483-
if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }))
4480+
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
44844481
return false;
4482+
}
44854483

44864484
// Calculate nChainWork
44874485
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
@@ -4547,25 +4545,20 @@ void BlockManager::Unload() {
45474545
m_prev_block_index.clear();
45484546
}
45494547

4550-
bool CChainState::LoadBlockIndexDB()
4548+
bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates)
45514549
{
4552-
if (!m_blockman.LoadBlockIndex(
4553-
m_params.GetConsensus(), *pblocktree,
4550+
if (!LoadBlockIndex(
4551+
::Params().GetConsensus(),
45544552
setBlockIndexCandidates)) {
45554553
return false;
4556-
}
45574554

45584555
// Load block file info
4559-
pblocktree->ReadLastBlockFile(nLastBlockFile);
4560-
vinfoBlockFile.resize(nLastBlockFile + 1);
4561-
LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
4562-
for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
4563-
pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
4556+
m_block_tree_db->ReadLastBlockFile(nLastBlockFile);
45644557
}
45654558
LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
45664559
for (int nFile = nLastBlockFile + 1; true; nFile++) {
45674560
CBlockFileInfo info;
4568-
if (pblocktree->ReadBlockFileInfo(nFile, info)) {
4561+
if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
45694562
vinfoBlockFile.push_back(info);
45704563
} else {
45714564
break;
@@ -4575,7 +4568,7 @@ bool CChainState::LoadBlockIndexDB()
45754568
// Check presence of blk files
45764569
LogPrintf("Checking all blk files are present...\n");
45774570
std::set<int> setBlkDataFiles;
4578-
for (const std::pair<const uint256, CBlockIndex*>& item : m_blockman.m_block_index) {
4571+
for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
45794572
CBlockIndex* pindex = item.second;
45804573
if (pindex->nStatus & BLOCK_HAVE_DATA) {
45814574
setBlkDataFiles.insert(pindex->nFile);
@@ -4590,13 +4583,13 @@ bool CChainState::LoadBlockIndexDB()
45904583
}
45914584

45924585
// Check whether we have ever pruned block & undo files
4593-
pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
4586+
m_block_tree_db->ReadFlag("prunedblockfiles", fHavePruned);
45944587
if (fHavePruned)
45954588
LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
45964589

45974590
// Check whether we need to continue reindexing
45984591
bool fReindexing = false;
4599-
pblocktree->ReadReindexing(fReindexing);
4592+
m_block_tree_db->ReadReindexing(fReindexing);
46004593
if(fReindexing) fReindex = true;
46014594

46024595
// Check whether we have an address index
@@ -4988,7 +4981,7 @@ bool ChainstateManager::LoadBlockIndex()
49884981
// Load block index from databases
49894982
bool needs_init = fReindex;
49904983
if (!fReindex) {
4991-
bool ret = ActiveChainstate().LoadBlockIndexDB();
4984+
bool ret = m_blockman.LoadBlockIndexDB(ActiveChainstate().setBlockIndexCandidates);
49924985
if (!ret) return false;
49934986
needs_init = m_blockman.m_block_index.empty();
49944987
}

src/validation.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@ class BlockManager
494494
*/
495495
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
496496

497+
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
498+
499+
bool LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
500+
497501
/**
498502
* Load the blocktree off disk and into memory. Populate certain metadata
499503
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
@@ -504,7 +508,6 @@ class BlockManager
504508
*/
505509
bool LoadBlockIndex(
506510
const Consensus::Params& consensus_params,
507-
CBlockTreeDB& blocktree,
508511
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
509512
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
510513
/** Clear all data members. */
@@ -864,8 +867,6 @@ class CChainState
864867
void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
865868
void ConflictingChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
866869

867-
bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
868-
869870
//! Indirection necessary to make lock annotations work with an optional mempool.
870871
RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs)
871872
{

0 commit comments

Comments
 (0)