diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index b0d54add7c1..84b7cfec7c7 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -1247,26 +1247,27 @@ void ImportBlocks(ChainstateManager& chainman, std::span import_ // -reindex if (!chainman.m_blockman.m_blockfiles_indexed) { - int nFile = 0; + int total_files{0}; + while (fs::exists(chainman.m_blockman.GetBlockPosFilename(FlatFilePos(total_files, 0)))) { + total_files++; + } + // Map of disk positions for blocks with unknown parent (only used for reindex); // parent hash -> child disk position, multiple children can have the same parent. std::multimap blocks_with_unknown_parent; - while (true) { + + for (int nFile{0}; nFile < total_files; ++nFile) { FlatFilePos pos(nFile, 0); - if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) { - break; // No block files left to reindex - } AutoFile file{chainman.m_blockman.OpenBlockFile(pos, /*fReadOnly=*/true)}; if (file.IsNull()) { break; // This error is logged in OpenBlockFile } - LogInfo("Reindexing block file blk%05u.dat...", (unsigned int)nFile); + LogInfo("Reindexing block file blk%05u.dat (%d%% complete)...", (unsigned int)nFile, nFile * 100 / total_files); chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent); if (chainman.m_interrupt) { LogInfo("Interrupt requested. Exit reindexing."); return; } - nFile++; } WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false)); chainman.m_blockman.m_blockfiles_indexed = true;