/root/bitcoin/src/blockfilter.cpp
| Line | Count | Source | 
| 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 |  | static const std::map<BlockFilterType, std::string> g_filter_types = { | 
| 22 |  |     {BlockFilterType::BASIC, "basic"}, | 
| 23 |  | }; | 
| 24 |  |  | 
| 25 |  | uint64_t GCSFilter::HashToRange(const Element& element) const | 
| 26 | 0 | { | 
| 27 | 0 |     uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1) | 
| 28 | 0 |         .Write(element) | 
| 29 | 0 |         .Finalize(); | 
| 30 | 0 |     return FastRange64(hash, m_F); | 
| 31 | 0 | } | 
| 32 |  |  | 
| 33 |  | std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const | 
| 34 | 0 | { | 
| 35 | 0 |     std::vector<uint64_t> hashed_elements; | 
| 36 | 0 |     hashed_elements.reserve(elements.size()); | 
| 37 | 0 |     for (const Element& element : elements) { | 
| 38 | 0 |         hashed_elements.push_back(HashToRange(element)); | 
| 39 | 0 |     } | 
| 40 | 0 |     std::sort(hashed_elements.begin(), hashed_elements.end()); | 
| 41 | 0 |     return hashed_elements; | 
| 42 | 0 | } | 
| 43 |  |  | 
| 44 |  | GCSFilter::GCSFilter(const Params& params) | 
| 45 | 0 |     : m_params(params), m_N(0), m_F(0), m_encoded{0} | 
| 46 | 0 | {} | 
| 47 |  |  | 
| 48 |  | GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check) | 
| 49 | 0 |     : m_params(params), m_encoded(std::move(encoded_filter)) | 
| 50 | 0 | { | 
| 51 | 0 |     SpanReader stream{m_encoded}; | 
| 52 |  | 
 | 
| 53 | 0 |     uint64_t N = ReadCompactSize(stream); | 
| 54 | 0 |     m_N = static_cast<uint32_t>(N); | 
| 55 | 0 |     if (m_N != N) { | 
| 56 | 0 |         throw std::ios_base::failure("N must be <2^32"); | 
| 57 | 0 |     } | 
| 58 | 0 |     m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M); | 
| 59 |  | 
 | 
| 60 | 0 |     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 | 0 |     BitStreamReader bitreader{stream}; | 
| 65 | 0 |     for (uint64_t i = 0; i < m_N; ++i) { | 
| 66 | 0 |         GolombRiceDecode(bitreader, m_params.m_P); | 
| 67 | 0 |     } | 
| 68 | 0 |     if (!stream.empty()) { | 
| 69 | 0 |         throw std::ios_base::failure("encoded_filter contains excess data"); | 
| 70 | 0 |     } | 
| 71 | 0 | } | 
| 72 |  |  | 
| 73 |  | GCSFilter::GCSFilter(const Params& params, const ElementSet& elements) | 
| 74 | 0 |     : m_params(params) | 
| 75 | 0 | { | 
| 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 | 0 |     } | 
| 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 | 0 |     } | 
| 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 |  | bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const | 
| 104 | 0 | { | 
| 105 | 0 |     SpanReader stream{m_encoded}; | 
| 106 |  |  | 
| 107 |  |     // Seek forward by size of N | 
| 108 | 0 |     uint64_t N = ReadCompactSize(stream); | 
| 109 | 0 |     assert(N == m_N); | 
| 110 |  |  | 
| 111 | 0 |     BitStreamReader bitreader{stream}; | 
| 112 |  | 
 | 
| 113 | 0 |     uint64_t value = 0; | 
| 114 | 0 |     size_t hashes_index = 0; | 
| 115 | 0 |     for (uint32_t i = 0; i < m_N; ++i) { | 
| 116 | 0 |         uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P); | 
| 117 | 0 |         value += delta; | 
| 118 |  | 
 | 
| 119 | 0 |         while (true) { | 
| 120 | 0 |             if (hashes_index == size) { | 
| 121 | 0 |                 return false; | 
| 122 | 0 |             } else if (element_hashes[hashes_index] == value) { | 
| 123 | 0 |                 return true; | 
| 124 | 0 |             } else if (element_hashes[hashes_index] > value) { | 
| 125 | 0 |                 break; | 
| 126 | 0 |             } | 
| 127 |  |  | 
| 128 | 0 |             hashes_index++; | 
| 129 | 0 |         } | 
| 130 | 0 |     } | 
| 131 |  |  | 
| 132 | 0 |     return false; | 
| 133 | 0 | } | 
| 134 |  |  | 
| 135 |  | bool GCSFilter::Match(const Element& element) const | 
| 136 | 0 | { | 
| 137 | 0 |     uint64_t query = HashToRange(element); | 
| 138 | 0 |     return MatchInternal(&query, 1); | 
| 139 | 0 | } | 
| 140 |  |  | 
| 141 |  | bool GCSFilter::MatchAny(const ElementSet& elements) const | 
| 142 | 0 | { | 
| 143 | 0 |     const std::vector<uint64_t> queries = BuildHashedSet(elements); | 
| 144 | 0 |     return MatchInternal(queries.data(), queries.size()); | 
| 145 | 0 | } | 
| 146 |  |  | 
| 147 |  | const std::string& BlockFilterTypeName(BlockFilterType filter_type) | 
| 148 | 0 | { | 
| 149 | 0 |     static std::string unknown_retval; | 
| 150 | 0 |     auto it = g_filter_types.find(filter_type); | 
| 151 | 0 |     return it != g_filter_types.end() ? it->second : unknown_retval; | 
| 152 | 0 | } | 
| 153 |  |  | 
| 154 | 0 | bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type) { | 
| 155 | 0 |     for (const auto& entry : g_filter_types) { | 
| 156 | 0 |         if (entry.second == name) { | 
| 157 | 0 |             filter_type = entry.first; | 
| 158 | 0 |             return true; | 
| 159 | 0 |         } | 
| 160 | 0 |     } | 
| 161 | 0 |     return false; | 
| 162 | 0 | } | 
| 163 |  |  | 
| 164 |  | const std::set<BlockFilterType>& AllBlockFilterTypes() | 
| 165 | 0 | { | 
| 166 | 0 |     static std::set<BlockFilterType> types; | 
| 167 |  | 
 | 
| 168 | 0 |     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 | 0 | } | 
| 177 |  |  | 
| 178 |  | const std::string& ListBlockFilterTypes() | 
| 179 | 0 | { | 
| 180 | 0 |     static std::string type_list{Join(g_filter_types, ", ", [](const auto& entry) { return entry.second; })}; | 
| 181 |  | 
 | 
| 182 | 0 |     return type_list; | 
| 183 | 0 | } | 
| 184 |  |  | 
| 185 |  | static GCSFilter::ElementSet BasicFilterElements(const CBlock& block, | 
| 186 |  |                                                  const CBlockUndo& block_undo) | 
| 187 | 0 | { | 
| 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 |  | 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 | 0 | { | 
| 213 | 0 |     GCSFilter::Params params; | 
| 214 | 0 |     if (!BuildParams(params)) { | 
| 215 | 0 |         throw std::invalid_argument("unknown filter_type"); | 
| 216 | 0 |     } | 
| 217 | 0 |     m_filter = GCSFilter(params, std::move(filter), skip_decode_check); | 
| 218 | 0 | } | 
| 219 |  |  | 
| 220 |  | 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 | 0 | { | 
| 223 | 0 |     GCSFilter::Params params; | 
| 224 | 0 |     if (!BuildParams(params)) { | 
| 225 | 0 |         throw std::invalid_argument("unknown filter_type"); | 
| 226 | 0 |     } | 
| 227 | 0 |     m_filter = GCSFilter(params, BasicFilterElements(block, block_undo)); | 
| 228 | 0 | } | 
| 229 |  |  | 
| 230 |  | bool BlockFilter::BuildParams(GCSFilter::Params& params) const | 
| 231 | 0 | { | 
| 232 | 0 |     switch (m_filter_type) { | 
| 233 | 0 |     case BlockFilterType::BASIC: | 
| 234 | 0 |         params.m_siphash_k0 = m_block_hash.GetUint64(0); | 
| 235 | 0 |         params.m_siphash_k1 = m_block_hash.GetUint64(1); | 
| 236 | 0 |         params.m_P = BASIC_FILTER_P; | 
| 237 | 0 |         params.m_M = BASIC_FILTER_M; | 
| 238 | 0 |         return true; | 
| 239 | 0 |     case BlockFilterType::INVALID: | 
| 240 | 0 |         return false; | 
| 241 | 0 |     } | 
| 242 |  |  | 
| 243 | 0 |     return false; | 
| 244 | 0 | } | 
| 245 |  |  | 
| 246 |  | uint256 BlockFilter::GetHash() const | 
| 247 | 0 | { | 
| 248 | 0 |     return Hash(GetEncodedFilter()); | 
| 249 | 0 | } | 
| 250 |  |  | 
| 251 |  | uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const | 
| 252 | 0 | { | 
| 253 | 0 |     return Hash(GetHash(), prev_header); | 
| 254 | 0 | } |