Line data Source code
1 : // Copyright (c) 2026 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_INDEX_TIMESTAMPINDEX_H 6 : #define BITCOIN_INDEX_TIMESTAMPINDEX_H 7 : 8 : #include <index/base.h> 9 : #include <index/timestampindex_types.h> 10 : 11 : static constexpr bool DEFAULT_TIMESTAMPINDEX{false}; 12 : 13 : /** 14 : * TimestampIndex is used to map block timestamps to block hashes. 15 : * This allows efficient querying of blocks within a timestamp range. 16 : * 17 : * The index maintains a separate LevelDB database at <datadir>/indexes/timestampindex/ 18 : */ 19 : class TimestampIndex final : public BaseIndex 20 : { 21 : protected: 22 : class DB; 23 : 24 : private: 25 : const std::unique_ptr<DB> m_db; 26 : 27 : protected: 28 : class DB : public BaseIndex::DB 29 : { 30 : public: 31 : explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 32 : 33 : /// Write a timestamp index entry to the database 34 : bool Write(const CTimestampIndexKey& key); 35 : 36 : /// Read timestamp index entries within the given range 37 : bool ReadRange(uint32_t high, uint32_t low, std::vector<uint256>& hashes); 38 : 39 : /// Erase timestamp index entry 40 : bool EraseTimestampIndex(const CTimestampIndexKey& key); 41 : }; 42 : 43 : bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override; 44 : 45 : /// Custom rewind to handle timestamp index cleanup 46 : bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override; 47 : 48 : BaseIndex::DB& GetDB() const override; 49 160 : const char* GetName() const override { return "timestampindex"; } 50 : 51 : /// TimestampIndex works with pruned nodes since it only stores block metadata 52 34 : bool AllowPrune() const override { return true; } 53 : 54 : public: 55 : /// Constructs a new TimestampIndex. 56 : explicit TimestampIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 57 : 58 : /// Destructor 59 : virtual ~TimestampIndex() override; 60 : 61 : /// Retrieve block hashes within the given timestamp range [low, high] 62 : bool GetBlockHashes(uint32_t high, uint32_t low, std::vector<uint256>& hashes) const; 63 : }; 64 : 65 : /// Global TimestampIndex instance 66 : extern std::unique_ptr<TimestampIndex> g_timestampindex; 67 : 68 : #endif // BITCOIN_INDEX_TIMESTAMPINDEX_H