Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1251m_blockfiles_indexed = false;1252// If we're reindexing in prune mode, wipe away unusable block files and all undo data files1253if (m_prune_mode) {1254CleanupBlockRevFiles();1255}1256}1257}1259class ImportingNow1260{1261std::atomic<bool>& m_importing;1263public:1264ImportingNow(std::atomic<bool>& importing) : m_importing{importing}1265{1266assert(m_importing == false);1267m_importing = true;1268}1269~ImportingNow()1270{1271assert(m_importing == true);1272m_importing = false;1273}1274};1276void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths)1277{1278ImportingNow imp{chainman.m_blockman.m_importing};1280// -reindex1281if (!chainman.m_blockman.m_blockfiles_indexed) {1282int total_files{0};1283while (fs::exists(chainman.m_blockman.GetBlockPosFilename(FlatFilePos(total_files, 0)))) {1284total_files++;1285}1287// Map of disk positions for blocks with unknown parent (only used for reindex);1288// parent hash -> child disk position, multiple children can have the same parent.1289std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;1291for (int nFile{0}; nFile < total_files; ++nFile) {1292FlatFilePos pos(nFile, 0);1293AutoFile file{chainman.m_blockman.OpenBlockFile(pos, /*fReadOnly=*/true)};1294if (file.IsNull()) {1295break; // This error is logged in OpenBlockFile1296}1297LogInfo("Reindexing block file blk%05u.dat (%d%% complete)...", (unsigned int)nFile, nFile * 100 / total_files);1298chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);1299if (chainman.m_interrupt) {1300LogInfo("Interrupt requested. Exit reindexing.");1301return;1302}1303}1304WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));1305chainman.m_blockman.m_blockfiles_indexed = true;1306LogInfo("Reindexing finished");1307// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):1308(void)chainman.LoadGenesisBlock();1309}1311// -loadblock=1312for (const fs::path& path : import_paths) {1313AutoFile file{fsbridge::fopen(path, "rb")};1314if (!file.IsNull()) {1315LogInfo("Importing blocks file %s...", fs::PathToString(path));1316chainman.LoadExternalBlockFile(file);1317if (chainman.m_interrupt) {1318LogInfo("Interrupt requested. Exit block importing.");1319return;1320}1321} else {1322LogWarning("Could not open blocks file %s", fs::PathToString(path));1323}1324}1326// scan for better chains in the block chain database, that are not yet connected in the active best chain1327if (auto result = chainman.ActivateBestChains(); !result) {1328chainman.GetNotifications().fatalError(util::ErrorString(result));1329}1330// End scope of ImportingNow1331}1333std::ostream& operator<<(std::ostream& os, const BlockfileType& type) {1334switch(type) {1335case BlockfileType::NORMAL: os << "normal"; break;1336case BlockfileType::ASSUMED: os << "assumed"; break;1337default: os.setstate(std::ios_base::failbit);1338}1339return os;1340}1342std::ostream& operator<<(std::ostream& os, const BlockfileCursor& cursor) {1343os << strprintf("BlockfileCursor(file_num=%d, undo_height=%d)", cursor.file_num, cursor.undo_height);1344return os;1345}1346} // namespace node