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: 2025-06-01 05:54:06 Functions: 77.8 % 27 21
Branches: 45.5 % 242 110

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

Generated by: LCOV version 2.0-1