From c50dd0aa198b7df9ad90d04ffca4727812445d4d Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 20 Jul 2021 17:37:21 +0200 Subject: [PATCH] Merge bitcoin/bitcoin#22371: Move pblocktree global to BlockManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit faa54e375782b21cbc2761c763128131c569e903 Move pblocktree global to BlockManager (MarcoFalke) fa27f03b4943540aa2eab283d4cf50ad4a1a01f8 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 faa54e375782b21cbc2761c763128131c569e903 ([`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 faa54e375782b21cbc2761c763128131c569e903 🥧 ryanofsky: Code review ACK faa54e375782b21cbc2761c763128131c569e903. I was thinking this looked like a change Carl would like, so no surprised he [Mega-acked](https://github.com/bitcoin/bitcoin/pull/22371#pullrequestreview-696450475) Tree-SHA512: 1b7badbf503d53f5d4dbd9ed8f2e5c1ebfe48102665197048cc9e37bc87b5cec5f2277f3aae9f73a1095bfe879b19d288286ca3daa28031f5f1b64b1184439a9 --- src/index/txindex.cpp | 2 +- src/init.cpp | 2 +- src/node/blockstorage.cpp | 2 +- src/test/util/setup_common.cpp | 4 +-- src/test/validation_chainstate_tests.cpp | 1 + src/validation.cpp | 33 ++++++++++-------------- src/validation.h | 7 ++--- 7 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index 7b405436f844de..ef296e761c1a80 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -203,7 +203,7 @@ bool TxIndex::Init() // Attempt to migrate txindex from the old database to the new one. Even if // chain_tip is null, the node could be reindexing and we still want to // delete txindex records in the old database. - if (!m_db->MigrateData(*pblocktree, m_chainstate->m_chain.GetLocator())) { + if (!m_db->MigrateData(*m_chainstate->m_blockman.m_block_tree_db, m_chainstate->m_chain.GetLocator())) { return false; } diff --git a/src/init.cpp b/src/init.cpp index 005cca71c5fa84..cfdbc976a79669 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -338,7 +338,6 @@ void PrepareShutdown(NodeContext& node) chainstate->ResetCoinsViews(); } } - pblocktree.reset(); node.chain_helper.reset(); if (node.mnhf_manager) { node.mnhf_manager->DisconnectManagers(); @@ -1992,6 +1991,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc UnloadBlockIndex(node.mempool.get(), chainman); + auto& pblocktree{chainman.m_blockman.m_block_tree_db}; // new CBlockTreeDB tries to delete the existing file, which // fails if it's still open from the previous loop. Close it first: pblocktree.reset(); diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index d271f93ccac073..97c290024a4fea 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -147,7 +147,7 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman, } nFile++; } - pblocktree->WriteReindexing(false); + WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false)); fReindex = false; LogPrintf("Reindexing finished\n"); // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked): diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 6bd00529f0a9e2..742eda02d449d5 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -224,12 +224,11 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); }); GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler); - pblocktree.reset(new CBlockTreeDB(1 << 20, true)); - m_node.fee_estimator = std::make_unique(); m_node.mempool = std::make_unique(m_node.fee_estimator.get(), 1); m_node.chainman = std::make_unique(); + m_node.chainman->m_blockman.m_block_tree_db = std::make_unique(1 << 20, true); m_node.connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests. @@ -264,7 +263,6 @@ ChainTestingSetup::~ChainTestingSetup() m_node.scheduler.reset(); m_node.chainman->Reset(); m_node.chainman.reset(); - pblocktree.reset(); } TestingSetup::TestingSetup(const std::string& chainName, const std::vector& extra_args) diff --git a/src/test/validation_chainstate_tests.cpp b/src/test/validation_chainstate_tests.cpp index c853747e5a1903..746733c75ddd83 100644 --- a/src/test/validation_chainstate_tests.cpp +++ b/src/test/validation_chainstate_tests.cpp @@ -25,6 +25,7 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup) BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches) { ChainstateManager manager; + WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique(1 << 20, true)); CTxMemPool mempool; //! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view. diff --git a/src/validation.cpp b/src/validation.cpp index 3a7ab0424959f7..4414b83402e1fe 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -197,8 +197,6 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo return chain.Genesis(); } -std::unique_ptr pblocktree; - bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector *pvChecks = nullptr); static FILE* OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false); static FlatFileSeq BlockFileSeq(); @@ -2525,7 +2523,7 @@ bool CChainState::FlushStateToDisk( if (!setFilesToPrune.empty()) { fFlushForPrune = true; if (!fHavePruned) { - pblocktree->WriteFlag("prunedblockfiles", true); + m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true); fHavePruned = true; } } @@ -2580,7 +2578,7 @@ bool CChainState::FlushStateToDisk( vBlocks.push_back(*it); setDirtyBlockIndex.erase(it++); } - if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) { + if (!m_blockman.m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) { return AbortNode(state, "Failed to write to block index database"); } } @@ -4477,11 +4475,11 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash) bool BlockManager::LoadBlockIndex( const Consensus::Params& consensus_params, - CBlockTreeDB& blocktree, std::set& block_index_candidates) { - if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) + if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) { return false; + } // Calculate nChainWork std::vector > vSortedByHeight; @@ -4547,25 +4545,20 @@ void BlockManager::Unload() { m_prev_block_index.clear(); } -bool CChainState::LoadBlockIndexDB() +bool BlockManager::LoadBlockIndexDB(std::set& setBlockIndexCandidates) { - if (!m_blockman.LoadBlockIndex( - m_params.GetConsensus(), *pblocktree, + if (!LoadBlockIndex( + ::Params().GetConsensus(), setBlockIndexCandidates)) { return false; - } // Load block file info - pblocktree->ReadLastBlockFile(nLastBlockFile); - vinfoBlockFile.resize(nLastBlockFile + 1); - LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile); - for (int nFile = 0; nFile <= nLastBlockFile; nFile++) { - pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]); + m_block_tree_db->ReadLastBlockFile(nLastBlockFile); } LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString()); for (int nFile = nLastBlockFile + 1; true; nFile++) { CBlockFileInfo info; - if (pblocktree->ReadBlockFileInfo(nFile, info)) { + if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) { vinfoBlockFile.push_back(info); } else { break; @@ -4575,7 +4568,7 @@ bool CChainState::LoadBlockIndexDB() // Check presence of blk files LogPrintf("Checking all blk files are present...\n"); std::set setBlkDataFiles; - for (const std::pair& item : m_blockman.m_block_index) { + for (const std::pair& item : m_block_index) { CBlockIndex* pindex = item.second; if (pindex->nStatus & BLOCK_HAVE_DATA) { setBlkDataFiles.insert(pindex->nFile); @@ -4590,13 +4583,13 @@ bool CChainState::LoadBlockIndexDB() } // Check whether we have ever pruned block & undo files - pblocktree->ReadFlag("prunedblockfiles", fHavePruned); + m_block_tree_db->ReadFlag("prunedblockfiles", fHavePruned); if (fHavePruned) LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n"); // Check whether we need to continue reindexing bool fReindexing = false; - pblocktree->ReadReindexing(fReindexing); + m_block_tree_db->ReadReindexing(fReindexing); if(fReindexing) fReindex = true; // Check whether we have an address index @@ -4988,7 +4981,7 @@ bool ChainstateManager::LoadBlockIndex() // Load block index from databases bool needs_init = fReindex; if (!fReindex) { - bool ret = ActiveChainstate().LoadBlockIndexDB(); + bool ret = m_blockman.LoadBlockIndexDB(ActiveChainstate().setBlockIndexCandidates); if (!ret) return false; needs_init = m_blockman.m_block_index.empty(); } diff --git a/src/validation.h b/src/validation.h index 9b2541fd933a5d..75e7c1a5df9d4c 100644 --- a/src/validation.h +++ b/src/validation.h @@ -494,6 +494,10 @@ class BlockManager */ std::multimap m_blocks_unlinked; + std::unique_ptr m_block_tree_db GUARDED_BY(::cs_main); + + bool LoadBlockIndexDB(std::set& setBlockIndexCandidates) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + /** * Load the blocktree off disk and into memory. Populate certain metadata * per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral @@ -504,7 +508,6 @@ class BlockManager */ bool LoadBlockIndex( const Consensus::Params& consensus_params, - CBlockTreeDB& blocktree, std::set& block_index_candidates) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Clear all data members. */ @@ -864,8 +867,6 @@ class CChainState void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main); void ConflictingChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main); - bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(cs_main); - //! Indirection necessary to make lock annotations work with an optional mempool. RecursiveMutex* MempoolMutex() const LOCK_RETURNED(m_mempool->cs) {