@@ -1175,69 +1175,66 @@ class ImportingNow
11751175
11761176void ImportBlocks (ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
11771177{
1178- ScheduleBatchPriority ();
1179-
1180- {
1181- ImportingNow imp{chainman.m_blockman .m_importing };
1182-
1183- // -reindex
1184- if (fReindex ) {
1185- int nFile = 0 ;
1186- // Map of disk positions for blocks with unknown parent (only used for reindex);
1187- // parent hash -> child disk position, multiple children can have the same parent.
1188- std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
1189- while (true ) {
1190- FlatFilePos pos (nFile, 0 );
1191- if (!fs::exists (chainman.m_blockman .GetBlockPosFilename (pos))) {
1192- break ; // No block files left to reindex
1193- }
1194- AutoFile file{chainman.m_blockman .OpenBlockFile (pos, true )};
1195- if (file.IsNull ()) {
1196- break ; // This error is logged in OpenBlockFile
1197- }
1198- LogPrintf (" Reindexing block file blk%05u.dat...\n " , (unsigned int )nFile);
1199- chainman.LoadExternalBlockFile (file, &pos, &blocks_with_unknown_parent);
1200- if (chainman.m_interrupt ) {
1201- LogPrintf (" Interrupt requested. Exit %s\n " , __func__);
1202- return ;
1203- }
1204- nFile++;
1178+ ImportingNow imp{chainman.m_blockman .m_importing };
1179+
1180+ // -reindex
1181+ if (fReindex ) {
1182+ int nFile = 0 ;
1183+ // Map of disk positions for blocks with unknown parent (only used for reindex);
1184+ // parent hash -> child disk position, multiple children can have the same parent.
1185+ std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
1186+ while (true ) {
1187+ FlatFilePos pos (nFile, 0 );
1188+ if (!fs::exists (chainman.m_blockman .GetBlockPosFilename (pos))) {
1189+ break ; // No block files left to reindex
12051190 }
1206- WITH_LOCK (::cs_main, chainman.m_blockman .m_block_tree_db ->WriteReindexing (false ));
1207- fReindex = false ;
1208- LogPrintf (" Reindexing finished\n " );
1209- // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
1210- chainman.ActiveChainstate ().LoadGenesisBlock ();
1191+ AutoFile file{chainman.m_blockman .OpenBlockFile (pos, true )};
1192+ if (file.IsNull ()) {
1193+ break ; // This error is logged in OpenBlockFile
1194+ }
1195+ LogPrintf (" Reindexing block file blk%05u.dat...\n " , (unsigned int )nFile);
1196+ chainman.LoadExternalBlockFile (file, &pos, &blocks_with_unknown_parent);
1197+ if (chainman.m_interrupt ) {
1198+ LogPrintf (" Interrupt requested. Exit %s\n " , __func__);
1199+ return ;
1200+ }
1201+ nFile++;
12111202 }
1212-
1213- // -loadblock=
1214- for (const fs::path& path : vImportFiles) {
1215- AutoFile file{fsbridge::fopen (path, " rb" )};
1216- if (!file.IsNull ()) {
1217- LogPrintf (" Importing blocks file %s...\n " , fs::PathToString (path));
1218- chainman.LoadExternalBlockFile (file);
1219- if (chainman.m_interrupt ) {
1220- LogPrintf (" Interrupt requested. Exit %s\n " , __func__);
1221- return ;
1222- }
1223- } else {
1224- LogPrintf (" Warning: Could not open blocks file %s\n " , fs::PathToString (path));
1203+ WITH_LOCK (::cs_main, chainman.m_blockman .m_block_tree_db ->WriteReindexing (false ));
1204+ fReindex = false ;
1205+ LogPrintf (" Reindexing finished\n " );
1206+ // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
1207+ chainman.ActiveChainstate ().LoadGenesisBlock ();
1208+ }
1209+
1210+ // -loadblock=
1211+ for (const fs::path& path : vImportFiles) {
1212+ AutoFile file{fsbridge::fopen (path, " rb" )};
1213+ if (!file.IsNull ()) {
1214+ LogPrintf (" Importing blocks file %s...\n " , fs::PathToString (path));
1215+ chainman.LoadExternalBlockFile (file);
1216+ if (chainman.m_interrupt ) {
1217+ LogPrintf (" Interrupt requested. Exit %s\n " , __func__);
1218+ return ;
12251219 }
1220+ } else {
1221+ LogPrintf (" Warning: Could not open blocks file %s\n " , fs::PathToString (path));
12261222 }
1223+ }
12271224
1228- // scan for better chains in the block chain database, that are not yet connected in the active best chain
1225+ // scan for better chains in the block chain database, that are not yet connected in the active best chain
12291226
1230- // We can't hold cs_main during ActivateBestChain even though we're accessing
1231- // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
1232- // the relevant pointers before the ABC call.
1233- for (Chainstate* chainstate : WITH_LOCK (::cs_main, return chainman.GetAll ())) {
1234- BlockValidationState state;
1235- if (!chainstate->ActivateBestChain (state, nullptr )) {
1236- chainman.GetNotifications ().fatalError (strprintf (_ (" Failed to connect best block (%s)." ), state.ToString ()));
1237- return ;
1238- }
1227+ // We can't hold cs_main during ActivateBestChain even though we're accessing
1228+ // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
1229+ // the relevant pointers before the ABC call.
1230+ for (Chainstate* chainstate : WITH_LOCK (::cs_main, return chainman.GetAll ())) {
1231+ BlockValidationState state;
1232+ if (!chainstate->ActivateBestChain (state, nullptr )) {
1233+ chainman.GetNotifications ().fatalError (strprintf (_ (" Failed to connect best block (%s)." ), state.ToString ()));
1234+ return ;
12391235 }
1240- } // End scope of ImportingNow
1236+ }
1237+ // End scope of ImportingNow
12411238}
12421239
12431240std::ostream& operator <<(std::ostream& os, const BlockfileType& type) {
0 commit comments