LCOV - code coverage report
Current view: top level - src - blockfilter.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 59.6 % 156 93
Test Date: 2024-09-01 05:20:30 Functions: 70.0 % 20 14
Branches: 31.9 % 182 58

             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 <mutex>
       6                 :             : #include <set>
       7                 :             : 
       8                 :             : #include <blockfilter.h>
       9                 :             : #include <crypto/siphash.h>
      10                 :             : #include <hash.h>
      11                 :             : #include <primitives/block.h>
      12                 :             : #include <primitives/transaction.h>
      13                 :             : #include <script/script.h>
      14                 :             : #include <streams.h>
      15                 :             : #include <undo.h>
      16                 :             : #include <util/golombrice.h>
      17                 :             : #include <util/string.h>
      18                 :             : 
      19                 :             : using util::Join;
      20                 :             : 
      21   [ +  -  #  # ]:           1 : static const std::map<BlockFilterType, std::string> g_filter_types = {
      22         [ +  - ]:           1 :     {BlockFilterType::BASIC, "basic"},
      23                 :             : };
      24                 :             : 
      25                 :      233487 : uint64_t GCSFilter::HashToRange(const Element& element) const
      26                 :             : {
      27                 :      466974 :     uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)
      28                 :      233487 :         .Write(element)
      29                 :      233487 :         .Finalize();
      30                 :      466974 :     return FastRange64(hash, m_F);
      31                 :      233487 : }
      32                 :             : 
      33                 :         371 : std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const
      34                 :             : {
      35                 :         371 :     std::vector<uint64_t> hashed_elements;
      36         [ +  - ]:         371 :     hashed_elements.reserve(elements.size());
      37         [ +  + ]:      233487 :     for (const Element& element : elements) {
      38   [ +  -  +  - ]:      233116 :         hashed_elements.push_back(HashToRange(element));
      39                 :      233116 :     }
      40         [ +  - ]:         371 :     std::sort(hashed_elements.begin(), hashed_elements.end());
      41                 :         371 :     return hashed_elements;
      42         [ +  - ]:         371 : }
      43                 :             : 
      44                 :         611 : GCSFilter::GCSFilter(const Params& params)
      45         [ +  - ]:         611 :     : m_params(params), m_N(0), m_F(0), m_encoded{0}
      46                 :         611 : {}
      47                 :             : 
      48                 :         529 : GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
      49                 :         529 :     : m_params(params), m_encoded(std::move(encoded_filter))
      50                 :             : {
      51   [ +  -  +  - ]:         529 :     SpanReader stream{m_encoded};
      52                 :             : 
      53         [ +  + ]:         529 :     uint64_t N = ReadCompactSize(stream);
      54                 :         492 :     m_N = static_cast<uint32_t>(N);
      55         [ +  - ]:         492 :     if (m_N != N) {
      56   [ #  #  #  # ]:           0 :         throw std::ios_base::failure("N must be <2^32");
      57                 :             :     }
      58                 :         492 :     m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
      59                 :             : 
      60         [ -  + ]:         492 :     if (skip_decode_check) return;
      61                 :             : 
      62                 :             :     // Verify that the encoded filter contains exactly N elements. If it has too much or too little
      63                 :             :     // data, a std::ios_base::failure exception will be raised.
      64         [ +  - ]:         492 :     BitStreamReader bitreader{stream};
      65         [ +  + ]:      120584 :     for (uint64_t i = 0; i < m_N; ++i) {
      66         [ +  + ]:      120092 :         GolombRiceDecode(bitreader, m_params.m_P);
      67                 :      120039 :     }
      68   [ +  -  +  + ]:         439 :     if (!stream.empty()) {
      69   [ +  -  +  - ]:          10 :         throw std::ios_base::failure("encoded_filter contains excess data");
      70                 :             :     }
      71                 :         539 : }
      72                 :             : 
      73                 :           0 : GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
      74                 :           0 :     : m_params(params)
      75                 :             : {
      76                 :           0 :     size_t N = elements.size();
      77                 :           0 :     m_N = static_cast<uint32_t>(N);
      78         [ #  # ]:           0 :     if (m_N != N) {
      79         [ #  # ]:           0 :         throw std::invalid_argument("N must be <2^32");
      80                 :             :     }
      81                 :           0 :     m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
      82                 :             : 
      83         [ #  # ]:           0 :     VectorWriter stream{m_encoded, 0};
      84                 :             : 
      85         [ #  # ]:           0 :     WriteCompactSize(stream, m_N);
      86                 :             : 
      87         [ #  # ]:           0 :     if (elements.empty()) {
      88                 :           0 :         return;
      89                 :             :     }
      90                 :             : 
      91         [ #  # ]:           0 :     BitStreamWriter bitwriter{stream};
      92                 :             : 
      93                 :           0 :     uint64_t last_value = 0;
      94   [ #  #  #  # ]:           0 :     for (uint64_t value : BuildHashedSet(elements)) {
      95                 :           0 :         uint64_t delta = value - last_value;
      96         [ #  # ]:           0 :         GolombRiceEncode(bitwriter, m_params.m_P, delta);
      97                 :           0 :         last_value = value;
      98                 :           0 :     }
      99                 :             : 
     100         [ #  # ]:           0 :     bitwriter.Flush();
     101                 :           0 : }
     102                 :             : 
     103                 :         742 : bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
     104                 :             : {
     105                 :         742 :     SpanReader stream{m_encoded};
     106                 :             : 
     107                 :             :     // Seek forward by size of N
     108                 :         742 :     uint64_t N = ReadCompactSize(stream);
     109         [ +  - ]:         742 :     assert(N == m_N);
     110                 :             : 
     111                 :         742 :     BitStreamReader bitreader{stream};
     112                 :             : 
     113                 :         742 :     uint64_t value = 0;
     114                 :         742 :     size_t hashes_index = 0;
     115   [ +  +  +  + ]:       90127 :     for (uint32_t i = 0; i < m_N; ++i) {
     116                 :       89385 :         uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P);
     117                 :       89385 :         value += delta;
     118                 :             : 
     119                 :      299086 :         while (true) {
     120         [ +  + ]:      299086 :             if (hashes_index == size) {
     121                 :         475 :                 return false;
     122         [ +  + ]:      298611 :             } else if (element_hashes[hashes_index] == value) {
     123                 :           7 :                 return true;
     124         [ +  + ]:      298604 :             } else if (element_hashes[hashes_index] > value) {
     125                 :       88903 :                 break;
     126                 :             :             }
     127                 :             : 
     128                 :      209701 :             hashes_index++;
     129                 :             :         }
     130         [ +  + ]:       89385 :     }
     131                 :             : 
     132                 :         260 :     return false;
     133                 :         742 : }
     134                 :             : 
     135                 :         371 : bool GCSFilter::Match(const Element& element) const
     136                 :             : {
     137                 :         371 :     uint64_t query = HashToRange(element);
     138                 :         742 :     return MatchInternal(&query, 1);
     139                 :         371 : }
     140                 :             : 
     141                 :         371 : bool GCSFilter::MatchAny(const ElementSet& elements) const
     142                 :             : {
     143                 :         371 :     const std::vector<uint64_t> queries = BuildHashedSet(elements);
     144         [ +  - ]:         371 :     return MatchInternal(queries.data(), queries.size());
     145                 :         371 : }
     146                 :             : 
     147                 :         436 : const std::string& BlockFilterTypeName(BlockFilterType filter_type)
     148                 :             : {
     149   [ +  +  -  + ]:         436 :     static std::string unknown_retval;
     150                 :         436 :     auto it = g_filter_types.find(filter_type);
     151         [ +  - ]:         436 :     return it != g_filter_types.end() ? it->second : unknown_retval;
     152                 :         436 : }
     153                 :             : 
     154                 :        1313 : bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type) {
     155   [ +  +  -  +  :        2626 :     for (const auto& entry : g_filter_types) {
                      + ]
     156         [ +  + ]:        1313 :         if (entry.second == name) {
     157                 :           3 :             filter_type = entry.first;
     158                 :           3 :             return true;
     159                 :             :         }
     160         [ +  + ]:        1313 :     }
     161                 :        1310 :     return false;
     162                 :        1313 : }
     163                 :             : 
     164                 :           0 : const std::set<BlockFilterType>& AllBlockFilterTypes()
     165                 :             : {
     166   [ #  #  #  # ]:           0 :     static std::set<BlockFilterType> types;
     167                 :             : 
     168                 :             :     static std::once_flag flag;
     169                 :           0 :     std::call_once(flag, []() {
     170         [ #  # ]:           0 :             for (const auto& entry : g_filter_types) {
     171                 :           0 :                 types.insert(entry.first);
     172                 :           0 :             }
     173                 :           0 :         });
     174                 :             : 
     175                 :           0 :     return types;
     176                 :             : }
     177                 :             : 
     178                 :        1826 : const std::string& ListBlockFilterTypes()
     179                 :             : {
     180   [ +  +  -  +  :        1827 :     static std::string type_list{Join(g_filter_types, ", ", [](const auto& entry) { return entry.second; })};
                   +  - ]
     181                 :             : 
     182                 :        1826 :     return type_list;
     183                 :           0 : }
     184                 :             : 
     185                 :           0 : static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
     186                 :             :                                                  const CBlockUndo& block_undo)
     187                 :             : {
     188                 :           0 :     GCSFilter::ElementSet elements;
     189                 :             : 
     190         [ #  # ]:           0 :     for (const CTransactionRef& tx : block.vtx) {
     191         [ #  # ]:           0 :         for (const CTxOut& txout : tx->vout) {
     192                 :           0 :             const CScript& script = txout.scriptPubKey;
     193   [ #  #  #  #  :           0 :             if (script.empty() || script[0] == OP_RETURN) continue;
             #  #  #  # ]
     194   [ #  #  #  #  :           0 :             elements.emplace(script.begin(), script.end());
                   #  # ]
     195   [ #  #  #  # ]:           0 :         }
     196                 :           0 :     }
     197                 :             : 
     198         [ #  # ]:           0 :     for (const CTxUndo& tx_undo : block_undo.vtxundo) {
     199         [ #  # ]:           0 :         for (const Coin& prevout : tx_undo.vprevout) {
     200                 :           0 :             const CScript& script = prevout.out.scriptPubKey;
     201   [ #  #  #  # ]:           0 :             if (script.empty()) continue;
     202   [ #  #  #  #  :           0 :             elements.emplace(script.begin(), script.end());
                   #  # ]
     203   [ #  #  #  # ]:           0 :         }
     204                 :           0 :     }
     205                 :             : 
     206                 :           0 :     return elements;
     207         [ #  # ]:           0 : }
     208                 :             : 
     209                 :           0 : BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
     210                 :             :                          std::vector<unsigned char> filter, bool skip_decode_check)
     211                 :           0 :     : m_filter_type(filter_type), m_block_hash(block_hash)
     212                 :             : {
     213         [ #  # ]:           0 :     GCSFilter::Params params;
     214   [ #  #  #  # ]:           0 :     if (!BuildParams(params)) {
     215   [ #  #  #  # ]:           0 :         throw std::invalid_argument("unknown filter_type");
     216                 :             :     }
     217         [ #  # ]:           0 :     m_filter = GCSFilter(params, std::move(filter), skip_decode_check);
     218                 :           0 : }
     219                 :             : 
     220                 :           0 : BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
     221                 :           0 :     : m_filter_type(filter_type), m_block_hash(block.GetHash())
     222                 :             : {
     223         [ #  # ]:           0 :     GCSFilter::Params params;
     224   [ #  #  #  # ]:           0 :     if (!BuildParams(params)) {
     225   [ #  #  #  # ]:           0 :         throw std::invalid_argument("unknown filter_type");
     226                 :             :     }
     227   [ #  #  #  # ]:           0 :     m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));
     228                 :           0 : }
     229                 :             : 
     230                 :         539 : bool BlockFilter::BuildParams(GCSFilter::Params& params) const
     231                 :             : {
     232      [ -  +  + ]:         539 :     switch (m_filter_type) {
     233                 :             :     case BlockFilterType::BASIC:
     234                 :         529 :         params.m_siphash_k0 = m_block_hash.GetUint64(0);
     235                 :         529 :         params.m_siphash_k1 = m_block_hash.GetUint64(1);
     236                 :         529 :         params.m_P = BASIC_FILTER_P;
     237                 :         529 :         params.m_M = BASIC_FILTER_M;
     238                 :         529 :         return true;
     239                 :             :     case BlockFilterType::INVALID:
     240                 :           0 :         return false;
     241                 :             :     }
     242                 :             : 
     243                 :          10 :     return false;
     244                 :         539 : }
     245                 :             : 
     246                 :         742 : uint256 BlockFilter::GetHash() const
     247                 :             : {
     248                 :         742 :     return Hash(GetEncodedFilter());
     249                 :             : }
     250                 :             : 
     251                 :         371 : uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
     252                 :             : {
     253                 :         371 :     return Hash(GetHash(), prev_header);
     254                 :             : }
        

Generated by: LCOV version 2.0-1