LCOV - code coverage report
Current view: top level - src/node - blockstorage.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 317 714 44.4 %
Date: 2023-10-05 15:40:34 Functions: 42 66 63.6 %
Branches: 222 1128 19.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2011-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/blockstorage.h>
       6                 :            : 
       7                 :            : #include <chain.h>
       8                 :            : #include <clientversion.h>
       9                 :            : #include <consensus/validation.h>
      10                 :            : #include <dbwrapper.h>
      11                 :            : #include <flatfile.h>
      12                 :            : #include <hash.h>
      13                 :            : #include <kernel/chain.h>
      14                 :            : #include <kernel/chainparams.h>
      15                 :            : #include <kernel/messagestartchars.h>
      16                 :            : #include <logging.h>
      17         [ +  - ]:        173 : #include <pow.h>
      18         [ +  - ]:        173 : #include <reverse_iterator.h>
      19                 :            : #include <signet.h>
      20                 :            : #include <streams.h>
      21                 :            : #include <sync.h>
      22                 :            : #include <undo.h>
      23                 :            : #include <util/batchpriority.h>
      24                 :            : #include <util/fs.h>
      25                 :            : #include <util/signalinterrupt.h>
      26                 :            : #include <util/strencodings.h>
      27                 :            : #include <util/translation.h>
      28                 :            : #include <validation.h>
      29                 :            : 
      30                 :            : #include <map>
      31                 :            : #include <unordered_map>
      32                 :            : 
      33                 :            : namespace kernel {
      34                 :            : static constexpr uint8_t DB_BLOCK_FILES{'f'};
      35                 :            : static constexpr uint8_t DB_BLOCK_INDEX{'b'};
      36                 :            : static constexpr uint8_t DB_FLAG{'F'};
      37                 :            : static constexpr uint8_t DB_REINDEX_FLAG{'R'};
      38                 :            : static constexpr uint8_t DB_LAST_BLOCK{'l'};
      39                 :            : // Keys used in previous version that might still be found in the DB:
      40                 :            : // BlockTreeDB::DB_TXINDEX_BLOCK{'T'};
      41                 :            : // BlockTreeDB::DB_TXINDEX{'t'}
      42                 :            : // BlockTreeDB::ReadFlag("txindex")
      43                 :            : 
      44                 :       1672 : bool BlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
      45                 :            : {
      46                 :       1672 :     return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
      47                 :            : }
      48                 :            : 
      49                 :          0 : bool BlockTreeDB::WriteReindexing(bool fReindexing)
      50                 :            : {
      51         [ #  # ]:          0 :     if (fReindexing) {
      52                 :          0 :         return Write(DB_REINDEX_FLAG, uint8_t{'1'});
      53                 :            :     } else {
      54                 :          0 :         return Erase(DB_REINDEX_FLAG);
      55                 :            :     }
      56                 :          0 : }
      57                 :            : 
      58                 :        836 : void BlockTreeDB::ReadReindexing(bool& fReindexing)
      59                 :            : {
      60                 :        836 :     fReindexing = Exists(DB_REINDEX_FLAG);
      61                 :        836 : }
      62                 :            : 
      63                 :        836 : bool BlockTreeDB::ReadLastBlockFile(int& nFile)
      64                 :            : {
      65                 :        836 :     return Read(DB_LAST_BLOCK, nFile);
      66                 :            : }
      67                 :            : 
      68                 :      50036 : bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo)
      69                 :            : {
      70                 :      50036 :     CDBBatch batch(*this);
      71         [ +  + ]:      94211 :     for (const auto& [file, info] : fileInfo) {
      72   [ +  -  +  -  :      44175 :         batch.Write(std::make_pair(DB_BLOCK_FILES, file), *info);
                   +  - ]
      73                 :            :     }
      74         [ +  - ]:      50209 :     batch.Write(DB_LAST_BLOCK, nLastFile);
      75         [ +  + ]:     124211 :     for (const CBlockIndex* bi : blockinfo) {
      76   [ +  -  +  -  :      74175 :         batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
             +  -  +  - ]
      77                 :            :     }
      78         [ +  - ]:      50036 :     return WriteBatch(batch, true);
      79                 :      50036 : }
      80                 :            : 
      81                 :          0 : bool BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
      82                 :            : {
      83         [ #  # ]:          0 :     return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
      84                 :          0 : }
      85                 :            : 
      86                 :        836 : bool BlockTreeDB::ReadFlag(const std::string& name, bool& fValue)
      87                 :            : {
      88                 :            :     uint8_t ch;
      89   [ +  -  +  - ]:        836 :     if (!Read(std::make_pair(DB_FLAG, name), ch)) {
      90                 :        836 :         return false;
      91                 :            :     }
      92                 :          0 :     fValue = ch == uint8_t{'1'};
      93                 :          0 :     return true;
      94                 :        836 : }
      95                 :            : 
      96                 :        836 : bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
      97                 :            : {
      98                 :        836 :     AssertLockHeld(::cs_main);
      99                 :        836 :     std::unique_ptr<CDBIterator> pcursor(NewIterator());
     100   [ +  -  +  -  :        836 :     pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
                   +  - ]
     101                 :            : 
     102                 :            :     // Load m_block_index
     103   [ +  -  -  + ]:        836 :     while (pcursor->Valid()) {
     104   [ #  #  #  # ]:          0 :         if (interrupt) return false;
     105         [ #  # ]:          0 :         std::pair<uint8_t, uint256> key;
     106   [ #  #  #  #  :          0 :         if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
                   #  # ]
     107         [ #  # ]:          0 :             CDiskBlockIndex diskindex;
     108   [ #  #  #  # ]:          0 :             if (pcursor->GetValue(diskindex)) {
     109                 :            :                 // Construct block index object
     110   [ #  #  #  # ]:          0 :                 CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
     111         [ #  # ]:          0 :                 pindexNew->pprev          = insertBlockIndex(diskindex.hashPrev);
     112                 :          0 :                 pindexNew->nHeight        = diskindex.nHeight;
     113                 :          0 :                 pindexNew->nFile          = diskindex.nFile;
     114                 :          0 :                 pindexNew->nDataPos       = diskindex.nDataPos;
     115                 :          0 :                 pindexNew->nUndoPos       = diskindex.nUndoPos;
     116                 :          0 :                 pindexNew->nVersion       = diskindex.nVersion;
     117                 :          0 :                 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
     118                 :          0 :                 pindexNew->nTime          = diskindex.nTime;
     119                 :          0 :                 pindexNew->nBits          = diskindex.nBits;
     120                 :          0 :                 pindexNew->nNonce         = diskindex.nNonce;
     121                 :          0 :                 pindexNew->nStatus        = diskindex.nStatus;
     122                 :          0 :                 pindexNew->nTx            = diskindex.nTx;
     123                 :            : 
     124   [ #  #  #  #  :          0 :                 if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
                   #  # ]
     125   [ #  #  #  # ]:          0 :                     return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
     126                 :            :                 }
     127                 :            : 
     128         [ #  # ]:          0 :                 pcursor->Next();
     129                 :          0 :             } else {
     130         [ #  # ]:          0 :                 return error("%s: failed to read value", __func__);
     131                 :            :             }
     132                 :          0 :         } else {
     133                 :          0 :             break;
     134                 :            :         }
     135                 :            :     }
     136                 :            : 
     137                 :        836 :     return true;
     138                 :        836 : }
     139                 :            : } // namespace kernel
     140                 :            : 
     141                 :            : namespace node {
     142                 :            : std::atomic_bool fReindex(false);
     143                 :            : 
     144                 :   67651359 : bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
     145                 :            : {
     146                 :            :     // First sort by most total work, ...
     147         [ +  + ]:   67651359 :     if (pa->nChainWork > pb->nChainWork) return false;
     148         [ +  + ]:   42206912 :     if (pa->nChainWork < pb->nChainWork) return true;
     149                 :            : 
     150                 :            :     // ... then by earliest time received, ...
     151         [ +  + ]:     845288 :     if (pa->nSequenceId < pb->nSequenceId) return false;
     152         [ -  + ]:     825009 :     if (pa->nSequenceId > pb->nSequenceId) return true;
     153                 :            : 
     154                 :            :     // Use pointer address as tie breaker (should only happen with blocks
     155                 :            :     // loaded from disk, as those all have id 0).
     156         [ -  + ]:     825009 :     if (pa < pb) return false;
     157         [ -  + ]:     825009 :     if (pa > pb) return true;
     158                 :            : 
     159                 :            :     // Identical blocks.
     160                 :     825009 :     return false;
     161                 :   67651359 : }
     162                 :            : 
     163                 :          0 : bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
     164                 :            : {
     165                 :          0 :     return pa->nHeight < pb->nHeight;
     166                 :            : }
     167                 :            : 
     168                 :       1672 : std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
     169                 :            : {
     170                 :       1672 :     AssertLockHeld(cs_main);
     171                 :       1672 :     std::vector<CBlockIndex*> rv;
     172         [ +  - ]:       1672 :     rv.reserve(m_block_index.size());
     173         [ +  - ]:       1672 :     for (auto& [_, block_index] : m_block_index) {
     174         [ #  # ]:          0 :         rv.push_back(&block_index);
     175                 :            :     }
     176                 :       1672 :     return rv;
     177         [ +  - ]:       1672 : }
     178                 :            : 
     179                 :    1764172 : CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash)
     180                 :            : {
     181                 :    1764172 :     AssertLockHeld(cs_main);
     182                 :    1764172 :     BlockMap::iterator it = m_block_index.find(hash);
     183         [ +  + ]:    1764172 :     return it == m_block_index.end() ? nullptr : &it->second;
     184                 :            : }
     185                 :            : 
     186                 :          4 : const CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
     187                 :            : {
     188                 :          4 :     AssertLockHeld(cs_main);
     189                 :          4 :     BlockMap::const_iterator it = m_block_index.find(hash);
     190         [ +  + ]:          4 :     return it == m_block_index.end() ? nullptr : &it->second;
     191                 :            : }
     192                 :            : 
     193                 :      82952 : CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockIndex*& best_header)
     194                 :            : {
     195                 :      82952 :     AssertLockHeld(cs_main);
     196                 :            : 
     197                 :      82952 :     auto [mi, inserted] = m_block_index.try_emplace(block.GetHash(), block);
     198         [ -  + ]:      82952 :     if (!inserted) {
     199                 :          0 :         return &mi->second;
     200                 :            :     }
     201                 :      82952 :     CBlockIndex* pindexNew = &(*mi).second;
     202                 :            : 
     203                 :            :     // We assign the sequence id to blocks only when the full data is available,
     204                 :            :     // to avoid miners withholding blocks but broadcasting headers, to get a
     205                 :            :     // competitive advantage.
     206                 :      82952 :     pindexNew->nSequenceId = 0;
     207                 :            : 
     208                 :      82952 :     pindexNew->phashBlock = &((*mi).first);
     209                 :      82952 :     BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);
     210         [ +  + ]:      82952 :     if (miPrev != m_block_index.end()) {
     211                 :      82116 :         pindexNew->pprev = &(*miPrev).second;
     212                 :      82116 :         pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
     213                 :      82116 :         pindexNew->BuildSkip();
     214                 :      82116 :     }
     215         [ +  + ]:      82952 :     pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
     216         [ +  + ]:      82952 :     pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
     217                 :      82952 :     pindexNew->RaiseValidity(BLOCK_VALID_TREE);
     218   [ +  +  +  + ]:      82952 :     if (best_header == nullptr || best_header->nChainWork < pindexNew->nChainWork) {
     219                 :      82447 :         best_header = pindexNew;
     220                 :      82447 :     }
     221                 :            : 
     222                 :      82952 :     m_dirty_blockindex.insert(pindexNew);
     223                 :            : 
     224                 :      82952 :     return pindexNew;
     225                 :      82952 : }
     226                 :            : 
     227                 :          0 : void BlockManager::PruneOneBlockFile(const int fileNumber)
     228                 :            : {
     229                 :          0 :     AssertLockHeld(cs_main);
     230                 :          0 :     LOCK(cs_LastBlockFile);
     231                 :            : 
     232         [ #  # ]:          0 :     for (auto& entry : m_block_index) {
     233                 :          0 :         CBlockIndex* pindex = &entry.second;
     234         [ #  # ]:          0 :         if (pindex->nFile == fileNumber) {
     235                 :          0 :             pindex->nStatus &= ~BLOCK_HAVE_DATA;
     236                 :          0 :             pindex->nStatus &= ~BLOCK_HAVE_UNDO;
     237                 :          0 :             pindex->nFile = 0;
     238                 :          0 :             pindex->nDataPos = 0;
     239                 :          0 :             pindex->nUndoPos = 0;
     240         [ #  # ]:          0 :             m_dirty_blockindex.insert(pindex);
     241                 :            : 
     242                 :            :             // Prune from m_blocks_unlinked -- any block we prune would have
     243                 :            :             // to be downloaded again in order to consider its chain, at which
     244                 :            :             // point it would be considered as a candidate for
     245                 :            :             // m_blocks_unlinked or setBlockIndexCandidates.
     246         [ #  # ]:          0 :             auto range = m_blocks_unlinked.equal_range(pindex->pprev);
     247         [ #  # ]:          0 :             while (range.first != range.second) {
     248                 :          0 :                 std::multimap<CBlockIndex*, CBlockIndex*>::iterator _it = range.first;
     249                 :          0 :                 range.first++;
     250         [ #  # ]:          0 :                 if (_it->second == pindex) {
     251         [ #  # ]:          0 :                     m_blocks_unlinked.erase(_it);
     252                 :          0 :                 }
     253                 :            :             }
     254                 :          0 :         }
     255                 :            :     }
     256                 :            : 
     257         [ #  # ]:          0 :     m_blockfile_info[fileNumber].SetNull();
     258         [ #  # ]:          0 :     m_dirty_fileinfo.insert(fileNumber);
     259                 :          0 : }
     260                 :            : 
     261                 :          0 : void BlockManager::FindFilesToPruneManual(
     262                 :            :     std::set<int>& setFilesToPrune,
     263                 :            :     int nManualPruneHeight,
     264                 :            :     const Chainstate& chain,
     265                 :            :     ChainstateManager& chainman)
     266                 :            : {
     267   [ #  #  #  # ]:          0 :     assert(IsPruneMode() && nManualPruneHeight > 0);
     268                 :            : 
     269   [ #  #  #  # ]:          0 :     LOCK2(cs_main, cs_LastBlockFile);
     270   [ #  #  #  # ]:          0 :     if (chain.m_chain.Height() < 0) {
     271                 :          0 :         return;
     272                 :            :     }
     273                 :            : 
     274         [ #  # ]:          0 :     const auto [min_block_to_prune, last_block_can_prune] = chainman.GetPruneRange(chain, nManualPruneHeight);
     275                 :            : 
     276                 :          0 :     int count = 0;
     277   [ #  #  #  # ]:          0 :     for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {
     278                 :          0 :         const auto& fileinfo = m_blockfile_info[fileNumber];
     279   [ #  #  #  #  :          0 :         if (fileinfo.nSize == 0 || fileinfo.nHeightLast > (unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
          #  #  #  #  #  
                      # ]
     280                 :          0 :             continue;
     281                 :            :         }
     282                 :            : 
     283         [ #  # ]:          0 :         PruneOneBlockFile(fileNumber);
     284         [ #  # ]:          0 :         setFilesToPrune.insert(fileNumber);
     285                 :          0 :         count++;
     286                 :          0 :     }
     287   [ #  #  #  #  :          0 :     LogPrintf("[%s] Prune (Manual): prune_height=%d removed %d blk/rev pairs\n",
          #  #  #  #  #  
                      # ]
     288                 :            :         chain.GetRole(), last_block_can_prune, count);
     289         [ #  # ]:          0 : }
     290                 :            : 
     291                 :          0 : void BlockManager::FindFilesToPrune(
     292                 :            :     std::set<int>& setFilesToPrune,
     293                 :            :     int last_prune,
     294                 :            :     const Chainstate& chain,
     295                 :            :     ChainstateManager& chainman)
     296                 :            : {
     297   [ #  #  #  # ]:          0 :     LOCK2(cs_main, cs_LastBlockFile);
     298                 :            :     // Distribute our -prune budget over all chainstates.
     299         [ #  # ]:          0 :     const auto target = std::max(
     300   [ #  #  #  # ]:          0 :         MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget() / chainman.GetAll().size());
     301                 :            : 
     302   [ #  #  #  #  :          0 :     if (chain.m_chain.Height() < 0 || target == 0) {
                   #  # ]
     303                 :          0 :         return;
     304                 :            :     }
     305   [ #  #  #  #  :          0 :     if (static_cast<uint64_t>(chain.m_chain.Height()) <= chainman.GetParams().PruneAfterHeight()) {
             #  #  #  # ]
     306                 :          0 :         return;
     307                 :            :     }
     308                 :            : 
     309         [ #  # ]:          0 :     const auto [min_block_to_prune, last_block_can_prune] = chainman.GetPruneRange(chain, last_prune);
     310                 :            : 
     311         [ #  # ]:          0 :     uint64_t nCurrentUsage = CalculateCurrentUsage();
     312                 :            :     // We don't check to prune until after we've allocated new space for files
     313                 :            :     // So we should leave a buffer under our target to account for another allocation
     314                 :            :     // before the next pruning.
     315                 :          0 :     uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
     316                 :            :     uint64_t nBytesToPrune;
     317                 :          0 :     int count = 0;
     318                 :            : 
     319         [ #  # ]:          0 :     if (nCurrentUsage + nBuffer >= target) {
     320                 :            :         // On a prune event, the chainstate DB is flushed.
     321                 :            :         // To avoid excessive prune events negating the benefit of high dbcache
     322                 :            :         // values, we should not prune too rapidly.
     323                 :            :         // So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
     324   [ #  #  #  # ]:          0 :         if (chainman.IsInitialBlockDownload()) {
     325                 :            :             // Since this is only relevant during IBD, we use a fixed 10%
     326                 :          0 :             nBuffer += target / 10;
     327                 :          0 :         }
     328                 :            : 
     329   [ #  #  #  # ]:          0 :         for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {
     330                 :          0 :             const auto& fileinfo = m_blockfile_info[fileNumber];
     331                 :          0 :             nBytesToPrune = fileinfo.nSize + fileinfo.nUndoSize;
     332                 :            : 
     333         [ #  # ]:          0 :             if (fileinfo.nSize == 0) {
     334                 :          0 :                 continue;
     335                 :            :             }
     336                 :            : 
     337         [ #  # ]:          0 :             if (nCurrentUsage + nBuffer < target) { // are we below our target?
     338                 :          0 :                 break;
     339                 :            :             }
     340                 :            : 
     341                 :            :             // don't prune files that could have a block that's not within the allowable
     342                 :            :             // prune range for the chain being pruned.
     343   [ #  #  #  #  :          0 :             if (fileinfo.nHeightLast > (unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
             #  #  #  # ]
     344                 :          0 :                 continue;
     345                 :            :             }
     346                 :            : 
     347         [ #  # ]:          0 :             PruneOneBlockFile(fileNumber);
     348                 :            :             // Queue up the files for removal
     349         [ #  # ]:          0 :             setFilesToPrune.insert(fileNumber);
     350                 :          0 :             nCurrentUsage -= nBytesToPrune;
     351                 :          0 :             count++;
     352                 :          0 :         }
     353                 :          0 :     }
     354                 :            : 
     355   [ #  #  #  #  :          0 :     LogPrint(BCLog::PRUNE, "[%s] target=%dMiB actual=%dMiB diff=%dMiB min_height=%d max_prune_height=%d removed %d blk/rev pairs\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     356                 :            :              chain.GetRole(), target / 1024 / 1024, nCurrentUsage / 1024 / 1024,
     357                 :            :              (int64_t(target) - int64_t(nCurrentUsage)) / 1024 / 1024,
     358                 :            :              min_block_to_prune, last_block_can_prune, count);
     359         [ #  # ]:          0 : }
     360                 :            : 
     361                 :          0 : void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) {
     362                 :          0 :     AssertLockHeld(::cs_main);
     363                 :          0 :     m_prune_locks[name] = lock_info;
     364                 :          0 : }
     365                 :            : 
     366                 :          0 : CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
     367                 :            : {
     368                 :          0 :     AssertLockHeld(cs_main);
     369                 :            : 
     370         [ #  # ]:          0 :     if (hash.IsNull()) {
     371                 :          0 :         return nullptr;
     372                 :            :     }
     373                 :            : 
     374                 :          0 :     const auto [mi, inserted]{m_block_index.try_emplace(hash)};
     375                 :          0 :     CBlockIndex* pindex = &(*mi).second;
     376         [ #  # ]:          0 :     if (inserted) {
     377                 :          0 :         pindex->phashBlock = &((*mi).first);
     378                 :          0 :     }
     379                 :          0 :     return pindex;
     380                 :          0 : }
     381                 :            : 
     382                 :        836 : bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockhash)
     383                 :            : {
     384   [ +  -  +  -  :       1672 :     if (!m_block_tree_db->LoadBlockIndexGuts(
                   -  + ]
     385                 :        836 :             GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, m_interrupt)) {
     386                 :          0 :         return false;
     387                 :            :     }
     388                 :            : 
     389         [ -  + ]:        836 :     if (snapshot_blockhash) {
     390                 :          0 :         const AssumeutxoData au_data = *Assert(GetParams().AssumeutxoForBlockhash(*snapshot_blockhash));
     391                 :          0 :         m_snapshot_height = au_data.height;
     392                 :          0 :         CBlockIndex* base{LookupBlockIndex(*snapshot_blockhash)};
     393                 :            : 
     394                 :            :         // Since nChainTx (responsible for estimated progress) isn't persisted
     395                 :            :         // to disk, we must bootstrap the value for assumedvalid chainstates
     396                 :            :         // from the hardcoded assumeutxo chainparams.
     397                 :          0 :         base->nChainTx = au_data.nChainTx;
     398   [ #  #  #  #  :          0 :         LogPrintf("[snapshot] set nChainTx=%d for %s\n", au_data.nChainTx, snapshot_blockhash->ToString());
          #  #  #  #  #  
                      # ]
     399                 :          0 :     } else {
     400                 :            :         // If this isn't called with a snapshot blockhash, make sure the cached snapshot height
     401                 :            :         // is null. This is relevant during snapshot completion, when the blockman may be loaded
     402                 :            :         // with a height that then needs to be cleared after the snapshot is fully validated.
     403                 :        836 :         m_snapshot_height.reset();
     404                 :            :     }
     405                 :            : 
     406                 :        836 :     Assert(m_snapshot_height.has_value() == snapshot_blockhash.has_value());
     407                 :            : 
     408                 :            :     // Calculate nChainWork
     409                 :        836 :     std::vector<CBlockIndex*> vSortedByHeight{GetAllBlockIndices()};
     410         [ +  - ]:        836 :     std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
     411                 :            :               CBlockIndexHeightOnlyComparator());
     412                 :            : 
     413         [ -  + ]:        836 :     for (CBlockIndex* pindex : vSortedByHeight) {
     414   [ #  #  #  # ]:          0 :         if (m_interrupt) return false;
     415   [ #  #  #  #  :          0 :         pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     416   [ #  #  #  # ]:          0 :         pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
     417                 :            : 
     418                 :            :         // We can link the chain of blocks for which we've received transactions at some point, or
     419                 :            :         // blocks that are assumed-valid on the basis of snapshot load (see
     420                 :            :         // PopulateAndValidateSnapshot()).
     421                 :            :         // Pruned nodes may have deleted the block.
     422         [ #  # ]:          0 :         if (pindex->nTx > 0) {
     423         [ #  # ]:          0 :             if (pindex->pprev) {
     424   [ #  #  #  #  :          0 :                 if (m_snapshot_height && pindex->nHeight == *m_snapshot_height &&
             #  #  #  # ]
     425   [ #  #  #  #  :          0 :                         pindex->GetBlockHash() == *snapshot_blockhash) {
                   #  # ]
     426                 :            :                     // Should have been set above; don't disturb it with code below.
     427         [ #  # ]:          0 :                     Assert(pindex->nChainTx > 0);
     428         [ #  # ]:          0 :                 } else if (pindex->pprev->nChainTx > 0) {
     429                 :          0 :                     pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
     430                 :          0 :                 } else {
     431                 :          0 :                     pindex->nChainTx = 0;
     432   [ #  #  #  # ]:          0 :                     m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
     433                 :            :                 }
     434                 :          0 :             } else {
     435                 :          0 :                 pindex->nChainTx = pindex->nTx;
     436                 :            :             }
     437                 :          0 :         }
     438   [ #  #  #  #  :          0 :         if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
                   #  # ]
     439                 :          0 :             pindex->nStatus |= BLOCK_FAILED_CHILD;
     440         [ #  # ]:          0 :             m_dirty_blockindex.insert(pindex);
     441                 :          0 :         }
     442         [ #  # ]:          0 :         if (pindex->pprev) {
     443         [ #  # ]:          0 :             pindex->BuildSkip();
     444                 :          0 :         }
     445                 :            :     }
     446                 :            : 
     447                 :        836 :     return true;
     448                 :        836 : }
     449                 :            : 
     450                 :      50036 : bool BlockManager::WriteBlockIndexDB()
     451                 :            : {
     452                 :      50036 :     AssertLockHeld(::cs_main);
     453                 :      50036 :     std::vector<std::pair<int, const CBlockFileInfo*>> vFiles;
     454         [ +  - ]:      50036 :     vFiles.reserve(m_dirty_fileinfo.size());
     455         [ +  + ]:      94211 :     for (std::set<int>::iterator it = m_dirty_fileinfo.begin(); it != m_dirty_fileinfo.end();) {
     456   [ +  -  +  -  :      44175 :         vFiles.push_back(std::make_pair(*it, &m_blockfile_info[*it]));
                   +  - ]
     457         [ +  - ]:      44175 :         m_dirty_fileinfo.erase(it++);
     458                 :            :     }
     459                 :      50036 :     std::vector<const CBlockIndex*> vBlocks;
     460         [ +  - ]:      50036 :     vBlocks.reserve(m_dirty_blockindex.size());
     461         [ +  + ]:     124211 :     for (std::set<CBlockIndex*>::iterator it = m_dirty_blockindex.begin(); it != m_dirty_blockindex.end();) {
     462         [ +  - ]:      74175 :         vBlocks.push_back(*it);
     463         [ +  - ]:      74175 :         m_dirty_blockindex.erase(it++);
     464                 :            :     }
     465   [ +  -  +  -  :     100072 :     int max_blockfile = WITH_LOCK(cs_LastBlockFile, return this->MaxBlockfileNum());
                   +  - ]
     466   [ +  -  +  - ]:      50036 :     if (!m_block_tree_db->WriteBatchSync(vFiles, max_blockfile, vBlocks)) {
     467                 :          0 :         return false;
     468                 :            :     }
     469                 :      50036 :     return true;
     470                 :      50036 : }
     471                 :            : 
     472                 :        836 : bool BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_blockhash)
     473                 :            : {
     474         [ -  + ]:        836 :     if (!LoadBlockIndex(snapshot_blockhash)) {
     475                 :          0 :         return false;
     476                 :            :     }
     477                 :        836 :     int max_blockfile_num{0};
     478                 :            : 
     479                 :            :     // Load block file info
     480                 :        836 :     m_block_tree_db->ReadLastBlockFile(max_blockfile_num);
     481                 :        836 :     m_blockfile_info.resize(max_blockfile_num + 1);
     482   [ +  -  +  -  :        836 :     LogPrintf("%s: last block file = %i\n", __func__, max_blockfile_num);
                   +  - ]
     483         [ +  + ]:       1672 :     for (int nFile = 0; nFile <= max_blockfile_num; nFile++) {
     484                 :        836 :         m_block_tree_db->ReadBlockFileInfo(nFile, m_blockfile_info[nFile]);
     485                 :        836 :     }
     486   [ +  -  +  -  :        836 :     LogPrintf("%s: last block file info: %s\n", __func__, m_blockfile_info[max_blockfile_num].ToString());
             +  -  +  - ]
     487         [ -  + ]:        836 :     for (int nFile = max_blockfile_num + 1; true; nFile++) {
     488                 :        836 :         CBlockFileInfo info;
     489         [ -  + ]:        836 :         if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
     490                 :          0 :             m_blockfile_info.push_back(info);
     491                 :          0 :         } else {
     492                 :        836 :             break;
     493                 :            :         }
     494                 :          0 :     }
     495                 :            : 
     496                 :            :     // Check presence of blk files
     497   [ +  -  +  -  :        836 :     LogPrintf("Checking all blk files are present...\n");
                   +  - ]
     498                 :        836 :     std::set<int> setBlkDataFiles;
     499         [ -  + ]:        836 :     for (const auto& [_, block_index] : m_block_index) {
     500         [ #  # ]:          0 :         if (block_index.nStatus & BLOCK_HAVE_DATA) {
     501         [ #  # ]:          0 :             setBlkDataFiles.insert(block_index.nFile);
     502                 :          0 :         }
     503                 :            :     }
     504         [ +  - ]:        836 :     for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
     505         [ #  # ]:          0 :         FlatFilePos pos(*it, 0);
     506   [ #  #  #  #  :          0 :         if (OpenBlockFile(pos, true).IsNull()) {
                   #  # ]
     507                 :          0 :             return false;
     508                 :            :         }
     509                 :          0 :     }
     510                 :            : 
     511                 :            :     {
     512                 :            :         // Initialize the blockfile cursors.
     513   [ +  -  +  - ]:        836 :         LOCK(cs_LastBlockFile);
     514         [ +  + ]:       1672 :         for (size_t i = 0; i < m_blockfile_info.size(); ++i) {
     515                 :        836 :             const auto last_height_in_file = m_blockfile_info[i].nHeightLast;
     516   [ +  -  +  - ]:        836 :             m_blockfile_cursors[BlockfileTypeForHeight(last_height_in_file)] = {static_cast<int>(i), 0};
     517                 :        836 :         }
     518                 :        836 :     }
     519                 :            : 
     520                 :            :     // Check whether we have ever pruned block & undo files
     521   [ +  -  +  - ]:        836 :     m_block_tree_db->ReadFlag("prunedblockfiles", m_have_pruned);
     522         [ +  - ]:        836 :     if (m_have_pruned) {
     523   [ #  #  #  #  :          0 :         LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
                   #  # ]
     524                 :          0 :     }
     525                 :            : 
     526                 :            :     // Check whether we need to continue reindexing
     527                 :        836 :     bool fReindexing = false;
     528         [ +  - ]:        836 :     m_block_tree_db->ReadReindexing(fReindexing);
     529         [ +  - ]:        836 :     if (fReindexing) fReindex = true;
     530                 :            : 
     531                 :        836 :     return true;
     532                 :        836 : }
     533                 :            : 
     534                 :        836 : void BlockManager::ScanAndUnlinkAlreadyPrunedFiles()
     535                 :            : {
     536                 :        836 :     AssertLockHeld(::cs_main);
     537         [ +  - ]:       1672 :     int max_blockfile = WITH_LOCK(cs_LastBlockFile, return this->MaxBlockfileNum());
     538         [ +  - ]:        836 :     if (!m_have_pruned) {
     539                 :        836 :         return;
     540                 :            :     }
     541                 :            : 
     542                 :          0 :     std::set<int> block_files_to_prune;
     543         [ #  # ]:          0 :     for (int file_number = 0; file_number < max_blockfile; file_number++) {
     544         [ #  # ]:          0 :         if (m_blockfile_info[file_number].nSize == 0) {
     545         [ #  # ]:          0 :             block_files_to_prune.insert(file_number);
     546                 :          0 :         }
     547                 :          0 :     }
     548                 :            : 
     549         [ #  # ]:          0 :     UnlinkPrunedFiles(block_files_to_prune);
     550                 :        836 : }
     551                 :            : 
     552                 :     154304 : const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
     553                 :            : {
     554                 :     154304 :     const MapCheckpoints& checkpoints = data.mapCheckpoints;
     555                 :            : 
     556         [ +  - ]:     154304 :     for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
     557                 :     154304 :         const uint256& hash = i.second;
     558                 :     154304 :         const CBlockIndex* pindex = LookupBlockIndex(hash);
     559         [ +  - ]:     154304 :         if (pindex) {
     560                 :     154304 :             return pindex;
     561                 :            :         }
     562                 :            :     }
     563                 :          0 :     return nullptr;
     564                 :     154304 : }
     565                 :            : 
     566                 :         45 : bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
     567                 :            : {
     568                 :         45 :     AssertLockHeld(::cs_main);
     569   [ -  +  #  # ]:         45 :     return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
     570                 :            : }
     571                 :            : 
     572                 :          0 : const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& upper_block, const CBlockIndex* lower_block)
     573                 :            : {
     574                 :          0 :     AssertLockHeld(::cs_main);
     575                 :          0 :     const CBlockIndex* last_block = &upper_block;
     576         [ #  # ]:          0 :     assert(last_block->nStatus & BLOCK_HAVE_DATA); // 'upper_block' must have data
     577   [ #  #  #  # ]:          0 :     while (last_block->pprev && (last_block->pprev->nStatus & BLOCK_HAVE_DATA)) {
     578         [ #  # ]:          0 :         if (lower_block) {
     579                 :            :             // Return if we reached the lower_block
     580         [ #  # ]:          0 :             if (last_block == lower_block) return lower_block;
     581                 :            :             // if range was surpassed, means that 'lower_block' is not part of the 'upper_block' chain
     582                 :            :             // and so far this is not allowed.
     583         [ #  # ]:          0 :             assert(last_block->nHeight >= lower_block->nHeight);
     584                 :          0 :         }
     585                 :          0 :         last_block = last_block->pprev;
     586                 :            :     }
     587         [ #  # ]:          0 :     assert(last_block != nullptr);
     588                 :          0 :     return last_block;
     589                 :          0 : }
     590                 :            : 
     591                 :          0 : bool BlockManager::CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block)
     592                 :            : {
     593         [ #  # ]:          0 :     if (!(upper_block.nStatus & BLOCK_HAVE_DATA)) return false;
     594                 :          0 :     return GetFirstStoredBlock(upper_block, &lower_block) == &lower_block;
     595                 :          0 : }
     596                 :            : 
     597                 :            : // If we're using -prune with -reindex, then delete block files that will be ignored by the
     598                 :            : // reindex.  Since reindexing works by starting at block file 0 and looping until a blockfile
     599                 :            : // is missing, do the same here to delete any later block files after a gap.  Also delete all
     600                 :            : // rev files since they'll be rewritten by the reindex anyway.  This ensures that m_blockfile_info
     601                 :            : // is in sync with what's actually on disk by the time we start downloading, so that pruning
     602                 :            : // works correctly.
     603                 :          0 : void BlockManager::CleanupBlockRevFiles() const
     604                 :            : {
     605                 :          0 :     std::map<std::string, fs::path> mapBlockFiles;
     606                 :            : 
     607                 :            :     // Glob all blk?????.dat and rev?????.dat files from the blocks directory.
     608                 :            :     // Remove the rev files immediately and insert the blk file paths into an
     609                 :            :     // ordered map keyed by block file index.
     610   [ #  #  #  #  :          0 :     LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
                   #  # ]
     611   [ #  #  #  #  :          0 :     for (fs::directory_iterator it(m_opts.blocks_dir); it != fs::directory_iterator(); it++) {
                   #  # ]
     612   [ #  #  #  #  :          0 :         const std::string path = fs::PathToString(it->path().filename());
                   #  # ]
     613   [ #  #  #  #  :          0 :         if (fs::is_regular_file(*it) &&
          #  #  #  #  #  
                      # ]
     614         [ #  # ]:          0 :             path.length() == 12 &&
     615   [ #  #  #  # ]:          0 :             path.substr(8,4) == ".dat")
     616                 :            :         {
     617   [ #  #  #  #  :          0 :             if (path.substr(0, 3) == "blk") {
                   #  # ]
     618   [ #  #  #  #  :          0 :                 mapBlockFiles[path.substr(3, 5)] = it->path();
             #  #  #  # ]
     619   [ #  #  #  #  :          0 :             } else if (path.substr(0, 3) == "rev") {
                   #  # ]
     620         [ #  # ]:          0 :                 remove(it->path());
     621                 :          0 :             }
     622                 :          0 :         }
     623                 :          0 :     }
     624                 :            : 
     625                 :            :     // Remove all block files that aren't part of a contiguous set starting at
     626                 :            :     // zero by walking the ordered map (keys are block file indices) by
     627                 :            :     // keeping a separate counter.  Once we hit a gap (or if 0 doesn't exist)
     628                 :            :     // start removing block files.
     629                 :          0 :     int nContigCounter = 0;
     630         [ #  # ]:          0 :     for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
     631   [ #  #  #  # ]:          0 :         if (LocaleIndependentAtoi<int>(item.first) == nContigCounter) {
     632                 :          0 :             nContigCounter++;
     633                 :          0 :             continue;
     634                 :            :         }
     635         [ #  # ]:          0 :         remove(item.second);
     636                 :            :     }
     637                 :          0 : }
     638                 :            : 
     639                 :          0 : CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
     640                 :            : {
     641                 :          0 :     LOCK(cs_LastBlockFile);
     642                 :            : 
     643         [ #  # ]:          0 :     return &m_blockfile_info.at(n);
     644                 :          0 : }
     645                 :            : 
     646                 :      40547 : bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const
     647                 :            : {
     648                 :            :     // Open history file to append
     649                 :      40547 :     CAutoFile fileout{OpenUndoFile(pos)};
     650   [ +  -  -  + ]:      40547 :     if (fileout.IsNull()) {
     651         [ #  # ]:          0 :         return error("%s: OpenUndoFile failed", __func__);
     652                 :            :     }
     653                 :            : 
     654                 :            :     // Write index header
     655         [ +  - ]:      40547 :     unsigned int nSize = GetSerializeSize(blockundo, CLIENT_VERSION);
     656   [ +  -  +  -  :      40547 :     fileout << GetParams().MessageStart() << nSize;
             +  -  +  - ]
     657                 :            : 
     658                 :            :     // Write undo data
     659   [ +  -  +  - ]:      40547 :     long fileOutPos = ftell(fileout.Get());
     660         [ -  + ]:      40547 :     if (fileOutPos < 0) {
     661         [ #  # ]:          0 :         return error("%s: ftell failed", __func__);
     662                 :            :     }
     663                 :      40547 :     pos.nPos = (unsigned int)fileOutPos;
     664         [ +  - ]:      40547 :     fileout << blockundo;
     665                 :            : 
     666                 :            :     // calculate & write checksum
     667         [ +  - ]:      40547 :     HashWriter hasher{};
     668         [ +  - ]:      40547 :     hasher << hashBlock;
     669         [ +  - ]:      40547 :     hasher << blockundo;
     670   [ +  -  +  - ]:      40547 :     fileout << hasher.GetHash();
     671                 :            : 
     672                 :      40547 :     return true;
     673                 :      40547 : }
     674                 :            : 
     675                 :          0 : bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const
     676                 :            : {
     677         [ #  # ]:          0 :     const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
     678                 :            : 
     679         [ #  # ]:          0 :     if (pos.IsNull()) {
     680                 :          0 :         return error("%s: no undo data available", __func__);
     681                 :            :     }
     682                 :            : 
     683                 :            :     // Open history file to read
     684                 :          0 :     CAutoFile filein{OpenUndoFile(pos, true)};
     685   [ #  #  #  # ]:          0 :     if (filein.IsNull()) {
     686         [ #  # ]:          0 :         return error("%s: OpenUndoFile failed", __func__);
     687                 :            :     }
     688                 :            : 
     689                 :            :     // Read block
     690         [ #  # ]:          0 :     uint256 hashChecksum;
     691         [ #  # ]:          0 :     HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
     692                 :            :     try {
     693   [ #  #  #  # ]:          0 :         verifier << index.pprev->GetBlockHash();
     694         [ #  # ]:          0 :         verifier >> blockundo;
     695         [ #  # ]:          0 :         filein >> hashChecksum;
     696         [ #  # ]:          0 :     } catch (const std::exception& e) {
     697         [ #  # ]:          0 :         return error("%s: Deserialize or I/O error - %s", __func__, e.what());
     698   [ #  #  #  # ]:          0 :     }
     699                 :            : 
     700                 :            :     // Verify checksum
     701   [ #  #  #  #  :          0 :     if (hashChecksum != verifier.GetHash()) {
                   #  # ]
     702         [ #  # ]:          0 :         return error("%s: Checksum mismatch", __func__);
     703                 :            :     }
     704                 :            : 
     705                 :          0 :     return true;
     706                 :          0 : }
     707                 :            : 
     708                 :      50036 : bool BlockManager::FlushUndoFile(int block_file, bool finalize)
     709                 :            : {
     710                 :      50036 :     FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
     711   [ +  -  -  + ]:      50036 :     if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
     712   [ #  #  #  # ]:          0 :         m_opts.notifications.flushError("Flushing undo file to disk failed. This is likely the result of an I/O error.");
     713                 :          0 :         return false;
     714                 :            :     }
     715                 :      50036 :     return true;
     716                 :      50036 : }
     717                 :            : 
     718                 :      50036 : bool BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
     719                 :            : {
     720                 :      50036 :     bool success = true;
     721                 :      50036 :     LOCK(cs_LastBlockFile);
     722                 :            : 
     723         [ -  + ]:      50036 :     if (m_blockfile_info.size() < 1) {
     724                 :            :         // Return if we haven't loaded any blockfiles yet. This happens during
     725                 :            :         // chainstate init, when we call ChainstateManager::MaybeRebalanceCaches() (which
     726                 :            :         // then calls FlushStateToDisk()), resulting in a call to this function before we
     727                 :            :         // have populated `m_blockfile_info` via LoadBlockIndexDB().
     728                 :          0 :         return true;
     729                 :            :     }
     730         [ +  - ]:      50036 :     assert(static_cast<int>(m_blockfile_info.size()) > blockfile_num);
     731                 :            : 
     732         [ +  - ]:      50036 :     FlatFilePos block_pos_old(blockfile_num, m_blockfile_info[blockfile_num].nSize);
     733   [ +  -  +  -  :      50036 :     if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
                   +  - ]
     734   [ #  #  #  # ]:          0 :         m_opts.notifications.flushError("Flushing block file to disk failed. This is likely the result of an I/O error.");
     735                 :          0 :         success = false;
     736                 :          0 :     }
     737                 :            :     // we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
     738                 :            :     // e.g. during IBD or a sync after a node going offline
     739   [ -  +  #  # ]:      50036 :     if (!fFinalize || finalize_undo) {
     740   [ +  -  +  - ]:      50036 :         if (!FlushUndoFile(blockfile_num, finalize_undo)) {
     741                 :          0 :             success = false;
     742                 :          0 :         }
     743                 :      50036 :     }
     744                 :      50036 :     return success;
     745                 :      50036 : }
     746                 :            : 
     747                 :     136662 : BlockfileType BlockManager::BlockfileTypeForHeight(int height)
     748                 :            : {
     749         [ +  - ]:     136662 :     if (!m_snapshot_height) {
     750                 :     136662 :         return BlockfileType::NORMAL;
     751                 :            :     }
     752                 :          0 :     return (height >= *m_snapshot_height) ? BlockfileType::ASSUMED : BlockfileType::NORMAL;
     753                 :     136662 : }
     754                 :            : 
     755                 :      50036 : bool BlockManager::FlushChainstateBlockFile(int tip_height)
     756                 :            : {
     757                 :      50036 :     LOCK(cs_LastBlockFile);
     758         [ +  - ]:      50036 :     auto& cursor = m_blockfile_cursors[BlockfileTypeForHeight(tip_height)];
     759         [ +  - ]:      50036 :     if (cursor) {
     760                 :            :         // The cursor may not exist after a snapshot has been loaded but before any
     761                 :            :         // blocks have been downloaded.
     762   [ +  -  +  - ]:      50036 :         return FlushBlockFile(cursor->file_num, /*fFinalize=*/false, /*finalize_undo=*/false);
     763                 :            :     }
     764                 :          0 :     return false;
     765                 :      50036 : }
     766                 :            : 
     767                 :          1 : uint64_t BlockManager::CalculateCurrentUsage()
     768                 :            : {
     769                 :          1 :     LOCK(cs_LastBlockFile);
     770                 :            : 
     771                 :          1 :     uint64_t retval = 0;
     772         [ +  + ]:          2 :     for (const CBlockFileInfo& file : m_blockfile_info) {
     773                 :          1 :         retval += file.nSize + file.nUndoSize;
     774                 :            :     }
     775                 :          1 :     return retval;
     776                 :          1 : }
     777                 :            : 
     778                 :          0 : void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
     779                 :            : {
     780                 :          0 :     std::error_code ec;
     781         [ #  # ]:          0 :     for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
     782                 :          0 :         FlatFilePos pos(*it, 0);
     783         [ #  # ]:          0 :         const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
     784         [ #  # ]:          0 :         const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
     785   [ #  #  #  # ]:          0 :         if (removed_blockfile || removed_undofile) {
     786   [ #  #  #  #  :          0 :             LogPrint(BCLog::BLOCKSTORAGE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
             #  #  #  # ]
     787                 :          0 :         }
     788                 :          0 :     }
     789                 :          0 : }
     790                 :            : 
     791                 :     190238 : FlatFileSeq BlockManager::BlockFileSeq() const
     792                 :            : {
     793         [ +  - ]:     190238 :     return FlatFileSeq(m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
     794                 :          0 : }
     795                 :            : 
     796                 :     131130 : FlatFileSeq BlockManager::UndoFileSeq() const
     797                 :            : {
     798         [ +  - ]:     131130 :     return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
     799                 :          0 : }
     800                 :            : 
     801                 :      94959 : CAutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
     802                 :            : {
     803   [ +  -  -  + ]:      94959 :     return CAutoFile{BlockFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
     804                 :          0 : }
     805                 :            : 
     806                 :            : /** Open an undo file (rev?????.dat) */
     807                 :      40547 : CAutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
     808                 :            : {
     809   [ +  -  -  + ]:      40547 :     return CAutoFile{UndoFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
     810                 :          0 : }
     811                 :            : 
     812                 :          0 : fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
     813                 :            : {
     814         [ #  # ]:          0 :     return BlockFileSeq().FileName(pos);
     815                 :          0 : }
     816                 :            : 
     817                 :      45243 : bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown)
     818                 :            : {
     819                 :      45243 :     LOCK(cs_LastBlockFile);
     820                 :            : 
     821         [ +  - ]:      45243 :     const BlockfileType chain_type = BlockfileTypeForHeight(nHeight);
     822                 :            : 
     823         [ +  - ]:      45243 :     if (!m_blockfile_cursors[chain_type]) {
     824                 :            :         // If a snapshot is loaded during runtime, we may not have initialized this cursor yet.
     825         [ #  # ]:          0 :         assert(chain_type == BlockfileType::ASSUMED);
     826         [ #  # ]:          0 :         const auto new_cursor = BlockfileCursor{this->MaxBlockfileNum() + 1};
     827         [ #  # ]:          0 :         m_blockfile_cursors[chain_type] = new_cursor;
     828   [ #  #  #  #  :          0 :         LogPrint(BCLog::BLOCKSTORAGE, "[%s] initializing blockfile cursor to %s\n", chain_type, new_cursor);
          #  #  #  #  #  
                      # ]
     829                 :          0 :     }
     830         [ +  - ]:      45243 :     const int last_blockfile = m_blockfile_cursors[chain_type]->file_num;
     831                 :            : 
     832         [ -  + ]:      45243 :     int nFile = fKnown ? pos.nFile : last_blockfile;
     833         [ -  + ]:      45243 :     if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
     834         [ #  # ]:          0 :         m_blockfile_info.resize(nFile + 1);
     835                 :          0 :     }
     836                 :            : 
     837                 :      45243 :     bool finalize_undo = false;
     838         [ -  + ]:      45243 :     if (!fKnown) {
     839                 :      45243 :         unsigned int max_blockfile_size{MAX_BLOCKFILE_SIZE};
     840                 :            :         // Use smaller blockfiles in test-only -fastprune mode - but avoid
     841                 :            :         // the possibility of having a block not fit into the block file.
     842         [ +  - ]:      45243 :         if (m_opts.fast_prune) {
     843                 :          0 :             max_blockfile_size = 0x10000; // 64kiB
     844         [ #  # ]:          0 :             if (nAddSize >= max_blockfile_size) {
     845                 :            :                 // dynamically adjust the blockfile size to be larger than the added size
     846                 :          0 :                 max_blockfile_size = nAddSize + 1;
     847                 :          0 :             }
     848                 :          0 :         }
     849         [ +  - ]:      45243 :         assert(nAddSize < max_blockfile_size);
     850                 :            : 
     851         [ -  + ]:      45243 :         while (m_blockfile_info[nFile].nSize + nAddSize >= max_blockfile_size) {
     852                 :            :             // when the undo file is keeping up with the block file, we want to flush it explicitly
     853                 :            :             // when it is lagging behind (more blocks arrive than are being connected), we let the
     854                 :            :             // undo block write case handle it
     855                 :          0 :             finalize_undo = (static_cast<int>(m_blockfile_info[nFile].nHeightLast) ==
     856   [ #  #  #  # ]:          0 :                     Assert(m_blockfile_cursors[chain_type])->undo_height);
     857                 :            : 
     858                 :            :             // Try the next unclaimed blockfile number
     859         [ #  # ]:          0 :             nFile = this->MaxBlockfileNum() + 1;
     860                 :            :             // Set to increment MaxBlockfileNum() for next iteration
     861         [ #  # ]:          0 :             m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
     862                 :            : 
     863         [ #  # ]:          0 :             if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
     864         [ #  # ]:          0 :                 m_blockfile_info.resize(nFile + 1);
     865                 :          0 :             }
     866                 :            :         }
     867                 :      45243 :         pos.nFile = nFile;
     868                 :      45243 :         pos.nPos = m_blockfile_info[nFile].nSize;
     869                 :      45243 :     }
     870                 :            : 
     871         [ -  + ]:      45243 :     if (nFile != last_blockfile) {
     872         [ #  # ]:          0 :         if (!fKnown) {
     873   [ #  #  #  #  :          0 :             LogPrint(BCLog::BLOCKSTORAGE, "Leaving block file %i: %s (onto %i) (height %i)\n",
          #  #  #  #  #  
                #  #  # ]
     874                 :            :                 last_blockfile, m_blockfile_info[last_blockfile].ToString(), nFile, nHeight);
     875                 :          0 :         }
     876                 :            : 
     877                 :            :         // Do not propagate the return code. The flush concerns a previous block
     878                 :            :         // and undo file that has already been written to. If a flush fails
     879                 :            :         // here, and we crash, there is no expected additional block data
     880                 :            :         // inconsistency arising from the flush failure here. However, the undo
     881                 :            :         // data may be inconsistent after a crash if the flush is called during
     882                 :            :         // a reindex. A flush error might also leave some of the data files
     883                 :            :         // untrimmed.
     884   [ #  #  #  # ]:          0 :         if (!FlushBlockFile(last_blockfile, !fKnown, finalize_undo)) {
     885   [ #  #  #  #  :          0 :             LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning,
          #  #  #  #  #  
                      # ]
     886                 :            :                           "Failed to flush previous block file %05i (finalize=%i, finalize_undo=%i) before opening new block file %05i\n",
     887                 :            :                           last_blockfile, !fKnown, finalize_undo, nFile);
     888                 :          0 :         }
     889                 :            :         // No undo data yet in the new file, so reset our undo-height tracking.
     890         [ #  # ]:          0 :         m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
     891                 :          0 :     }
     892                 :            : 
     893         [ +  - ]:      45243 :     m_blockfile_info[nFile].AddBlock(nHeight, nTime);
     894         [ -  + ]:      45243 :     if (fKnown) {
     895         [ #  # ]:          0 :         m_blockfile_info[nFile].nSize = std::max(pos.nPos + nAddSize, m_blockfile_info[nFile].nSize);
     896                 :          0 :     } else {
     897                 :      45243 :         m_blockfile_info[nFile].nSize += nAddSize;
     898                 :            :     }
     899                 :            : 
     900         [ +  - ]:      45243 :     if (!fKnown) {
     901                 :            :         bool out_of_space;
     902   [ +  -  +  - ]:      45243 :         size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
     903         [ -  + ]:      45243 :         if (out_of_space) {
     904   [ #  #  #  #  :          0 :             m_opts.notifications.fatalError("Disk space is too low!", _("Disk space is too low!"));
                   #  # ]
     905                 :          0 :             return false;
     906                 :            :         }
     907   [ +  +  +  -  :      45243 :         if (bytes_allocated != 0 && IsPruneMode()) {
                   +  - ]
     908                 :          0 :             m_check_for_pruning = true;
     909                 :          0 :         }
     910                 :      45243 :     }
     911                 :            : 
     912         [ +  - ]:      45243 :     m_dirty_fileinfo.insert(nFile);
     913                 :      45243 :     return true;
     914                 :      45243 : }
     915                 :            : 
     916                 :      40547 : bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
     917                 :            : {
     918                 :      40547 :     pos.nFile = nFile;
     919                 :            : 
     920                 :      40547 :     LOCK(cs_LastBlockFile);
     921                 :            : 
     922                 :      40547 :     pos.nPos = m_blockfile_info[nFile].nUndoSize;
     923                 :      40547 :     m_blockfile_info[nFile].nUndoSize += nAddSize;
     924         [ +  - ]:      40547 :     m_dirty_fileinfo.insert(nFile);
     925                 :            : 
     926                 :            :     bool out_of_space;
     927   [ +  -  +  - ]:      40547 :     size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
     928         [ -  + ]:      40547 :     if (out_of_space) {
     929   [ #  #  #  #  :          0 :         return FatalError(m_opts.notifications, state, "Disk space is too low!", _("Disk space is too low!"));
                   #  # ]
     930                 :            :     }
     931   [ +  +  +  -  :      40547 :     if (bytes_allocated != 0 && IsPruneMode()) {
                   +  - ]
     932                 :          0 :         m_check_for_pruning = true;
     933                 :          0 :     }
     934                 :            : 
     935                 :      40547 :     return true;
     936                 :      40547 : }
     937                 :            : 
     938                 :      45243 : bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
     939                 :            : {
     940                 :            :     // Open history file to append
     941                 :      45243 :     CAutoFile fileout{OpenBlockFile(pos)};
     942   [ +  -  -  + ]:      45243 :     if (fileout.IsNull()) {
     943         [ #  # ]:          0 :         return error("WriteBlockToDisk: OpenBlockFile failed");
     944                 :            :     }
     945                 :            : 
     946                 :            :     // Write index header
     947   [ +  -  +  - ]:      45243 :     unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
     948   [ +  -  +  -  :      45243 :     fileout << GetParams().MessageStart() << nSize;
             +  -  +  - ]
     949                 :            : 
     950                 :            :     // Write block
     951   [ +  -  +  - ]:      45243 :     long fileOutPos = ftell(fileout.Get());
     952         [ -  + ]:      45243 :     if (fileOutPos < 0) {
     953         [ #  # ]:          0 :         return error("WriteBlockToDisk: ftell failed");
     954                 :            :     }
     955                 :      45243 :     pos.nPos = (unsigned int)fileOutPos;
     956         [ +  - ]:      45243 :     fileout << block;
     957                 :            : 
     958                 :      45243 :     return true;
     959                 :      45243 : }
     960                 :            : 
     961                 :      40547 : bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
     962                 :            : {
     963                 :      40547 :     AssertLockHeld(::cs_main);
     964                 :      40547 :     const BlockfileType type = BlockfileTypeForHeight(block.nHeight);
     965                 :      81094 :     auto& cursor = *Assert(WITH_LOCK(cs_LastBlockFile, return m_blockfile_cursors[type]));
     966                 :            : 
     967                 :            :     // Write undo information to disk
     968         [ -  + ]:      40547 :     if (block.GetUndoPos().IsNull()) {
     969                 :      40547 :         FlatFilePos _pos;
     970         [ +  - ]:      40547 :         if (!FindUndoPos(state, block.nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
     971                 :          0 :             return error("ConnectBlock(): FindUndoPos failed");
     972                 :            :         }
     973         [ -  + ]:      40547 :         if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash())) {
     974   [ #  #  #  # ]:          0 :             return FatalError(m_opts.notifications, state, "Failed to write undo data");
     975                 :            :         }
     976                 :            :         // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
     977                 :            :         // we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
     978                 :            :         // in the block file info as below; note that this does not catch the case where the undo writes are keeping up
     979                 :            :         // with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
     980                 :            :         // the FindBlockPos function
     981   [ -  +  #  # ]:      40547 :         if (_pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
     982                 :            :             // Do not propagate the return code, a failed flush here should not
     983                 :            :             // be an indication for a failed write. If it were propagated here,
     984                 :            :             // the caller would assume the undo data not to be written, when in
     985                 :            :             // fact it is. Note though, that a failed flush might leave the data
     986                 :            :             // file untrimmed.
     987         [ #  # ]:          0 :             if (!FlushUndoFile(_pos.nFile, true)) {
     988   [ #  #  #  #  :          0 :                 LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", _pos.nFile);
             #  #  #  # ]
     989                 :          0 :             }
     990   [ +  -  -  + ]:      40547 :         } else if (_pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) {
     991                 :      40547 :             cursor.undo_height = block.nHeight;
     992                 :      40547 :         }
     993                 :            :         // update nUndoPos in block index
     994                 :      40547 :         block.nUndoPos = _pos.nPos;
     995                 :      40547 :         block.nStatus |= BLOCK_HAVE_UNDO;
     996                 :      40547 :         m_dirty_blockindex.insert(&block);
     997                 :      40547 :     }
     998                 :            : 
     999                 :      40547 :     return true;
    1000                 :      40547 : }
    1001                 :            : 
    1002                 :      45210 : bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) const
    1003                 :            : {
    1004                 :      45210 :     block.SetNull();
    1005                 :            : 
    1006                 :            :     // Open history file to read
    1007                 :      45210 :     CAutoFile filein{OpenBlockFile(pos, true)};
    1008   [ +  -  +  + ]:      45210 :     if (filein.IsNull()) {
    1009   [ +  -  +  - ]:       8510 :         return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
    1010                 :            :     }
    1011                 :            : 
    1012                 :            :     // Read block
    1013                 :            :     try {
    1014         [ +  - ]:      36700 :         filein >> block;
    1015         [ #  # ]:      36700 :     } catch (const std::exception& e) {
    1016   [ #  #  #  # ]:          0 :         return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
    1017   [ #  #  #  # ]:          0 :     }
    1018                 :            : 
    1019                 :            :     // Check the header
    1020   [ +  -  +  -  :      36700 :     if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
             +  -  -  + ]
    1021   [ #  #  #  # ]:          0 :         return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
    1022                 :            :     }
    1023                 :            : 
    1024                 :            :     // Signet only: check block solution
    1025   [ +  -  -  +  :      36700 :     if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
          #  #  #  #  #  
                      # ]
    1026   [ #  #  #  # ]:          0 :         return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString());
    1027                 :            :     }
    1028                 :            : 
    1029                 :      36700 :     return true;
    1030                 :      45210 : }
    1031                 :            : 
    1032                 :      36700 : bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) const
    1033                 :            : {
    1034         [ +  - ]:      73400 :     const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
    1035                 :            : 
    1036         [ -  + ]:      36700 :     if (!ReadBlockFromDisk(block, block_pos)) {
    1037                 :          0 :         return false;
    1038                 :            :     }
    1039         [ -  + ]:      36700 :     if (block.GetHash() != index.GetBlockHash()) {
    1040         [ #  # ]:          0 :         return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
    1041         [ #  # ]:          0 :                      index.ToString(), block_pos.ToString());
    1042                 :            :     }
    1043                 :      36700 :     return true;
    1044                 :      36700 : }
    1045                 :            : 
    1046                 :       4506 : bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos) const
    1047                 :            : {
    1048                 :       4506 :     FlatFilePos hpos = pos;
    1049                 :       4506 :     hpos.nPos -= 8; // Seek back 8 bytes for meta header
    1050                 :       4506 :     CAutoFile filein{OpenBlockFile(hpos, true)};
    1051   [ +  -  -  + ]:       4506 :     if (filein.IsNull()) {
    1052   [ #  #  #  # ]:          0 :         return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
    1053                 :            :     }
    1054                 :            : 
    1055                 :            :     try {
    1056                 :            :         MessageStartChars blk_start;
    1057                 :            :         unsigned int blk_size;
    1058                 :            : 
    1059   [ +  -  +  - ]:       4506 :         filein >> blk_start >> blk_size;
    1060                 :            : 
    1061   [ +  -  +  -  :       4506 :         if (blk_start != GetParams().MessageStart()) {
             +  -  -  + ]
    1062   [ #  #  #  # ]:          0 :             return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
    1063   [ #  #  #  # ]:          0 :                          HexStr(blk_start),
    1064   [ #  #  #  #  :          0 :                          HexStr(GetParams().MessageStart()));
             #  #  #  # ]
    1065                 :            :         }
    1066                 :            : 
    1067         [ -  + ]:       4506 :         if (blk_size > MAX_SIZE) {
    1068   [ #  #  #  # ]:          0 :             return error("%s: Block data is larger than maximum deserialization size for %s: %s versus %s", __func__, pos.ToString(),
    1069                 :            :                          blk_size, MAX_SIZE);
    1070                 :            :         }
    1071                 :            : 
    1072         [ +  - ]:       4506 :         block.resize(blk_size); // Zeroing of memory is intentional here
    1073         [ +  - ]:       4506 :         filein.read(MakeWritableByteSpan(block));
    1074         [ #  # ]:       4506 :     } catch (const std::exception& e) {
    1075   [ #  #  #  # ]:          0 :         return error("%s: Read from block file failed: %s for %s", __func__, e.what(), pos.ToString());
    1076   [ #  #  #  # ]:          0 :     }
    1077                 :            : 
    1078                 :       4506 :     return true;
    1079                 :       4506 : }
    1080                 :            : 
    1081                 :      45243 : FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
    1082                 :            : {
    1083                 :      45243 :     unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
    1084                 :      45243 :     FlatFilePos blockPos;
    1085                 :      45243 :     const auto position_known {dbp != nullptr};
    1086         [ -  + ]:      45243 :     if (position_known) {
    1087                 :          0 :         blockPos = *dbp;
    1088                 :          0 :     } else {
    1089                 :            :         // when known, blockPos.nPos points at the offset of the block data in the blk file. that already accounts for
    1090                 :            :         // the serialization header present in the file (the 4 magic message start bytes + the 4 length bytes = 8 bytes = BLOCK_SERIALIZATION_HEADER_SIZE).
    1091                 :            :         // we add BLOCK_SERIALIZATION_HEADER_SIZE only for new blocks since they will have the serialization header added when written to disk.
    1092                 :      45243 :         nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
    1093                 :            :     }
    1094         [ -  + ]:      45243 :     if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime(), position_known)) {
    1095                 :          0 :         error("%s: FindBlockPos failed", __func__);
    1096                 :          0 :         return FlatFilePos();
    1097                 :            :     }
    1098         [ -  + ]:      45243 :     if (!position_known) {
    1099         [ +  - ]:      45243 :         if (!WriteBlockToDisk(block, blockPos)) {
    1100   [ #  #  #  # ]:          0 :             m_opts.notifications.fatalError("Failed to write block");
    1101                 :          0 :             return FlatFilePos();
    1102                 :            :         }
    1103                 :      45243 :     }
    1104                 :      45243 :     return blockPos;
    1105                 :      45243 : }
    1106                 :            : 
    1107                 :            : class ImportingNow
    1108                 :            : {
    1109                 :            :     std::atomic<bool>& m_importing;
    1110                 :            : 
    1111                 :            : public:
    1112                 :          0 :     ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
    1113                 :            :     {
    1114         [ #  # ]:          0 :         assert(m_importing == false);
    1115                 :          0 :         m_importing = true;
    1116                 :          0 :     }
    1117                 :          0 :     ~ImportingNow()
    1118                 :            :     {
    1119         [ #  # ]:          0 :         assert(m_importing == true);
    1120                 :          0 :         m_importing = false;
    1121                 :          0 :     }
    1122                 :            : };
    1123                 :            : 
    1124                 :          0 : void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
    1125                 :            : {
    1126                 :          0 :     ScheduleBatchPriority();
    1127                 :            : 
    1128                 :            :     {
    1129                 :          0 :         ImportingNow imp{chainman.m_blockman.m_importing};
    1130                 :            : 
    1131                 :            :         // -reindex
    1132         [ #  # ]:          0 :         if (fReindex) {
    1133                 :          0 :             int nFile = 0;
    1134                 :            :             // Map of disk positions for blocks with unknown parent (only used for reindex);
    1135                 :            :             // parent hash -> child disk position, multiple children can have the same parent.
    1136                 :          0 :             std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
    1137                 :          0 :             while (true) {
    1138         [ #  # ]:          0 :                 FlatFilePos pos(nFile, 0);
    1139   [ #  #  #  #  :          0 :                 if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
                   #  # ]
    1140                 :          0 :                     break; // No block files left to reindex
    1141                 :            :                 }
    1142         [ #  # ]:          0 :                 CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
    1143   [ #  #  #  # ]:          0 :                 if (file.IsNull()) {
    1144                 :          0 :                     break; // This error is logged in OpenBlockFile
    1145                 :            :                 }
    1146   [ #  #  #  #  :          0 :                 LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
                   #  # ]
    1147         [ #  # ]:          0 :                 chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
    1148   [ #  #  #  # ]:          0 :                 if (chainman.m_interrupt) {
    1149   [ #  #  #  #  :          0 :                     LogPrintf("Interrupt requested. Exit %s\n", __func__);
                   #  # ]
    1150                 :          0 :                     return;
    1151                 :            :                 }
    1152                 :          0 :                 nFile++;
    1153      [ #  #  # ]:          0 :             }
    1154   [ #  #  #  #  :          0 :             WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
                   #  # ]
    1155                 :          0 :             fReindex = false;
    1156   [ #  #  #  #  :          0 :             LogPrintf("Reindexing finished\n");
                   #  # ]
    1157                 :            :             // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
    1158   [ #  #  #  # ]:          0 :             chainman.ActiveChainstate().LoadGenesisBlock();
    1159         [ #  # ]:          0 :         }
    1160                 :            : 
    1161                 :            :         // -loadblock=
    1162         [ #  # ]:          0 :         for (const fs::path& path : vImportFiles) {
    1163   [ #  #  #  # ]:          0 :             CAutoFile file{fsbridge::fopen(path, "rb"), CLIENT_VERSION};
    1164   [ #  #  #  # ]:          0 :             if (!file.IsNull()) {
    1165   [ #  #  #  #  :          0 :                 LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
             #  #  #  # ]
    1166         [ #  # ]:          0 :                 chainman.LoadExternalBlockFile(file);
    1167   [ #  #  #  # ]:          0 :                 if (chainman.m_interrupt) {
    1168   [ #  #  #  #  :          0 :                     LogPrintf("Interrupt requested. Exit %s\n", __func__);
                   #  # ]
    1169                 :          0 :                     return;
    1170                 :            :                 }
    1171                 :          0 :             } else {
    1172   [ #  #  #  #  :          0 :                 LogPrintf("Warning: Could not open blocks file %s\n", fs::PathToString(path));
             #  #  #  # ]
    1173                 :            :             }
    1174         [ #  # ]:          0 :         }
    1175                 :            : 
    1176                 :            :         // scan for better chains in the block chain database, that are not yet connected in the active best chain
    1177                 :            : 
    1178                 :            :         // We can't hold cs_main during ActivateBestChain even though we're accessing
    1179                 :            :         // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
    1180                 :            :         // the relevant pointers before the ABC call.
    1181   [ #  #  #  #  :          0 :         for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
          #  #  #  #  #  
                      # ]
    1182                 :          0 :             BlockValidationState state;
    1183   [ #  #  #  # ]:          0 :             if (!chainstate->ActivateBestChain(state, nullptr)) {
    1184   [ #  #  #  #  :          0 :                 chainman.GetNotifications().fatalError(strprintf("Failed to connect best block (%s)", state.ToString()));
             #  #  #  # ]
    1185                 :          0 :                 return;
    1186                 :            :             }
    1187         [ #  # ]:          0 :         }
    1188         [ #  # ]:          0 :     } // End scope of ImportingNow
    1189                 :          0 : }
    1190                 :            : 
    1191                 :          0 : std::ostream& operator<<(std::ostream& os, const BlockfileType& type) {
    1192      [ #  #  # ]:          0 :     switch(type) {
    1193                 :          0 :         case BlockfileType::NORMAL: os << "normal"; break;
    1194                 :          0 :         case BlockfileType::ASSUMED: os << "assumed"; break;
    1195                 :          0 :         default: os.setstate(std::ios_base::failbit);
    1196                 :          0 :     }
    1197                 :          0 :     return os;
    1198                 :            : }
    1199                 :            : 
    1200                 :          0 : std::ostream& operator<<(std::ostream& os, const BlockfileCursor& cursor) {
    1201         [ #  # ]:          0 :     os << strprintf("BlockfileCursor(file_num=%d, undo_height=%d)", cursor.file_num, cursor.undo_height);
    1202                 :          0 :     return os;
    1203                 :          0 : }
    1204                 :            : } // namespace node

Generated by: LCOV version 1.14