11#include <validation.h>
66 DBHeightKey() : height(0) {}
77 throw std::ios_base::failure(
78 "Invalid format for block filter index DB height key");
93 throw std::ios_base::failure(
94 "Invalid format for block filter index DB hash key");
111 throw std::invalid_argument(
"unknown filter_type");
132 "%s: Cannot read current %s state; index may be corrupted",
153 return error(
"%s: Failed to commit filter file %d",
__func__,
174 }
catch (
const std::exception &
e) {
175 return error(
"%s: Failed to deserialize block filter from disk: %s",
242 std::pair<BlockHash, DBVal>
read_out;
249 return error(
"%s: previous block header belongs to unexpected "
250 "block %s; expected %s",
265 std::pair<BlockHash, DBVal> value;
267 value.second.hash = filter.
GetHash();
271 if (!
m_db->Write(DBHeightKey(pindex->
nHeight), value)) {
286 if (!
db_it.GetKey(key) || key.height != height) {
287 return error(
"%s: unexpected key in %s: expected (%c, %d)",
291 std::pair<BlockHash, DBVal> value;
292 if (!
db_it.GetValue(value)) {
293 return error(
"%s: unable to read value in %s at key (%c, %d)",
297 batch.
Write(DBHashKey(value.first), std::move(value.second));
309 std::unique_ptr<CDBIterator>
db_it(
m_db->NewIterator());
324 if (!
m_db->WriteBatch(batch)) {
336 std::pair<BlockHash, DBVal>
read_out;
341 result = std::move(
read_out.second);
347 return db.Read(DBHashKey(
block_index->GetBlockHash()), result);
358 return error(
"%s: start height (%d) is greater than stop height (%d)",
364 std::vector<std::pair<BlockHash, DBVal>> values(
results_size);
367 std::unique_ptr<CDBIterator>
db_it(
db.NewIterator());
370 if (!
db_it->Valid() || !
db_it->GetKey(key) || key.height != height) {
375 if (!
db_it->GetValue(values[i])) {
376 return error(
"%s: unable to read value in %s at key (%c, %d)",
394 if (block_hash == values[i].first) {
399 if (!
db.Read(DBHashKey(block_hash),
results[i])) {
400 return error(
"%s: unable to read value in %s at key (%c, %s)",
452 std::vector<DBVal> entries;
459 for (
const auto &entry : entries) {
474 std::vector<DBVal> entries;
481 for (
const auto &entry : entries) {
501 std::piecewise_construct, std::forward_as_tuple(
filter_type),
503 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.
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int start_height, int stop_height)
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
The index database stores three items for each block: the disk location of the encoded filter,...
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.
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
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.
static std::map< BlockFilterType, BlockFilterIndex > g_filter_indexes
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Non-refcounted RAII wrapper for FILE*.
virtual bool Init()
Initialize internal state from the database and block index.
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Chainstate * m_chainstate
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Complete block filter struct as defined in BIP 157.
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType GetFilterType() const
const BlockHash & GetBlockHash() const
uint256 GetHash() const
Compute the filter hash.
const std::vector< uint8_t > & GetEncodedFilter() const
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 CommitInternal(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const
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.
const char * GetName() const override
Get the name of the index for display in logs.
BlockFilterType GetFilterType() const
BlockFilterType m_filter_type
bool Init() override
Initialize internal state from the database and block index.
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
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.
std::unique_ptr< FlatFileSeq > m_filter_fileseq
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
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.
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.
FlatFilePos m_next_filter_pos
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
Undo information for a CBlock.
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
std::string ToString() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
bool TruncateFile(FILE *file, unsigned int length)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
bool error(const char *fmt, const Args &...args)
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...
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
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 &, char)=delete
void ser_writedata8(Stream &s, uint8_t obj)
Lowest-level serialization and conversion.
size_t GetSerializeSize(const T &t, int nVersion=0)
uint32_t ser_readdata32be(Stream &s)
void Serialize(Stream &, char)=delete
A BlockHash is a unqiue identifier for a block.