@@ -1391,23 +1391,19 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
1391
1391
delete pblocktree;
1392
1392
1393
1393
pblocktree = new CBlockTreeDB (nBlockTreeDBCache, false , fReindex );
1394
- pcoinsdbview = new CCoinsViewDB (nCoinDBCache, false , fReindex || fReindexChainState );
1395
- pcoinscatcher = new CCoinsViewErrorCatcher (pcoinsdbview);
1396
1394
1397
1395
if (fReindex ) {
1398
1396
pblocktree->WriteReindexing (true );
1399
1397
// If we're reindexing in prune mode, wipe away unusable block files and all undo data files
1400
1398
if (fPruneMode )
1401
1399
CleanupBlockRevFiles ();
1402
- } else {
1403
- // If necessary, upgrade from older database format.
1404
- if (!pcoinsdbview->Upgrade ()) {
1405
- strLoadError = _ (" Error upgrading chainstate database" );
1406
- break ;
1407
- }
1408
1400
}
1401
+
1409
1402
if (fRequestShutdown ) break ;
1410
1403
1404
+ // LoadBlockIndex will load fTxIndex from the db, or set it if
1405
+ // we're reindexing. It will also load fHavePruned if we've
1406
+ // ever removed a block file from disk.
1411
1407
if (!LoadBlockIndex (chainparams)) {
1412
1408
strLoadError = _ (" Error loading block database" );
1413
1409
break ;
@@ -1418,12 +1414,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
1418
1414
if (!mapBlockIndex.empty () && mapBlockIndex.count (chainparams.GetConsensus ().hashGenesisBlock ) == 0 )
1419
1415
return InitError (_ (" Incorrect or no genesis block found. Wrong datadir for network?" ));
1420
1416
1421
- // Initialize the block index (no-op if non-empty database was already loaded)
1422
- if (!fReindex && !LoadGenesisBlock (chainparams)) {
1423
- strLoadError = _ (" Error initializing block database" );
1424
- break ;
1425
- }
1426
-
1427
1417
// Check for changed -txindex state
1428
1418
if (fTxIndex != GetBoolArg (" -txindex" , DEFAULT_TXINDEX)) {
1429
1419
strLoadError = _ (" You need to rebuild the database using -reindex-chainstate to change -txindex" );
@@ -1437,10 +1427,34 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
1437
1427
break ;
1438
1428
}
1439
1429
1430
+ // At this point blocktree args are consistent with what's on disk.
1431
+ // If we're not mid-reindex (based on disk + args), add a genesis block on disk.
1432
+ // This is called again in ThreadImport in the reindex completes.
1433
+ if (!fReindex && !LoadGenesisBlock (chainparams)) {
1434
+ strLoadError = _ (" Error initializing block database" );
1435
+ break ;
1436
+ }
1437
+
1438
+ // At this point we're either in reindex or we've loaded a useful
1439
+ // block tree into mapBlockIndex!
1440
+
1441
+ pcoinsdbview = new CCoinsViewDB (nCoinDBCache, false , fReindex || fReindexChainState );
1442
+ pcoinscatcher = new CCoinsViewErrorCatcher (pcoinsdbview);
1443
+
1444
+ // If necessary, upgrade from older database format.
1445
+ // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1446
+ if (!pcoinsdbview->Upgrade ()) {
1447
+ strLoadError = _ (" Error upgrading chainstate database" );
1448
+ break ;
1449
+ }
1450
+
1451
+ // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1440
1452
if (!ReplayBlocks (chainparams, pcoinsdbview)) {
1441
1453
strLoadError = _ (" Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate." );
1442
1454
break ;
1443
1455
}
1456
+
1457
+ // The on-disk coinsdb is now in a good state, create the cache
1444
1458
pcoinsTip = new CCoinsViewCache (pcoinscatcher);
1445
1459
1446
1460
if (!fReindex && !fReindexChainState ) {
@@ -1463,28 +1477,30 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
1463
1477
}
1464
1478
}
1465
1479
1466
- uiInterface.InitMessage (_ (" Verifying blocks..." ));
1467
- if (fHavePruned && GetArg (" -checkblocks" , DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1468
- LogPrintf (" Prune: pruned datadir may not have more than %d blocks; only checking available blocks" ,
1469
- MIN_BLOCKS_TO_KEEP);
1470
- }
1480
+ if (!fReindex && !fReindexChainState ) {
1481
+ uiInterface.InitMessage (_ (" Verifying blocks..." ));
1482
+ if (fHavePruned && GetArg (" -checkblocks" , DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1483
+ LogPrintf (" Prune: pruned datadir may not have more than %d blocks; only checking available blocks" ,
1484
+ MIN_BLOCKS_TO_KEEP);
1485
+ }
1471
1486
1472
- {
1473
- LOCK (cs_main);
1474
- CBlockIndex* tip = chainActive.Tip ();
1475
- RPCNotifyBlockChange (true , tip);
1476
- if (tip && tip->nTime > GetAdjustedTime () + 2 * 60 * 60 ) {
1477
- strLoadError = _ (" The block database contains a block which appears to be from the future. "
1478
- " This may be due to your computer's date and time being set incorrectly. "
1479
- " Only rebuild the block database if you are sure that your computer's date and time are correct" );
1480
- break ;
1487
+ {
1488
+ LOCK (cs_main);
1489
+ CBlockIndex* tip = chainActive.Tip ();
1490
+ RPCNotifyBlockChange (true , tip);
1491
+ if (tip && tip->nTime > GetAdjustedTime () + 2 * 60 * 60 ) {
1492
+ strLoadError = _ (" The block database contains a block which appears to be from the future. "
1493
+ " This may be due to your computer's date and time being set incorrectly. "
1494
+ " Only rebuild the block database if you are sure that your computer's date and time are correct" );
1495
+ break ;
1496
+ }
1481
1497
}
1482
- }
1483
1498
1484
- if (!CVerifyDB ().VerifyDB (chainparams, pcoinsdbview, GetArg (" -checklevel" , DEFAULT_CHECKLEVEL),
1485
- GetArg (" -checkblocks" , DEFAULT_CHECKBLOCKS))) {
1486
- strLoadError = _ (" Corrupted block database detected" );
1487
- break ;
1499
+ if (!CVerifyDB ().VerifyDB (chainparams, pcoinsdbview, GetArg (" -checklevel" , DEFAULT_CHECKLEVEL),
1500
+ GetArg (" -checkblocks" , DEFAULT_CHECKBLOCKS))) {
1501
+ strLoadError = _ (" Corrupted block database detected" );
1502
+ break ;
1503
+ }
1488
1504
}
1489
1505
} catch (const std::exception& e) {
1490
1506
LogPrintf (" %s\n " , e.what ());
0 commit comments