LCOV - code coverage report
Current view: top level - src/node - chainstate.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 70.8 % 130 92
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 5 5
Branches: 40.9 % 242 99

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

Generated by: LCOV version 2.0-1