From d7de5b109f69293b375729363b4e5038f3fb6b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Tue, 10 Jun 2025 16:14:22 +0200 Subject: [PATCH] logs: show reindex progress in `ImportBlocks` ### Summary When triggering a reindex, users had no indication of how many files remained or how far along the process was. ### Fix This patch prefetches the target file block file count to be able to show progress information. Instead of just displaying which block file is being processed, it now indicates how many files remain. ### Reproducer + expected results Running ```bash cmake -B build && make -C build -DCMAKE_BUILD_TYPE=Release && ./build/bin/bitcoind -datadir=demo -reindex ``` Shows the block files one-by-one currently, there's no way to see how much work is left: ``` Reindexing block file blk00000.dat... Loaded 119920 blocks from external file in 1228ms Reindexing block file blk00001.dat... Loaded 10671 blocks from external file in 284ms Reindexing block file blk00002.dat... Loaded 5459 blocks from external file in 263ms Reindexing block file blk00003.dat... Loaded 5595 blocks from external file in 267ms ``` After the change: ``` Reindexing block file blk00000.dat (0% complete)... Loaded 119920 blocks from external file in 1255ms Reindexing block file blk00001.dat (1% complete)... Loaded 10671 blocks from external file in 303ms Reindexing block file blk00002.dat (2% complete)... Loaded 5459 blocks from external file in 278ms Reindexing block file blk00003.dat (3% complete)... Loaded 5595 blocks from external file in 285ms ``` --- src/node/blockstorage.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 3c01ff2e8d6..1163feb3e35 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -1220,26 +1220,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;