Branch data Line data Source code
1 : :
2 : : // Copyright (c) The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_INDEX_TXOSPENDERINDEX_H
7 : : #define BITCOIN_INDEX_TXOSPENDERINDEX_H
8 : :
9 : : #include <index/base.h>
10 : : #include <interfaces/chain.h>
11 : : #include <primitives/transaction.h>
12 : : #include <uint256.h>
13 : : #include <util/expected.h>
14 : :
15 : : #include <cstddef>
16 : : #include <cstdint>
17 : : #include <memory>
18 : : #include <optional>
19 : : #include <string>
20 : : #include <utility>
21 : : #include <vector>
22 : :
23 : : struct CDiskTxPos;
24 : :
25 : : static constexpr bool DEFAULT_TXOSPENDERINDEX{false};
26 : :
27 [ + + ][ + - : 140 : struct TxoSpender {
- - - + -
- ]
28 : : CTransactionRef tx;
29 : : uint256 block_hash;
30 : : };
31 : :
32 : : /**
33 : : * TxoSpenderIndex is used to look up which transaction spent a given output.
34 : : * The index is written to a LevelDB database and, for each input of each transaction in a block,
35 : : * records the outpoint that is spent and the hash of the spending transaction.
36 : : */
37 : : class TxoSpenderIndex final : public BaseIndex
38 : : {
39 : : private:
40 : : std::unique_ptr<BaseIndex::DB> m_db;
41 : : std::pair<uint64_t, uint64_t> m_siphash_key;
42 : 41 : bool AllowPrune() const override { return false; }
43 : : void WriteSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
44 : : void EraseSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
45 : : util::Expected<TxoSpender, std::string> ReadTransaction(const CDiskTxPos& pos) const;
46 : :
47 : : protected:
48 : : interfaces::Chain::NotifyOptions CustomOptions() override;
49 : :
50 : : bool CustomAppend(const interfaces::BlockInfo& block) override;
51 : :
52 : : bool CustomRemove(const interfaces::BlockInfo& block) override;
53 : :
54 : : BaseIndex::DB& GetDB() const override;
55 : :
56 : : public:
57 : : explicit TxoSpenderIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
58 : :
59 : : util::Expected<std::optional<TxoSpender>, std::string> FindSpender(const COutPoint& txo) const;
60 : : };
61 : :
62 : : /// The global txo spender index. May be null.
63 : : extern std::unique_ptr<TxoSpenderIndex> g_txospenderindex;
64 : :
65 : :
66 : : #endif // BITCOIN_INDEX_TXOSPENDERINDEX_H
|