LCOV - code coverage report
Current view: top level - src/node - blockstorage.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.3 % 681 615
Test Date: 2026-01-22 05:57:03 Functions: 100.0 % 60 60
Branches: 59.5 % 1033 615

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <node/blockstorage.h>
       6                 :             : 
       7                 :             : #include <arith_uint256.h>
       8                 :             : #include <chain.h>
       9                 :             : #include <consensus/params.h>
      10                 :             : #include <crypto/hex_base.h>
      11                 :             : #include <dbwrapper.h>
      12                 :             : #include <flatfile.h>
      13                 :             : #include <hash.h>
      14                 :             : #include <kernel/blockmanager_opts.h>
      15                 :             : #include <kernel/chainparams.h>
      16                 :             : #include <kernel/messagestartchars.h>
      17                 :             : #include <kernel/notifications_interface.h>
      18                 :             : #include <kernel/types.h>
      19                 :             : #include <logging.h>
      20                 :             : #include <pow.h>
      21                 :             : #include <primitives/block.h>
      22                 :             : #include <primitives/transaction.h>
      23                 :             : #include <random.h>
      24                 :             : #include <serialize.h>
      25                 :             : #include <signet.h>
      26                 :             : #include <streams.h>
      27                 :             : #include <sync.h>
      28                 :             : #include <tinyformat.h>
      29                 :             : #include <uint256.h>
      30                 :             : #include <undo.h>
      31                 :             : #include <util/check.h>
      32                 :             : #include <util/expected.h>
      33                 :             : #include <util/fs.h>
      34                 :             : #include <util/obfuscation.h>
      35                 :             : #include <util/overflow.h>
      36                 :             : #include <util/result.h>
      37                 :             : #include <util/signalinterrupt.h>
      38                 :             : #include <util/strencodings.h>
      39                 :             : #include <util/syserror.h>
      40                 :             : #include <util/time.h>
      41                 :             : #include <util/translation.h>
      42                 :             : #include <validation.h>
      43                 :             : 
      44                 :             : #include <cerrno>
      45                 :             : #include <compare>
      46                 :             : #include <cstddef>
      47                 :             : #include <cstdio>
      48                 :             : #include <exception>
      49                 :             : #include <map>
      50                 :             : #include <optional>
      51                 :             : #include <ostream>
      52                 :             : #include <span>
      53                 :             : #include <stdexcept>
      54                 :             : #include <system_error>
      55                 :             : #include <unordered_map>
      56                 :             : 
      57                 :             : namespace kernel {
      58                 :             : static constexpr uint8_t DB_BLOCK_FILES{'f'};
      59                 :             : static constexpr uint8_t DB_BLOCK_INDEX{'b'};
      60                 :             : static constexpr uint8_t DB_FLAG{'F'};
      61                 :             : static constexpr uint8_t DB_REINDEX_FLAG{'R'};
      62                 :             : static constexpr uint8_t DB_LAST_BLOCK{'l'};
      63                 :             : // Keys used in previous version that might still be found in the DB:
      64                 :             : // BlockTreeDB::DB_TXINDEX_BLOCK{'T'};
      65                 :             : // BlockTreeDB::DB_TXINDEX{'t'}
      66                 :             : // BlockTreeDB::ReadFlag("txindex")
      67                 :             : 
      68                 :        2603 : bool BlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
      69                 :             : {
      70                 :        2603 :     return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
      71                 :             : }
      72                 :             : 
      73                 :          37 : void BlockTreeDB::WriteReindexing(bool fReindexing)
      74                 :             : {
      75         [ +  + ]:          37 :     if (fReindexing) {
      76                 :          19 :         Write(DB_REINDEX_FLAG, uint8_t{'1'});
      77                 :             :     } else {
      78                 :          18 :         Erase(DB_REINDEX_FLAG);
      79                 :             :     }
      80                 :          37 : }
      81                 :             : 
      82                 :        1234 : void BlockTreeDB::ReadReindexing(bool& fReindexing)
      83                 :             : {
      84                 :        1234 :     fReindexing = Exists(DB_REINDEX_FLAG);
      85                 :        1234 : }
      86                 :             : 
      87                 :        1235 : bool BlockTreeDB::ReadLastBlockFile(int& nFile)
      88                 :             : {
      89                 :        1235 :     return Read(DB_LAST_BLOCK, nFile);
      90                 :             : }
      91                 :             : 
      92                 :        3508 : void BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo)
      93                 :             : {
      94                 :        3508 :     CDBBatch batch(*this);
      95   [ +  -  +  + ]:        5304 :     for (const auto& [file, info] : fileInfo) {
      96         [ +  - ]:        1796 :         batch.Write(std::make_pair(DB_BLOCK_FILES, file), *info);
      97                 :             :     }
      98         [ +  - ]:        3508 :     batch.Write(DB_LAST_BLOCK, nLastFile);
      99         [ +  + ]:      162512 :     for (const CBlockIndex* bi : blockinfo) {
     100         [ +  - ]:      159004 :         batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
     101                 :             :     }
     102         [ +  - ]:        3508 :     WriteBatch(batch, true);
     103                 :        3508 : }
     104                 :             : 
     105                 :          17 : void BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
     106                 :             : {
     107   [ -  +  -  +  :          34 :     Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
                   +  - ]
     108                 :          17 : }
     109                 :             : 
     110                 :        1234 : bool BlockTreeDB::ReadFlag(const std::string& name, bool& fValue)
     111                 :             : {
     112                 :        1234 :     uint8_t ch;
     113   [ -  +  +  -  :        2468 :     if (!Read(std::make_pair(DB_FLAG, name), ch)) {
                   +  + ]
     114                 :             :         return false;
     115                 :             :     }
     116                 :          16 :     fValue = ch == uint8_t{'1'};
     117                 :          16 :     return true;
     118                 :             : }
     119                 :             : 
     120                 :        1239 : bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
     121                 :             : {
     122                 :        1239 :     AssertLockHeld(::cs_main);
     123         [ +  - ]:        1239 :     std::unique_ptr<CDBIterator> pcursor(NewIterator());
     124         [ +  - ]:        1239 :     pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
     125                 :             : 
     126                 :             :     // Load m_block_index
     127   [ +  -  +  + ]:      169264 :     while (pcursor->Valid()) {
     128   [ +  -  +  + ]:      168796 :         if (interrupt) return false;
     129                 :      168794 :         std::pair<uint8_t, uint256> key;
     130   [ +  -  +  +  :      168794 :         if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
                   +  - ]
     131                 :      168025 :             CDiskBlockIndex diskindex;
     132   [ +  -  +  - ]:      168025 :             if (pcursor->GetValue(diskindex)) {
     133                 :             :                 // Construct block index object
     134   [ +  -  +  - ]:      168025 :                 CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
     135         [ +  - ]:      168025 :                 pindexNew->pprev          = insertBlockIndex(diskindex.hashPrev);
     136                 :      168025 :                 pindexNew->nHeight        = diskindex.nHeight;
     137                 :      168025 :                 pindexNew->nFile          = diskindex.nFile;
     138                 :      168025 :                 pindexNew->nDataPos       = diskindex.nDataPos;
     139                 :      168025 :                 pindexNew->nUndoPos       = diskindex.nUndoPos;
     140                 :      168025 :                 pindexNew->nVersion       = diskindex.nVersion;
     141                 :      168025 :                 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
     142                 :      168025 :                 pindexNew->nTime          = diskindex.nTime;
     143                 :      168025 :                 pindexNew->nBits          = diskindex.nBits;
     144                 :      168025 :                 pindexNew->nNonce         = diskindex.nNonce;
     145                 :      168025 :                 pindexNew->nStatus        = diskindex.nStatus;
     146                 :      168025 :                 pindexNew->nTx            = diskindex.nTx;
     147                 :             : 
     148   [ +  -  -  + ]:      168025 :                 if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
     149   [ #  #  #  # ]:           0 :                     LogError("%s: CheckProofOfWork failed: %s\n", __func__, pindexNew->ToString());
     150                 :           0 :                     return false;
     151                 :             :                 }
     152                 :             : 
     153         [ +  - ]:      168025 :                 pcursor->Next();
     154                 :             :             } else {
     155         [ #  # ]:           0 :                 LogError("%s: failed to read value\n", __func__);
     156                 :             :                 return false;
     157                 :             :             }
     158                 :             :         } else {
     159                 :             :             break;
     160                 :             :         }
     161                 :             :     }
     162                 :             : 
     163                 :             :     return true;
     164                 :        1239 : }
     165                 :             : 
     166                 :        1368 : std::string CBlockFileInfo::ToString() const
     167                 :             : {
     168   [ +  -  +  - ]:        2736 :     return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
     169                 :             : }
     170                 :             : } // namespace kernel
     171                 :             : 
     172                 :             : namespace node {
     173                 :             : 
     174                 :   798769653 : bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
     175                 :             : {
     176                 :             :     // First sort by most total work, ...
     177         [ +  + ]:   798769653 :     if (pa->nChainWork > pb->nChainWork) return false;
     178         [ +  + ]:   523035968 :     if (pa->nChainWork < pb->nChainWork) return true;
     179                 :             : 
     180                 :             :     // ... then by earliest activatable time, ...
     181         [ +  + ]:     2946934 :     if (pa->nSequenceId < pb->nSequenceId) return false;
     182         [ +  + ]:     2905692 :     if (pa->nSequenceId > pb->nSequenceId) return true;
     183                 :             : 
     184                 :             :     // Use pointer address as tie breaker (should only happen with blocks
     185                 :             :     // loaded from disk, as those share the same id: 0 for blocks on the
     186                 :             :     // best chain, 1 for all others).
     187         [ +  + ]:     2887416 :     if (pa < pb) return false;
     188         [ +  + ]:     2885646 :     if (pa > pb) return true;
     189                 :             : 
     190                 :             :     // Identical blocks.
     191                 :             :     return false;
     192                 :             : }
     193                 :             : 
     194                 :     3307784 : bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
     195                 :             : {
     196                 :     3307784 :     return pa->nHeight < pb->nHeight;
     197                 :             : }
     198                 :             : 
     199                 :        2470 : std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
     200                 :             : {
     201                 :        2470 :     AssertLockHeld(cs_main);
     202                 :        2470 :     std::vector<CBlockIndex*> rv;
     203         [ +  - ]:        2470 :     rv.reserve(m_block_index.size());
     204   [ +  +  +  - ]:      337788 :     for (auto& [_, block_index] : m_block_index) {
     205         [ +  - ]:      335318 :         rv.push_back(&block_index);
     206                 :             :     }
     207                 :        2470 :     return rv;
     208                 :           0 : }
     209                 :             : 
     210                 :      683371 : CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash)
     211                 :             : {
     212                 :      683371 :     AssertLockHeld(cs_main);
     213                 :      683371 :     BlockMap::iterator it = m_block_index.find(hash);
     214         [ +  + ]:      683371 :     return it == m_block_index.end() ? nullptr : &it->second;
     215                 :             : }
     216                 :             : 
     217                 :           6 : const CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
     218                 :             : {
     219                 :           6 :     AssertLockHeld(cs_main);
     220                 :           6 :     BlockMap::const_iterator it = m_block_index.find(hash);
     221         [ +  + ]:           6 :     return it == m_block_index.end() ? nullptr : &it->second;
     222                 :             : }
     223                 :             : 
     224                 :      147475 : CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockIndex*& best_header)
     225                 :             : {
     226                 :      147475 :     AssertLockHeld(cs_main);
     227                 :             : 
     228         [ +  + ]:      147475 :     auto [mi, inserted] = m_block_index.try_emplace(block.GetHash(), block);
     229         [ +  + ]:      147475 :     if (!inserted) {
     230                 :           3 :         return &mi->second;
     231                 :             :     }
     232                 :      147472 :     CBlockIndex* pindexNew = &(*mi).second;
     233                 :             : 
     234                 :             :     // We assign the sequence id to blocks only when the full data is available,
     235                 :             :     // to avoid miners withholding blocks but broadcasting headers, to get a
     236                 :             :     // competitive advantage.
     237                 :      147472 :     pindexNew->nSequenceId = SEQ_ID_INIT_FROM_DISK;
     238                 :             : 
     239                 :      147472 :     pindexNew->phashBlock = &((*mi).first);
     240                 :      147472 :     BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);
     241         [ +  + ]:      147472 :     if (miPrev != m_block_index.end()) {
     242                 :      146986 :         pindexNew->pprev = &(*miPrev).second;
     243                 :      146986 :         pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
     244                 :      146986 :         pindexNew->BuildSkip();
     245                 :             :     }
     246   [ +  +  +  + ]:      147472 :     pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
     247         [ +  + ]:      147472 :     pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
     248                 :      147472 :     pindexNew->RaiseValidity(BLOCK_VALID_TREE);
     249   [ +  +  +  + ]:      147472 :     if (best_header == nullptr || best_header->nChainWork < pindexNew->nChainWork) {
     250                 :      126584 :         best_header = pindexNew;
     251                 :             :     }
     252                 :             : 
     253                 :      147472 :     m_dirty_blockindex.insert(pindexNew);
     254                 :             : 
     255                 :      147472 :     return pindexNew;
     256                 :             : }
     257                 :             : 
     258                 :          75 : void BlockManager::PruneOneBlockFile(const int fileNumber)
     259                 :             : {
     260                 :          75 :     AssertLockHeld(cs_main);
     261                 :          75 :     LOCK(cs_LastBlockFile);
     262                 :             : 
     263   [ +  +  +  + ]:      129906 :     for (auto& entry : m_block_index) {
     264                 :      129831 :         CBlockIndex* pindex = &entry.second;
     265         [ +  + ]:      129831 :         if (pindex->nFile == fileNumber) {
     266                 :       17923 :             pindex->nStatus &= ~BLOCK_HAVE_DATA;
     267                 :       17923 :             pindex->nStatus &= ~BLOCK_HAVE_UNDO;
     268                 :       17923 :             pindex->nFile = 0;
     269                 :       17923 :             pindex->nDataPos = 0;
     270                 :       17923 :             pindex->nUndoPos = 0;
     271         [ +  - ]:       17923 :             m_dirty_blockindex.insert(pindex);
     272                 :             : 
     273                 :             :             // Prune from m_blocks_unlinked -- any block we prune would have
     274                 :             :             // to be downloaded again in order to consider its chain, at which
     275                 :             :             // point it would be considered as a candidate for
     276                 :             :             // m_blocks_unlinked or setBlockIndexCandidates.
     277                 :       17923 :             auto range = m_blocks_unlinked.equal_range(pindex->pprev);
     278         [ -  + ]:       17923 :             while (range.first != range.second) {
     279                 :           0 :                 std::multimap<CBlockIndex*, CBlockIndex*>::iterator _it = range.first;
     280                 :           0 :                 range.first++;
     281         [ #  # ]:           0 :                 if (_it->second == pindex) {
     282                 :           0 :                     m_blocks_unlinked.erase(_it);
     283                 :             :                 }
     284                 :             :             }
     285                 :             :         }
     286                 :             :     }
     287                 :             : 
     288         [ +  - ]:          75 :     m_blockfile_info.at(fileNumber) = CBlockFileInfo{};
     289         [ +  - ]:          75 :     m_dirty_fileinfo.insert(fileNumber);
     290                 :          75 : }
     291                 :             : 
     292                 :          30 : void BlockManager::FindFilesToPruneManual(
     293                 :             :     std::set<int>& setFilesToPrune,
     294                 :             :     int nManualPruneHeight,
     295                 :             :     const Chainstate& chain)
     296                 :             : {
     297   [ +  -  -  + ]:          30 :     assert(IsPruneMode() && nManualPruneHeight > 0);
     298                 :             : 
     299         [ +  - ]:          30 :     LOCK2(cs_main, cs_LastBlockFile);
     300   [ -  +  -  + ]:          30 :     if (chain.m_chain.Height() < 0) {
     301         [ #  # ]:           0 :         return;
     302                 :             :     }
     303                 :             : 
     304         [ +  - ]:          30 :     const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(nManualPruneHeight);
     305                 :             : 
     306                 :          30 :     int count = 0;
     307         [ +  + ]:         158 :     for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {
     308         [ +  + ]:         128 :         const auto& fileinfo = m_blockfile_info[fileNumber];
     309   [ +  +  +  +  :         128 :         if (fileinfo.nSize == 0 || fileinfo.nHeightLast > (unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
                   -  + ]
     310                 :          72 :             continue;
     311                 :             :         }
     312                 :             : 
     313         [ +  - ]:          56 :         PruneOneBlockFile(fileNumber);
     314         [ +  - ]:          56 :         setFilesToPrune.insert(fileNumber);
     315                 :          56 :         count++;
     316                 :             :     }
     317   [ +  -  +  - ]:          30 :     LogInfo("[%s] Prune (Manual): prune_height=%d removed %d blk/rev pairs",
     318                 :             :         chain.GetRole(), last_block_can_prune, count);
     319   [ -  -  +  - ]:          60 : }
     320                 :             : 
     321                 :         620 : void BlockManager::FindFilesToPrune(
     322                 :             :     std::set<int>& setFilesToPrune,
     323                 :             :     int last_prune,
     324                 :             :     const Chainstate& chain,
     325                 :             :     ChainstateManager& chainman)
     326                 :             : {
     327         [ +  - ]:         620 :     LOCK2(cs_main, cs_LastBlockFile);
     328                 :             :     // Compute `target` value with maximum size (in bytes) of blocks below the
     329                 :             :     // `last_prune` height which should be preserved and not pruned. The
     330                 :             :     // `target` value will be derived from the -prune preference provided by the
     331                 :             :     // user. If there is a historical chainstate being used to populate indexes
     332                 :             :     // and validate the snapshot, the target is divided by two so half of the
     333                 :             :     // block storage will be reserved for the historical chainstate, and the
     334                 :             :     // other half will be reserved for the most-work chainstate.
     335         [ +  + ]:         620 :     const int num_chainstates{chainman.HistoricalChainstate() ? 2 : 1};
     336         [ +  + ]:         620 :     const auto target = std::max(
     337         [ +  + ]:         620 :         MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget() / num_chainstates);
     338                 :         620 :     const uint64_t target_sync_height = chainman.m_best_header->nHeight;
     339                 :             : 
     340   [ -  +  +  +  :         620 :     if (chain.m_chain.Height() < 0 || target == 0) {
                   +  - ]
     341                 :             :         return;
     342                 :             :     }
     343         [ +  + ]:         601 :     if (static_cast<uint64_t>(chain.m_chain.Height()) <= chainman.GetParams().PruneAfterHeight()) {
     344                 :             :         return;
     345                 :             :     }
     346                 :             : 
     347   [ +  -  +  - ]:         491 :     const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(last_prune);
     348                 :             : 
     349         [ +  - ]:         491 :     uint64_t nCurrentUsage = CalculateCurrentUsage();
     350                 :             :     // We don't check to prune until after we've allocated new space for files
     351                 :             :     // So we should leave a buffer under our target to account for another allocation
     352                 :             :     // before the next pruning.
     353                 :         491 :     uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
     354                 :         491 :     uint64_t nBytesToPrune;
     355                 :         491 :     int count = 0;
     356                 :             : 
     357         [ +  + ]:         491 :     if (nCurrentUsage + nBuffer >= target) {
     358                 :             :         // On a prune event, the chainstate DB is flushed.
     359                 :             :         // To avoid excessive prune events negating the benefit of high dbcache
     360                 :             :         // values, we should not prune too rapidly.
     361                 :             :         // So when pruning in IBD, increase the buffer to avoid a re-prune too soon.
     362         [ -  + ]:          23 :         const auto chain_tip_height = chain.m_chain.Height();
     363   [ +  -  -  +  :          23 :         if (chainman.IsInitialBlockDownload() && target_sync_height > (uint64_t)chain_tip_height) {
                   -  - ]
     364                 :             :             // Since this is only relevant during IBD, we assume blocks are at least 1 MB on average
     365                 :           0 :             static constexpr uint64_t average_block_size = 1000000;  /* 1 MB */
     366                 :           0 :             const uint64_t remaining_blocks = target_sync_height - chain_tip_height;
     367                 :           0 :             nBuffer += average_block_size * remaining_blocks;
     368                 :             :         }
     369                 :             : 
     370         [ +  + ]:         169 :         for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {
     371         [ +  + ]:         158 :             const auto& fileinfo = m_blockfile_info[fileNumber];
     372                 :         158 :             nBytesToPrune = fileinfo.nSize + fileinfo.nUndoSize;
     373                 :             : 
     374         [ +  + ]:         158 :             if (fileinfo.nSize == 0) {
     375                 :          86 :                 continue;
     376                 :             :             }
     377                 :             : 
     378         [ +  + ]:          72 :             if (nCurrentUsage + nBuffer < target) { // are we below our target?
     379                 :             :                 break;
     380                 :             :             }
     381                 :             : 
     382                 :             :             // don't prune files that could have a block that's not within the allowable
     383                 :             :             // prune range for the chain being pruned.
     384   [ +  +  -  + ]:          60 :             if (fileinfo.nHeightLast > (unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
     385                 :          44 :                 continue;
     386                 :             :             }
     387                 :             : 
     388         [ +  - ]:          16 :             PruneOneBlockFile(fileNumber);
     389                 :             :             // Queue up the files for removal
     390         [ +  - ]:          16 :             setFilesToPrune.insert(fileNumber);
     391                 :          16 :             nCurrentUsage -= nBytesToPrune;
     392                 :          16 :             count++;
     393                 :             :         }
     394                 :             :     }
     395                 :             : 
     396   [ +  -  +  -  :         491 :     LogDebug(BCLog::PRUNE, "[%s] target=%dMiB actual=%dMiB diff=%dMiB min_height=%d max_prune_height=%d removed %d blk/rev pairs\n",
          +  -  +  -  +  
                      - ]
     397                 :             :              chain.GetRole(), target / 1024 / 1024, nCurrentUsage / 1024 / 1024,
     398                 :             :              (int64_t(target) - int64_t(nCurrentUsage)) / 1024 / 1024,
     399                 :             :              min_block_to_prune, last_block_can_prune, count);
     400   [ +  -  +  - ]:        1240 : }
     401                 :             : 
     402                 :       20501 : void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) {
     403                 :       20501 :     AssertLockHeld(::cs_main);
     404                 :       20501 :     m_prune_locks[name] = lock_info;
     405                 :       20501 : }
     406                 :             : 
     407                 :      336050 : CBlockIndex* BlockManager::InsertBlockIndex(const uint256& hash)
     408                 :             : {
     409                 :      336050 :     AssertLockHeld(cs_main);
     410                 :             : 
     411         [ +  + ]:      672100 :     if (hash.IsNull()) {
     412                 :             :         return nullptr;
     413                 :             :     }
     414                 :             : 
     415         [ +  + ]:      335280 :     const auto [mi, inserted]{m_block_index.try_emplace(hash)};
     416         [ +  + ]:      335280 :     CBlockIndex* pindex = &(*mi).second;
     417         [ +  + ]:      335280 :     if (inserted) {
     418                 :      167059 :         pindex->phashBlock = &((*mi).first);
     419                 :             :     }
     420                 :             :     return pindex;
     421                 :             : }
     422                 :             : 
     423                 :        1239 : bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockhash)
     424                 :             : {
     425         [ +  - ]:        1239 :     if (!m_block_tree_db->LoadBlockIndexGuts(
     426   [ +  -  +  + ]:      338528 :             GetConsensus(), [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, m_interrupt)) {
     427                 :             :         return false;
     428                 :             :     }
     429                 :             : 
     430         [ +  + ]:        1237 :     if (snapshot_blockhash) {
     431                 :          12 :         const std::optional<AssumeutxoData> maybe_au_data = GetParams().AssumeutxoForBlockhash(*snapshot_blockhash);
     432         [ +  + ]:          12 :         if (!maybe_au_data) {
     433   [ +  -  +  - ]:           2 :             m_opts.notifications.fatalError(strprintf(_("Assumeutxo data not found for the given blockhash '%s'."), snapshot_blockhash->ToString()));
     434                 :           1 :             return false;
     435                 :             :         }
     436                 :          11 :         const AssumeutxoData& au_data = *Assert(maybe_au_data);
     437                 :          11 :         m_snapshot_height = au_data.height;
     438                 :          11 :         CBlockIndex* base{LookupBlockIndex(*snapshot_blockhash)};
     439                 :             : 
     440                 :             :         // Since m_chain_tx_count (responsible for estimated progress) isn't persisted
     441                 :             :         // to disk, we must bootstrap the value for assumedvalid chainstates
     442                 :             :         // from the hardcoded assumeutxo chainparams.
     443                 :          11 :         base->m_chain_tx_count = au_data.m_chain_tx_count;
     444         [ +  - ]:          22 :         LogInfo("[snapshot] set m_chain_tx_count=%d for %s", au_data.m_chain_tx_count, snapshot_blockhash->ToString());
     445                 :             :     } else {
     446                 :             :         // If this isn't called with a snapshot blockhash, make sure the cached snapshot height
     447                 :             :         // is null. This is relevant during snapshot completion, when the blockman may be loaded
     448                 :             :         // with a height that then needs to be cleared after the snapshot is fully validated.
     449         [ +  + ]:        1225 :         m_snapshot_height.reset();
     450                 :             :     }
     451                 :             : 
     452         [ -  + ]:        1236 :     Assert(m_snapshot_height.has_value() == snapshot_blockhash.has_value());
     453                 :             : 
     454                 :             :     // Calculate nChainWork
     455                 :        1236 :     std::vector<CBlockIndex*> vSortedByHeight{GetAllBlockIndices()};
     456         [ +  - ]:        1236 :     std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
     457                 :             :               CBlockIndexHeightOnlyComparator());
     458                 :             : 
     459                 :        1236 :     CBlockIndex* previous_index{nullptr};
     460         [ +  + ]:      168925 :     for (CBlockIndex* pindex : vSortedByHeight) {
     461   [ +  -  +  - ]:      167690 :         if (m_interrupt) return false;
     462   [ +  +  +  + ]:      167690 :         if (previous_index && pindex->nHeight > previous_index->nHeight + 1) {
     463         [ +  - ]:           1 :             LogError("%s: block index is non-contiguous, index of height %d missing\n", __func__, previous_index->nHeight + 1);
     464                 :             :             return false;
     465                 :             :         }
     466                 :      167689 :         previous_index = pindex;
     467   [ +  -  +  +  :      167689 :         pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
                   +  + ]
     468   [ +  +  +  + ]:      167689 :         pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
     469                 :             : 
     470                 :             :         // We can link the chain of blocks for which we've received transactions at some point, or
     471                 :             :         // blocks that are assumed-valid on the basis of snapshot load (see
     472                 :             :         // PopulateAndValidateSnapshot()).
     473                 :             :         // Pruned nodes may have deleted the block.
     474         [ +  + ]:      167689 :         if (pindex->nTx > 0) {
     475         [ +  + ]:      165985 :             if (pindex->pprev) {
     476   [ +  +  +  +  :      165220 :                 if (m_snapshot_height && pindex->nHeight == *m_snapshot_height &&
                   +  - ]
     477         [ +  - ]:           4 :                         pindex->GetBlockHash() == *snapshot_blockhash) {
     478                 :             :                     // Should have been set above; don't disturb it with code below.
     479         [ -  + ]:           4 :                     Assert(pindex->m_chain_tx_count > 0);
     480         [ +  + ]:      165212 :                 } else if (pindex->pprev->m_chain_tx_count > 0) {
     481                 :      165205 :                     pindex->m_chain_tx_count = pindex->pprev->m_chain_tx_count + pindex->nTx;
     482                 :             :                 } else {
     483                 :           7 :                     pindex->m_chain_tx_count = 0;
     484         [ +  - ]:           7 :                     m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
     485                 :             :                 }
     486                 :             :             } else {
     487                 :         769 :                 pindex->m_chain_tx_count = pindex->nTx;
     488                 :             :             }
     489                 :             :         }
     490   [ +  +  +  +  :      167689 :         if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
                   -  + ]
     491                 :           0 :             pindex->nStatus |= BLOCK_FAILED_CHILD;
     492         [ #  # ]:           0 :             m_dirty_blockindex.insert(pindex);
     493                 :             :         }
     494         [ +  + ]:      167689 :         if (pindex->pprev) {
     495         [ +  - ]:      166897 :             pindex->BuildSkip();
     496                 :             :         }
     497                 :             :     }
     498                 :             : 
     499                 :             :     return true;
     500                 :        1236 : }
     501                 :             : 
     502                 :        3508 : void BlockManager::WriteBlockIndexDB()
     503                 :             : {
     504                 :        3508 :     AssertLockHeld(::cs_main);
     505                 :        3508 :     std::vector<std::pair<int, const CBlockFileInfo*>> vFiles;
     506         [ +  - ]:        3508 :     vFiles.reserve(m_dirty_fileinfo.size());
     507         [ +  + ]:        5304 :     for (std::set<int>::iterator it = m_dirty_fileinfo.begin(); it != m_dirty_fileinfo.end();) {
     508         [ +  - ]:        1796 :         vFiles.emplace_back(*it, &m_blockfile_info[*it]);
     509                 :        1796 :         m_dirty_fileinfo.erase(it++);
     510                 :             :     }
     511                 :        3508 :     std::vector<const CBlockIndex*> vBlocks;
     512         [ +  - ]:        3508 :     vBlocks.reserve(m_dirty_blockindex.size());
     513         [ +  + ]:      162512 :     for (std::set<CBlockIndex*>::iterator it = m_dirty_blockindex.begin(); it != m_dirty_blockindex.end();) {
     514         [ +  - ]:      159004 :         vBlocks.push_back(*it);
     515                 :      159004 :         m_dirty_blockindex.erase(it++);
     516                 :             :     }
     517   [ +  -  +  - ]:        7016 :     int max_blockfile = WITH_LOCK(cs_LastBlockFile, return this->MaxBlockfileNum());
     518         [ +  - ]:        3508 :     m_block_tree_db->WriteBatchSync(vFiles, max_blockfile, vBlocks);
     519                 :        3508 : }
     520                 :             : 
     521                 :        1239 : bool BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_blockhash)
     522                 :             : {
     523         [ +  + ]:        1239 :     if (!LoadBlockIndex(snapshot_blockhash)) {
     524                 :             :         return false;
     525                 :             :     }
     526                 :        1235 :     int max_blockfile_num{0};
     527                 :             : 
     528                 :             :     // Load block file info
     529                 :        1235 :     m_block_tree_db->ReadLastBlockFile(max_blockfile_num);
     530                 :        1235 :     m_blockfile_info.resize(max_blockfile_num + 1);
     531                 :        1235 :     LogInfo("Loading block index db: last block file = %i", max_blockfile_num);
     532         [ +  + ]:        2603 :     for (int nFile = 0; nFile <= max_blockfile_num; nFile++) {
     533                 :        1368 :         m_block_tree_db->ReadBlockFileInfo(nFile, m_blockfile_info[nFile]);
     534                 :             :     }
     535         [ +  - ]:        1235 :     LogInfo("Loading block index db: last block file info: %s", m_blockfile_info[max_blockfile_num].ToString());
     536                 :        1235 :     for (int nFile = max_blockfile_num + 1; true; nFile++) {
     537                 :        1235 :         CBlockFileInfo info;
     538         [ -  + ]:        1235 :         if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
     539                 :           0 :             m_blockfile_info.push_back(info);
     540                 :             :         } else {
     541                 :             :             break;
     542                 :             :         }
     543                 :           0 :     }
     544                 :             : 
     545                 :             :     // Check presence of blk files
     546                 :        1235 :     LogInfo("Checking all blk files are present...");
     547                 :        1235 :     std::set<int> setBlkDataFiles;
     548   [ +  +  +  + ]:      168898 :     for (const auto& [_, block_index] : m_block_index) {
     549         [ +  + ]:      167663 :         if (block_index.nStatus & BLOCK_HAVE_DATA) {
     550         [ +  - ]:      151409 :             setBlkDataFiles.insert(block_index.nFile);
     551                 :             :         }
     552                 :             :     }
     553         [ +  + ]:        2075 :     for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
     554         [ +  - ]:         841 :         FlatFilePos pos(*it, 0);
     555   [ +  -  +  + ]:         841 :         if (OpenBlockFile(pos, /*fReadOnly=*/true).IsNull()) {
     556                 :             :             return false;
     557                 :             :         }
     558                 :             :     }
     559                 :             : 
     560                 :        1234 :     {
     561                 :             :         // Initialize the blockfile cursors.
     562         [ +  - ]:        1234 :         LOCK(cs_LastBlockFile);
     563   [ -  +  +  + ]:        2601 :         for (size_t i = 0; i < m_blockfile_info.size(); ++i) {
     564         [ +  - ]:        1367 :             const auto last_height_in_file = m_blockfile_info[i].nHeightLast;
     565   [ +  -  +  + ]:        2734 :             m_blockfile_cursors[BlockfileTypeForHeight(last_height_in_file)] = {static_cast<int>(i), 0};
     566                 :             :         }
     567                 :           0 :     }
     568                 :             : 
     569                 :             :     // Check whether we have ever pruned block & undo files
     570   [ +  -  +  - ]:        1234 :     m_block_tree_db->ReadFlag("prunedblockfiles", m_have_pruned);
     571         [ +  + ]:        1234 :     if (m_have_pruned) {
     572         [ +  - ]:          16 :         LogInfo("Loading block index db: Block files have previously been pruned");
     573                 :             :     }
     574                 :             : 
     575                 :             :     // Check whether we need to continue reindexing
     576                 :        1234 :     bool fReindexing = false;
     577         [ +  - ]:        1234 :     m_block_tree_db->ReadReindexing(fReindexing);
     578         [ +  + ]:        1234 :     if (fReindexing) m_blockfiles_indexed = false;
     579                 :             : 
     580                 :             :     return true;
     581                 :        1235 : }
     582                 :             : 
     583                 :        1237 : void BlockManager::ScanAndUnlinkAlreadyPrunedFiles()
     584                 :             : {
     585                 :        1237 :     AssertLockHeld(::cs_main);
     586         [ +  - ]:        2474 :     int max_blockfile = WITH_LOCK(cs_LastBlockFile, return this->MaxBlockfileNum());
     587         [ +  + ]:        1237 :     if (!m_have_pruned) {
     588                 :             :         return;
     589                 :             :     }
     590                 :             : 
     591                 :          18 :     std::set<int> block_files_to_prune;
     592         [ +  + ]:         111 :     for (int file_number = 0; file_number < max_blockfile; file_number++) {
     593         [ +  + ]:          93 :         if (m_blockfile_info[file_number].nSize == 0) {
     594         [ +  - ]:          63 :             block_files_to_prune.insert(file_number);
     595                 :             :         }
     596                 :             :     }
     597                 :             : 
     598         [ +  - ]:          18 :     UnlinkPrunedFiles(block_files_to_prune);
     599                 :          18 : }
     600                 :             : 
     601                 :         412 : bool BlockManager::IsBlockPruned(const CBlockIndex& block) const
     602                 :             : {
     603                 :         412 :     AssertLockHeld(::cs_main);
     604   [ +  +  +  -  :         412 :     return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
                   -  + ]
     605                 :             : }
     606                 :             : 
     607                 :         113 : const CBlockIndex& BlockManager::GetFirstBlock(const CBlockIndex& upper_block, uint32_t status_mask, const CBlockIndex* lower_block) const
     608                 :             : {
     609                 :         113 :     AssertLockHeld(::cs_main);
     610                 :         113 :     const CBlockIndex* last_block = &upper_block;
     611         [ -  + ]:         113 :     assert((last_block->nStatus & status_mask) == status_mask); // 'upper_block' must satisfy the status mask
     612   [ +  +  +  + ]:       41769 :     while (last_block->pprev && ((last_block->pprev->nStatus & status_mask) == status_mask)) {
     613         [ +  + ]:       41669 :         if (lower_block) {
     614                 :             :             // Return if we reached the lower_block
     615         [ +  + ]:       41520 :             if (last_block == lower_block) return *lower_block;
     616                 :             :             // if range was surpassed, means that 'lower_block' is not part of the 'upper_block' chain
     617                 :             :             // and so far this is not allowed.
     618         [ -  + ]:       41507 :             assert(last_block->nHeight >= lower_block->nHeight);
     619                 :             :         }
     620                 :             :         last_block = last_block->pprev;
     621                 :             :     }
     622         [ -  + ]:         100 :     assert(last_block != nullptr);
     623                 :             :     return *last_block;
     624                 :             : }
     625                 :             : 
     626                 :          39 : bool BlockManager::CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block)
     627                 :             : {
     628         [ +  - ]:          39 :     if (!(upper_block.nStatus & BLOCK_HAVE_DATA)) return false;
     629                 :          39 :     return &GetFirstBlock(upper_block, BLOCK_HAVE_DATA, &lower_block) == &lower_block;
     630                 :             : }
     631                 :             : 
     632                 :             : // If we're using -prune with -reindex, then delete block files that will be ignored by the
     633                 :             : // reindex.  Since reindexing works by starting at block file 0 and looping until a blockfile
     634                 :             : // is missing, do the same here to delete any later block files after a gap.  Also delete all
     635                 :             : // rev files since they'll be rewritten by the reindex anyway.  This ensures that m_blockfile_info
     636                 :             : // is in sync with what's actually on disk by the time we start downloading, so that pruning
     637                 :             : // works correctly.
     638                 :           5 : void BlockManager::CleanupBlockRevFiles() const
     639                 :             : {
     640         [ +  - ]:           5 :     std::map<std::string, fs::path> mapBlockFiles;
     641                 :             : 
     642                 :             :     // Glob all blk?????.dat and rev?????.dat files from the blocks directory.
     643                 :             :     // Remove the rev files immediately and insert the blk file paths into an
     644                 :             :     // ordered map keyed by block file index.
     645         [ +  - ]:           5 :     LogInfo("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune");
     646   [ +  -  +  +  :          49 :     for (fs::directory_iterator it(m_opts.blocks_dir); it != fs::directory_iterator(); it++) {
                   +  + ]
     647   [ +  -  -  + ]:         156 :         const std::string path = fs::PathToString(it->path().filename());
     648         [ +  - ]:          39 :         if (fs::is_regular_file(*it) &&
     649   [ +  +  +  + ]:          73 :             path.length() == 12 &&
     650         [ +  - ]:          24 :             path.ends_with(".dat"))
     651                 :             :         {
     652         [ +  + ]:          24 :             if (path.starts_with("blk")) {
     653   [ +  -  +  -  :          24 :                 mapBlockFiles[path.substr(3, 5)] = it->path();
                   +  - ]
     654         [ +  - ]:          12 :             } else if (path.starts_with("rev")) {
     655         [ +  - ]:          12 :                 remove(it->path());
     656                 :             :             }
     657                 :             :         }
     658         [ +  - ]:          39 :     }
     659                 :             : 
     660                 :             :     // Remove all block files that aren't part of a contiguous set starting at
     661                 :             :     // zero by walking the ordered map (keys are block file indices) by
     662                 :             :     // keeping a separate counter.  Once we hit a gap (or if 0 doesn't exist)
     663                 :             :     // start removing block files.
     664                 :           5 :     int nContigCounter = 0;
     665         [ +  + ]:          17 :     for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
     666   [ -  +  +  -  :          12 :         if (LocaleIndependentAtoi<int>(item.first) == nContigCounter) {
                   +  + ]
     667                 :           1 :             nContigCounter++;
     668                 :           1 :             continue;
     669                 :             :         }
     670         [ +  - ]:          11 :         remove(item.second);
     671                 :             :     }
     672                 :           5 : }
     673                 :             : 
     674                 :           3 : CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
     675                 :             : {
     676                 :           3 :     LOCK(cs_LastBlockFile);
     677                 :             : 
     678   [ +  -  +  - ]:           3 :     return &m_blockfile_info.at(n);
     679                 :           3 : }
     680                 :             : 
     681                 :       55027 : bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index) const
     682                 :             : {
     683         [ +  - ]:      110054 :     const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
     684                 :             : 
     685                 :             :     // Open history file to read
     686                 :       55027 :     AutoFile file{OpenUndoFile(pos, true)};
     687         [ +  + ]:       55027 :     if (file.IsNull()) {
     688   [ +  -  +  - ]:           8 :         LogError("OpenUndoFile failed for %s while reading block undo", pos.ToString());
     689                 :           8 :         return false;
     690                 :             :     }
     691         [ +  - ]:       55019 :     BufferedReader filein{std::move(file)};
     692                 :             : 
     693                 :       55019 :     try {
     694                 :             :         // Read block
     695         [ +  - ]:       55019 :         HashVerifier verifier{filein}; // Use HashVerifier, as reserializing may lose data, c.f. commit d3424243
     696                 :             : 
     697         [ +  - ]:       55019 :         verifier << index.pprev->GetBlockHash();
     698         [ +  + ]:       55019 :         verifier >> blockundo;
     699                 :             : 
     700                 :       55018 :         uint256 hashChecksum;
     701         [ +  - ]:       55018 :         filein >> hashChecksum;
     702                 :             : 
     703                 :             :         // Verify checksum
     704   [ +  -  -  + ]:       55018 :         if (hashChecksum != verifier.GetHash()) {
     705   [ #  #  #  # ]:           0 :             LogError("Checksum mismatch at %s while reading block undo", pos.ToString());
     706                 :           0 :             return false;
     707                 :             :         }
     708         [ -  + ]:           1 :     } catch (const std::exception& e) {
     709   [ +  -  +  - ]:           1 :         LogError("Deserialize or I/O error - %s at %s while reading block undo", e.what(), pos.ToString());
     710                 :           1 :         return false;
     711                 :           1 :     }
     712                 :             : 
     713                 :             :     return true;
     714                 :       55027 : }
     715                 :             : 
     716                 :        3613 : bool BlockManager::FlushUndoFile(int block_file, bool finalize)
     717                 :             : {
     718                 :        3613 :     FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
     719         [ -  + ]:        3613 :     if (!m_undo_file_seq.Flush(undo_pos_old, finalize)) {
     720         [ #  # ]:           0 :         m_opts.notifications.flushError(_("Flushing undo file to disk failed. This is likely the result of an I/O error."));
     721                 :           0 :         return false;
     722                 :             :     }
     723                 :             :     return true;
     724                 :             : }
     725                 :             : 
     726                 :        3612 : bool BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
     727                 :             : {
     728                 :        3612 :     bool success = true;
     729                 :        3612 :     LOCK(cs_LastBlockFile);
     730                 :             : 
     731   [ -  +  +  - ]:        3612 :     if (m_blockfile_info.size() < 1) {
     732                 :             :         // Return if we haven't loaded any blockfiles yet. This happens during
     733                 :             :         // chainstate init, when we call ChainstateManager::MaybeRebalanceCaches() (which
     734                 :             :         // then calls FlushStateToDisk()), resulting in a call to this function before we
     735                 :             :         // have populated `m_blockfile_info` via LoadBlockIndexDB().
     736                 :             :         return true;
     737                 :             :     }
     738         [ -  + ]:        3612 :     assert(static_cast<int>(m_blockfile_info.size()) > blockfile_num);
     739                 :             : 
     740         [ +  - ]:        3612 :     FlatFilePos block_pos_old(blockfile_num, m_blockfile_info[blockfile_num].nSize);
     741   [ +  -  -  + ]:        3612 :     if (!m_block_file_seq.Flush(block_pos_old, fFinalize)) {
     742   [ #  #  #  # ]:           0 :         m_opts.notifications.flushError(_("Flushing block file to disk failed. This is likely the result of an I/O error."));
     743                 :           0 :         success = false;
     744                 :             :     }
     745                 :             :     // we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
     746                 :             :     // e.g. during IBD or a sync after a node going offline
     747         [ +  - ]:        3612 :     if (!fFinalize || finalize_undo) {
     748   [ +  -  -  + ]:        3612 :         if (!FlushUndoFile(blockfile_num, finalize_undo)) {
     749                 :           0 :             success = false;
     750                 :             :         }
     751                 :             :     }
     752                 :             :     return success;
     753                 :        3612 : }
     754                 :             : 
     755                 :      271034 : BlockfileType BlockManager::BlockfileTypeForHeight(int height)
     756                 :             : {
     757         [ +  + ]:      271034 :     if (!m_snapshot_height) {
     758                 :             :         return BlockfileType::NORMAL;
     759                 :             :     }
     760         [ +  + ]:        7156 :     return (height >= *m_snapshot_height) ? BlockfileType::ASSUMED : BlockfileType::NORMAL;
     761                 :             : }
     762                 :             : 
     763                 :        3508 : bool BlockManager::FlushChainstateBlockFile(int tip_height)
     764                 :             : {
     765                 :        3508 :     LOCK(cs_LastBlockFile);
     766   [ +  -  +  + ]:        3508 :     auto& cursor = m_blockfile_cursors[BlockfileTypeForHeight(tip_height)];
     767                 :             :     // If the cursor does not exist, it means an assumeutxo snapshot is loaded,
     768                 :             :     // but no blocks past the snapshot height have been written yet, so there
     769                 :             :     // is no data associated with the chainstate, and it is safe not to flush.
     770         [ +  + ]:        3508 :     if (cursor) {
     771         [ +  - ]:        3479 :         return FlushBlockFile(cursor->file_num, /*fFinalize=*/false, /*finalize_undo=*/false);
     772                 :             :     }
     773                 :             :     // No need to log warnings in this case.
     774                 :             :     return true;
     775                 :        3508 : }
     776                 :             : 
     777                 :       18229 : uint64_t BlockManager::CalculateCurrentUsage()
     778                 :             : {
     779                 :       18229 :     LOCK(cs_LastBlockFile);
     780                 :             : 
     781                 :       18229 :     uint64_t retval = 0;
     782         [ +  + ]:       38804 :     for (const CBlockFileInfo& file : m_blockfile_info) {
     783                 :       20575 :         retval += file.nSize + file.nUndoSize;
     784                 :             :     }
     785         [ +  - ]:       18229 :     return retval;
     786                 :       18229 : }
     787                 :             : 
     788                 :          57 : void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
     789                 :             : {
     790                 :          57 :     std::error_code ec;
     791         [ +  + ]:         194 :     for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
     792                 :         137 :         FlatFilePos pos(*it, 0);
     793                 :         137 :         const bool removed_blockfile{fs::remove(m_block_file_seq.FileName(pos), ec)};
     794                 :         137 :         const bool removed_undofile{fs::remove(m_undo_file_seq.FileName(pos), ec)};
     795         [ +  + ]:         137 :         if (removed_blockfile || removed_undofile) {
     796         [ +  - ]:          75 :             LogDebug(BCLog::BLOCKSTORAGE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
     797                 :             :         }
     798                 :             :     }
     799                 :          57 : }
     800                 :             : 
     801                 :      308353 : AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
     802                 :             : {
     803                 :      308353 :     return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_obfuscation};
     804                 :             : }
     805                 :             : 
     806                 :             : /** Open an undo file (rev?????.dat) */
     807                 :      182673 : AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
     808                 :             : {
     809                 :      182673 :     return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_obfuscation};
     810                 :             : }
     811                 :             : 
     812                 :          37 : fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
     813                 :             : {
     814                 :          37 :     return m_block_file_seq.FileName(pos);
     815                 :             : }
     816                 :             : 
     817                 :      130180 : FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
     818                 :             : {
     819                 :      130180 :     LOCK(cs_LastBlockFile);
     820                 :             : 
     821         [ +  - ]:      130180 :     const BlockfileType chain_type = BlockfileTypeForHeight(nHeight);
     822                 :             : 
     823         [ +  + ]:      130180 :     if (!m_blockfile_cursors[chain_type]) {
     824                 :             :         // If a snapshot is loaded during runtime, we may not have initialized this cursor yet.
     825         [ -  + ]:          13 :         assert(chain_type == BlockfileType::ASSUMED);
     826                 :          13 :         const auto new_cursor = BlockfileCursor{this->MaxBlockfileNum() + 1};
     827         [ +  - ]:          13 :         m_blockfile_cursors[chain_type] = new_cursor;
     828   [ +  -  +  +  :          13 :         LogDebug(BCLog::BLOCKSTORAGE, "[%s] initializing blockfile cursor to %s\n", chain_type, new_cursor);
                   +  - ]
     829                 :             :     }
     830         [ -  + ]:      130180 :     const int last_blockfile = m_blockfile_cursors[chain_type]->file_num;
     831                 :             : 
     832                 :      130180 :     int nFile = last_blockfile;
     833   [ -  +  +  + ]:      130180 :     if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
     834         [ +  - ]:          20 :         m_blockfile_info.resize(nFile + 1);
     835                 :             :     }
     836                 :             : 
     837                 :      130180 :     bool finalize_undo = false;
     838                 :      130180 :     unsigned int max_blockfile_size{MAX_BLOCKFILE_SIZE};
     839                 :             :     // Use smaller blockfiles in test-only -fastprune mode - but avoid
     840                 :             :     // the possibility of having a block not fit into the block file.
     841         [ +  + ]:      130180 :     if (m_opts.fast_prune) {
     842                 :       19038 :         max_blockfile_size = 0x10000; // 64kiB
     843         [ +  + ]:       19038 :         if (nAddSize >= max_blockfile_size) {
     844                 :             :             // dynamically adjust the blockfile size to be larger than the added size
     845                 :           2 :             max_blockfile_size = nAddSize + 1;
     846                 :             :         }
     847                 :             :     }
     848         [ -  + ]:      130180 :     assert(nAddSize < max_blockfile_size);
     849                 :             : 
     850         [ +  + ]:      130313 :     while (m_blockfile_info[nFile].nSize + nAddSize >= max_blockfile_size) {
     851                 :             :         // when the undo file is keeping up with the block file, we want to flush it explicitly
     852                 :             :         // when it is lagging behind (more blocks arrive than are being connected), we let the
     853                 :             :         // undo block write case handle it
     854                 :         266 :         finalize_undo = (static_cast<int>(m_blockfile_info[nFile].nHeightLast) ==
     855   [ -  +  +  - ]:         133 :                          Assert(m_blockfile_cursors[chain_type])->undo_height);
     856                 :             : 
     857                 :             :         // Try the next unclaimed blockfile number
     858                 :         133 :         nFile = this->MaxBlockfileNum() + 1;
     859                 :             :         // Set to increment MaxBlockfileNum() for next iteration
     860         [ +  - ]:         133 :         m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
     861                 :             : 
     862   [ -  +  +  - ]:         133 :         if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
     863         [ +  - ]:         133 :             m_blockfile_info.resize(nFile + 1);
     864                 :             :         }
     865                 :             :     }
     866                 :      130180 :     FlatFilePos pos;
     867                 :      130180 :     pos.nFile = nFile;
     868                 :      130180 :     pos.nPos = m_blockfile_info[nFile].nSize;
     869                 :             : 
     870         [ +  + ]:      130180 :     if (nFile != last_blockfile) {
     871   [ +  -  +  -  :         266 :         LogDebug(BCLog::BLOCKSTORAGE, "Leaving block file %i: %s (onto %i) (height %i)\n",
             +  -  +  - ]
     872                 :             :                  last_blockfile, m_blockfile_info[last_blockfile].ToString(), nFile, nHeight);
     873                 :             : 
     874                 :             :         // Do not propagate the return code. The flush concerns a previous block
     875                 :             :         // and undo file that has already been written to. If a flush fails
     876                 :             :         // here, and we crash, there is no expected additional block data
     877                 :             :         // inconsistency arising from the flush failure here. However, the undo
     878                 :             :         // data may be inconsistent after a crash if the flush is called during
     879                 :             :         // a reindex. A flush error might also leave some of the data files
     880                 :             :         // untrimmed.
     881   [ +  -  -  + ]:         133 :         if (!FlushBlockFile(last_blockfile, /*fFinalize=*/true, finalize_undo)) {
     882         [ #  # ]:           0 :             LogWarning(
     883                 :             :                           "Failed to flush previous block file %05i (finalize=1, finalize_undo=%i) before opening new block file %05i\n",
     884                 :             :                           last_blockfile, finalize_undo, nFile);
     885                 :             :         }
     886                 :             :         // No undo data yet in the new file, so reset our undo-height tracking.
     887         [ +  - ]:         133 :         m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
     888                 :             :     }
     889                 :             : 
     890                 :      130180 :     m_blockfile_info[nFile].AddBlock(nHeight, nTime);
     891         [ +  - ]:      130180 :     m_blockfile_info[nFile].nSize += nAddSize;
     892                 :             : 
     893                 :      130180 :     bool out_of_space;
     894         [ +  - ]:      130180 :     size_t bytes_allocated = m_block_file_seq.Allocate(pos, nAddSize, out_of_space);
     895         [ -  + ]:      130180 :     if (out_of_space) {
     896   [ #  #  #  # ]:           0 :         m_opts.notifications.fatalError(_("Disk space is too low!"));
     897                 :           0 :         return {};
     898                 :             :     }
     899   [ +  +  +  + ]:      130180 :     if (bytes_allocated != 0 && IsPruneMode()) {
     900                 :         466 :         m_check_for_pruning = true;
     901                 :             :     }
     902                 :             : 
     903         [ +  - ]:      130180 :     m_dirty_fileinfo.insert(nFile);
     904                 :      130180 :     return pos;
     905                 :      130180 : }
     906                 :             : 
     907                 :        2236 : void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos)
     908                 :             : {
     909                 :        2236 :     LOCK(cs_LastBlockFile);
     910                 :             : 
     911                 :             :     // Update the cursor so it points to the last file.
     912         [ +  - ]:        2236 :     const BlockfileType chain_type{BlockfileTypeForHeight(nHeight)};
     913         [ +  - ]:        2236 :     auto& cursor{m_blockfile_cursors[chain_type]};
     914   [ +  -  +  + ]:        2236 :     if (!cursor || cursor->file_num < pos.nFile) {
     915         [ +  - ]:           1 :         m_blockfile_cursors[chain_type] = BlockfileCursor{pos.nFile};
     916                 :             :     }
     917                 :             : 
     918                 :             :     // Update the file information with the current block.
     919                 :        2236 :     const unsigned int added_size = ::GetSerializeSize(TX_WITH_WITNESS(block));
     920                 :        2236 :     const int nFile = pos.nFile;
     921   [ -  +  +  + ]:        2236 :     if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
     922         [ +  - ]:          16 :         m_blockfile_info.resize(nFile + 1);
     923                 :             :     }
     924                 :        2236 :     m_blockfile_info[nFile].AddBlock(nHeight, block.GetBlockTime());
     925         [ +  + ]:        2236 :     m_blockfile_info[nFile].nSize = std::max(pos.nPos + added_size, m_blockfile_info[nFile].nSize);
     926         [ +  - ]:        2236 :     m_dirty_fileinfo.insert(nFile);
     927                 :        2236 : }
     928                 :             : 
     929                 :      127646 : bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
     930                 :             : {
     931                 :      127646 :     pos.nFile = nFile;
     932                 :             : 
     933                 :      127646 :     LOCK(cs_LastBlockFile);
     934                 :             : 
     935         [ +  - ]:      127646 :     pos.nPos = m_blockfile_info[nFile].nUndoSize;
     936                 :      127646 :     m_blockfile_info[nFile].nUndoSize += nAddSize;
     937         [ +  - ]:      127646 :     m_dirty_fileinfo.insert(nFile);
     938                 :             : 
     939                 :      127646 :     bool out_of_space;
     940         [ +  - ]:      127646 :     size_t bytes_allocated = m_undo_file_seq.Allocate(pos, nAddSize, out_of_space);
     941         [ -  + ]:      127646 :     if (out_of_space) {
     942   [ #  #  #  # ]:           0 :         return FatalError(m_opts.notifications, state, _("Disk space is too low!"));
     943                 :             :     }
     944   [ +  +  +  + ]:      127646 :     if (bytes_allocated != 0 && IsPruneMode()) {
     945                 :         106 :         m_check_for_pruning = true;
     946                 :             :     }
     947                 :             : 
     948                 :             :     return true;
     949                 :      127646 : }
     950                 :             : 
     951                 :      133743 : bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
     952                 :             : {
     953                 :      133743 :     AssertLockHeld(::cs_main);
     954                 :      133743 :     const BlockfileType type = BlockfileTypeForHeight(block.nHeight);
     955   [ -  +  +  +  :      267486 :     auto& cursor = *Assert(WITH_LOCK(cs_LastBlockFile, return m_blockfile_cursors[type]));
                   +  - ]
     956                 :             : 
     957                 :             :     // Write undo information to disk
     958         [ +  + ]:      133743 :     if (block.GetUndoPos().IsNull()) {
     959                 :      127646 :         FlatFilePos pos;
     960                 :      127646 :         const auto blockundo_size{static_cast<uint32_t>(GetSerializeSize(blockundo))};
     961         [ -  + ]:      127646 :         if (!FindUndoPos(state, block.nFile, pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
     962         [ #  # ]:           0 :             LogError("FindUndoPos failed for %s while writing block undo", pos.ToString());
     963                 :           0 :             return false;
     964                 :             :         }
     965                 :             : 
     966                 :             :         // Open history file to append
     967                 :      127646 :         AutoFile file{OpenUndoFile(pos)};
     968         [ -  + ]:      127646 :         if (file.IsNull()) {
     969   [ #  #  #  # ]:           0 :             LogError("OpenUndoFile failed for %s while writing block undo", pos.ToString());
     970   [ #  #  #  # ]:           0 :             return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
     971                 :             :         }
     972                 :      127646 :         {
     973         [ +  - ]:      127646 :             BufferedWriter fileout{file};
     974                 :             : 
     975                 :             :             // Write index header
     976   [ +  -  +  - ]:      127646 :             fileout << GetParams().MessageStart() << blockundo_size;
     977                 :      127646 :             pos.nPos += STORAGE_HEADER_BYTES;
     978                 :      127646 :             {
     979                 :             :                 // Calculate checksum
     980         [ +  - ]:      127646 :                 HashWriter hasher{};
     981   [ +  -  +  - ]:      127646 :                 hasher << block.pprev->GetBlockHash() << blockundo;
     982                 :             :                 // Write undo data & checksum
     983   [ +  -  +  - ]:      255292 :                 fileout << blockundo << hasher.GetHash();
     984                 :             :             }
     985                 :             :             // BufferedWriter will flush pending data to file when fileout goes out of scope.
     986                 :           0 :         }
     987                 :             : 
     988                 :             :         // Make sure that the file is closed before we call `FlushUndoFile`.
     989   [ +  -  -  + ]:      255292 :         if (file.fclose() != 0) {
     990   [ #  #  #  #  :           0 :             LogError("Failed to close block undo file %s: %s", pos.ToString(), SysErrorString(errno));
                   #  # ]
     991   [ #  #  #  # ]:           0 :             return FatalError(m_opts.notifications, state, _("Failed to close block undo file."));
     992                 :             :         }
     993                 :             : 
     994                 :             :         // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
     995                 :             :         // we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
     996                 :             :         // in the block file info as below; note that this does not catch the case where the undo writes are keeping up
     997                 :             :         // with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
     998                 :             :         // the FindNextBlockPos function
     999   [ +  +  +  + ]:      127646 :         if (pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[pos.nFile].nHeightLast) {
    1000                 :             :             // Do not propagate the return code, a failed flush here should not
    1001                 :             :             // be an indication for a failed write. If it were propagated here,
    1002                 :             :             // the caller would assume the undo data not to be written, when in
    1003                 :             :             // fact it is. Note though, that a failed flush might leave the data
    1004                 :             :             // file untrimmed.
    1005   [ +  -  -  + ]:           1 :             if (!FlushUndoFile(pos.nFile, true)) {
    1006         [ #  # ]:           0 :                 LogWarning("Failed to flush undo file %05i\n", pos.nFile);
    1007                 :             :             }
    1008   [ +  +  +  + ]:      127645 :         } else if (pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) {
    1009                 :      118093 :             cursor.undo_height = block.nHeight;
    1010                 :             :         }
    1011                 :             :         // update nUndoPos in block index
    1012                 :      127646 :         block.nUndoPos = pos.nPos;
    1013                 :      127646 :         block.nStatus |= BLOCK_HAVE_UNDO;
    1014         [ +  - ]:      127646 :         m_dirty_blockindex.insert(&block);
    1015                 :      127646 :     }
    1016                 :             : 
    1017                 :             :     return true;
    1018                 :             : }
    1019                 :             : 
    1020                 :      137169 : bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const
    1021                 :             : {
    1022                 :      137169 :     block.SetNull();
    1023                 :             : 
    1024                 :             :     // Open history file to read
    1025                 :      137169 :     const auto block_data{ReadRawBlock(pos)};
    1026         [ +  + ]:      137169 :     if (!block_data) {
    1027                 :             :         return false;
    1028                 :             :     }
    1029                 :             : 
    1030                 :      137062 :     try {
    1031                 :             :         // Read block
    1032   [ -  +  +  - ]:      137062 :         SpanReader{*block_data} >> TX_WITH_WITNESS(block);
    1033         [ -  - ]:           0 :     } catch (const std::exception& e) {
    1034   [ -  -  -  - ]:           0 :         LogError("Deserialize or I/O error - %s at %s while reading block", e.what(), pos.ToString());
    1035                 :           0 :         return false;
    1036                 :           0 :     }
    1037                 :             : 
    1038         [ +  - ]:      137062 :     const auto block_hash{block.GetHash()};
    1039                 :             : 
    1040                 :             :     // Check the header
    1041   [ +  -  +  + ]:      137062 :     if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) {
    1042   [ +  -  +  - ]:           3 :         LogError("Errors in block header at %s while reading block", pos.ToString());
    1043                 :           3 :         return false;
    1044                 :             :     }
    1045                 :             : 
    1046                 :             :     // Signet only: check block solution
    1047   [ +  +  +  -  :      137059 :     if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
                   -  + ]
    1048   [ #  #  #  # ]:           0 :         LogError("Errors in block solution at %s while reading block", pos.ToString());
    1049                 :           0 :         return false;
    1050                 :             :     }
    1051                 :             : 
    1052   [ +  +  +  + ]:      137059 :     if (expected_hash && block_hash != *expected_hash) {
    1053   [ +  -  +  -  :           2 :         LogError("GetHash() doesn't match index at %s while reading block (%s != %s)",
             +  -  +  - ]
    1054                 :             :                  pos.ToString(), block_hash.ToString(), expected_hash->ToString());
    1055                 :           1 :         return false;
    1056                 :             :     }
    1057                 :             : 
    1058                 :             :     return true;
    1059                 :      137169 : }
    1060                 :             : 
    1061                 :      131677 : bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
    1062                 :             : {
    1063         [ +  - ]:      263354 :     const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
    1064                 :      131677 :     return ReadBlock(block, block_pos, index.GetBlockHash());
    1065                 :             : }
    1066                 :             : 
    1067                 :      177273 : BlockManager::ReadRawBlockResult BlockManager::ReadRawBlock(const FlatFilePos& pos, std::optional<std::pair<size_t, size_t>> block_part) const
    1068                 :             : {
    1069         [ +  + ]:      177273 :     if (pos.nPos < STORAGE_HEADER_BYTES) {
    1070                 :             :         // If nPos is less than STORAGE_HEADER_BYTES, we can't read the header that precedes the block data
    1071                 :             :         // This would cause an unsigned integer underflow when trying to position the file cursor
    1072                 :             :         // This can happen after pruning or default constructed positions
    1073         [ +  - ]:         103 :         LogError("Failed for %s while reading raw block storage header", pos.ToString());
    1074                 :         103 :         return util::Unexpected{ReadRawError::IO};
    1075                 :             :     }
    1076                 :      177170 :     AutoFile filein{OpenBlockFile({pos.nFile, pos.nPos - STORAGE_HEADER_BYTES}, /*fReadOnly=*/true)};
    1077         [ +  + ]:      177170 :     if (filein.IsNull()) {
    1078   [ +  -  +  - ]:           7 :         LogError("OpenBlockFile failed for %s while reading raw block", pos.ToString());
    1079                 :           7 :         return util::Unexpected{ReadRawError::IO};
    1080                 :             :     }
    1081                 :             : 
    1082                 :      177163 :     try {
    1083                 :      177163 :         MessageStartChars blk_start;
    1084                 :      177163 :         unsigned int blk_size;
    1085                 :             : 
    1086   [ +  -  +  - ]:      177163 :         filein >> blk_start >> blk_size;
    1087                 :             : 
    1088         [ +  + ]:      177163 :         if (blk_start != GetParams().MessageStart()) {
    1089   [ +  -  +  -  :           2 :             LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block",
             +  -  +  - ]
    1090                 :             :                 pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
    1091                 :           1 :             return util::Unexpected{ReadRawError::IO};
    1092                 :             :         }
    1093                 :             : 
    1094         [ -  + ]:      177162 :         if (blk_size > MAX_SIZE) {
    1095   [ #  #  #  # ]:           0 :             LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
    1096                 :             :                 pos.ToString(), blk_size, MAX_SIZE);
    1097                 :           0 :             return util::Unexpected{ReadRawError::IO};
    1098                 :             :         }
    1099                 :             : 
    1100         [ +  + ]:      177162 :         if (block_part) {
    1101         [ +  + ]:          39 :             const auto [offset, size]{*block_part};
    1102   [ +  +  +  + ]:          39 :             if (size == 0 || SaturatingAdd(offset, size) > blk_size) {
    1103                 :          24 :                 return util::Unexpected{ReadRawError::BadPartRange}; // Avoid logging - offset/size come from untrusted REST input
    1104                 :             :             }
    1105         [ +  - ]:          15 :             filein.seek(offset, SEEK_CUR);
    1106                 :          15 :             blk_size = size;
    1107                 :             :         }
    1108                 :             : 
    1109         [ +  - ]:      177138 :         std::vector<std::byte> data(blk_size); // Zeroing of memory is intentional here
    1110   [ -  +  +  - ]:      177138 :         filein.read(data);
    1111                 :      177138 :         return data;
    1112         [ -  - ]:      177138 :     } catch (const std::exception& e) {
    1113   [ -  -  -  - ]:           0 :         LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
    1114                 :           0 :         return util::Unexpected{ReadRawError::IO};
    1115                 :           0 :     }
    1116                 :      177170 : }
    1117                 :             : 
    1118                 :      130180 : FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
    1119                 :             : {
    1120                 :      130180 :     const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
    1121                 :      130180 :     FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
    1122         [ -  + ]:      130180 :     if (pos.IsNull()) {
    1123         [ #  # ]:           0 :         LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
    1124                 :           0 :         return FlatFilePos();
    1125                 :             :     }
    1126                 :      130180 :     AutoFile file{OpenBlockFile(pos, /*fReadOnly=*/false)};
    1127         [ -  + ]:      130180 :     if (file.IsNull()) {
    1128   [ #  #  #  # ]:           0 :         LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
    1129   [ #  #  #  # ]:           0 :         m_opts.notifications.fatalError(_("Failed to write block."));
    1130                 :           0 :         return FlatFilePos();
    1131                 :             :     }
    1132                 :      130180 :     {
    1133         [ +  - ]:      130180 :         BufferedWriter fileout{file};
    1134                 :             : 
    1135                 :             :         // Write index header
    1136   [ +  -  +  - ]:      130180 :         fileout << GetParams().MessageStart() << block_size;
    1137                 :      130180 :         pos.nPos += STORAGE_HEADER_BYTES;
    1138                 :             :         // Write block
    1139         [ +  - ]:      260360 :         fileout << TX_WITH_WITNESS(block);
    1140                 :           0 :     }
    1141                 :             : 
    1142   [ +  -  -  + ]:      260360 :     if (file.fclose() != 0) {
    1143   [ #  #  #  #  :           0 :         LogError("Failed to close block file %s: %s", pos.ToString(), SysErrorString(errno));
                   #  # ]
    1144   [ #  #  #  # ]:           0 :         m_opts.notifications.fatalError(_("Failed to close file when writing block."));
    1145                 :           0 :         return FlatFilePos();
    1146                 :             :     }
    1147                 :             : 
    1148                 :      130180 :     return pos;
    1149                 :      130180 : }
    1150                 :             : 
    1151                 :        1266 : static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
    1152                 :             : {
    1153                 :             :     // Bytes are serialized without length indicator, so this is also the exact
    1154                 :             :     // size of the XOR-key file.
    1155                 :        1266 :     std::array<std::byte, Obfuscation::KEY_SIZE> obfuscation{};
    1156                 :             : 
    1157                 :             :     // Consider this to be the first run if the blocksdir contains only hidden
    1158                 :             :     // files (those which start with a .). Checking for a fully-empty dir would
    1159                 :             :     // be too aggressive as a .lock file may have already been written.
    1160                 :        1266 :     bool first_run = true;
    1161   [ +  +  +  +  :        5574 :     for (const auto& entry : fs::directory_iterator(opts.blocks_dir)) {
          +  +  +  +  +  
                      + ]
    1162   [ +  -  -  + ]:        4308 :         const std::string path = fs::PathToString(entry.path().filename());
    1163   [ +  -  +  - ]:        1359 :         if (!entry.is_regular_file() || !path.starts_with('.')) {
    1164                 :         795 :             first_run = false;
    1165                 :         795 :             break;
    1166                 :             :         }
    1167   [ +  -  +  +  :        2343 :     }
             +  +  -  - ]
    1168                 :             : 
    1169   [ +  +  +  + ]:        1266 :     if (opts.use_xor && first_run) {
    1170                 :             :         // Only use random fresh key when the boolean option is set and on the
    1171                 :             :         // very first start of the program.
    1172                 :         471 :         FastRandomContext{}.fillrand(obfuscation);
    1173                 :             :     }
    1174                 :             : 
    1175         [ +  - ]:        2532 :     const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
    1176   [ +  -  +  + ]:        1266 :     if (fs::exists(xor_key_path)) {
    1177                 :             :         // A pre-existing xor key file has priority.
    1178   [ +  -  +  - ]:        1586 :         AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
    1179         [ +  - ]:         793 :         xor_key_file >> obfuscation;
    1180                 :         793 :     } else {
    1181                 :             :         // Create initial or missing xor key file
    1182                 :         473 :         AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
    1183                 :             : #ifdef __MINGW64__
    1184                 :             :             "wb" // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
    1185                 :             : #else
    1186                 :             :             "wbx"
    1187                 :             : #endif
    1188   [ +  -  +  - ]:         946 :         )};
    1189         [ +  - ]:         473 :         xor_key_file << obfuscation;
    1190   [ +  -  -  + ]:         946 :         if (xor_key_file.fclose() != 0) {
    1191                 :           0 :             throw std::runtime_error{strprintf("Error closing XOR key file %s: %s",
    1192         [ #  # ]:           0 :                                                fs::PathToString(xor_key_path),
    1193   [ #  #  #  # ]:           0 :                                                SysErrorString(errno))};
    1194                 :             :         }
    1195                 :         473 :     }
    1196                 :             :     // If the user disabled the key, it must be zero.
    1197   [ +  +  +  + ]:        1266 :     if (!opts.use_xor && obfuscation != decltype(obfuscation){}) {
    1198                 :           1 :         throw std::runtime_error{
    1199         [ +  - ]:           2 :             strprintf("The blocksdir XOR-key can not be disabled when a random key was already stored! "
    1200                 :             :                       "Stored key: '%s', stored path: '%s'.",
    1201         [ +  - ]:           2 :                       HexStr(obfuscation), fs::PathToString(xor_key_path)),
    1202   [ -  +  +  - ]:           3 :         };
    1203                 :             :     }
    1204   [ +  -  -  +  :        3795 :     LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(obfuscation));
                   +  - ]
    1205                 :        1265 :     return Obfuscation{obfuscation};
    1206                 :        1265 : }
    1207                 :             : 
    1208                 :        1266 : BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts)
    1209                 :        1266 :     : m_prune_mode{opts.prune_target > 0},
    1210                 :        1266 :       m_obfuscation{InitBlocksdirXorKey(opts)},
    1211         [ +  - ]:        1265 :       m_opts{std::move(opts)},
    1212   [ +  -  +  +  :        2497 :       m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
                   +  - ]
    1213   [ +  -  +  - ]:        1266 :       m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},
    1214   [ +  -  +  + ]:        2531 :       m_interrupt{interrupt}
    1215                 :             : {
    1216         [ +  + ]:        1265 :     m_block_tree_db = std::make_unique<BlockTreeDB>(m_opts.block_tree_db_params);
    1217                 :             : 
    1218         [ +  + ]:        1264 :     if (m_opts.block_tree_db_params.wipe_data) {
    1219         [ +  - ]:          19 :         m_block_tree_db->WriteReindexing(true);
    1220         [ +  + ]:          19 :         m_blockfiles_indexed = false;
    1221                 :             :         // If we're reindexing in prune mode, wipe away unusable block files and all undo data files
    1222         [ +  + ]:          19 :         if (m_prune_mode) {
    1223         [ +  - ]:           5 :             CleanupBlockRevFiles();
    1224                 :             :         }
    1225                 :             :     }
    1226                 :        1269 : }
    1227                 :             : 
    1228                 :             : class ImportingNow
    1229                 :             : {
    1230                 :             :     std::atomic<bool>& m_importing;
    1231                 :             : 
    1232                 :             : public:
    1233                 :        1037 :     ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
    1234                 :             :     {
    1235         [ -  + ]:        1037 :         assert(m_importing == false);
    1236                 :        1037 :         m_importing = true;
    1237                 :        1037 :     }
    1238                 :        1037 :     ~ImportingNow()
    1239                 :             :     {
    1240         [ -  + ]:        1037 :         assert(m_importing == true);
    1241                 :        1037 :         m_importing = false;
    1242                 :        1037 :     }
    1243                 :             : };
    1244                 :             : 
    1245                 :        1037 : void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths)
    1246                 :             : {
    1247                 :        1037 :     ImportingNow imp{chainman.m_blockman.m_importing};
    1248                 :             : 
    1249                 :             :     // -reindex
    1250         [ +  + ]:        1037 :     if (!chainman.m_blockman.m_blockfiles_indexed) {
    1251                 :             :         int total_files{0};
    1252   [ +  -  +  + ]:         111 :         while (fs::exists(chainman.m_blockman.GetBlockPosFilename(FlatFilePos(total_files, 0)))) {
    1253                 :          17 :             total_files++;
    1254                 :             :         }
    1255                 :             : 
    1256                 :             :         // Map of disk positions for blocks with unknown parent (only used for reindex);
    1257                 :             :         // parent hash -> child disk position, multiple children can have the same parent.
    1258                 :          20 :         std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
    1259                 :             : 
    1260         [ +  + ]:          35 :         for (int nFile{0}; nFile < total_files; ++nFile) {
    1261         [ +  - ]:          17 :             FlatFilePos pos(nFile, 0);
    1262         [ +  - ]:          17 :             AutoFile file{chainman.m_blockman.OpenBlockFile(pos, /*fReadOnly=*/true)};
    1263         [ +  - ]:          17 :             if (file.IsNull()) {
    1264                 :             :                 break; // This error is logged in OpenBlockFile
    1265                 :             :             }
    1266         [ +  - ]:          17 :             LogInfo("Reindexing block file blk%05u.dat (%d%% complete)...", (unsigned int)nFile, nFile * 100 / total_files);
    1267         [ +  - ]:          17 :             chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
    1268   [ +  -  +  + ]:          17 :             if (chainman.m_interrupt) {
    1269         [ +  - ]:           2 :                 LogInfo("Interrupt requested. Exit reindexing.");
    1270                 :           2 :                 return;
    1271                 :             :             }
    1272                 :          17 :         }
    1273   [ +  -  +  - ]:          54 :         WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
    1274         [ +  - ]:          18 :         chainman.m_blockman.m_blockfiles_indexed = true;
    1275         [ +  - ]:          18 :         LogInfo("Reindexing finished");
    1276                 :             :         // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
    1277   [ +  -  +  - ]:          18 :         chainman.ActiveChainstate().LoadGenesisBlock();
    1278                 :          20 :     }
    1279                 :             : 
    1280                 :             :     // -loadblock=
    1281         [ +  + ]:        1037 :     for (const fs::path& path : import_paths) {
    1282   [ +  -  +  - ]:           4 :         AutoFile file{fsbridge::fopen(path, "rb")};
    1283         [ +  - ]:           2 :         if (!file.IsNull()) {
    1284   [ -  +  +  - ]:           4 :             LogInfo("Importing blocks file %s...", fs::PathToString(path));
    1285         [ +  - ]:           2 :             chainman.LoadExternalBlockFile(file);
    1286   [ +  -  -  + ]:           2 :             if (chainman.m_interrupt) {
    1287         [ #  # ]:           0 :                 LogInfo("Interrupt requested. Exit block importing.");
    1288                 :           0 :                 return;
    1289                 :             :             }
    1290                 :             :         } else {
    1291   [ #  #  #  # ]:           0 :             LogWarning("Could not open blocks file %s", fs::PathToString(path));
    1292                 :             :         }
    1293                 :           2 :     }
    1294                 :             : 
    1295                 :             :     // scan for better chains in the block chain database, that are not yet connected in the active best chain
    1296   [ +  -  -  + ]:        1035 :     if (auto result = chainman.ActivateBestChains(); !result) {
    1297   [ #  #  #  # ]:           0 :         chainman.GetNotifications().fatalError(util::ErrorString(result));
    1298                 :           0 :     }
    1299                 :             :     // End scope of ImportingNow
    1300                 :        1037 : }
    1301                 :             : 
    1302                 :          12 : std::ostream& operator<<(std::ostream& os, const BlockfileType& type) {
    1303      [ -  +  - ]:          12 :     switch(type) {
    1304                 :           0 :         case BlockfileType::NORMAL: os << "normal"; break;
    1305                 :          12 :         case BlockfileType::ASSUMED: os << "assumed"; break;
    1306                 :           0 :         default: os.setstate(std::ios_base::failbit);
    1307                 :             :     }
    1308                 :          12 :     return os;
    1309                 :             : }
    1310                 :             : 
    1311                 :          12 : std::ostream& operator<<(std::ostream& os, const BlockfileCursor& cursor) {
    1312         [ -  + ]:          24 :     os << strprintf("BlockfileCursor(file_num=%d, undo_height=%d)", cursor.file_num, cursor.undo_height);
    1313                 :          12 :     return os;
    1314                 :             : }
    1315                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1