@@ -108,8 +108,6 @@ using kernel::DumpMempool;
108
108
109
109
using node::CacheSizes;
110
110
using node::CalculateCacheSizes;
111
- using node::ChainstateLoadVerifyError;
112
- using node::ChainstateLoadingError;
113
111
using node::DEFAULT_PERSIST_MEMPOOL;
114
112
using node::DEFAULT_PRINTPRIORITY;
115
113
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
@@ -1452,8 +1450,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1452
1450
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
1453
1451
ChainstateManager& chainman = *node.chainman ;
1454
1452
1455
- bilingual_str strLoadError;
1456
-
1457
1453
node::ChainstateLoadOptions options;
1458
1454
options.mempool = Assert (node.mempool .get ());
1459
1455
options.reindex = node::fReindex ;
@@ -1470,87 +1466,38 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1470
1466
1471
1467
uiInterface.InitMessage (_ (" Loading block index…" ).translated );
1472
1468
const int64_t load_block_index_start_time = GetTimeMillis ();
1473
- std::optional<ChainstateLoadingError> maybe_load_error;
1474
- try {
1475
- maybe_load_error = LoadChainstate (chainman, cache_sizes, options);
1476
- } catch (const std::exception& e) {
1477
- LogPrintf (" %s\n " , e.what ());
1478
- maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
1479
- }
1480
- if (maybe_load_error.has_value ()) {
1481
- switch (maybe_load_error.value ()) {
1482
- case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
1483
- strLoadError = _ (" Error loading block database" );
1484
- break ;
1485
- case ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK:
1486
- // If the loaded chain has a wrong genesis, bail out immediately
1487
- // (we're likely using a testnet datadir, or the other way around).
1488
- return InitError (_ (" Incorrect or no genesis block found. Wrong datadir for network?" ));
1489
- case ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX:
1490
- strLoadError = _ (" You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain" );
1491
- break ;
1492
- case ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED:
1493
- strLoadError = _ (" Error initializing block database" );
1494
- break ;
1495
- case ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED:
1496
- return InitError (_ (" Unsupported chainstate database format found. "
1497
- " Please restart with -reindex-chainstate. This will "
1498
- " rebuild the chainstate database." ));
1499
- case ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED:
1500
- strLoadError = _ (" Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate." );
1501
- break ;
1502
- case ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED:
1503
- strLoadError = _ (" Error initializing block database" );
1504
- break ;
1505
- case ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED:
1506
- strLoadError = _ (" Error opening block database" );
1507
- break ;
1508
- case ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED:
1509
- strLoadError = strprintf (_ (" Witness data for blocks after height %d requires validation. Please restart with -reindex." ),
1510
- chainman.GetConsensus ().SegwitHeight );
1511
- break ;
1512
- case ChainstateLoadingError::SHUTDOWN_PROBED:
1513
- break ;
1514
- }
1515
- } else {
1516
- std::optional<ChainstateLoadVerifyError> maybe_verify_error;
1469
+ auto catch_exceptions = [](auto && f) {
1517
1470
try {
1518
- uiInterface.InitMessage (_ (" Verifying blocks…" ).translated );
1519
- if (chainman.m_blockman .m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
1520
- LogPrintfCategory (BCLog::PRUNE, " pruned datadir may not have more than %d blocks; only checking available blocks\n " ,
1521
- MIN_BLOCKS_TO_KEEP);
1522
- }
1523
- maybe_verify_error = VerifyLoadedChainstate (chainman, options);
1471
+ return f ();
1524
1472
} catch (const std::exception& e) {
1525
1473
LogPrintf (" %s\n " , e.what ());
1526
- maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE ;
1474
+ return std::make_tuple (node::ChainstateLoadStatus::FAILURE, _ ( " Error opening block database " )) ;
1527
1475
}
1528
- if (maybe_verify_error.has_value ()) {
1529
- switch (maybe_verify_error.value ()) {
1530
- case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE:
1531
- strLoadError = _ (" The block database contains a block which appears to be from the future. "
1532
- " This may be due to your computer's date and time being set incorrectly. "
1533
- " Only rebuild the block database if you are sure that your computer's date and time are correct" );
1534
- break ;
1535
- case ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB:
1536
- strLoadError = _ (" Corrupted block database detected" );
1537
- break ;
1538
- case ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE:
1539
- strLoadError = _ (" Error opening block database" );
1540
- break ;
1541
- }
1542
- } else {
1476
+ };
1477
+ auto [status, error] = catch_exceptions ([&]{ return LoadChainstate (chainman, cache_sizes, options); });
1478
+ if (status == node::ChainstateLoadStatus::SUCCESS) {
1479
+ uiInterface.InitMessage (_ (" Verifying blocks…" ).translated );
1480
+ if (chainman.m_blockman .m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
1481
+ LogPrintfCategory (BCLog::PRUNE, " pruned datadir may not have more than %d blocks; only checking available blocks\n " ,
1482
+ MIN_BLOCKS_TO_KEEP);
1483
+ }
1484
+ std::tie (status, error) = catch_exceptions ([&]{ return VerifyLoadedChainstate (chainman, options);});
1485
+ if (status == node::ChainstateLoadStatus::SUCCESS) {
1543
1486
fLoaded = true ;
1544
1487
LogPrintf (" block index %15dms\n " , GetTimeMillis () - load_block_index_start_time);
1545
1488
}
1546
1489
}
1547
1490
1491
+ if (status == node::ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB) {
1492
+ return InitError (error);
1493
+ }
1494
+
1548
1495
if (!fLoaded && !ShutdownRequested ()) {
1549
1496
// first suggest a reindex
1550
1497
if (!options.reindex ) {
1551
1498
bool fRet = uiInterface.ThreadSafeQuestion (
1552
- strLoadError + Untranslated (" .\n\n " ) + _ (" Do you want to rebuild the block database now?" ),
1553
- strLoadError .original + " .\n Please restart with -reindex or -reindex-chainstate to recover." ,
1499
+ error + Untranslated (" .\n\n " ) + _ (" Do you want to rebuild the block database now?" ),
1500
+ error .original + " .\n Please restart with -reindex or -reindex-chainstate to recover." ,
1554
1501
" " , CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1555
1502
if (fRet ) {
1556
1503
fReindex = true ;
@@ -1560,7 +1507,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1560
1507
return false ;
1561
1508
}
1562
1509
} else {
1563
- return InitError (strLoadError );
1510
+ return InitError (error );
1564
1511
}
1565
1512
}
1566
1513
}
0 commit comments