LCOV - code coverage report
Current view: top level - src/index - blockfilterindex.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 2.5 % 239 6
Test Date: 2025-06-16 04:13:27 Functions: 8.0 % 25 2
Branches: 0.8 % 262 2

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2018-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <map>
       6                 :             : 
       7                 :             : #include <clientversion.h>
       8                 :             : #include <common/args.h>
       9                 :             : #include <dbwrapper.h>
      10                 :             : #include <hash.h>
      11                 :             : #include <index/blockfilterindex.h>
      12                 :             : #include <logging.h>
      13                 :             : #include <node/blockstorage.h>
      14                 :             : #include <undo.h>
      15                 :             : #include <util/fs_helpers.h>
      16                 :             : 
      17                 :             : /* The index database stores three items for each block: the disk location of the encoded filter,
      18                 :             :  * its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by
      19                 :             :  * height, and those belonging to blocks that have been reorganized out of the active chain are
      20                 :             :  * indexed by block hash. This ensures that filter data for any block that becomes part of the
      21                 :             :  * active chain can always be retrieved, alleviating timing concerns.
      22                 :             :  *
      23                 :             :  * The filters themselves are stored in flat files and referenced by the LevelDB entries. This
      24                 :             :  * minimizes the amount of data written to LevelDB and keeps the database values constant size. The
      25                 :             :  * disk location of the next block filter to be written (represented as a FlatFilePos) is stored
      26                 :             :  * under the DB_FILTER_POS key.
      27                 :             :  *
      28                 :             :  * Keys for the height index have the type [DB_BLOCK_HEIGHT, uint32 (BE)]. The height is represented
      29                 :             :  * as big-endian so that sequential reads of filters by height are fast.
      30                 :             :  * Keys for the hash index have the type [DB_BLOCK_HASH, uint256].
      31                 :             :  */
      32                 :             : constexpr uint8_t DB_BLOCK_HASH{'s'};
      33                 :             : constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
      34                 :             : constexpr uint8_t DB_FILTER_POS{'P'};
      35                 :             : 
      36                 :             : constexpr unsigned int MAX_FLTR_FILE_SIZE = 0x1000000; // 16 MiB
      37                 :             : /** The pre-allocation chunk size for fltr?????.dat files */
      38                 :             : constexpr unsigned int FLTR_FILE_CHUNK_SIZE = 0x100000; // 1 MiB
      39                 :             : /** Maximum size of the cfheaders cache
      40                 :             :  *  We have a limit to prevent a bug in filling this cache
      41                 :             :  *  potentially turning into an OOM. At 2000 entries, this cache
      42                 :             :  *  is big enough for a 2,000,000 length block chain, which
      43                 :             :  *  we should be enough until ~2047. */
      44                 :             : constexpr size_t CF_HEADERS_CACHE_MAX_SZ{2000};
      45                 :             : 
      46                 :             : namespace {
      47                 :             : 
      48                 :           0 : struct DBVal {
      49                 :             :     uint256 hash;
      50                 :             :     uint256 header;
      51                 :             :     FlatFilePos pos;
      52                 :             : 
      53                 :           0 :     SERIALIZE_METHODS(DBVal, obj) { READWRITE(obj.hash, obj.header, obj.pos); }
      54                 :             : };
      55                 :             : 
      56                 :             : struct DBHeightKey {
      57                 :             :     int height;
      58                 :             : 
      59         [ #  # ]:           0 :     explicit DBHeightKey(int height_in) : height(height_in) {}
      60                 :             : 
      61                 :             :     template<typename Stream>
      62                 :           0 :     void Serialize(Stream& s) const
      63                 :             :     {
      64                 :           0 :         ser_writedata8(s, DB_BLOCK_HEIGHT);
      65                 :           0 :         ser_writedata32be(s, height);
      66                 :           0 :     }
      67                 :             : 
      68                 :             :     template<typename Stream>
      69                 :           0 :     void Unserialize(Stream& s)
      70                 :             :     {
      71                 :           0 :         const uint8_t prefix{ser_readdata8(s)};
      72         [ #  # ]:           0 :         if (prefix != DB_BLOCK_HEIGHT) {
      73         [ #  # ]:           0 :             throw std::ios_base::failure("Invalid format for block filter index DB height key");
      74                 :             :         }
      75                 :           0 :         height = ser_readdata32be(s);
      76                 :           0 :     }
      77                 :             : };
      78                 :             : 
      79                 :             : struct DBHashKey {
      80                 :             :     uint256 hash;
      81                 :             : 
      82         [ #  # ]:           0 :     explicit DBHashKey(const uint256& hash_in) : hash(hash_in) {}
      83                 :             : 
      84                 :           0 :     SERIALIZE_METHODS(DBHashKey, obj) {
      85                 :           0 :         uint8_t prefix{DB_BLOCK_HASH};
      86                 :           0 :         READWRITE(prefix);
      87         [ #  # ]:           0 :         if (prefix != DB_BLOCK_HASH) {
      88         [ #  # ]:           0 :             throw std::ios_base::failure("Invalid format for block filter index DB hash key");
      89                 :             :         }
      90                 :             : 
      91                 :           0 :         READWRITE(obj.hash);
      92                 :           0 :     }
      93                 :             : };
      94                 :             : 
      95                 :             : }; // namespace
      96                 :             : 
      97                 :             : static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
      98                 :             : 
      99                 :           0 : BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
     100                 :           0 :                                    size_t n_cache_size, bool f_memory, bool f_wipe)
     101         [ #  # ]:           0 :     : BaseIndex(std::move(chain), BlockFilterTypeName(filter_type) + " block filter index")
     102   [ #  #  #  # ]:           0 :     , m_filter_type(filter_type)
     103                 :             : {
     104         [ #  # ]:           0 :     const std::string& filter_name = BlockFilterTypeName(filter_type);
     105   [ #  #  #  # ]:           0 :     if (filter_name.empty()) throw std::invalid_argument("unknown filter_type");
     106                 :             : 
     107   [ #  #  #  #  :           0 :     fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / fs::u8path(filter_name);
             #  #  #  # ]
     108         [ #  # ]:           0 :     fs::create_directories(path);
     109                 :             : 
     110   [ #  #  #  #  :           0 :     m_db = std::make_unique<BaseIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
                   #  # ]
     111         [ #  # ]:           0 :     m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
     112                 :           0 : }
     113                 :             : 
     114                 :           0 : interfaces::Chain::NotifyOptions BlockFilterIndex::CustomOptions()
     115                 :             : {
     116                 :           0 :     interfaces::Chain::NotifyOptions options;
     117                 :           0 :     options.connect_undo_data = true;
     118                 :           0 :     return options;
     119                 :             : }
     120                 :             : 
     121                 :           0 : bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
     122                 :             : {
     123         [ #  # ]:           0 :     if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
     124                 :             :         // Check that the cause of the read failure is that the key does not exist. Any other errors
     125                 :             :         // indicate database corruption or a disk failure, and starting the index would cause
     126                 :             :         // further corruption.
     127         [ #  # ]:           0 :         if (m_db->Exists(DB_FILTER_POS)) {
     128                 :           0 :             LogError("%s: Cannot read current %s state; index may be corrupted\n",
     129                 :             :                          __func__, GetName());
     130                 :           0 :             return false;
     131                 :             :         }
     132                 :             : 
     133                 :             :         // If the DB_FILTER_POS is not set, then initialize to the first location.
     134                 :           0 :         m_next_filter_pos.nFile = 0;
     135                 :           0 :         m_next_filter_pos.nPos = 0;
     136                 :             :     }
     137                 :             : 
     138         [ #  # ]:           0 :     if (block) {
     139                 :           0 :         auto op_last_header = ReadFilterHeader(block->height, block->hash);
     140         [ #  # ]:           0 :         if (!op_last_header) {
     141                 :           0 :             LogError("Cannot read last block filter header; index may be corrupted\n");
     142                 :           0 :             return false;
     143                 :             :         }
     144                 :           0 :         m_last_header = *op_last_header;
     145                 :             :     }
     146                 :             : 
     147                 :             :     return true;
     148                 :             : }
     149                 :             : 
     150                 :           0 : bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
     151                 :             : {
     152                 :           0 :     const FlatFilePos& pos = m_next_filter_pos;
     153                 :             : 
     154                 :             :     // Flush current filter file to disk.
     155         [ #  # ]:           0 :     AutoFile file{m_filter_fileseq->Open(pos)};
     156         [ #  # ]:           0 :     if (file.IsNull()) {
     157         [ #  # ]:           0 :         LogError("%s: Failed to open filter file %d\n", __func__, pos.nFile);
     158                 :             :         return false;
     159                 :             :     }
     160   [ #  #  #  # ]:           0 :     if (!file.Commit()) {
     161         [ #  # ]:           0 :         LogError("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
     162                 :             :         return false;
     163                 :             :     }
     164                 :             : 
     165         [ #  # ]:           0 :     batch.Write(DB_FILTER_POS, pos);
     166                 :             :     return true;
     167                 :           0 : }
     168                 :             : 
     169                 :           0 : bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const
     170                 :             : {
     171         [ #  # ]:           0 :     AutoFile filein{m_filter_fileseq->Open(pos, true)};
     172         [ #  # ]:           0 :     if (filein.IsNull()) {
     173                 :             :         return false;
     174                 :             :     }
     175                 :             : 
     176                 :             :     // Check that the hash of the encoded_filter matches the one stored in the db.
     177                 :           0 :     uint256 block_hash;
     178                 :           0 :     std::vector<uint8_t> encoded_filter;
     179                 :           0 :     try {
     180   [ #  #  #  # ]:           0 :         filein >> block_hash >> encoded_filter;
     181   [ #  #  #  # ]:           0 :         if (Hash(encoded_filter) != hash) {
     182         [ #  # ]:           0 :             LogError("Checksum mismatch in filter decode.\n");
     183                 :             :             return false;
     184                 :             :         }
     185         [ #  # ]:           0 :         filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter), /*skip_decode_check=*/true);
     186                 :             :     }
     187         [ -  - ]:           0 :     catch (const std::exception& e) {
     188         [ -  - ]:           0 :         LogError("%s: Failed to deserialize block filter from disk: %s\n", __func__, e.what());
     189                 :           0 :         return false;
     190                 :           0 :     }
     191                 :             : 
     192                 :           0 :     return true;
     193                 :           0 : }
     194                 :             : 
     195                 :           0 : size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter)
     196                 :             : {
     197         [ #  # ]:           0 :     assert(filter.GetFilterType() == GetFilterType());
     198                 :             : 
     199                 :           0 :     size_t data_size =
     200                 :           0 :         GetSerializeSize(filter.GetBlockHash()) +
     201                 :           0 :         GetSerializeSize(filter.GetEncodedFilter());
     202                 :             : 
     203                 :             :     // If writing the filter would overflow the file, flush and move to the next one.
     204         [ #  # ]:           0 :     if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
     205         [ #  # ]:           0 :         AutoFile last_file{m_filter_fileseq->Open(pos)};
     206         [ #  # ]:           0 :         if (last_file.IsNull()) {
     207         [ #  # ]:           0 :             LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
     208                 :             :             return 0;
     209                 :             :         }
     210   [ #  #  #  # ]:           0 :         if (!last_file.Truncate(pos.nPos)) {
     211         [ #  # ]:           0 :             LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
     212                 :             :             return 0;
     213                 :             :         }
     214   [ #  #  #  # ]:           0 :         if (!last_file.Commit()) {
     215         [ #  # ]:           0 :             LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
     216                 :             :             return 0;
     217                 :             :         }
     218                 :             : 
     219                 :           0 :         pos.nFile++;
     220                 :           0 :         pos.nPos = 0;
     221                 :           0 :     }
     222                 :             : 
     223                 :             :     // Pre-allocate sufficient space for filter data.
     224                 :           0 :     bool out_of_space;
     225                 :           0 :     m_filter_fileseq->Allocate(pos, data_size, out_of_space);
     226         [ #  # ]:           0 :     if (out_of_space) {
     227                 :           0 :         LogPrintf("%s: out of disk space\n", __func__);
     228                 :           0 :         return 0;
     229                 :             :     }
     230                 :             : 
     231         [ #  # ]:           0 :     AutoFile fileout{m_filter_fileseq->Open(pos)};
     232         [ #  # ]:           0 :     if (fileout.IsNull()) {
     233         [ #  # ]:           0 :         LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
     234                 :             :         return 0;
     235                 :             :     }
     236                 :             : 
     237   [ #  #  #  # ]:           0 :     fileout << filter.GetBlockHash() << filter.GetEncodedFilter();
     238                 :             :     return data_size;
     239                 :           0 : }
     240                 :             : 
     241                 :           0 : std::optional<uint256> BlockFilterIndex::ReadFilterHeader(int height, const uint256& expected_block_hash)
     242                 :             : {
     243                 :           0 :     std::pair<uint256, DBVal> read_out;
     244         [ #  # ]:           0 :     if (!m_db->Read(DBHeightKey(height), read_out)) {
     245                 :           0 :         return std::nullopt;
     246                 :             :     }
     247                 :             : 
     248         [ #  # ]:           0 :     if (read_out.first != expected_block_hash) {
     249   [ #  #  #  # ]:           0 :         LogError("%s: previous block header belongs to unexpected block %s; expected %s\n",
     250                 :             :                          __func__, read_out.first.ToString(), expected_block_hash.ToString());
     251                 :           0 :         return std::nullopt;
     252                 :             :     }
     253                 :             : 
     254                 :           0 :     return read_out.second.header;
     255                 :             : }
     256                 :             : 
     257                 :           0 : bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
     258                 :             : {
     259                 :           0 :     BlockFilter filter(m_filter_type, *Assert(block.data), *Assert(block.undo_data));
     260         [ #  # ]:           0 :     const uint256& header = filter.ComputeHeader(m_last_header);
     261         [ #  # ]:           0 :     bool res = Write(filter, block.height, header);
     262         [ #  # ]:           0 :     if (res) m_last_header = header; // update last header
     263                 :           0 :     return res;
     264                 :           0 : }
     265                 :             : 
     266                 :           0 : bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header)
     267                 :             : {
     268                 :           0 :     size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
     269         [ #  # ]:           0 :     if (bytes_written == 0) return false;
     270                 :             : 
     271                 :           0 :     std::pair<uint256, DBVal> value;
     272                 :           0 :     value.first = filter.GetBlockHash();
     273                 :           0 :     value.second.hash = filter.GetHash();
     274                 :           0 :     value.second.header = filter_header;
     275                 :           0 :     value.second.pos = m_next_filter_pos;
     276                 :             : 
     277         [ #  # ]:           0 :     if (!m_db->Write(DBHeightKey(block_height), value)) {
     278                 :             :         return false;
     279                 :             :     }
     280                 :             : 
     281                 :           0 :     m_next_filter_pos.nPos += bytes_written;
     282                 :           0 :     return true;
     283                 :             : }
     284                 :             : 
     285                 :           0 : [[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
     286                 :             :                                                      const std::string& index_name, int height)
     287                 :             : {
     288                 :           0 :     DBHeightKey key(height);
     289                 :           0 :     db_it.Seek(key);
     290                 :             : 
     291   [ #  #  #  # ]:           0 :     if (!db_it.GetKey(key) || key.height != height) {
     292                 :           0 :         LogError("%s: unexpected key in %s: expected (%c, %d)\n",
     293                 :             :                  __func__, index_name, DB_BLOCK_HEIGHT, height);
     294                 :           0 :         return false;
     295                 :             :     }
     296                 :             : 
     297                 :           0 :     std::pair<uint256, DBVal> value;
     298         [ #  # ]:           0 :     if (!db_it.GetValue(value)) {
     299                 :           0 :         LogError("%s: unable to read value in %s at key (%c, %d)\n",
     300                 :             :                  __func__, index_name, DB_BLOCK_HEIGHT, height);
     301                 :           0 :         return false;
     302                 :             :     }
     303                 :             : 
     304                 :           0 :     batch.Write(DBHashKey(value.first), std::move(value.second));
     305                 :           0 :     return true;
     306                 :             : }
     307                 :             : 
     308                 :           0 : bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
     309                 :             : {
     310                 :           0 :     CDBBatch batch(*m_db);
     311   [ #  #  #  # ]:           0 :     std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
     312                 :             : 
     313                 :             :     // During a reorg, we need to copy block filter that is getting disconnected from the
     314                 :             :     // height index to the hash index so we can still find it when the height index entry
     315                 :             :     // is overwritten.
     316   [ #  #  #  # ]:           0 :     if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height)) {
     317                 :             :         return false;
     318                 :             :     }
     319                 :             : 
     320                 :             :     // The latest filter position gets written in Commit by the call to the BaseIndex::Rewind.
     321                 :             :     // But since this creates new references to the filter, the position should get updated here
     322                 :             :     // atomically as well in case Commit fails.
     323         [ #  # ]:           0 :     batch.Write(DB_FILTER_POS, m_next_filter_pos);
     324   [ #  #  #  # ]:           0 :     if (!m_db->WriteBatch(batch)) return false;
     325                 :             : 
     326                 :             :     // Update cached header to the previous block hash
     327   [ #  #  #  #  :           0 :     m_last_header = *Assert(ReadFilterHeader(block.height - 1, *Assert(block.prev_hash)));
                   #  # ]
     328                 :           0 :     return true;
     329                 :           0 : }
     330                 :             : 
     331                 :           0 : static bool LookupOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVal& result)
     332                 :             : {
     333                 :             :     // First check if the result is stored under the height index and the value there matches the
     334                 :             :     // block hash. This should be the case if the block is on the active chain.
     335                 :           0 :     std::pair<uint256, DBVal> read_out;
     336         [ #  # ]:           0 :     if (!db.Read(DBHeightKey(block_index->nHeight), read_out)) {
     337                 :             :         return false;
     338                 :             :     }
     339         [ #  # ]:           0 :     if (read_out.first == block_index->GetBlockHash()) {
     340                 :           0 :         result = std::move(read_out.second);
     341                 :           0 :         return true;
     342                 :             :     }
     343                 :             : 
     344                 :             :     // If value at the height index corresponds to an different block, the result will be stored in
     345                 :             :     // the hash index.
     346                 :           0 :     return db.Read(DBHashKey(block_index->GetBlockHash()), result);
     347                 :             : }
     348                 :             : 
     349                 :           0 : static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start_height,
     350                 :             :                         const CBlockIndex* stop_index, std::vector<DBVal>& results)
     351                 :             : {
     352         [ #  # ]:           0 :     if (start_height < 0) {
     353                 :           0 :         LogError("%s: start height (%d) is negative\n", __func__, start_height);
     354                 :           0 :         return false;
     355                 :             :     }
     356         [ #  # ]:           0 :     if (start_height > stop_index->nHeight) {
     357                 :           0 :         LogError("%s: start height (%d) is greater than stop height (%d)\n",
     358                 :             :                      __func__, start_height, stop_index->nHeight);
     359                 :           0 :         return false;
     360                 :             :     }
     361                 :             : 
     362                 :           0 :     size_t results_size = static_cast<size_t>(stop_index->nHeight - start_height + 1);
     363                 :           0 :     std::vector<std::pair<uint256, DBVal>> values(results_size);
     364                 :             : 
     365                 :           0 :     DBHeightKey key(start_height);
     366   [ #  #  #  # ]:           0 :     std::unique_ptr<CDBIterator> db_it(db.NewIterator());
     367         [ #  # ]:           0 :     db_it->Seek(DBHeightKey(start_height));
     368         [ #  # ]:           0 :     for (int height = start_height; height <= stop_index->nHeight; ++height) {
     369   [ #  #  #  #  :           0 :         if (!db_it->Valid() || !db_it->GetKey(key) || key.height != height) {
          #  #  #  #  #  
                      # ]
     370                 :           0 :             return false;
     371                 :             :         }
     372                 :             : 
     373                 :           0 :         size_t i = static_cast<size_t>(height - start_height);
     374   [ #  #  #  # ]:           0 :         if (!db_it->GetValue(values[i])) {
     375         [ #  # ]:           0 :             LogError("%s: unable to read value in %s at key (%c, %d)\n",
     376                 :             :                          __func__, index_name, DB_BLOCK_HEIGHT, height);
     377                 :             :             return false;
     378                 :             :         }
     379                 :             : 
     380         [ #  # ]:           0 :         db_it->Next();
     381                 :             :     }
     382                 :             : 
     383         [ #  # ]:           0 :     results.resize(results_size);
     384                 :             : 
     385                 :             :     // Iterate backwards through block indexes collecting results in order to access the block hash
     386                 :             :     // of each entry in case we need to look it up in the hash index.
     387                 :           0 :     for (const CBlockIndex* block_index = stop_index;
     388   [ #  #  #  # ]:           0 :          block_index && block_index->nHeight >= start_height;
     389                 :           0 :          block_index = block_index->pprev) {
     390                 :           0 :         uint256 block_hash = block_index->GetBlockHash();
     391                 :             : 
     392                 :           0 :         size_t i = static_cast<size_t>(block_index->nHeight - start_height);
     393         [ #  # ]:           0 :         if (block_hash == values[i].first) {
     394                 :           0 :             results[i] = std::move(values[i].second);
     395                 :           0 :             continue;
     396                 :             :         }
     397                 :             : 
     398   [ #  #  #  # ]:           0 :         if (!db.Read(DBHashKey(block_hash), results[i])) {
     399   [ #  #  #  # ]:           0 :             LogError("%s: unable to read value in %s at key (%c, %s)\n",
     400                 :             :                          __func__, index_name, DB_BLOCK_HASH, block_hash.ToString());
     401                 :           0 :             return false;
     402                 :             :         }
     403                 :             :     }
     404                 :             : 
     405                 :             :     return true;
     406                 :           0 : }
     407                 :             : 
     408                 :           0 : bool BlockFilterIndex::LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const
     409                 :             : {
     410                 :           0 :     DBVal entry;
     411         [ #  # ]:           0 :     if (!LookupOne(*m_db, block_index, entry)) {
     412                 :             :         return false;
     413                 :             :     }
     414                 :             : 
     415                 :           0 :     return ReadFilterFromDisk(entry.pos, entry.hash, filter_out);
     416                 :             : }
     417                 :             : 
     418                 :           0 : bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out)
     419                 :             : {
     420                 :           0 :     LOCK(m_cs_headers_cache);
     421                 :             : 
     422                 :           0 :     bool is_checkpoint{block_index->nHeight % CFCHECKPT_INTERVAL == 0};
     423                 :             : 
     424         [ #  # ]:           0 :     if (is_checkpoint) {
     425                 :             :         // Try to find the block in the headers cache if this is a checkpoint height.
     426                 :           0 :         auto header = m_headers_cache.find(block_index->GetBlockHash());
     427         [ #  # ]:           0 :         if (header != m_headers_cache.end()) {
     428                 :           0 :             header_out = header->second;
     429                 :           0 :             return true;
     430                 :             :         }
     431                 :             :     }
     432                 :             : 
     433                 :           0 :     DBVal entry;
     434   [ #  #  #  # ]:           0 :     if (!LookupOne(*m_db, block_index, entry)) {
     435                 :             :         return false;
     436                 :             :     }
     437                 :             : 
     438   [ #  #  #  # ]:           0 :     if (is_checkpoint &&
     439         [ #  # ]:           0 :         m_headers_cache.size() < CF_HEADERS_CACHE_MAX_SZ) {
     440                 :             :         // Add to the headers cache if this is a checkpoint height.
     441         [ #  # ]:           0 :         m_headers_cache.emplace(block_index->GetBlockHash(), entry.header);
     442                 :             :     }
     443                 :             : 
     444                 :           0 :     header_out = entry.header;
     445                 :           0 :     return true;
     446                 :           0 : }
     447                 :             : 
     448                 :           0 : bool BlockFilterIndex::LookupFilterRange(int start_height, const CBlockIndex* stop_index,
     449                 :             :                                          std::vector<BlockFilter>& filters_out) const
     450                 :             : {
     451                 :           0 :     std::vector<DBVal> entries;
     452   [ #  #  #  # ]:           0 :     if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
     453                 :             :         return false;
     454                 :             :     }
     455                 :             : 
     456         [ #  # ]:           0 :     filters_out.resize(entries.size());
     457                 :           0 :     auto filter_pos_it = filters_out.begin();
     458         [ #  # ]:           0 :     for (const auto& entry : entries) {
     459   [ #  #  #  # ]:           0 :         if (!ReadFilterFromDisk(entry.pos, entry.hash, *filter_pos_it)) {
     460                 :             :             return false;
     461                 :             :         }
     462                 :           0 :         ++filter_pos_it;
     463                 :             :     }
     464                 :             : 
     465                 :             :     return true;
     466                 :           0 : }
     467                 :             : 
     468                 :           0 : bool BlockFilterIndex::LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
     469                 :             :                                              std::vector<uint256>& hashes_out) const
     470                 :             : 
     471                 :             : {
     472                 :           0 :     std::vector<DBVal> entries;
     473   [ #  #  #  # ]:           0 :     if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
     474                 :             :         return false;
     475                 :             :     }
     476                 :             : 
     477         [ #  # ]:           0 :     hashes_out.clear();
     478         [ #  # ]:           0 :     hashes_out.reserve(entries.size());
     479         [ #  # ]:           0 :     for (const auto& entry : entries) {
     480         [ #  # ]:           0 :         hashes_out.push_back(entry.hash);
     481                 :             :     }
     482                 :             :     return true;
     483                 :           0 : }
     484                 :             : 
     485                 :           2 : BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type)
     486                 :             : {
     487                 :           2 :     auto it = g_filter_indexes.find(filter_type);
     488         [ -  + ]:           2 :     return it != g_filter_indexes.end() ? &it->second : nullptr;
     489                 :             : }
     490                 :             : 
     491                 :           7 : void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn)
     492                 :             : {
     493         [ -  + ]:           7 :     for (auto& entry : g_filter_indexes) fn(entry.second);
     494                 :           7 : }
     495                 :             : 
     496                 :           0 : bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
     497                 :             :                           size_t n_cache_size, bool f_memory, bool f_wipe)
     498                 :             : {
     499                 :           0 :     auto result = g_filter_indexes.emplace(std::piecewise_construct,
     500         [ #  # ]:           0 :                                            std::forward_as_tuple(filter_type),
     501         [ #  # ]:           0 :                                            std::forward_as_tuple(make_chain(), filter_type,
     502                 :             :                                                                  n_cache_size, f_memory, f_wipe));
     503                 :           0 :     return result.second;
     504                 :             : }
     505                 :             : 
     506                 :           0 : bool DestroyBlockFilterIndex(BlockFilterType filter_type)
     507                 :             : {
     508                 :           0 :     return g_filter_indexes.erase(filter_type);
     509                 :             : }
     510                 :             : 
     511                 :           0 : void DestroyAllBlockFilterIndexes()
     512                 :             : {
     513                 :           0 :     g_filter_indexes.clear();
     514                 :           0 : }
        

Generated by: LCOV version 2.0-1