60 explicit DBHeightKey(
int height_in) : height(height_in) {}
62 template<
typename Stream>
69 template<
typename Stream>
74 throw std::ios_base::failure(
"Invalid format for block filter index DB height key");
83 explicit DBHashKey(
const uint256& hash_in) : hash(hash_in) {}
89 throw std::ios_base::failure(
"Invalid format for block filter index DB hash key");
101 size_t n_cache_size,
bool f_memory,
bool f_wipe)
103 , m_filter_type(filter_type)
106 if (filter_name.empty())
throw std::invalid_argument(
"unknown filter_type");
111 m_db = std::make_unique<BaseIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
129 LogError(
"Cannot read current %s state; index may be corrupted",
141 if (!op_last_header) {
142 LogError(
"Cannot read last block filter header; index may be corrupted");
161 if (!file.Commit()) {
166 if (file.fclose() != 0) {
178 if (filein.IsNull()) {
184 std::vector<uint8_t> encoded_filter;
186 filein >> block_hash >> encoded_filter;
187 if (
Hash(encoded_filter) != hash) {
188 LogError(
"Checksum mismatch in filter decode.");
193 catch (
const std::exception& e) {
194 LogError(
"Failed to deserialize block filter from disk: %s", e.what());
212 if (last_file.IsNull()) {
216 if (!last_file.Truncate(pos.
nPos)) {
220 if (!last_file.Commit()) {
222 (void)last_file.fclose();
225 if (last_file.fclose() != 0) {
243 if (fileout.IsNull()) {
250 if (fileout.fclose() != 0) {
260 std::pair<uint256, DBVal> read_out;
261 if (!
m_db->Read(DBHeightKey(height), read_out)) {
265 if (read_out.first != expected_block_hash) {
266 LogError(
"previous block header belongs to unexpected block %s; expected %s",
267 read_out.first.ToString(), expected_block_hash.
ToString());
271 return read_out.second.header;
286 if (bytes_written == 0)
return false;
288 std::pair<uint256, DBVal> value;
290 value.second.hash = filter.
GetHash();
291 value.second.header = filter_header;
294 if (!
m_db->Write(DBHeightKey(block_height), value)) {
303 const std::string& index_name,
int height)
305 DBHeightKey key(height);
308 if (!db_it.
GetKey(key) || key.height != height) {
309 LogError(
"unexpected key in %s: expected (%c, %d)",
314 std::pair<uint256, DBVal> value;
316 LogError(
"unable to read value in %s at key (%c, %d)",
321 batch.
Write(DBHashKey(value.first), std::move(value.second));
328 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
341 if (!
m_db->WriteBatch(batch))
return false;
352 std::pair<uint256, DBVal> read_out;
353 if (!db.
Read(DBHeightKey(block_index->
nHeight), read_out)) {
357 result = std::move(read_out.second);
367 const CBlockIndex* stop_index, std::vector<DBVal>& results)
369 if (start_height < 0) {
370 LogError(
"start height (%d) is negative", start_height);
373 if (start_height > stop_index->
nHeight) {
374 LogError(
"start height (%d) is greater than stop height (%d)",
375 start_height, stop_index->
nHeight);
379 size_t results_size =
static_cast<size_t>(stop_index->
nHeight - start_height + 1);
380 std::vector<std::pair<uint256, DBVal>>
values(results_size);
382 DBHeightKey key(start_height);
383 std::unique_ptr<CDBIterator> db_it(db.
NewIterator());
384 db_it->Seek(DBHeightKey(start_height));
385 for (
int height = start_height; height <= stop_index->
nHeight; ++height) {
386 if (!db_it->Valid() || !db_it->GetKey(key) || key.height != height) {
390 size_t i =
static_cast<size_t>(height - start_height);
391 if (!db_it->GetValue(
values[i])) {
392 LogError(
"unable to read value in %s at key (%c, %d)",
400 results.resize(results_size);
405 block_index && block_index->
nHeight >= start_height;
406 block_index = block_index->pprev) {
407 uint256 block_hash = block_index->GetBlockHash();
409 size_t i =
static_cast<size_t>(block_index->nHeight - start_height);
410 if (block_hash ==
values[i].first) {
411 results[i] = std::move(
values[i].second);
415 if (!db.
Read(DBHashKey(block_hash), results[i])) {
416 LogError(
"unable to read value in %s at key (%c, %s)",
443 auto header = m_headers_cache.find(block_index->
GetBlockHash());
444 if (header != m_headers_cache.end()) {
445 header_out = header->second;
458 m_headers_cache.emplace(block_index->
GetBlockHash(), entry.header);
461 header_out = entry.header;
466 std::vector<BlockFilter>& filters_out)
const
468 std::vector<DBVal> entries;
473 filters_out.resize(entries.size());
474 auto filter_pos_it = filters_out.begin();
475 for (
const auto& entry : entries) {
486 std::vector<uint256>& hashes_out)
const
489 std::vector<DBVal> entries;
495 hashes_out.reserve(entries.size());
496 for (
const auto& entry : entries) {
497 hashes_out.push_back(entry.hash);
514 size_t n_cache_size,
bool f_memory,
bool f_wipe)
517 std::forward_as_tuple(filter_type),
518 std::forward_as_tuple(make_chain(), filter_type,
519 n_cache_size, f_memory, f_wipe));
520 return result.second;
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
constexpr unsigned int FLTR_FILE_CHUNK_SIZE
The pre-allocation chunk size for fltr?????.dat files.
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
constexpr uint8_t DB_FILTER_POS
static bool LookupOne(const CDBWrapper &db, const CBlockIndex *block_index, DBVal &result)
constexpr unsigned int MAX_FLTR_FILE_SIZE
constexpr uint8_t DB_BLOCK_HASH
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int height)
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...
bool InitBlockFilterIndex(std::function< std::unique_ptr< interfaces::Chain >()> make_chain, 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.
static bool LookupRange(CDBWrapper &db, const std::string &index_name, int start_height, const CBlockIndex *stop_index, std::vector< DBVal > &results)
constexpr uint8_t DB_BLOCK_HEIGHT
static std::map< BlockFilterType, BlockFilterIndex > g_filter_indexes
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
#define Assert(val)
Identity function.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Non-refcounted RAII wrapper for FILE*.
Base class for indices of blockchain data.
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Complete block filter struct as defined in BIP 157.
const uint256 & GetBlockHash() const LIFETIMEBOUND
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType GetFilterType() const
uint256 GetHash() const
Compute the filter hash.
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
std::unique_ptr< BaseIndex::DB > m_db
bool CustomInit(const std::optional< interfaces::BlockRef > &block) override
Initialize internal state from the database and block index.
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.
BlockFilterType GetFilterType() const
bool CustomRemove(const interfaces::BlockInfo &block) override
Rewind index by one block during a chain reorg.
bool CustomCommit(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
BlockFilterType m_filter_type
BlockFilterIndex(std::unique_ptr< interfaces::Chain > chain, 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.
interfaces::Chain::NotifyOptions CustomOptions() override
Return custom notification options for index.
std::unique_ptr< FlatFileSeq > m_filter_fileseq
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
bool ReadFilterFromDisk(const FlatFilePos &pos, const uint256 &hash, BlockFilter &filter) const
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.
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
std::optional< uint256 > ReadFilterHeader(int height, const uint256 &expected_block_hash)
bool Write(const BlockFilter &filter, uint32_t block_height, const uint256 &filter_header)
FlatFilePos m_next_filter_pos
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
bool Read(const K &key, V &value) const
CDBIterator * NewIterator()
std::string ToString() const
static path u8path(const std::string &utf8_str)
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...
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
size_t GetSerializeSize(const T &t)
void Serialize(Stream &, V)=delete
uint8_t ser_readdata8(Stream &s)
void ser_writedata32be(Stream &s, uint32_t obj)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
void Unserialize(Stream &, V)=delete
void ser_writedata8(Stream &s, uint8_t obj)
uint32_t ser_readdata32be(Stream &s)
Block data sent with blockConnected, blockDisconnected notifications.
const uint256 * prev_hash
const CBlockUndo * undo_data
Options specifying which chain notifications are required.
bool connect_undo_data
Include undo data with block connected notifications.
std::string SysErrorString(int err)
Return system error string from errno value.