7#include <blockfilter.h>
83 if (
filter_name.empty())
throw std::invalid_argument(
"unknown filter_type");
86 fs::create_directories(path);
106 LogError(
"Cannot read current %s state; index may be corrupted",
119 LogError(
"Cannot read last block filter header; index may be corrupted");
138 if (!file.Commit()) {
143 if (file.fclose() != 0) {
165 LogError(
"Checksum mismatch in filter decode.");
170 catch (
const std::exception&
e) {
171 LogError(
"Failed to deserialize block filter from disk: %s",
e.what());
243 LogError(
"previous block header belongs to unexpected block %s; expected %s",
265 std::pair<uint256, DBVal> value;
267 value.second.hash = filter.
GetHash();
280 std::unique_ptr<CDBIterator>
db_it(
m_db->NewIterator());
285 if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*
db_it, batch,
m_name, block.
height)) {
293 m_db->WriteBatch(batch);
308 LogError(
"start height (%d) is greater than stop height (%d)",
317 std::unique_ptr<CDBIterator>
db_it(
db.NewIterator());
326 LogError(
"unable to read value in %s at key (%c, %d)",
350 LogError(
"unable to read value in %s at key (%c, %s)",
402 std::vector<DBVal> entries;
409 for (
const auto& entry : entries) {
423 std::vector<DBVal> entries;
430 for (
const auto& entry : entries) {
454 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
constexpr unsigned int MAX_FLTR_FILE_SIZE
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
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)
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,...
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)
std::string ToString() const
static path u8path(std::string_view utf8_str)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
static bool LookUpOne(const CDBWrapper &db, const interfaces::BlockRef &block, DBVal &result)
static constexpr uint8_t DB_BLOCK_HASH
static constexpr uint8_t DB_BLOCK_HEIGHT
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
uint64_t GetSerializeSize(const T &t)
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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.