| 546 | } |
| 547 | |
| 548 | bool BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_blockhash) |
| 549 | { |
| 550 | AssertLockHeld(::cs_main); |
| 551 | if (!LoadBlockIndex(snapshot_blockhash)) { |
| 552 | return false; |
| 553 | } |
| 554 | int max_blockfile_num{0}; |
| 555 | |
| 556 | // Load block file info |
| 557 | m_block_tree_db->ReadLastBlockFile(max_blockfile_num); |
| 558 | m_blockfile_info.resize(max_blockfile_num + 1); |
| 559 | LogInfo("Loading block index db: last block file = %i", max_blockfile_num); |
| 560 | for (int nFile = 0; nFile <= max_blockfile_num; nFile++) { |
| 561 | m_block_tree_db->ReadBlockFileInfo(nFile, m_blockfile_info[nFile]); |
| 562 | } |
| 563 | LogInfo("Loading block index db: last block file info: %s", m_blockfile_info[max_blockfile_num].ToString()); |
| 564 | for (int nFile = max_blockfile_num + 1; true; nFile++) { |
| 565 | CBlockFileInfo info; |
| 566 | if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) { |
| 567 | m_blockfile_info.push_back(info); |
| 568 | } else { |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Check presence of blk files |
| 574 | LogInfo("Checking all blk files are present..."); |
| 575 | std::set<int> setBlkDataFiles; |
| 576 | for (const auto& [_, block_index] : m_block_index) { |
| 577 | if (block_index.nStatus & BLOCK_HAVE_DATA) { |
| 578 | setBlkDataFiles.insert(block_index.nFile); |
| 579 | } |
| 580 | } |
| 581 | for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) { |
| 582 | FlatFilePos pos(*it, 0); |
| 583 | if (OpenBlockFile(pos, /*fReadOnly=*/true).IsNull()) { |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | { |
| 589 | // Initialize the blockfile cursors. |
| 590 | for (size_t i = 0; i < m_blockfile_info.size(); ++i) { |
| 591 | const auto last_height_in_file = m_blockfile_info[i].nHeightLast; |
| 592 | m_blockfile_cursors[BlockfileTypeForHeight(last_height_in_file)] = {static_cast<int>(i), 0}; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // Check whether we have ever pruned block & undo files |
| 597 | m_block_tree_db->ReadFlag("prunedblockfiles", m_have_pruned); |
| 598 | if (m_have_pruned) { |
| 599 | LogInfo("Loading block index db: Block files have previously been pruned"); |
| 600 | } |
| 601 | |
| 602 | // Check whether we need to continue reindexing |
| 603 | bool fReindexing = false; |
| 604 | m_block_tree_db->ReadReindexing(fReindexing); |
| 605 | if (fReindexing) m_blockfiles_indexed = false; |
no test coverage detected