LCOV - code coverage report
Current view: top level - src/index - base.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 73.0 % 211 154
Test Date: 2024-08-28 04:44:32 Functions: 77.8 % 27 21
Branches: 45.1 % 244 110

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-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 <chainparams.h>
       6                 :             : #include <common/args.h>
       7                 :             : #include <index/base.h>
       8                 :             : #include <interfaces/chain.h>
       9                 :             : #include <kernel/chain.h>
      10                 :             : #include <logging.h>
      11                 :             : #include <node/abort.h>
      12                 :             : #include <node/blockstorage.h>
      13                 :             : #include <node/context.h>
      14                 :             : #include <node/database_args.h>
      15                 :             : #include <node/interface_ui.h>
      16                 :             : #include <tinyformat.h>
      17                 :             : #include <util/thread.h>
      18                 :             : #include <util/translation.h>
      19                 :             : #include <validation.h> // For g_chainman
      20                 :             : 
      21                 :             : #include <string>
      22                 :             : #include <utility>
      23                 :             : 
      24                 :             : constexpr uint8_t DB_BEST_BLOCK{'B'};
      25                 :             : 
      26                 :             : constexpr auto SYNC_LOG_INTERVAL{30s};
      27                 :             : constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s};
      28                 :             : 
      29                 :             : template <typename... Args>
      30                 :           0 : void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
      31                 :             : {
      32                 :           0 :     auto message = tfm::format(fmt, args...);
      33   [ #  #  #  #  :           0 :     node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
          #  #  #  #  #  
                #  #  # ]
      34                 :           0 : }
      35                 :             : 
      36                 :          10 : CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
      37                 :             : {
      38                 :          10 :     CBlockLocator locator;
      39         [ +  - ]:          10 :     bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator));
      40         [ -  + ]:          10 :     assert(found);
      41         [ -  + ]:          10 :     assert(!locator.IsNull());
      42                 :          10 :     return locator;
      43                 :           0 : }
      44                 :             : 
      45                 :           8 : BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
      46                 :           0 :     CDBWrapper{DBParams{
      47                 :             :         .path = path,
      48                 :             :         .cache_bytes = n_cache_size,
      49                 :             :         .memory_only = f_memory,
      50                 :             :         .wipe_data = f_wipe,
      51                 :             :         .obfuscate = f_obfuscate,
      52   [ +  -  +  - ]:           8 :         .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
      53                 :           8 : {}
      54                 :             : 
      55                 :           5 : bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
      56                 :             : {
      57                 :           5 :     bool success = Read(DB_BEST_BLOCK, locator);
      58         [ +  + ]:           5 :     if (!success) {
      59         [ -  + ]:           4 :         locator.SetNull();
      60                 :             :     }
      61                 :           5 :     return success;
      62                 :             : }
      63                 :             : 
      64                 :          10 : void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
      65                 :             : {
      66                 :          10 :     batch.Write(DB_BEST_BLOCK, locator);
      67                 :          10 : }
      68                 :             : 
      69                 :           8 : BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
      70         [ +  - ]:           8 :     : m_chain{std::move(chain)}, m_name{std::move(name)} {}
      71                 :             : 
      72                 :           8 : BaseIndex::~BaseIndex()
      73                 :             : {
      74                 :           8 :     Interrupt();
      75                 :           8 :     Stop();
      76                 :           8 : }
      77                 :             : 
      78                 :           5 : bool BaseIndex::Init()
      79                 :             : {
      80                 :           5 :     AssertLockNotHeld(cs_main);
      81                 :             : 
      82                 :             :     // May need reset if index is being restarted.
      83                 :           5 :     m_interrupt.reset();
      84                 :             : 
      85                 :             :     // m_chainstate member gives indexing code access to node internals. It is
      86                 :             :     // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
      87   [ +  -  +  -  :          15 :     m_chainstate = WITH_LOCK(::cs_main,
                   +  - ]
      88                 :             :         return &m_chain->context()->chainman->GetChainstateForIndexing());
      89                 :             :     // Register to validation interface before setting the 'm_synced' flag, so that
      90                 :             :     // callbacks are not missed once m_synced is true.
      91                 :           5 :     m_chain->context()->validation_signals->RegisterValidationInterface(this);
      92                 :             : 
      93                 :           5 :     CBlockLocator locator;
      94   [ +  -  +  -  :           5 :     if (!GetDB().ReadBestBlock(locator)) {
                   +  + ]
      95         [ -  + ]:           4 :         locator.SetNull();
      96                 :             :     }
      97                 :             : 
      98         [ +  - ]:           5 :     LOCK(cs_main);
      99                 :           5 :     CChain& index_chain = m_chainstate->m_chain;
     100                 :             : 
     101         [ +  + ]:           5 :     if (locator.IsNull()) {
     102         [ +  - ]:           4 :         SetBestBlockIndex(nullptr);
     103                 :             :     } else {
     104                 :             :         // Setting the best block to the locator's top block. If it is not part of the
     105                 :             :         // best chain, we will rewind to the fork point during index sync
     106   [ +  -  +  - ]:           1 :         const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
     107         [ -  + ]:           1 :         if (!locator_index) {
     108   [ #  #  #  #  :           0 :             return InitError(strprintf(Untranslated("%s: best block of the index not found. Please rebuild the index."), GetName()));
             #  #  #  # ]
     109                 :             :         }
     110         [ +  - ]:           1 :         SetBestBlockIndex(locator_index);
     111                 :             :     }
     112                 :             : 
     113                 :             :     // Child init
     114         [ +  + ]:           5 :     const CBlockIndex* start_block = m_best_block_index.load();
     115   [ +  +  +  -  :           5 :     if (!CustomInit(start_block ? std::make_optional(interfaces::BlockKey{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
                   +  - ]
     116                 :             :         return false;
     117                 :             :     }
     118                 :             : 
     119                 :             :     // Note: this will latch to true immediately if the user starts up with an empty
     120                 :             :     // datadir and an index enabled. If this is the case, indexation will happen solely
     121                 :             :     // via `BlockConnected` signals until, possibly, the next restart.
     122         [ +  - ]:          10 :     m_synced = start_block == index_chain.Tip();
     123                 :           5 :     m_init = true;
     124                 :           5 :     return true;
     125                 :           5 : }
     126                 :             : 
     127                 :         412 : static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     128                 :             : {
     129                 :         412 :     AssertLockHeld(cs_main);
     130                 :             : 
     131         [ +  + ]:         412 :     if (!pindex_prev) {
     132         [ +  - ]:           4 :         return chain.Genesis();
     133                 :             :     }
     134                 :             : 
     135                 :         408 :     const CBlockIndex* pindex = chain.Next(pindex_prev);
     136         [ +  + ]:         408 :     if (pindex) {
     137                 :             :         return pindex;
     138                 :             :     }
     139                 :             : 
     140                 :           8 :     return chain.Next(chain.FindFork(pindex_prev));
     141                 :             : }
     142                 :             : 
     143                 :           5 : void BaseIndex::Sync()
     144                 :             : {
     145         [ +  + ]:           5 :     const CBlockIndex* pindex = m_best_block_index.load();
     146         [ +  + ]:           5 :     if (!m_synced) {
     147                 :           4 :         std::chrono::steady_clock::time_point last_log_time{0s};
     148                 :           4 :         std::chrono::steady_clock::time_point last_locator_write_time{0s};
     149                 :         812 :         while (true) {
     150         [ -  + ]:         408 :             if (m_interrupt) {
     151                 :           0 :                 LogPrintf("%s: m_interrupt set; exiting ThreadSync\n", GetName());
     152                 :             : 
     153                 :           0 :                 SetBestBlockIndex(pindex);
     154                 :             :                 // No need to handle errors in Commit. If it fails, the error will be already be
     155                 :             :                 // logged. The best way to recover is to continue, as index cannot be corrupted by
     156                 :             :                 // a missed commit to disk for an advanced index state.
     157                 :           0 :                 Commit();
     158                 :           0 :                 return;
     159                 :             :             }
     160                 :             : 
     161   [ +  -  +  - ]:        1224 :             const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
     162                 :             :             // If pindex_next is null, it means pindex is the chain tip, so
     163                 :             :             // commit data indexed so far.
     164         [ +  + ]:         408 :             if (!pindex_next) {
     165                 :           4 :                 SetBestBlockIndex(pindex);
     166                 :             :                 // No need to handle errors in Commit. See rationale above.
     167                 :           4 :                 Commit();
     168                 :             : 
     169                 :             :                 // If pindex is still the chain tip after committing, exit the
     170                 :             :                 // sync loop. It is important for cs_main to be locked while
     171                 :             :                 // setting m_synced = true, otherwise a new block could be
     172                 :             :                 // attached while m_synced is still false, and it would not be
     173                 :             :                 // indexed.
     174                 :           4 :                 LOCK(::cs_main);
     175         [ +  - ]:           4 :                 pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
     176         [ +  - ]:           4 :                 if (!pindex_next) {
     177         [ +  - ]:           4 :                     m_synced = true;
     178         [ +  - ]:           4 :                     break;
     179                 :             :                 }
     180                 :           4 :             }
     181   [ -  +  -  - ]:         404 :             if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
     182                 :           0 :                 FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName());
     183                 :           0 :                 return;
     184                 :             :             }
     185                 :         404 :             pindex = pindex_next;
     186                 :             : 
     187                 :             : 
     188                 :         404 :             CBlock block;
     189         [ +  - ]:         404 :             interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
     190   [ +  -  -  + ]:         404 :             if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *pindex)) {
     191         [ #  # ]:           0 :                 FatalErrorf("%s: Failed to read block %s from disk",
     192         [ #  # ]:           0 :                            __func__, pindex->GetBlockHash().ToString());
     193                 :           0 :                 return;
     194                 :             :             } else {
     195                 :         404 :                 block_info.data = &block;
     196                 :             :             }
     197   [ +  -  -  + ]:         404 :             if (!CustomAppend(block_info)) {
     198         [ #  # ]:           0 :                 FatalErrorf("%s: Failed to write block %s to index database",
     199         [ #  # ]:           0 :                            __func__, pindex->GetBlockHash().ToString());
     200                 :           0 :                 return;
     201                 :             :             }
     202                 :             : 
     203                 :         404 :             auto current_time{std::chrono::steady_clock::now()};
     204         [ +  + ]:         404 :             if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
     205         [ +  - ]:           4 :                 LogPrintf("Syncing %s with block chain from height %d\n",
     206                 :             :                           GetName(), pindex->nHeight);
     207                 :           4 :                 last_log_time = current_time;
     208                 :             :             }
     209                 :             : 
     210         [ +  + ]:         404 :             if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
     211         [ +  - ]:           4 :                 SetBestBlockIndex(pindex);
     212                 :           4 :                 last_locator_write_time = current_time;
     213                 :             :                 // No need to handle errors in Commit. See rationale above.
     214         [ +  - ]:           4 :                 Commit();
     215                 :             :             }
     216                 :         404 :         }
     217                 :             :     }
     218                 :             : 
     219         [ +  - ]:           5 :     if (pindex) {
     220                 :           5 :         LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
     221                 :             :     } else {
     222                 :           0 :         LogPrintf("%s is enabled\n", GetName());
     223                 :             :     }
     224                 :             : }
     225                 :             : 
     226                 :          10 : bool BaseIndex::Commit()
     227                 :             : {
     228                 :             :     // Don't commit anything if we haven't indexed any block yet
     229                 :             :     // (this could happen if init is interrupted).
     230         [ +  - ]:          10 :     bool ok = m_best_block_index != nullptr;
     231         [ +  - ]:          10 :     if (ok) {
     232                 :          10 :         CDBBatch batch(GetDB());
     233         [ +  - ]:          10 :         ok = CustomCommit(batch);
     234         [ +  - ]:          10 :         if (ok) {
     235   [ +  -  +  -  :          10 :             GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
                   +  - ]
     236   [ +  -  +  - ]:          10 :             ok = GetDB().WriteBatch(batch);
     237                 :             :         }
     238                 :          10 :     }
     239         [ -  + ]:          10 :     if (!ok) {
     240                 :           0 :         LogError("%s: Failed to commit latest %s state\n", __func__, GetName());
     241                 :           0 :         return false;
     242                 :             :     }
     243                 :             :     return true;
     244                 :             : }
     245                 :             : 
     246                 :           2 : bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
     247                 :             : {
     248         [ -  + ]:           2 :     assert(current_tip == m_best_block_index);
     249         [ -  + ]:           2 :     assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
     250                 :             : 
     251         [ +  - ]:           2 :     if (!CustomRewind({current_tip->GetBlockHash(), current_tip->nHeight}, {new_tip->GetBlockHash(), new_tip->nHeight})) {
     252                 :             :         return false;
     253                 :             :     }
     254                 :             : 
     255                 :             :     // In the case of a reorg, ensure persisted block locator is not stale.
     256                 :             :     // Pruning has a minimum of 288 blocks-to-keep and getting the index
     257                 :             :     // out of sync may be possible but a users fault.
     258                 :             :     // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
     259                 :             :     // throw and lead to a graceful shutdown
     260                 :           2 :     SetBestBlockIndex(new_tip);
     261         [ -  + ]:           2 :     if (!Commit()) {
     262                 :             :         // If commit fails, revert the best block index to avoid corruption.
     263                 :           0 :         SetBestBlockIndex(current_tip);
     264                 :           0 :         return false;
     265                 :             :     }
     266                 :             : 
     267                 :             :     return true;
     268                 :             : }
     269                 :             : 
     270                 :          21 : void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
     271                 :             : {
     272                 :             :     // Ignore events from the assumed-valid chain; we will process its blocks
     273                 :             :     // (sequentially) after it is fully verified by the background chainstate. This
     274                 :             :     // is to avoid any out-of-order indexing.
     275                 :             :     //
     276                 :             :     // TODO at some point we could parameterize whether a particular index can be
     277                 :             :     // built out of order, but for now just do the conservative simple thing.
     278         [ +  - ]:          21 :     if (role == ChainstateRole::ASSUMEDVALID) {
     279                 :             :         return;
     280                 :             :     }
     281                 :             : 
     282                 :             :     // Ignore BlockConnected signals until we have fully indexed the chain.
     283         [ +  - ]:          21 :     if (!m_synced) {
     284                 :             :         return;
     285                 :             :     }
     286                 :             : 
     287         [ -  + ]:          21 :     const CBlockIndex* best_block_index = m_best_block_index.load();
     288         [ -  + ]:          21 :     if (!best_block_index) {
     289         [ #  # ]:           0 :         if (pindex->nHeight != 0) {
     290                 :           0 :             FatalErrorf("%s: First block connected is not the genesis block (height=%d)",
     291                 :           0 :                        __func__, pindex->nHeight);
     292                 :           0 :             return;
     293                 :             :         }
     294                 :             :     } else {
     295                 :             :         // Ensure block connects to an ancestor of the current best block. This should be the case
     296                 :             :         // most of the time, but may not be immediately after the sync thread catches up and sets
     297                 :             :         // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
     298                 :             :         // in the ValidationInterface queue backlog even after the sync thread has caught up to the
     299                 :             :         // new chain tip. In this unlikely event, log a warning and let the queue clear.
     300         [ -  + ]:          21 :         if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
     301   [ #  #  #  # ]:           0 :             LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of "
     302                 :             :                       "known best chain (tip=%s); not updating index\n",
     303                 :             :                       __func__, pindex->GetBlockHash().ToString(),
     304                 :             :                       best_block_index->GetBlockHash().ToString());
     305                 :           0 :             return;
     306                 :             :         }
     307   [ +  +  -  + ]:          21 :         if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
     308                 :           0 :             FatalErrorf("%s: Failed to rewind index %s to a previous chain tip",
     309                 :           0 :                        __func__, GetName());
     310                 :           0 :             return;
     311                 :             :         }
     312                 :             :     }
     313                 :          21 :     interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get());
     314         [ +  - ]:          21 :     if (CustomAppend(block_info)) {
     315                 :             :         // Setting the best block index is intentionally the last step of this
     316                 :             :         // function, so BlockUntilSyncedToCurrentChain callers waiting for the
     317                 :             :         // best block index to be updated can rely on the block being fully
     318                 :             :         // processed, and the index object being safe to delete.
     319                 :          21 :         SetBestBlockIndex(pindex);
     320                 :             :     } else {
     321         [ #  # ]:           0 :         FatalErrorf("%s: Failed to write block %s to index",
     322                 :           0 :                    __func__, pindex->GetBlockHash().ToString());
     323                 :           0 :         return;
     324                 :             :     }
     325                 :             : }
     326                 :             : 
     327                 :           0 : void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator)
     328                 :             : {
     329                 :             :     // Ignore events from the assumed-valid chain; we will process its blocks
     330                 :             :     // (sequentially) after it is fully verified by the background chainstate.
     331         [ #  # ]:           0 :     if (role == ChainstateRole::ASSUMEDVALID) {
     332                 :             :         return;
     333                 :             :     }
     334                 :             : 
     335         [ #  # ]:           0 :     if (!m_synced) {
     336                 :             :         return;
     337                 :             :     }
     338                 :             : 
     339                 :           0 :     const uint256& locator_tip_hash = locator.vHave.front();
     340                 :           0 :     const CBlockIndex* locator_tip_index;
     341                 :           0 :     {
     342                 :           0 :         LOCK(cs_main);
     343   [ #  #  #  # ]:           0 :         locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
     344                 :           0 :     }
     345                 :             : 
     346         [ #  # ]:           0 :     if (!locator_tip_index) {
     347         [ #  # ]:           0 :         FatalErrorf("%s: First block (hash=%s) in locator was not found",
     348                 :           0 :                    __func__, locator_tip_hash.ToString());
     349                 :           0 :         return;
     350                 :             :     }
     351                 :             : 
     352                 :             :     // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
     353                 :             :     // immediately after the sync thread catches up and sets m_synced. Consider the case where
     354                 :             :     // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
     355                 :             :     // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
     356                 :             :     // event, log a warning and let the queue clear.
     357                 :           0 :     const CBlockIndex* best_block_index = m_best_block_index.load();
     358         [ #  # ]:           0 :     if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
     359   [ #  #  #  # ]:           0 :         LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best "
     360                 :             :                   "chain (tip=%s); not writing index locator\n",
     361                 :             :                   __func__, locator_tip_hash.ToString(),
     362                 :             :                   best_block_index->GetBlockHash().ToString());
     363                 :           0 :         return;
     364                 :             :     }
     365                 :             : 
     366                 :             :     // No need to handle errors in Commit. If it fails, the error will be already be logged. The
     367                 :             :     // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
     368                 :             :     // for an advanced index state.
     369                 :           0 :     Commit();
     370                 :             : }
     371                 :             : 
     372                 :         147 : bool BaseIndex::BlockUntilSyncedToCurrentChain() const
     373                 :             : {
     374                 :         147 :     AssertLockNotHeld(cs_main);
     375                 :             : 
     376         [ +  + ]:         147 :     if (!m_synced) {
     377                 :             :         return false;
     378                 :             :     }
     379                 :             : 
     380                 :          28 :     {
     381                 :             :         // Skip the queue-draining stuff if we know we're caught up with
     382                 :             :         // m_chain.Tip().
     383                 :          28 :         LOCK(cs_main);
     384         [ +  - ]:          28 :         const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
     385         [ +  - ]:          28 :         const CBlockIndex* best_block_index = m_best_block_index.load();
     386   [ +  -  +  + ]:          28 :         if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
     387         [ +  - ]:          14 :             return true;
     388                 :             :         }
     389                 :          14 :     }
     390                 :             : 
     391                 :          14 :     LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
     392                 :          14 :     m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
     393                 :          14 :     return true;
     394                 :             : }
     395                 :             : 
     396                 :           9 : void BaseIndex::Interrupt()
     397                 :             : {
     398                 :           9 :     m_interrupt();
     399                 :           9 : }
     400                 :             : 
     401                 :           5 : bool BaseIndex::StartBackgroundSync()
     402                 :             : {
     403   [ -  +  -  - ]:           5 :     if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index");
     404                 :             : 
     405                 :          10 :     m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { Sync(); });
     406                 :           5 :     return true;
     407                 :             : }
     408                 :             : 
     409                 :          13 : void BaseIndex::Stop()
     410                 :             : {
     411         [ +  + ]:          13 :     if (m_chain->context()->validation_signals) {
     412                 :          10 :         m_chain->context()->validation_signals->UnregisterValidationInterface(this);
     413                 :             :     }
     414                 :             : 
     415         [ +  + ]:          13 :     if (m_thread_sync.joinable()) {
     416                 :           5 :         m_thread_sync.join();
     417                 :             :     }
     418                 :          13 : }
     419                 :             : 
     420                 :           4 : IndexSummary BaseIndex::GetSummary() const
     421                 :             : {
     422         [ +  - ]:           4 :     IndexSummary summary{};
     423         [ +  - ]:           4 :     summary.name = GetName();
     424         [ +  - ]:           4 :     summary.synced = m_synced;
     425         [ +  - ]:           4 :     if (const auto& pindex = m_best_block_index.load()) {
     426                 :           4 :         summary.best_block_height = pindex->nHeight;
     427                 :           4 :         summary.best_block_hash = pindex->GetBlockHash();
     428                 :             :     } else {
     429                 :           0 :         summary.best_block_height = 0;
     430         [ #  # ]:           0 :         summary.best_block_hash = m_chain->getBlockHash(0);
     431                 :             :     }
     432                 :           4 :     return summary;
     433                 :           0 : }
     434                 :             : 
     435                 :          36 : void BaseIndex::SetBestBlockIndex(const CBlockIndex* block)
     436                 :             : {
     437   [ -  +  -  - ]:          36 :     assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune());
     438                 :             : 
     439   [ +  +  +  + ]:          36 :     if (AllowPrune() && block) {
     440                 :          20 :         node::PruneLockInfo prune_lock;
     441                 :          20 :         prune_lock.height_first = block->nHeight;
     442         [ +  - ]:          60 :         WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock));
     443                 :             :     }
     444                 :             : 
     445                 :             :     // Intentionally set m_best_block_index as the last step in this function,
     446                 :             :     // after updating prune locks above, and after making any other references
     447                 :             :     // to *this, so the BlockUntilSyncedToCurrentChain function (which checks
     448                 :             :     // m_best_block_index as an optimization) can be used to wait for the last
     449                 :             :     // BlockConnected notification and safely assume that prune locks are
     450                 :             :     // updated and that the index object is safe to delete.
     451                 :          36 :     m_best_block_index = block;
     452                 :          36 : }
        

Generated by: LCOV version 2.0-1