#ifndef BITCOIN_INDEX_TXOSPENDERINDEX_H
#define BITCOIN_INDEX_TXOSPENDERINDEX_H
#include <index/base.h>
#include <interfaces/chain.h>
#include <primitives/transaction.h>
#include <uint256.h>
#include <util/expected.h>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
struct CDiskTxPos;
static constexpr bool DEFAULT_TXOSPENDERINDEX{false};
struct TxoSpender {
CTransactionRef tx;
uint256 block_hash;
};
class TxoSpenderIndex final : public BaseIndex
{
private:
std::unique_ptr<BaseIndex::DB> m_db;
std::pair<uint64_t, uint64_t> m_siphash_key;
bool AllowPrune() const override { return false; }
void WriteSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
void EraseSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
util::Expected<TxoSpender, std::string> ReadTransaction(const CDiskTxPos& pos) const;
protected:
interfaces::Chain::NotifyOptions CustomOptions() override;
bool CustomAppend(const interfaces::BlockInfo& block) override;
bool CustomRemove(const interfaces::BlockInfo& block) override;
BaseIndex::DB& GetDB() const override;
public:
explicit TxoSpenderIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
util::Expected<std::optional<TxoSpender>, std::string> FindSpender(const COutPoint& txo) const;
};
extern std::unique_ptr<TxoSpenderIndex> g_txospenderindex;
#endif