Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_INDEX_TXOSPENDERINDEX_H6#define BITCOIN_INDEX_TXOSPENDERINDEX_H8#include <index/base.h>9#include <interfaces/chain.h>10#include <primitives/transaction.h>11#include <uint256.h>12#include <util/expected.h>14#include <cstddef>15#include <cstdint>16#include <memory>17#include <optional>18#include <string>19#include <utility>20#include <vector>22struct CDiskTxPos;24inline constexpr bool DEFAULT_TXOSPENDERINDEX{false};26struct TxoSpender {27CTransactionRef tx;28uint256 block_hash;29};31/**32* TxoSpenderIndex is used to look up which transaction spent a given output.33* The index is written to a LevelDB database and, for each input of each transaction in a block,34* records the outpoint that is spent and the hash of the spending transaction.35*/36class TxoSpenderIndex final : public BaseIndex37{38private:39std::unique_ptr<BaseIndex::DB> m_db;40std::pair<uint64_t, uint64_t> m_siphash_key;41bool AllowPrune() const override { return false; }42void WriteSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);43void EraseSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);44util::Expected<TxoSpender, std::string> ReadTransaction(const CDiskTxPos& pos) const;46protected:47interfaces::Chain::NotifyOptions CustomOptions() override;49bool CustomAppend(const interfaces::BlockInfo& block) override;51bool CustomRemove(const interfaces::BlockInfo& block) override;53BaseIndex::DB& GetDB() const override;55public:56explicit TxoSpenderIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);58/**59* Search the index for a transaction that spends the given outpoint.60*61* @param[in] txo The outpoint to search for.62*63* @return std::nullopt if the outpoint has not been spent on-chain.64* std::optional{TxoSpender} if the output has been spent on-chain. Contains the spending transaction65* and the block it was confirmed in.66* util::Unexpected{error} if something unexpected happened (i.e. disk or deserialization error).67*/68util::Expected<std::optional<TxoSpender>, std::string> FindSpender(const COutPoint& txo) const;69};71/// The global txo spender index. May be null.72extern std::unique_ptr<TxoSpenderIndex> g_txospenderindex;75#endif // BITCOIN_INDEX_TXOSPENDERINDEX_H