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: 2025-12-25 05:18:09 Functions: 100.0 % 60 60
Branches: 59.4 % 1033 614

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

Generated by: LCOV version 2.0-1