Skip to content

Commit 472caa0

Browse files
committed
merge bitcoin#22371: Move pblocktree global to BlockManager
1 parent d69ca83 commit 472caa0

File tree

10 files changed

+62
-57
lines changed

10 files changed

+62
-57
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
@@ -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();
@@ -1951,6 +1950,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
19511950

19521951
UnloadBlockIndex(node.mempool.get(), chainman);
19531952

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

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ void ThreadImport(ChainstateManager& chainman, CDeterministicMNManager& dmnman,
472472
}
473473
nFile++;
474474
}
475-
pblocktree->WriteReindexing(false);
475+
WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
476476
fReindex = false;
477477
LogPrintf("Reindexing finished\n");
478478
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):

src/rpc/blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@ static RPCHelpMan getblockhashes()
835835
unsigned int low = request.params[1].get_int();
836836
std::vector<uint256> blockHashes;
837837

838-
if (LOCK(::cs_main); !GetTimestampIndex(*pblocktree, high, low, blockHashes)) {
838+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
839+
if (LOCK(::cs_main); !GetTimestampIndex(*chainman.m_blockman.m_block_tree_db, high, low, blockHashes)) {
839840
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for block hashes");
840841
}
841842

src/rpc/misc.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,11 @@ static RPCHelpMan getaddressutxos()
813813

814814
std::vector<CAddressUnspentIndexEntry> unspentOutputs;
815815

816+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
816817
{
817818
LOCK(::cs_main);
818819
for (const auto& address : addresses) {
819-
if (!GetAddressUnspentIndex(*pblocktree, address.first, address.second, unspentOutputs,
820+
if (!GetAddressUnspentIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, unspentOutputs,
820821
/* height_sort = */ true)) {
821822
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
822823
}
@@ -900,15 +901,20 @@ static RPCHelpMan getaddressdeltas()
900901

901902
std::vector<CAddressIndexEntry> addressIndex;
902903

904+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
903905
{
904906
LOCK(::cs_main);
905907
for (const auto& address : addresses) {
906908
if (start > 0 && end > 0) {
907-
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) {
909+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
910+
addressIndex, start, end))
911+
{
908912
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
909913
}
910914
} else {
911-
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
915+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
916+
addressIndex))
917+
{
912918
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
913919
}
914920
}
@@ -978,7 +984,7 @@ static RPCHelpMan getaddressbalance()
978984
{
979985
LOCK(::cs_main);
980986
for (const auto& address : addresses) {
981-
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
987+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex)) {
982988
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
983989
}
984990
}
@@ -1056,15 +1062,18 @@ static RPCHelpMan getaddresstxids()
10561062

10571063
std::vector<CAddressIndexEntry> addressIndex;
10581064

1065+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
10591066
{
10601067
LOCK(::cs_main);
10611068
for (const auto& address : addresses) {
10621069
if (start > 0 && end > 0) {
1063-
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex, start, end)) {
1070+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
1071+
addressIndex, start, end)) {
10641072
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
10651073
}
10661074
} else {
1067-
if (!GetAddressIndex(*pblocktree, address.first, address.second, addressIndex)) {
1075+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
1076+
addressIndex)) {
10681077
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
10691078
}
10701079
}
@@ -1137,8 +1146,9 @@ static RPCHelpMan getspentinfo()
11371146
CSpentIndexKey key(txid, outputIndex);
11381147
CSpentIndexValue value;
11391148

1149+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
11401150
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
1141-
if (LOCK(::cs_main); !GetSpentIndex(*pblocktree, mempool, key, value)) {
1151+
if (LOCK(::cs_main); !GetSpentIndex(*chainman.m_blockman.m_block_tree_db, mempool, key, value)) {
11421152
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info");
11431153
}
11441154

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, CTxMemPool& mempo
7575
if (!tx.IsCoinBase()) {
7676
CSpentIndexValue spentInfo;
7777
CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n);
78-
if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) {
78+
if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) {
7979
txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
8080
}
8181
}
8282
}
8383
for (unsigned int i = 0; i < tx.vout.size(); i++) {
8484
CSpentIndexValue spentInfo;
8585
CSpentIndexKey spentKey(txid, i);
86-
if (GetSpentIndex(*pblocktree, mempool, spentKey, spentInfo)) {
86+
if (GetSpentIndex(*active_chainstate.m_blockman.m_block_tree_db, mempool, spentKey, spentInfo)) {
8787
txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
8888
}
8989
}

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: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlo
179179
return chain.Genesis();
180180
}
181181

182-
std::unique_ptr<CBlockTreeDB> pblocktree;
183-
184182
bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
185183

186184
bool CheckFinalTx(const CBlockIndex* active_chain_tip, const CTransaction &tx, int flags)
@@ -1700,25 +1698,25 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
17001698

17011699

17021700
if (fSpentIndex) {
1703-
if (!pblocktree->UpdateSpentIndex(spentIndex)) {
1701+
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex)) {
17041702
AbortNode("Failed to delete spent index");
17051703
return DISCONNECT_FAILED;
17061704
}
17071705
}
17081706

17091707
if (fAddressIndex) {
1710-
if (!pblocktree->EraseAddressIndex(addressIndex)) {
1708+
if (!m_blockman.m_block_tree_db->EraseAddressIndex(addressIndex)) {
17111709
AbortNode("Failed to delete address index");
17121710
return DISCONNECT_FAILED;
17131711
}
1714-
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
1712+
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
17151713
AbortNode("Failed to write address unspent index");
17161714
return DISCONNECT_FAILED;
17171715
}
17181716
}
17191717

17201718
if (fTimestampIndex) {
1721-
if (!pblocktree->EraseTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash()))) {
1719+
if (!m_blockman.m_block_tree_db->EraseTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash()))) {
17221720
AbortNode("Failed to delete timestamp index");
17231721
return DISCONNECT_FAILED;
17241722
}
@@ -2270,21 +2268,21 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
22702268
int64_t nTime6 = GetTimeMicros();
22712269

22722270
if (fAddressIndex) {
2273-
if (!pblocktree->WriteAddressIndex(addressIndex)) {
2271+
if (!m_blockman.m_block_tree_db->WriteAddressIndex(addressIndex)) {
22742272
return AbortNode(state, "Failed to write address index");
22752273
}
22762274

2277-
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
2275+
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
22782276
return AbortNode(state, "Failed to write address unspent index");
22792277
}
22802278
}
22812279

22822280
if (fSpentIndex)
2283-
if (!pblocktree->UpdateSpentIndex(spentIndex))
2281+
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex))
22842282
return AbortNode(state, "Failed to write transaction index");
22852283

22862284
if (fTimestampIndex)
2287-
if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
2285+
if (!m_blockman.m_block_tree_db->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
22882286
return AbortNode(state, "Failed to write timestamp index");
22892287

22902288
int64_t nTime7 = GetTimeMicros(); nTimeIndexWrite += nTime7 - nTime6;
@@ -2387,7 +2385,7 @@ bool CChainState::FlushStateToDisk(
23872385
if (!setFilesToPrune.empty()) {
23882386
fFlushForPrune = true;
23892387
if (!fHavePruned) {
2390-
pblocktree->WriteFlag("prunedblockfiles", true);
2388+
m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
23912389
fHavePruned = true;
23922390
}
23932391
}
@@ -2442,7 +2440,7 @@ bool CChainState::FlushStateToDisk(
24422440
vBlocks.push_back(*it);
24432441
setDirtyBlockIndex.erase(it++);
24442442
}
2445-
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2443+
if (!m_blockman.m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
24462444
return AbortNode(state, "Failed to write to block index database");
24472445
}
24482446
}
@@ -4215,11 +4213,11 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash)
42154213

42164214
bool BlockManager::LoadBlockIndex(
42174215
const Consensus::Params& consensus_params,
4218-
CBlockTreeDB& blocktree,
42194216
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
42204217
{
4221-
if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }))
4218+
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); })) {
42224219
return false;
4220+
}
42234221

42244222
// Calculate nChainWork
42254223
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
@@ -4285,25 +4283,25 @@ void BlockManager::Unload() {
42854283
m_prev_block_index.clear();
42864284
}
42874285

4288-
bool CChainState::LoadBlockIndexDB()
4286+
bool BlockManager::LoadBlockIndexDB(std::set<CBlockIndex*, CBlockIndexWorkComparator>& setBlockIndexCandidates)
42894287
{
4290-
if (!m_blockman.LoadBlockIndex(
4291-
m_params.GetConsensus(), *pblocktree,
4288+
if (!LoadBlockIndex(
4289+
::Params().GetConsensus(),
42924290
setBlockIndexCandidates)) {
42934291
return false;
42944292
}
42954293

42964294
// Load block file info
4297-
pblocktree->ReadLastBlockFile(nLastBlockFile);
4295+
m_block_tree_db->ReadLastBlockFile(nLastBlockFile);
42984296
vinfoBlockFile.resize(nLastBlockFile + 1);
42994297
LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
43004298
for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
4301-
pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
4299+
m_block_tree_db->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
43024300
}
43034301
LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
43044302
for (int nFile = nLastBlockFile + 1; true; nFile++) {
43054303
CBlockFileInfo info;
4306-
if (pblocktree->ReadBlockFileInfo(nFile, info)) {
4304+
if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
43074305
vinfoBlockFile.push_back(info);
43084306
} else {
43094307
break;
@@ -4313,7 +4311,7 @@ bool CChainState::LoadBlockIndexDB()
43134311
// Check presence of blk files
43144312
LogPrintf("Checking all blk files are present...\n");
43154313
std::set<int> setBlkDataFiles;
4316-
for (const std::pair<const uint256, CBlockIndex*>& item : m_blockman.m_block_index) {
4314+
for (const std::pair<const uint256, CBlockIndex*>& item : m_block_index) {
43174315
CBlockIndex* pindex = item.second;
43184316
if (pindex->nStatus & BLOCK_HAVE_DATA) {
43194317
setBlkDataFiles.insert(pindex->nFile);
@@ -4328,25 +4326,25 @@ bool CChainState::LoadBlockIndexDB()
43284326
}
43294327

43304328
// Check whether we have ever pruned block & undo files
4331-
pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
4329+
m_block_tree_db->ReadFlag("prunedblockfiles", fHavePruned);
43324330
if (fHavePruned)
43334331
LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
43344332

43354333
// Check whether we need to continue reindexing
43364334
bool fReindexing = false;
4337-
pblocktree->ReadReindexing(fReindexing);
4335+
m_block_tree_db->ReadReindexing(fReindexing);
43384336
if(fReindexing) fReindex = true;
43394337

43404338
// Check whether we have an address index
4341-
pblocktree->ReadFlag("addressindex", fAddressIndex);
4339+
m_block_tree_db->ReadFlag("addressindex", fAddressIndex);
43424340
LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
43434341

43444342
// Check whether we have a timestamp index
4345-
pblocktree->ReadFlag("timestampindex", fTimestampIndex);
4343+
m_block_tree_db->ReadFlag("timestampindex", fTimestampIndex);
43464344
LogPrintf("%s: timestamp index %s\n", __func__, fTimestampIndex ? "enabled" : "disabled");
43474345

43484346
// Check whether we have a spent index
4349-
pblocktree->ReadFlag("spentindex", fSpentIndex);
4347+
m_block_tree_db->ReadFlag("spentindex", fSpentIndex);
43504348
LogPrintf("%s: spent index %s\n", __func__, fSpentIndex ? "enabled" : "disabled");
43514349

43524350
return true;
@@ -4596,22 +4594,22 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i
45964594
}
45974595

45984596
if (fAddressIndex) {
4599-
if (!pblocktree->WriteAddressIndex(addressIndex)) {
4597+
if (!m_blockman.m_block_tree_db->WriteAddressIndex(addressIndex)) {
46004598
return error("RollforwardBlock(DASH): Failed to write address index");
46014599
}
46024600

4603-
if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
4601+
if (!m_blockman.m_block_tree_db->UpdateAddressUnspentIndex(addressUnspentIndex)) {
46044602
return error("RollforwardBlock(DASH): Failed to write address unspent index");
46054603
}
46064604
}
46074605

46084606
if (fSpentIndex) {
4609-
if (!pblocktree->UpdateSpentIndex(spentIndex))
4607+
if (!m_blockman.m_block_tree_db->UpdateSpentIndex(spentIndex))
46104608
return error("RollforwardBlock(DASH): Failed to write transaction index");
46114609
}
46124610

46134611
if (fTimestampIndex) {
4614-
if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
4612+
if (!m_blockman.m_block_tree_db->WriteTimestampIndex(CTimestampIndexKey(pindex->nTime, pindex->GetBlockHash())))
46154613
return error("RollforwardBlock(DASH): Failed to write timestamp index");
46164614
}
46174615

@@ -4726,7 +4724,7 @@ bool ChainstateManager::LoadBlockIndex()
47264724
// Load block index from databases
47274725
bool needs_init = fReindex;
47284726
if (!fReindex) {
4729-
bool ret = ActiveChainstate().LoadBlockIndexDB();
4727+
bool ret = m_blockman.LoadBlockIndexDB(ActiveChainstate().setBlockIndexCandidates);
47304728
if (!ret) return false;
47314729
needs_init = m_blockman.m_block_index.empty();
47324730
}
@@ -4742,15 +4740,15 @@ bool ChainstateManager::LoadBlockIndex()
47424740

47434741
// Use the provided setting for -addressindex in the new database
47444742
fAddressIndex = gArgs.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX);
4745-
pblocktree->WriteFlag("addressindex", fAddressIndex);
4743+
m_blockman.m_block_tree_db->WriteFlag("addressindex", fAddressIndex);
47464744

47474745
// Use the provided setting for -timestampindex in the new database
47484746
fTimestampIndex = gArgs.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX);
4749-
pblocktree->WriteFlag("timestampindex", fTimestampIndex);
4747+
m_blockman.m_block_tree_db->WriteFlag("timestampindex", fTimestampIndex);
47504748

47514749
// Use the provided setting for -spentindex in the new database
47524750
fSpentIndex = gArgs.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX);
4753-
pblocktree->WriteFlag("spentindex", fSpentIndex);
4751+
m_blockman.m_block_tree_db->WriteFlag("spentindex", fSpentIndex);
47544752
}
47554753
return true;
47564754
}

0 commit comments

Comments
 (0)