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