LCOV - code coverage report
Current view: top level - src/node - chainstate.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.7 % 140 127
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 5 5
Branches: 60.0 % 260 156

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <node/chainstate.h>
       6                 :             : 
       7                 :             : #include <arith_uint256.h>
       8                 :             : #include <chain.h>
       9                 :             : #include <coins.h>
      10                 :             : #include <consensus/params.h>
      11                 :             : #include <logging.h>
      12                 :             : #include <node/blockstorage.h>
      13                 :             : #include <node/caches.h>
      14                 :             : #include <sync.h>
      15                 :             : #include <threadsafety.h>
      16                 :             : #include <tinyformat.h>
      17                 :             : #include <txdb.h>
      18                 :             : #include <uint256.h>
      19                 :             : #include <util/fs.h>
      20                 :             : #include <util/signalinterrupt.h>
      21                 :             : #include <util/time.h>
      22                 :             : #include <util/translation.h>
      23                 :             : #include <validation.h>
      24                 :             : 
      25                 :             : #include <algorithm>
      26                 :             : #include <atomic>
      27                 :             : #include <cassert>
      28                 :             : #include <limits>
      29                 :             : #include <memory>
      30                 :             : #include <vector>
      31                 :             : 
      32                 :             : namespace node {
      33                 :             : // Complete initialization of chainstates after the initial call has been made
      34                 :             : // to ChainstateManager::InitializeChainstate().
      35                 :        1082 : static ChainstateLoadResult CompleteChainstateInitialization(
      36                 :             :     ChainstateManager& chainman,
      37                 :             :     const CacheSizes& cache_sizes,
      38                 :             :     const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
      39                 :             : {
      40                 :        1082 :     auto& pblocktree{chainman.m_blockman.m_block_tree_db};
      41                 :             :     // new BlockTreeDB tries to delete the existing file, which
      42                 :             :     // fails if it's still open from the previous loop. Close it first:
      43         [ +  + ]:        1082 :     pblocktree.reset();
      44                 :        1082 :     try {
      45                 :        2163 :         pblocktree = std::make_unique<BlockTreeDB>(DBParams{
      46   [ +  -  +  -  :        3247 :             .path = chainman.m_options.datadir / "blocks" / "index",
                   +  - ]
      47                 :        1082 :             .cache_bytes = static_cast<size_t>(cache_sizes.block_tree_db),
      48                 :        1082 :             .memory_only = options.block_tree_db_in_memory,
      49         [ +  + ]:        1082 :             .wipe_data = options.wipe_block_tree_db,
      50                 :        1081 :             .options = chainman.m_options.block_tree_db});
      51         [ -  + ]:           1 :     } catch (dbwrapper_error& err) {
      52         [ +  - ]:           1 :         LogError("%s\n", err.what());
      53         [ +  - ]:           2 :         return {ChainstateLoadStatus::FAILURE, _("Error opening block database")};
      54                 :           1 :     }
      55                 :             : 
      56         [ +  + ]:        1081 :     if (options.wipe_block_tree_db) {
      57                 :          17 :         pblocktree->WriteReindexing(true);
      58         [ +  + ]:          17 :         chainman.m_blockman.m_blockfiles_indexed = false;
      59                 :             :         //If we're reindexing in prune mode, wipe away unusable block files and all undo data files
      60         [ +  + ]:          17 :         if (options.prune) {
      61                 :           3 :             chainman.m_blockman.CleanupBlockRevFiles();
      62                 :             :         }
      63                 :             :     }
      64                 :             : 
      65   [ +  +  +  - ]:        1088 :     if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
      66                 :             : 
      67                 :             :     // LoadBlockIndex will load m_have_pruned if we've ever removed a
      68                 :             :     // block file from disk.
      69                 :             :     // Note that it also sets m_blockfiles_indexed based on the disk flag!
      70         [ +  + ]:        1074 :     if (!chainman.LoadBlockIndex()) {
      71   [ +  +  +  - ]:           6 :         if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
      72                 :           4 :         return {ChainstateLoadStatus::FAILURE, _("Error loading block database")};
      73                 :             :     }
      74                 :             : 
      75   [ +  +  -  + ]:        1688 :     if (!chainman.BlockIndex().empty() &&
      76                 :         618 :             !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
      77                 :             :         // If the loaded chain has a wrong genesis, bail out immediately
      78                 :             :         // (we're likely using a testnet datadir, or the other way around).
      79                 :           0 :         return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Incorrect or no genesis block found. Wrong datadir for network?")};
      80                 :             :     }
      81                 :             : 
      82                 :             :     // Check for changed -prune state.  What we are concerned about is a user who has pruned blocks
      83                 :             :     // in the past, but is now trying to run unpruned.
      84   [ +  +  -  + ]:        1070 :     if (chainman.m_blockman.m_have_pruned && !options.prune) {
      85                 :           0 :         return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to go back to unpruned mode.  This will redownload the entire blockchain")};
      86                 :             :     }
      87                 :             : 
      88                 :             :     // At this point blocktree args are consistent with what's on disk.
      89                 :             :     // If we're not mid-reindex (based on disk + args), add a genesis block on disk
      90                 :             :     // (otherwise we use the one already on disk).
      91                 :             :     // This is called again in ImportBlocks after the reindex completes.
      92   [ +  +  +  - ]:        1070 :     if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
      93                 :           0 :         return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
      94                 :             :     }
      95                 :             : 
      96                 :        2146 :     auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
      97   [ +  +  +  + ]:        1076 :         return options.wipe_chainstate_db || chainstate->CoinsTip().GetBestBlock().IsNull();
      98                 :        1070 :     };
      99                 :             : 
     100         [ -  + ]:        1070 :     assert(chainman.m_total_coinstip_cache > 0);
     101         [ -  + ]:        1070 :     assert(chainman.m_total_coinsdb_cache > 0);
     102                 :             : 
     103                 :             :     // If running with multiple chainstates, limit the cache sizes with a
     104                 :             :     // discount factor. If discounted the actual cache size will be
     105                 :             :     // recalculated by `chainman.MaybeRebalanceCaches()`. The discount factor
     106                 :             :     // is conservatively chosen such that the sum of the caches does not exceed
     107                 :             :     // the allowable amount during this temporary initialization state.
     108         [ +  + ]:        2131 :     double init_cache_fraction = chainman.GetAll().size() > 1 ? 0.2 : 1.0;
     109                 :             : 
     110                 :             :     // At this point we're either in reindex or we've loaded a useful
     111                 :             :     // block tree into BlockIndex()!
     112                 :             : 
     113         [ +  + ]:        2146 :     for (Chainstate* chainstate : chainman.GetAll()) {
     114   [ +  -  +  - ]:        1079 :         LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
     115                 :             : 
     116                 :        1079 :         try {
     117         [ +  - ]:        1079 :             chainstate->InitCoinsDB(
     118                 :        1079 :                 /*cache_size_bytes=*/chainman.m_total_coinsdb_cache * init_cache_fraction,
     119                 :        1079 :                 /*in_memory=*/options.coins_db_in_memory,
     120         [ +  + ]:        1079 :                 /*should_wipe=*/options.wipe_chainstate_db);
     121         [ -  + ]:           2 :         } catch (dbwrapper_error& err) {
     122         [ +  - ]:           2 :             LogError("%s\n", err.what());
     123         [ +  - ]:           4 :             return {ChainstateLoadStatus::FAILURE, _("Error opening coins database")};
     124         [ +  - ]:           2 :         }
     125                 :             : 
     126         [ +  + ]:        1077 :         if (options.coins_error_cb) {
     127   [ +  -  +  - ]:        1812 :             chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
     128                 :             :         }
     129                 :             : 
     130                 :             :         // Refuse to load unsupported database format.
     131                 :             :         // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
     132   [ +  -  +  -  :        1077 :         if (chainstate->CoinsDB().NeedsUpgrade()) {
                   +  + ]
     133         [ +  - ]:           2 :             return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Unsupported chainstate database format found. "
     134                 :             :                                                                      "Please restart with -reindex-chainstate. This will "
     135                 :           1 :                                                                      "rebuild the chainstate database.")};
     136                 :             :         }
     137                 :             : 
     138                 :             :         // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
     139   [ +  -  -  + ]:        1076 :         if (!chainstate->ReplayBlocks()) {
     140         [ #  # ]:           0 :             return {ChainstateLoadStatus::FAILURE, _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.")};
     141                 :             :         }
     142                 :             : 
     143                 :             :         // The on-disk coinsdb is now in a good state, create the cache
     144         [ +  - ]:        1076 :         chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
     145         [ +  - ]:        1076 :         assert(chainstate->CanFlushToDisk());
     146                 :             : 
     147   [ +  -  +  + ]:        1076 :         if (!is_coinsview_empty(chainstate)) {
     148                 :             :             // LoadChainTip initializes the chain based on CoinsTip()'s best block
     149   [ +  -  -  + ]:         618 :             if (!chainstate->LoadChainTip()) {
     150         [ #  # ]:           0 :                 return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
     151                 :             :             }
     152   [ +  -  -  + ]:         618 :             assert(chainstate->m_chain.Tip() != nullptr);
     153                 :             :         }
     154                 :           3 :     }
     155                 :             : 
     156         [ +  + ]:        1067 :     if (!options.wipe_block_tree_db) {
     157                 :        1050 :         auto chainstates{chainman.GetAll()};
     158   [ +  -  +  + ]:        1050 :         if (std::any_of(chainstates.begin(), chainstates.end(),
     159                 :        1059 :                         [](const Chainstate* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
     160   [ +  -  +  - ]:           3 :             return {ChainstateLoadStatus::FAILURE, strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
     161         [ +  - ]:           2 :                                                              chainman.GetConsensus().SegwitHeight)};
     162                 :        1049 :         };
     163                 :        1050 :     }
     164                 :             : 
     165                 :             :     // Now that chainstates are loaded and we're able to flush to
     166                 :             :     // disk, rebalance the coins caches to desired levels based
     167                 :             :     // on the condition of each chainstate.
     168                 :        1066 :     chainman.MaybeRebalanceCaches();
     169                 :             : 
     170         [ +  - ]:        2132 :     return {ChainstateLoadStatus::SUCCESS, {}};
     171   [ +  -  +  -  :        1075 : }
                   +  - ]
     172                 :             : 
     173                 :        1079 : ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
     174                 :             :                                     const ChainstateLoadOptions& options)
     175                 :             : {
     176         [ +  + ]:        1079 :     if (!chainman.AssumedValidBlock().IsNull()) {
     177         [ +  - ]:         182 :         LogPrintf("Assuming ancestors of block %s have valid signatures.\n", chainman.AssumedValidBlock().GetHex());
     178                 :             :     } else {
     179                 :         988 :         LogPrintf("Validating signatures for all blocks.\n");
     180                 :             :     }
     181         [ +  - ]:        1079 :     LogPrintf("Setting nMinimumChainWork=%s\n", chainman.MinimumChainWork().GetHex());
     182         [ +  + ]:        1079 :     if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
     183         [ +  - ]:           6 :         LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex());
     184                 :             :     }
     185         [ +  + ]:        1079 :     if (chainman.m_blockman.GetPruneTarget() == BlockManager::PRUNE_TARGET_MANUAL) {
     186                 :          33 :         LogPrintf("Block pruning enabled.  Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
     187         [ +  + ]:        1046 :     } else if (chainman.m_blockman.GetPruneTarget()) {
     188                 :          20 :         LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
     189                 :             :     }
     190                 :             : 
     191                 :        1079 :     LOCK(cs_main);
     192                 :             : 
     193                 :        1079 :     chainman.m_total_coinstip_cache = cache_sizes.coins;
     194                 :        1079 :     chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
     195                 :             : 
     196                 :             :     // Load the fully validated chainstate.
     197         [ +  - ]:        1079 :     chainman.InitializeChainstate(options.mempool);
     198                 :             : 
     199                 :             :     // Load a chain created from a UTXO snapshot, if any exist.
     200         [ +  - ]:        1079 :     bool has_snapshot = chainman.DetectSnapshotChainstate();
     201                 :             : 
     202   [ +  +  +  + ]:        1079 :     if (has_snapshot && options.wipe_chainstate_db) {
     203         [ +  - ]:           2 :         LogPrintf("[snapshot] deleting snapshot chainstate due to reindexing\n");
     204   [ +  -  -  + ]:           2 :         if (!chainman.DeleteSnapshotChainstate()) {
     205   [ #  #  #  # ]:           0 :             return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
     206                 :             :         }
     207                 :             :     }
     208                 :             : 
     209   [ +  -  +  + ]:        1079 :     auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
     210         [ +  + ]:        1079 :     if (init_status != ChainstateLoadStatus::SUCCESS) {
     211         [ +  - ]:          16 :         return {init_status, init_error};
     212                 :             :     }
     213                 :             : 
     214                 :             :     // If a snapshot chainstate was fully validated by a background chainstate during
     215                 :             :     // the last run, detect it here and clean up the now-unneeded background
     216                 :             :     // chainstate.
     217                 :             :     //
     218                 :             :     // Why is this cleanup done here (on subsequent restart) and not just when the
     219                 :             :     // snapshot is actually validated? Because this entails unusual
     220                 :             :     // filesystem operations to move leveldb data directories around, and that seems
     221                 :             :     // too risky to do in the middle of normal runtime.
     222         [ +  - ]:        1063 :     auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
     223                 :             : 
     224         [ +  + ]:        1063 :     if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
     225                 :             :         // do nothing; expected case
     226         [ +  - ]:           3 :     } else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
     227         [ +  - ]:           3 :         LogPrintf("[snapshot] cleaning up unneeded background chainstate, then reinitializing\n");
     228   [ +  -  -  + ]:           3 :         if (!chainman.ValidatedSnapshotCleanup()) {
     229   [ #  #  #  # ]:           0 :             return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Background chainstate cleanup failed unexpectedly.")};
     230                 :             :         }
     231                 :             : 
     232                 :             :         // Because ValidatedSnapshotCleanup() has torn down chainstates with
     233                 :             :         // ChainstateManager::ResetChainstates(), reinitialize them here without
     234                 :             :         // duplicating the blockindex work above.
     235   [ +  -  -  + ]:           3 :         assert(chainman.GetAll().empty());
     236   [ +  -  -  + ]:           3 :         assert(!chainman.IsSnapshotActive());
     237         [ -  + ]:           3 :         assert(!chainman.IsSnapshotValidated());
     238                 :             : 
     239         [ +  - ]:           3 :         chainman.InitializeChainstate(options.mempool);
     240                 :             : 
     241                 :             :         // A reload of the block index is required to recompute setBlockIndexCandidates
     242                 :             :         // for the fully validated chainstate.
     243   [ +  -  +  - ]:           3 :         chainman.ActiveChainstate().ClearBlockIndexCandidates();
     244                 :             : 
     245   [ +  -  -  + ]:           3 :         auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
     246         [ -  + ]:           3 :         if (init_status != ChainstateLoadStatus::SUCCESS) {
     247         [ #  # ]:           0 :             return {init_status, init_error};
     248                 :             :         }
     249                 :           3 :     } else {
     250         [ #  # ]:           0 :         return {ChainstateLoadStatus::FAILURE_FATAL, _(
     251                 :             :            "UTXO snapshot failed to validate. "
     252                 :           0 :            "Restart to resume normal initial block download, or try loading a different snapshot.")};
     253                 :             :     }
     254                 :             : 
     255         [ +  - ]:        2126 :     return {ChainstateLoadStatus::SUCCESS, {}};
     256         [ +  - ]:        3221 : }
     257                 :             : 
     258                 :        1063 : ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options)
     259                 :             : {
     260                 :        2132 :     auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
     261   [ +  +  +  + ]:        1069 :         return options.wipe_chainstate_db || chainstate->CoinsTip().GetBestBlock().IsNull();
     262                 :        1063 :     };
     263                 :             : 
     264                 :        1063 :     LOCK(cs_main);
     265                 :             : 
     266   [ +  -  +  + ]:        2126 :     for (Chainstate* chainstate : chainman.GetAll()) {
     267   [ +  -  +  + ]:        1069 :         if (!is_coinsview_empty(chainstate)) {
     268         [ +  - ]:         611 :             const CBlockIndex* tip = chainstate->m_chain.Tip();
     269   [ +  -  +  -  :         611 :             if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
                   +  + ]
     270         [ +  - ]:           4 :                 return {ChainstateLoadStatus::FAILURE, _("The block database contains a block which appears to be from the future. "
     271                 :             :                                                          "This may be due to your computer's date and time being set incorrectly. "
     272                 :           2 :                                                          "Only rebuild the block database if you are sure that your computer's date and time are correct")};
     273                 :             :             }
     274                 :             : 
     275   [ +  -  +  - ]:        1218 :             VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
     276                 :         609 :                 *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
     277                 :         609 :                 options.check_level,
     278         [ +  - ]:         609 :                 options.check_blocks);
     279   [ +  +  -  + ]:         609 :             switch (result) {
     280                 :             :             case VerifyDBResult::SUCCESS:
     281                 :             :             case VerifyDBResult::SKIPPED_MISSING_BLOCKS:
     282                 :             :                 break;
     283                 :           2 :             case VerifyDBResult::INTERRUPTED:
     284         [ +  - ]:           6 :                 return {ChainstateLoadStatus::INTERRUPTED, _("Block verification was interrupted")};
     285                 :           2 :             case VerifyDBResult::CORRUPTED_BLOCK_DB:
     286         [ +  - ]:           6 :                 return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
     287                 :           0 :             case VerifyDBResult::SKIPPED_L3_CHECKS:
     288         [ #  # ]:           0 :                 if (options.require_full_verification) {
     289         [ #  # ]:           0 :                     return {ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE, _("Insufficient dbcache for block verification")};
     290                 :             :                 }
     291                 :             :                 break;
     292                 :             :             } // no default case, so the compiler can warn about missing cases
     293                 :             :         }
     294                 :           6 :     }
     295                 :             : 
     296         [ +  - ]:        2114 :     return {ChainstateLoadStatus::SUCCESS, {}};
     297         [ +  - ]:        2120 : }
     298                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1