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