@@ -1474,11 +1474,11 @@ bool AppInitMain(InitInterfaces& interfaces)
14741474
14751475 uiInterface.InitMessage (_ (" Loading block index..." ));
14761476
1477- LOCK (cs_main);
1478-
14791477 do {
14801478 const int64_t load_block_index_start_time = GetTimeMillis ();
1479+ bool is_coinsview_empty;
14811480 try {
1481+ LOCK (cs_main);
14821482 UnloadBlockIndex ();
14831483 pcoinsTip.reset ();
14841484 pcoinsdbview.reset ();
@@ -1550,7 +1550,7 @@ bool AppInitMain(InitInterfaces& interfaces)
15501550 // The on-disk coinsdb is now in a good state, create the cache
15511551 pcoinsTip.reset (new CCoinsViewCache (pcoinscatcher.get ()));
15521552
1553- bool is_coinsview_empty = fReset || fReindexChainState || pcoinsTip->GetBestBlock ().IsNull ();
1553+ is_coinsview_empty = fReset || fReindexChainState || pcoinsTip->GetBestBlock ().IsNull ();
15541554 if (!is_coinsview_empty) {
15551555 // LoadChainTip sets chainActive based on pcoinsTip's best block
15561556 if (!LoadChainTip (chainparams)) {
@@ -1559,18 +1559,25 @@ bool AppInitMain(InitInterfaces& interfaces)
15591559 }
15601560 assert (chainActive.Tip () != nullptr );
15611561 }
1562+ } catch (const std::exception& e) {
1563+ LogPrintf (" %s\n " , e.what ());
1564+ strLoadError = _ (" Error opening block database" );
1565+ break ;
1566+ }
15621567
1563- if (!fReset ) {
1564- // Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
1565- // It both disconnects blocks based on chainActive, and drops block data in
1566- // mapBlockIndex based on lack of available witness data.
1567- uiInterface.InitMessage (_ (" Rewinding blocks..." ));
1568- if (!RewindBlockIndex (chainparams)) {
1569- strLoadError = _ (" Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain" );
1570- break ;
1571- }
1568+ if (!fReset ) {
1569+ // Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
1570+ // It both disconnects blocks based on chainActive, and drops block data in
1571+ // mapBlockIndex based on lack of available witness data.
1572+ uiInterface.InitMessage (_ (" Rewinding blocks..." ));
1573+ if (!RewindBlockIndex (chainparams)) {
1574+ strLoadError = _ (" Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain" );
1575+ break ;
15721576 }
1577+ }
15731578
1579+ try {
1580+ LOCK (cs_main);
15741581 if (!is_coinsview_empty) {
15751582 uiInterface.InitMessage (_ (" Verifying blocks..." ));
15761583 if (fHavePruned && gArgs .GetArg (" -checkblocks" , DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
0 commit comments