merge bitcoin#22371: Move pblocktree global to BlockManager

This commit is contained in:
Kittywhiskers Van Gogh 2024-06-26 18:25:14 +00:00
parent d69ca833df
commit 472caa048a
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
10 changed files with 62 additions and 57 deletions

View File

@ -204,7 +204,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;
}

View File

@ -338,7 +338,6 @@ void PrepareShutdown(NodeContext& node)
chainstate->ResetCoinsViews();
}
}
pblocktree.reset();
node.chain_helper.reset();
if (node.mnhf_manager) {
node.mnhf_manager->DisconnectManagers();
@ -1951,6 +1950,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();

View File

@ -472,7 +472,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):

View File

@ -835,7 +835,8 @@ static RPCHelpMan getblockhashes()
unsigned int low = request.params[1].get_int();
std::vector<uint256> blockHashes;
if (LOCK(::cs_main); !GetTimestampIndex(*pblocktree, high, low, blockHashes)) {
ChainstateManager& chainman = EnsureAnyChainman(request.context);
if (LOCK(::cs_main); !GetTimestampIndex(*chainman.m_blockman.m_block_tree_db, high, low, blockHashes)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for block hashes");
}

View File

@ -813,10 +813,11 @@ static RPCHelpMan getaddressutxos()
std::vector<CAddressUnspentIndexEntry> unspentOutputs;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
{
LOCK(::cs_main);
for (const auto& address : addresses) {
if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs,
if (!GetAddressUnspentIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, unspentOutputs,
/* height_sort = */ true)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
@ -900,15 +901,20 @@ static RPCHelpMan getaddressdeltas()
std::vector<CAddressIndexEntry> addressIndex;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
{
LOCK(::cs_main);
for (const auto& address : addresses) {
if (start > 0 && end > 0) {
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) {
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
addressIndex, start, end))
{
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
} else {
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
addressIndex))
{
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
}
@ -978,7 +984,7 @@ static RPCHelpMan getaddressbalance()
{
LOCK(::cs_main);
for (const auto& address : addresses) {
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
}
@ -1056,15 +1062,18 @@ static RPCHelpMan getaddresstxids()
std::vector<CAddressIndexEntry> addressIndex;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
{
LOCK(::cs_main);
for (const auto& address : addresses) {
if (start > 0 && end > 0) {
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) {
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
addressIndex, start, end)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
} else {
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
addressIndex)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
}
}
@ -1137,8 +1146,9 @@ static RPCHelpMan getspentinfo()
CSpentIndexKey key(txid, outputIndex);
CSpentIndexValue value;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
if (LOCK(::cs_main); !GetSpentIndex(*pblocktree, mempool, key, value)) {
if (LOCK(::cs_main); !GetSpentIndex(*chainman.m_blockman.m_block_tree_db, mempool, key, value)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info");
}

View File

@ -75,7 +75,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo
if (!tx.IsCoinBase()) {
CSpentIndexValue spentInfo;
CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n);
if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) {
if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) {
txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
}
}
@ -83,7 +83,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo
for (unsigned int i = 0; i < tx.vout.size(); i++) {
CSpentIndexValue spentInfo;
CSpentIndexKey spentKey(txid, i);
if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) {
if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) {
txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
}
}

View File

@ -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<CBlockPolicyEstimator>();
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
m_node.chainman = std::make_unique<ChainstateManager>();
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
m_node.connman = std::make_unique<CConnman>(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<const char*>& extra_args)

View File

@ -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<CBlockTreeDB>(1 << 20, true));
CTxMemPool mempool;
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.

View File

@ -179,8 +179,6 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo
return chain.Genesis();
}
std::unique_ptr<CBlockTreeDB> pblocktree;
bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, int flags)
@ -1700,25 +1698,25 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
if (fSpentIndex) {
if (!pblocktree->UpdateSpentIndex(spentIndex)) {
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex)) {
AbortNode("Failed to delete spent index");
return DISCONNECT_FAILED;
}
}
if (fAddressIndex) {
if (!pblocktree->EraseAddressIndex(addressIndex)) {
if (!m_blockman.m_block_tree_db->EraseAddressIndex(addressIndex)) {
AbortNode("Failed to delete address index");
return DISCONNECT_FAILED;
}
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
AbortNode("Failed to write address unspent index");
return DISCONNECT_FAILED;
}
}
if (fTimestampIndex) {
if (!pblocktree->EraseTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash()))) {
if (!m_blockman.m_block_tree_db->EraseTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash()))) {
AbortNode("Failed to delete timestamp index");
return DISCONNECT_FAILED;
}
@ -2270,21 +2268,21 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
int64_t nTime6 = GetTimeMicros();
if (fAddressIndex) {
if (!pblocktree->WriteAddressIndex(addressIndex)) {
if (!m_blockman.m_block_tree_db->WriteAddressIndex(addressIndex)) {
return AbortNode(state, "Failed to write address index");
}
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
return AbortNode(state, "Failed to write address unspent index");
}
}
if (fSpentIndex)
if (!pblocktree->UpdateSpentIndex(spentIndex))
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex))
return AbortNode(state, "Failed to write transaction index");
if (fTimestampIndex)
if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
if (!m_blockman.m_block_tree_db->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
return AbortNode(state, "Failed to write timestamp index");
int64_t nTime7 = GetTimeMicros(); nTimeIndexWrite += nTime7 - nTime6;
@ -2387,7 +2385,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;
}
}
@ -2442,7 +2440,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");
}
}
@ -4215,11 +4213,11 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash)
bool BlockManager::LoadBlockIndex(
const Consensus::Params& consensus_params,
CBlockTreeDB& blocktree,
std::set<CBlockIndex*, CBlockIndexWorkComparator>& 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<std::pair<int, CBlockIndex*> > vSortedByHeight;
@ -4285,25 +4283,25 @@ void BlockManager::Unload() {
m_prev_block_index.clear();
}
bool CChainState::LoadBlockIndexDB()
bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates)
{
if (!m_blockman.LoadBlockIndex(
m_params.GetConsensus(), *pblocktree,
if (!LoadBlockIndex(
::Params().GetConsensus(),
setBlockIndexCandidates)) {
return false;
}
// Load block file info
pblocktree->ReadLastBlockFile(nLastBlockFile);
m_block_tree_db->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->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
}
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;
@ -4313,7 +4311,7 @@ bool CChainState::LoadBlockIndexDB()
// Check presence of blk files
LogPrintf("Checking all blk files are present...\n");
std::set<int> setBlkDataFiles;
for (const std::pair<const uint256, CBlockIndex*>& item : m_blockman.m_block_index) {
for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
CBlockIndex* pindex = item.second;
if (pindex->nStatus & BLOCK_HAVE_DATA) {
setBlkDataFiles.insert(pindex->nFile);
@ -4328,25 +4326,25 @@ 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
pblocktree->ReadFlag("addressindex", fAddressIndex);
m_block_tree_db->ReadFlag("addressindex", fAddressIndex);
LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
// Check whether we have a timestamp index
pblocktree->ReadFlag("timestampindex", fTimestampIndex);
m_block_tree_db->ReadFlag("timestampindex", fTimestampIndex);
LogPrintf("%s: timestamp index %s\n", __func__, fTimestampIndex ? "enabled" : "disabled");
// Check whether we have a spent index
pblocktree->ReadFlag("spentindex", fSpentIndex);
m_block_tree_db->ReadFlag("spentindex", fSpentIndex);
LogPrintf("%s: spent index %s\n", __func__, fSpentIndex ? "enabled" : "disabled");
return true;
@ -4596,22 +4594,22 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i
}
if (fAddressIndex) {
if (!pblocktree->WriteAddressIndex(addressIndex)) {
if (!m_blockman.m_block_tree_db->WriteAddressIndex(addressIndex)) {
return error("RollforwardBlock(DASH): Failed to write address index");
}
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
return error("RollforwardBlock(DASH): Failed to write address unspent index");
}
}
if (fSpentIndex) {
if (!pblocktree->UpdateSpentIndex(spentIndex))
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex))
return error("RollforwardBlock(DASH): Failed to write transaction index");
}
if (fTimestampIndex) {
if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
if (!m_blockman.m_block_tree_db->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
return error("RollforwardBlock(DASH): Failed to write timestamp index");
}
@ -4726,7 +4724,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();
}
@ -4742,15 +4740,15 @@ bool ChainstateManager::LoadBlockIndex()
// Use the provided setting for -addressindex in the new database
fAddressIndex = gArgs.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX);
pblocktree->WriteFlag("addressindex", fAddressIndex);
m_blockman.m_block_tree_db->WriteFlag("addressindex", fAddressIndex);
// Use the provided setting for -timestampindex in the new database
fTimestampIndex = gArgs.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX);
pblocktree->WriteFlag("timestampindex", fTimestampIndex);
m_blockman.m_block_tree_db->WriteFlag("timestampindex", fTimestampIndex);
// Use the provided setting for -spentindex in the new database
fSpentIndex = gArgs.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX);
pblocktree->WriteFlag("spentindex", fSpentIndex);
m_blockman.m_block_tree_db->WriteFlag("spentindex", fSpentIndex);
}
return true;
}

View File

@ -468,6 +468,10 @@ public:
*/
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
bool LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& 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
@ -478,7 +482,6 @@ public:
*/
bool LoadBlockIndex(
const Consensus::Params& consensus_params,
CBlockTreeDB& blocktree,
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Clear all data members. */
@ -838,8 +841,6 @@ private:
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)
{
@ -1097,10 +1098,6 @@ public:
}
};
/** Global variable that points to the active block tree (protected by cs_main) */
extern std::unique_ptr<CBlockTreeDB> pblocktree;
/**
* Return true if hash can be found in active_chain at nBlockHeight height.
* Fills hashRet with found hash, if no nBlockHeight is specified - active_chain.Height() is used.