Merge bitcoin/bitcoin#33353: log: show reindex progress in ImportBlocks

d7de5b109f logs: show reindex progress in `ImportBlocks` (Lőrinc)

Pull request description:

  ### Summary

  When triggering a reindex, users have no indication of progress.

  ### Fix

  This patch precomputes the total number of block files so progress can be shown.
  Instead of only displaying which block file is being processed, it now shows the percent complete.

  ### Reproducer + expected results

  ```bash
  cmake -B build -DCMAKE_BUILD_TYPE=Release && make -C build -j && ./build/bin/bitcoind -datadir=demo -reindex
  ```

  Before, the block files were shown one-by-one, 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 we add a percentage:
  ```
  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
  ```

ACKs for top commit:
  enirox001:
    Concept ACK d7de5b1
  rkrux:
    lgtm ACK d7de5b109f
  danielabrozzoni:
    tACK d7de5b109f - code reviewed and tested on my archival node.
  maflcko:
    review ACK d7de5b109f 💇

Tree-SHA512: 359a539b781ad8b73e2a616c951567062a76be27cf90e5b88bb5309295af9cd7994e327f185bacc1482b43b892b38329593b4043a5e71d8800e3e4b7a3954310
This commit is contained in:
merge-script
2025-12-22 08:12:03 -08:00

View File

@@ -1247,26 +1247,27 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> 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<uint256, FlatFilePos> 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;