Bitcoin Core  22.99.0
P2P Digital Currency
blockfilterindex.cpp
Go to the documentation of this file.
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>
9 #include <node/blockstorage.h>
10 #include <util/system.h>
11 
13 
14 /* The index database stores three items for each block: the disk location of the encoded filter,
15  * its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by
16  * height, and those belonging to blocks that have been reorganized out of the active chain are
17  * indexed by block hash. This ensures that filter data for any block that becomes part of the
18  * active chain can always be retrieved, alleviating timing concerns.
19  *
20  * The filters themselves are stored in flat files and referenced by the LevelDB entries. This
21  * minimizes the amount of data written to LevelDB and keeps the database values constant size. The
22  * disk location of the next block filter to be written (represented as a FlatFilePos) is stored
23  * under the DB_FILTER_POS key.
24  *
25  * Keys for the height index have the type [DB_BLOCK_HEIGHT, uint32 (BE)]. The height is represented
26  * as big-endian so that sequential reads of filters by height are fast.
27  * Keys for the hash index have the type [DB_BLOCK_HASH, uint256].
28  */
29 constexpr uint8_t DB_BLOCK_HASH{'s'};
30 constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
31 constexpr uint8_t DB_FILTER_POS{'P'};
32 
33 constexpr unsigned int MAX_FLTR_FILE_SIZE = 0x1000000; // 16 MiB
35 constexpr unsigned int FLTR_FILE_CHUNK_SIZE = 0x100000; // 1 MiB
41 constexpr size_t CF_HEADERS_CACHE_MAX_SZ{2000};
42 
43 namespace {
44 
45 struct DBVal {
46  uint256 hash;
47  uint256 header;
48  FlatFilePos pos;
49 
50  SERIALIZE_METHODS(DBVal, obj) { READWRITE(obj.hash, obj.header, obj.pos); }
51 };
52 
53 struct DBHeightKey {
54  int height;
55 
56  explicit DBHeightKey(int height_in) : height(height_in) {}
57 
58  template<typename Stream>
59  void Serialize(Stream& s) const
60  {
62  ser_writedata32be(s, height);
63  }
64 
65  template<typename Stream>
66  void Unserialize(Stream& s)
67  {
68  const uint8_t prefix{ser_readdata8(s)};
69  if (prefix != DB_BLOCK_HEIGHT) {
70  throw std::ios_base::failure("Invalid format for block filter index DB height key");
71  }
72  height = ser_readdata32be(s);
73  }
74 };
75 
76 struct DBHashKey {
77  uint256 hash;
78 
79  explicit DBHashKey(const uint256& hash_in) : hash(hash_in) {}
80 
81  SERIALIZE_METHODS(DBHashKey, obj) {
82  uint8_t prefix{DB_BLOCK_HASH};
84  if (prefix != DB_BLOCK_HASH) {
85  throw std::ios_base::failure("Invalid format for block filter index DB hash key");
86  }
87 
88  READWRITE(obj.hash);
89  }
90 };
91 
92 }; // namespace
93 
94 static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
95 
97  size_t n_cache_size, bool f_memory, bool f_wipe)
98  : m_filter_type(filter_type)
99 {
100  const std::string& filter_name = BlockFilterTypeName(filter_type);
101  if (filter_name.empty()) throw std::invalid_argument("unknown filter_type");
102 
103  fs::path path = gArgs.GetDataDirNet() / "indexes" / "blockfilter" / filter_name;
105 
106  m_name = filter_name + " block filter index";
107  m_db = std::make_unique<BaseIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
108  m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
109 }
110 
112 {
113  if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
114  // Check that the cause of the read failure is that the key does not exist. Any other errors
115  // indicate database corruption or a disk failure, and starting the index would cause
116  // further corruption.
117  if (m_db->Exists(DB_FILTER_POS)) {
118  return error("%s: Cannot read current %s state; index may be corrupted",
119  __func__, GetName());
120  }
121 
122  // If the DB_FILTER_POS is not set, then initialize to the first location.
125  }
126  return BaseIndex::Init();
127 }
128 
130 {
131  const FlatFilePos& pos = m_next_filter_pos;
132 
133  // Flush current filter file to disk.
135  if (file.IsNull()) {
136  return error("%s: Failed to open filter file %d", __func__, pos.nFile);
137  }
138  if (!FileCommit(file.Get())) {
139  return error("%s: Failed to commit filter file %d", __func__, pos.nFile);
140  }
141 
142  batch.Write(DB_FILTER_POS, pos);
143  return BaseIndex::CommitInternal(batch);
144 }
145 
147 {
148  CAutoFile filein(m_filter_fileseq->Open(pos, true), SER_DISK, CLIENT_VERSION);
149  if (filein.IsNull()) {
150  return false;
151  }
152 
153  uint256 block_hash;
154  std::vector<uint8_t> encoded_filter;
155  try {
156  filein >> block_hash >> encoded_filter;
157  filter = BlockFilter(GetFilterType(), block_hash, std::move(encoded_filter));
158  }
159  catch (const std::exception& e) {
160  return error("%s: Failed to deserialize block filter from disk: %s", __func__, e.what());
161  }
162 
163  return true;
164 }
165 
167 {
168  assert(filter.GetFilterType() == GetFilterType());
169 
170  size_t data_size =
173 
174  // If writing the filter would overflow the file, flush and move to the next one.
175  if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
176  CAutoFile last_file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
177  if (last_file.IsNull()) {
178  LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
179  return 0;
180  }
181  if (!TruncateFile(last_file.Get(), pos.nPos)) {
182  LogPrintf("%s: Failed to truncate filter file %d\n", __func__, pos.nFile);
183  return 0;
184  }
185  if (!FileCommit(last_file.Get())) {
186  LogPrintf("%s: Failed to commit filter file %d\n", __func__, pos.nFile);
187  return 0;
188  }
189 
190  pos.nFile++;
191  pos.nPos = 0;
192  }
193 
194  // Pre-allocate sufficient space for filter data.
195  bool out_of_space;
196  m_filter_fileseq->Allocate(pos, data_size, out_of_space);
197  if (out_of_space) {
198  LogPrintf("%s: out of disk space\n", __func__);
199  return 0;
200  }
201 
202  CAutoFile fileout(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
203  if (fileout.IsNull()) {
204  LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
205  return 0;
206  }
207 
208  fileout << filter.GetBlockHash() << filter.GetEncodedFilter();
209  return data_size;
210 }
211 
212 bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
213 {
214  CBlockUndo block_undo;
215  uint256 prev_header;
216 
217  if (pindex->nHeight > 0) {
218  if (!UndoReadFromDisk(block_undo, pindex)) {
219  return false;
220  }
221 
222  std::pair<uint256, DBVal> read_out;
223  if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
224  return false;
225  }
226 
227  uint256 expected_block_hash = pindex->pprev->GetBlockHash();
228  if (read_out.first != expected_block_hash) {
229  return error("%s: previous block header belongs to unexpected block %s; expected %s",
230  __func__, read_out.first.ToString(), expected_block_hash.ToString());
231  }
232 
233  prev_header = read_out.second.header;
234  }
235 
236  BlockFilter filter(m_filter_type, block, block_undo);
237 
238  size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
239  if (bytes_written == 0) return false;
240 
241  std::pair<uint256, DBVal> value;
242  value.first = pindex->GetBlockHash();
243  value.second.hash = filter.GetHash();
244  value.second.header = filter.ComputeHeader(prev_header);
245  value.second.pos = m_next_filter_pos;
246 
247  if (!m_db->Write(DBHeightKey(pindex->nHeight), value)) {
248  return false;
249  }
250 
251  m_next_filter_pos.nPos += bytes_written;
252  return true;
253 }
254 
256  const std::string& index_name,
257  int start_height, int stop_height)
258 {
259  DBHeightKey key(start_height);
260  db_it.Seek(key);
261 
262  for (int height = start_height; height <= stop_height; ++height) {
263  if (!db_it.GetKey(key) || key.height != height) {
264  return error("%s: unexpected key in %s: expected (%c, %d)",
265  __func__, index_name, DB_BLOCK_HEIGHT, height);
266  }
267 
268  std::pair<uint256, DBVal> value;
269  if (!db_it.GetValue(value)) {
270  return error("%s: unable to read value in %s at key (%c, %d)",
271  __func__, index_name, DB_BLOCK_HEIGHT, height);
272  }
273 
274  batch.Write(DBHashKey(value.first), std::move(value.second));
275 
276  db_it.Next();
277  }
278  return true;
279 }
280 
281 bool BlockFilterIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
282 {
283  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
284 
285  CDBBatch batch(*m_db);
286  std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
287 
288  // During a reorg, we need to copy all filters for blocks that are getting disconnected from the
289  // height index to the hash index so we can still find them when the height index entries are
290  // overwritten.
291  if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip->nHeight, current_tip->nHeight)) {
292  return false;
293  }
294 
295  // The latest filter position gets written in Commit by the call to the BaseIndex::Rewind.
296  // But since this creates new references to the filter, the position should get updated here
297  // atomically as well in case Commit fails.
299  if (!m_db->WriteBatch(batch)) return false;
300 
301  return BaseIndex::Rewind(current_tip, new_tip);
302 }
303 
304 static bool LookupOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVal& result)
305 {
306  // First check if the result is stored under the height index and the value there matches the
307  // block hash. This should be the case if the block is on the active chain.
308  std::pair<uint256, DBVal> read_out;
309  if (!db.Read(DBHeightKey(block_index->nHeight), read_out)) {
310  return false;
311  }
312  if (read_out.first == block_index->GetBlockHash()) {
313  result = std::move(read_out.second);
314  return true;
315  }
316 
317  // If value at the height index corresponds to an different block, the result will be stored in
318  // the hash index.
319  return db.Read(DBHashKey(block_index->GetBlockHash()), result);
320 }
321 
322 static bool LookupRange(CDBWrapper& db, const std::string& index_name, int start_height,
323  const CBlockIndex* stop_index, std::vector<DBVal>& results)
324 {
325  if (start_height < 0) {
326  return error("%s: start height (%d) is negative", __func__, start_height);
327  }
328  if (start_height > stop_index->nHeight) {
329  return error("%s: start height (%d) is greater than stop height (%d)",
330  __func__, start_height, stop_index->nHeight);
331  }
332 
333  size_t results_size = static_cast<size_t>(stop_index->nHeight - start_height + 1);
334  std::vector<std::pair<uint256, DBVal>> values(results_size);
335 
336  DBHeightKey key(start_height);
337  std::unique_ptr<CDBIterator> db_it(db.NewIterator());
338  db_it->Seek(DBHeightKey(start_height));
339  for (int height = start_height; height <= stop_index->nHeight; ++height) {
340  if (!db_it->Valid() || !db_it->GetKey(key) || key.height != height) {
341  return false;
342  }
343 
344  size_t i = static_cast<size_t>(height - start_height);
345  if (!db_it->GetValue(values[i])) {
346  return error("%s: unable to read value in %s at key (%c, %d)",
347  __func__, index_name, DB_BLOCK_HEIGHT, height);
348  }
349 
350  db_it->Next();
351  }
352 
353  results.resize(results_size);
354 
355  // Iterate backwards through block indexes collecting results in order to access the block hash
356  // of each entry in case we need to look it up in the hash index.
357  for (const CBlockIndex* block_index = stop_index;
358  block_index && block_index->nHeight >= start_height;
359  block_index = block_index->pprev) {
360  uint256 block_hash = block_index->GetBlockHash();
361 
362  size_t i = static_cast<size_t>(block_index->nHeight - start_height);
363  if (block_hash == values[i].first) {
364  results[i] = std::move(values[i].second);
365  continue;
366  }
367 
368  if (!db.Read(DBHashKey(block_hash), results[i])) {
369  return error("%s: unable to read value in %s at key (%c, %s)",
370  __func__, index_name, DB_BLOCK_HASH, block_hash.ToString());
371  }
372  }
373 
374  return true;
375 }
376 
377 bool BlockFilterIndex::LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const
378 {
379  DBVal entry;
380  if (!LookupOne(*m_db, block_index, entry)) {
381  return false;
382  }
383 
384  return ReadFilterFromDisk(entry.pos, filter_out);
385 }
386 
387 bool BlockFilterIndex::LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out)
388 {
390 
391  bool is_checkpoint{block_index->nHeight % CFCHECKPT_INTERVAL == 0};
392 
393  if (is_checkpoint) {
394  // Try to find the block in the headers cache if this is a checkpoint height.
395  auto header = m_headers_cache.find(block_index->GetBlockHash());
396  if (header != m_headers_cache.end()) {
397  header_out = header->second;
398  return true;
399  }
400  }
401 
402  DBVal entry;
403  if (!LookupOne(*m_db, block_index, entry)) {
404  return false;
405  }
406 
407  if (is_checkpoint &&
408  m_headers_cache.size() < CF_HEADERS_CACHE_MAX_SZ) {
409  // Add to the headers cache if this is a checkpoint height.
410  m_headers_cache.emplace(block_index->GetBlockHash(), entry.header);
411  }
412 
413  header_out = entry.header;
414  return true;
415 }
416 
417 bool BlockFilterIndex::LookupFilterRange(int start_height, const CBlockIndex* stop_index,
418  std::vector<BlockFilter>& filters_out) const
419 {
420  std::vector<DBVal> entries;
421  if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
422  return false;
423  }
424 
425  filters_out.resize(entries.size());
426  auto filter_pos_it = filters_out.begin();
427  for (const auto& entry : entries) {
428  if (!ReadFilterFromDisk(entry.pos, *filter_pos_it)) {
429  return false;
430  }
431  ++filter_pos_it;
432  }
433 
434  return true;
435 }
436 
437 bool BlockFilterIndex::LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
438  std::vector<uint256>& hashes_out) const
439 
440 {
441  std::vector<DBVal> entries;
442  if (!LookupRange(*m_db, m_name, start_height, stop_index, entries)) {
443  return false;
444  }
445 
446  hashes_out.clear();
447  hashes_out.reserve(entries.size());
448  for (const auto& entry : entries) {
449  hashes_out.push_back(entry.hash);
450  }
451  return true;
452 }
453 
455 {
456  auto it = g_filter_indexes.find(filter_type);
457  return it != g_filter_indexes.end() ? &it->second : nullptr;
458 }
459 
460 void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn)
461 {
462  for (auto& entry : g_filter_indexes) fn(entry.second);
463 }
464 
466  size_t n_cache_size, bool f_memory, bool f_wipe)
467 {
468  auto result = g_filter_indexes.emplace(std::piecewise_construct,
469  std::forward_as_tuple(filter_type),
470  std::forward_as_tuple(filter_type,
471  n_cache_size, f_memory, f_wipe));
472  return result.second;
473 }
474 
476 {
477  return g_filter_indexes.erase(filter_type);
478 }
479 
481 {
482  g_filter_indexes.clear();
483 }
BlockFilterIndex::m_name
std::string m_name
Definition: blockfilterindex.h:28
FLTR_FILE_CHUNK_SIZE
constexpr unsigned int FLTR_FILE_CHUNK_SIZE
The pre-allocation chunk size for fltr?????.dat files.
Definition: blockfilterindex.cpp:35
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1086
FlatFilePos::nFile
int nFile
Definition: flatfile.h:16
BlockFilterTypeName
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
Definition: blockfilter.cpp:147
dbwrapper.h
SERIALIZE_METHODS
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
Definition: serialize.h:176
CBlockIndex::GetAncestor
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:114
assert
assert(!tx.IsCoinBase())
BlockFilter::GetFilterType
BlockFilterType GetFilterType() const
Definition: blockfilter.h:130
SER_DISK
@ SER_DISK
Definition: serialize.h:132
BlockFilter::GetBlockHash
const uint256 & GetBlockHash() const
Definition: blockfilter.h:131
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:47
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:304
BlockFilterIndex::m_db
std::unique_ptr< BaseIndex::DB > m_db
Definition: blockfilterindex.h:29
BlockFilterIndex::WriteFilterToDisk
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
Definition: blockfilterindex.cpp:166
BaseIndex::Init
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:58
blockfilterindex.h
BlockFilter::GetEncodedFilter
const std::vector< unsigned char > & GetEncodedFilter() const
Definition: blockfilter.h:134
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:519
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:158
FileCommit
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: system.cpp:1100
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
BlockFilterIndex
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
Definition: blockfilterindex.h:24
BlockFilter::ComputeHeader
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
Definition: blockfilter.cpp:267
g_filter_indexes
static std::map< BlockFilterType, BlockFilterIndex > g_filter_indexes
Definition: blockfilterindex.cpp:92
BlockFilterIndex::CommitInternal
bool CommitInternal(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: blockfilterindex.cpp:129
CDBWrapper::Read
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:231
DestroyBlockFilterIndex
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
Definition: blockfilterindex.cpp:475
DB_BLOCK_HASH
constexpr uint8_t DB_BLOCK_HASH
Definition: blockfilterindex.cpp:29
Unserialize
void Unserialize(Stream &, char)=delete
Serialize
void Serialize(Stream &, char)=delete
CDBIterator::Next
void Next()
Definition: dbwrapper.cpp:244
prefix
const char * prefix
Definition: rest.cpp:926
CDBBatch::Write
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:478
node::UndoReadFromDisk
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
Definition: blockstorage.cpp:515
ser_readdata32be
uint32_t ser_readdata32be(Stream &s)
Definition: serialize.h:106
BlockFilterIndex::Init
bool Init() override
Initialize internal state from the database and block index.
Definition: blockfilterindex.cpp:111
CDBWrapper::NewIterator
CDBIterator * NewIterator()
Definition: dbwrapper.h:296
CDBIterator::GetValue
bool GetValue(V &value)
Definition: dbwrapper.h:158
BlockFilterIndex::m_filter_type
BlockFilterType m_filter_type
Definition: blockfilterindex.h:27
values
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Definition: scriptnum_tests.cpp:17
BaseIndex::Rewind
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.cpp:223
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
FlatFilePos
Definition: flatfile.h:14
BlockFilterIndex::LookupFilterRange
bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, std::vector< BlockFilter > &filters_out) const
Get a range of filters between two heights on a chain.
Definition: blockfilterindex.cpp:417
LookupOne
static bool LookupOne(const CDBWrapper &db, const CBlockIndex *block_index, DBVal &result)
Definition: blockfilterindex.cpp:304
LogPrintf
#define LogPrintf(...)
Definition: logging.h:188
ser_readdata8
uint8_t ser_readdata8(Stream &s)
Definition: serialize.h:82
BlockFilterIndex::Rewind
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
Definition: blockfilterindex.cpp:281
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:523
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:126
READWRITE
#define READWRITE(...)
Definition: serialize.h:140
ser_writedata8
void ser_writedata8(Stream &s, uint8_t obj)
Definition: serialize.h:53
LookupRange
static bool LookupRange(CDBWrapper &db, const std::string &index_name, int start_height, const CBlockIndex *stop_index, std::vector< DBVal > &results)
Definition: blockfilterindex.cpp:322
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:264
gArgs
ArgsManager gArgs
Definition: system.cpp:87
CDBIterator::Seek
void Seek(const K &key)
Definition: dbwrapper.h:137
BlockFilter::GetHash
uint256 GetHash() const
Compute the filter hash.
Definition: blockfilter.cpp:258
DB_BLOCK_HEIGHT
constexpr uint8_t DB_BLOCK_HEIGHT
Definition: blockfilterindex.cpp:30
CDBWrapper
Definition: dbwrapper.h:176
system.h
CBlock
Definition: block.h:62
BlockFilterIndex::ReadFilterFromDisk
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const
Definition: blockfilterindex.cpp:146
BlockFilterIndex::m_next_filter_pos
FlatFilePos m_next_filter_pos
Definition: blockfilterindex.h:31
BlockFilterIndex::LookupFilterHashRange
bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, std::vector< uint256 > &hashes_out) const
Get a range of filter hashes between two heights on a chain.
Definition: blockfilterindex.cpp:437
BaseIndex::CommitInternal
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:211
GetBlockFilterIndex
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
Definition: blockfilterindex.cpp:454
ForEachBlockFilterIndex
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
Definition: blockfilterindex.cpp:460
LOCK
#define LOCK(cs)
Definition: sync.h:226
blockstorage.h
ser_writedata32be
void ser_writedata32be(Stream &s, uint32_t obj)
Definition: serialize.h:72
fs::create_directories
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:150
CDBIterator::GetKey
bool GetKey(K &key)
Definition: dbwrapper.h:147
BlockFilterIndex::WriteBlock
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
Definition: blockfilterindex.cpp:212
CF_HEADERS_CACHE_MAX_SZ
constexpr size_t CF_HEADERS_CACHE_MAX_SZ
Maximum size of the cfheaders cache We have a limit to prevent a bug in filling this cache potentiall...
Definition: blockfilterindex.cpp:41
MAX_FLTR_FILE_SIZE
constexpr unsigned int MAX_FLTR_FILE_SIZE
Definition: blockfilterindex.cpp:33
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
CFCHECKPT_INTERVAL
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
Definition: blockfilterindex.h:15
BlockFilterIndex::LookupFilter
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
Definition: blockfilterindex.cpp:377
InitBlockFilterIndex
bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size, bool f_memory, bool f_wipe)
Initialize a block filter index for the given type if one does not already exist.
Definition: blockfilterindex.cpp:465
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
FlatFilePos::nPos
unsigned int nPos
Definition: flatfile.h:17
BlockFilterIndex::GetFilterType
BlockFilterType GetFilterType() const
Definition: blockfilterindex.h:59
BlockFilterIndex::m_cs_headers_cache
Mutex m_cs_headers_cache
Definition: blockfilterindex.h:37
CopyHeightIndexToHashIndex
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int start_height, int stop_height)
Definition: blockfilterindex.cpp:255
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
BlockFilterIndex::BlockFilterIndex
BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
Definition: blockfilterindex.cpp:96
DB_FILTER_POS
constexpr uint8_t DB_FILTER_POS
Definition: blockfilterindex.cpp:31
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
TruncateFile
bool TruncateFile(FILE *file, unsigned int length)
Definition: system.cpp:1142
BlockFilterIndex::LookupFilterHeader
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out)
Get a single filter header by block.
Definition: blockfilterindex.cpp:387
DestroyAllBlockFilterIndexes
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
Definition: blockfilterindex.cpp:480
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63
BlockFilterIndex::m_filter_fileseq
std::unique_ptr< FlatFileSeq > m_filter_fileseq
Definition: blockfilterindex.h:32
CDBIterator
Definition: dbwrapper.h:117
BlockFilterIndex::GetName
const char * GetName() const override
Get the name of the index for display in logs.
Definition: blockfilterindex.h:52
BlockFilter
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:110