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_SPENTINDEX_H 6 : #define BITCOIN_INDEX_SPENTINDEX_H 7 : 8 : #include <index/base.h> 9 : #include <index/spentindex_types.h> 10 : 11 : #include <map> 12 : 13 : static constexpr bool DEFAULT_SPENTINDEX{false}; 14 : 15 : struct CSpentIndexTxInfo { 16 : std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mSpentInfo; 17 : }; 18 : 19 : /** 20 : * SpentIndex tracks which transactions spend specific outputs. 21 : * For each spent output, it records the spending transaction details, 22 : * including height, amount, and address information. 23 : * 24 : * The index reads undo data to extract spent output information (amount, address), 25 : * which requires that undo files are available. Therefore, this index is NOT 26 : * compatible with pruned nodes. 27 : * 28 : * The index maintains a separate LevelDB database at <datadir>/indexes/spentindex/ 29 : */ 30 : class SpentIndex final : public BaseIndex 31 : { 32 : protected: 33 : class DB; 34 : 35 : private: 36 : const std::unique_ptr<DB> m_db; 37 : 38 : protected: 39 : class DB : public BaseIndex::DB 40 : { 41 : public: 42 : explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 43 : 44 : /// Write a batch of spent index entries 45 : bool WriteBatch(const std::vector<CSpentIndexEntry>& entries); 46 : 47 : /// Read spent information for a specific output 48 : bool ReadSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value); 49 : 50 : /// Erase spent index entries 51 : bool EraseSpentIndex(const std::vector<CSpentIndexKey>& keys); 52 : }; 53 : 54 : bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override; 55 : 56 : /// Custom rewind to handle spent index cleanup 57 : bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override; 58 : 59 : BaseIndex::DB& GetDB() const override; 60 0 : const char* GetName() const override { return "spentindex"; } 61 : 62 : /// SpentIndex cannot work with pruned nodes as it requires UTXO data 63 0 : bool AllowPrune() const override { return false; } 64 : 65 : public: 66 : /// Constructs a new SpentIndex. 67 : explicit SpentIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 68 : 69 : /// Destructor 70 : virtual ~SpentIndex() override; 71 : 72 : /// Retrieve spent information for a specific output 73 : bool GetSpentInfo(const CSpentIndexKey& key, CSpentIndexValue& value) const; 74 : }; 75 : 76 : /// Global SpentIndex instance 77 : extern std::unique_ptr<SpentIndex> g_spentindex; 78 : 79 : #endif // BITCOIN_INDEX_SPENTINDEX_H