 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
56 explicit DBHeightKey(
int height_in) : height(height_in) {}
58 template<
typename Stream>
65 template<
typename Stream>
70 throw std::ios_base::failure(
"Invalid format for block filter index DB height key");
79 explicit DBHashKey(
const uint256& hash_in) : hash(hash_in) {}
85 throw std::ios_base::failure(
"Invalid format for block filter index DB hash key");
97 size_t n_cache_size,
bool f_memory,
bool f_wipe)
98 : m_filter_type(filter_type)
101 if (filter_name.empty())
throw std::invalid_argument(
"unknown filter_type");
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);
118 return error(
"%s: Cannot read current %s state; index may be corrupted",
136 return error(
"%s: Failed to open filter file %d", __func__, pos.
nFile);
139 return error(
"%s: Failed to commit filter file %d", __func__, pos.
nFile);
154 std::vector<uint8_t> encoded_filter;
156 filein >> block_hash >> encoded_filter;
159 catch (
const std::exception& e) {
160 return error(
"%s: Failed to deserialize block filter from disk: %s", __func__, e.what());
178 LogPrintf(
"%s: Failed to open filter file %d\n", __func__, pos.
nFile);
182 LogPrintf(
"%s: Failed to truncate filter file %d\n", __func__, pos.
nFile);
186 LogPrintf(
"%s: Failed to commit filter file %d\n", __func__, pos.
nFile);
198 LogPrintf(
"%s: out of disk space\n", __func__);
204 LogPrintf(
"%s: Failed to open filter file %d\n", __func__, pos.
nFile);
222 std::pair<uint256, DBVal> read_out;
223 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
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());
233 prev_header = read_out.second.header;
239 if (bytes_written == 0)
return false;
241 std::pair<uint256, DBVal> value;
243 value.second.hash = filter.
GetHash();
247 if (!
m_db->Write(DBHeightKey(pindex->
nHeight), value)) {
256 const std::string& index_name,
257 int start_height,
int stop_height)
259 DBHeightKey key(start_height);
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)",
268 std::pair<uint256, DBVal> value;
270 return error(
"%s: unable to read value in %s at key (%c, %d)",
274 batch.
Write(DBHashKey(value.first), std::move(value.second));
286 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
299 if (!
m_db->WriteBatch(batch))
return false;
308 std::pair<uint256, DBVal> read_out;
309 if (!db.
Read(DBHeightKey(block_index->
nHeight), read_out)) {
313 result = std::move(read_out.second);
323 const CBlockIndex* stop_index, std::vector<DBVal>& results)
325 if (start_height < 0) {
326 return error(
"%s: start height (%d) is negative", __func__, start_height);
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);
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);
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) {
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)",
353 results.resize(results_size);
358 block_index && block_index->
nHeight >= start_height;
359 block_index = block_index->pprev) {
360 uint256 block_hash = block_index->GetBlockHash();
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);
368 if (!db.
Read(DBHashKey(block_hash), results[i])) {
369 return error(
"%s: unable to read value in %s at key (%c, %s)",
395 auto header = m_headers_cache.find(block_index->
GetBlockHash());
396 if (header != m_headers_cache.end()) {
397 header_out = header->second;
410 m_headers_cache.emplace(block_index->
GetBlockHash(), entry.header);
413 header_out = entry.header;
418 std::vector<BlockFilter>& filters_out)
const
420 std::vector<DBVal> entries;
425 filters_out.resize(entries.size());
426 auto filter_pos_it = filters_out.begin();
427 for (
const auto& entry : entries) {
438 std::vector<uint256>& hashes_out)
const
441 std::vector<DBVal> entries;
447 hashes_out.reserve(entries.size());
448 for (
const auto& entry : entries) {
449 hashes_out.push_back(entry.hash);
466 size_t n_cache_size,
bool f_memory,
bool f_wipe)
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;
constexpr unsigned int FLTR_FILE_CHUNK_SIZE
The pre-allocation chunk size for fltr?????.dat files.
size_t GetSerializeSize(const T &t, int nVersion=0)
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockFilterType GetFilterType() const
const uint256 & GetBlockHash() const
Batch of changes queued to be written to a CDBWrapper.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
std::unique_ptr< BaseIndex::DB > m_db
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
virtual bool Init()
Initialize internal state from the database and block index.
const std::vector< unsigned char > & GetEncodedFilter() const
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
int nHeight
height of the entry in the chain. The genesis block has height 0
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
static std::map< BlockFilterType, BlockFilterIndex > g_filter_indexes
bool CommitInternal(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
bool Read(const K &key, V &value) const
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
constexpr uint8_t DB_BLOCK_HASH
void Unserialize(Stream &, char)=delete
void Serialize(Stream &, char)=delete
void Write(const K &key, const V &value)
Non-refcounted RAII wrapper for FILE*.
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
uint32_t ser_readdata32be(Stream &s)
bool Init() override
Initialize internal state from the database and block index.
CDBIterator * NewIterator()
BlockFilterType m_filter_type
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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.
static bool LookupOne(const CDBWrapper &db, const CBlockIndex *block_index, DBVal &result)
uint8_t ser_readdata8(Stream &s)
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
std::string ToString() const
void ser_writedata8(Stream &s, uint8_t obj)
static bool LookupRange(CDBWrapper &db, const std::string &index_name, int start_height, const CBlockIndex *stop_index, std::vector< DBVal > &results)
uint256 GetBlockHash() const
uint256 GetHash() const
Compute the filter hash.
constexpr uint8_t DB_BLOCK_HEIGHT
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const
FlatFilePos m_next_filter_pos
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.
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
void ser_writedata32be(Stream &s, uint32_t obj)
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...
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
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...
constexpr unsigned int MAX_FLTR_FILE_SIZE
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
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.
BlockFilterType GetFilterType() const
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int start_height, int stop_height)
bool error(const char *fmt, const Args &... args)
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.
constexpr uint8_t DB_FILTER_POS
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool TruncateFile(FILE *file, unsigned int length)
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out)
Get a single filter header by block.
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
Undo information for a CBlock.
std::unique_ptr< FlatFileSeq > m_filter_fileseq
const char * GetName() const override
Get the name of the index for display in logs.
Complete block filter struct as defined in BIP 157.