diff options
author | John Newbery <john@johnnewbery.com> | 2020-05-10 22:28:21 -0400 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2020-05-18 12:54:07 -0400 |
commit | 0187d4c118ab4c0f5c2d4fb180c2a8dea8ac53cf (patch) | |
tree | 606fe9d9496a2270afefe0f7ac7ffe6513f07614 /src/index/blockfilterindex.h | |
parent | 8da1e43b63cb36759eeb1fcfd6768163265c44e2 (diff) | |
download | bitcoin-0187d4c118ab4c0f5c2d4fb180c2a8dea8ac53cf.tar.xz |
[indexes] Add compact block filter headers cache
Cache block filter headers at heights of multiples of 1000 in memory.
Block filter headers at height 1000x are checkpointed, and will be the
most frequently requested. Cache them in memory to avoid costly disk
reads.
Diffstat (limited to 'src/index/blockfilterindex.h')
-rw-r--r-- | src/index/blockfilterindex.h | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index 436d52515f..7ca43540c7 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -10,6 +10,14 @@ #include <flatfile.h> #include <index/base.h> +/** Interval between compact filter checkpoints. See BIP 157. */ +static constexpr int CFCHECKPT_INTERVAL = 1000; + +struct FilterHeaderHasher +{ + size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } +}; + /** * BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of * blocks by height. An index is constructed for each supported filter type with its own database @@ -30,6 +38,9 @@ private: bool ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const; size_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter); + Mutex m_cs_headers_cache; + std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache); + protected: bool Init() override; @@ -54,7 +65,7 @@ public: bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const; /** Get a single filter header by block. */ - bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) const; + bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out); /** Get a range of filters between two heights on a chain. */ bool LookupFilterRange(int start_height, const CBlockIndex* stop_index, |