Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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) 2017-present 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_TXINDEX_H6#define BITCOIN_INDEX_TXINDEX_H8#include <index/base.h>9#include <primitives/transaction.h>10#include <uint256.h>12#include <cstddef>13#include <memory>14#include <optional>16namespace interfaces {17class Chain;18}19namespace txindex_tests {20class TxIndexTest;21}23inline constexpr bool DEFAULT_TXINDEX{false};25/// A found transaction and the hash of the block that contains it.26struct TxIndexResult {27uint256 block_hash;28CTransactionRef tx;29};31/**32* TxIndex is used to look up transactions included in the blockchain by hash.33* The index is written to a LevelDB database and records the block sequence34* number and serialized block offset of each transaction by transaction hash.35*/36class TxIndex final : public BaseIndex37{38protected:39class DB;41private:42friend class txindex_tests::TxIndexTest;43const std::unique_ptr<DB> m_db;45bool AllowPrune() const override { return false; }47/// Look up a transaction among the legacy (full-txid) entries.48std::optional<TxIndexResult> FindLegacyTx(const Txid& tx_hash) const;50protected:51bool CustomAppend(const interfaces::BlockInfo& block) override;53BaseIndex::DB& GetDB() const override;55public:56/// Constructs the index, which becomes available to be queried.57explicit TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);59// Destructor is declared because this class contains a unique_ptr to an incomplete type.60virtual ~TxIndex() override;62/// Look up a transaction by hash.63///64/// @param[in] tx_hash The hash of the transaction to be returned.65/// @return the transaction and containing block hash, or nullopt if it is not found66std::optional<TxIndexResult> FindTx(const Txid& tx_hash) const;67};69/// The global transaction index, used in GetTransaction. May be null.70extern std::unique_ptr<TxIndex> g_txindex;72#endif // BITCOIN_INDEX_TXINDEX_H