Bitcoin Core  31.0.0
P2P Digital Currency
txospenderindex.h
Go to the documentation of this file.
1 // Copyright (c) The Bitcoin 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_TXOSPENDERINDEX_H
6 #define BITCOIN_INDEX_TXOSPENDERINDEX_H
7 
8 #include <index/base.h>
9 #include <interfaces/chain.h>
10 #include <primitives/transaction.h>
11 #include <uint256.h>
12 #include <util/expected.h>
13 
14 #include <cstddef>
15 #include <cstdint>
16 #include <memory>
17 #include <optional>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 struct CDiskTxPos;
23 
24 static constexpr bool DEFAULT_TXOSPENDERINDEX{false};
25 
26 struct TxoSpender {
29 };
30 
36 class TxoSpenderIndex final : public BaseIndex
37 {
38 private:
39  std::unique_ptr<BaseIndex::DB> m_db;
40  std::pair<uint64_t, uint64_t> m_siphash_key;
41  bool AllowPrune() const override { return false; }
42  void WriteSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
43  void EraseSpenderInfos(const std::vector<std::pair<COutPoint, CDiskTxPos>>& items);
45 
46 protected:
48 
49  bool CustomAppend(const interfaces::BlockInfo& block) override;
50 
51  bool CustomRemove(const interfaces::BlockInfo& block) override;
52 
53  BaseIndex::DB& GetDB() const override;
54 
55 public:
56  explicit TxoSpenderIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
57 
68  util::Expected<std::optional<TxoSpender>, std::string> FindSpender(const COutPoint& txo) const;
69 };
70 
72 extern std::unique_ptr<TxoSpenderIndex> g_txospenderindex;
73 
74 
75 #endif // BITCOIN_INDEX_TXOSPENDERINDEX_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static constexpr bool DEFAULT_TXOSPENDERINDEX
bool AllowPrune() const override
bool CustomRemove(const interfaces::BlockInfo &block) override
Rewind index by one block during a chain reorg.
uint256 block_hash
TxoSpenderIndex is used to look up which transaction spent a given output.
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
std::pair< uint64_t, uint64_t > m_siphash_key
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:64
CTransactionRef tx
interfaces::Chain::NotifyOptions CustomOptions() override
Return custom notification options for index.
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:19
Base class for indices of blockchain data.
Definition: base.h:54
Options specifying which chain notifications are required.
Definition: chain.h:318
util::Expected< TxoSpender, std::string > ReadTransaction(const CDiskTxPos &pos) const
void WriteSpenderInfos(const std::vector< std::pair< COutPoint, CDiskTxPos >> &items)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
BaseIndex::DB & GetDB() const override
256-bit opaque blob.
Definition: uint256.h:195
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
std::unique_ptr< BaseIndex::DB > m_db
std::unique_ptr< TxoSpenderIndex > g_txospenderindex
The global txo spender index. May be null.
util::Expected< std::optional< TxoSpender >, std::string > FindSpender(const COutPoint &txo) const
Search the index for a transaction that spends the given outpoint.
void EraseSpenderInfos(const std::vector< std::pair< COutPoint, CDiskTxPos >> &items)
TxoSpenderIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)