Bitcoin Core 29.99.0
P2P Digital Currency
txindex.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2022 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#include <index/txindex.h>
6
7#include <clientversion.h>
8#include <common/args.h>
9#include <index/disktxpos.h>
10#include <logging.h>
11#include <node/blockstorage.h>
13#include <validation.h>
14
15constexpr uint8_t DB_TXINDEX{'t'};
16
17std::unique_ptr<TxIndex> g_txindex;
18
19
22{
23public:
24 explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
25
28 bool ReadTxPos(const Txid& txid, CDiskTxPos& pos) const;
29
31 [[nodiscard]] bool WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos);
32};
33
34TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
35 BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
36{}
37
38bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const
39{
40 return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
41}
42
43bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos)
44{
45 CDBBatch batch(*this);
46 for (const auto& [txid, pos] : v_pos) {
47 batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
48 }
49 return WriteBatch(batch);
50}
51
52TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
53 : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
54{}
55
56TxIndex::~TxIndex() = default;
57
59{
60 // Exclude genesis block transaction because outputs are not spendable.
61 if (block.height == 0) return true;
62
63 assert(block.data);
64 CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
65 std::vector<std::pair<Txid, CDiskTxPos>> vPos;
66 vPos.reserve(block.data->vtx.size());
67 for (const auto& tx : block.data->vtx) {
68 vPos.emplace_back(tx->GetHash(), pos);
70 }
71 return m_db->WriteTxs(vPos);
72}
73
74BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
75
76bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const
77{
78 CDiskTxPos postx;
79 if (!m_db->ReadTxPos(tx_hash, postx)) {
80 return false;
81 }
82
84 if (file.IsNull()) {
85 LogError("OpenBlockFile failed");
86 return false;
87 }
88 CBlockHeader header;
89 try {
90 file >> header;
91 file.seek(postx.nTxOffset, SEEK_CUR);
92 file >> TX_WITH_WITNESS(tx);
93 } catch (const std::exception& e) {
94 LogError("Deserialize or I/O error - %s", e.what());
95 return false;
96 }
97 if (tx->GetHash() != tx_hash) {
98 LogError("txid mismatch");
99 return false;
100 }
101 block_hash = header.GetHash();
102 return true;
103}
ArgsManager gArgs
Definition: args.cpp:42
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:371
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:53
Base class for indices of blockchain data.
Definition: base.h:43
Chainstate * m_chainstate
Definition: base.h:105
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
uint256 GetHash() const
Definition: block.cpp:11
std::vector< CTransactionRef > vtx
Definition: block.h:72
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:72
void Write(const K &key, const V &value)
Definition: dbwrapper.h:96
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:568
Access to the txindex database (indexes/txindex/)
Definition: txindex.cpp:22
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Definition: txindex.cpp:34
bool WriteTxs(const std::vector< std::pair< Txid, CDiskTxPos > > &v_pos)
Write a batch of transaction positions to the DB.
Definition: txindex.cpp:43
bool ReadTxPos(const Txid &txid, CDiskTxPos &pos) const
Read the disk location of the transaction data with the given hash.
Definition: txindex.cpp:38
TxIndex is used to look up transactions included in the blockchain by hash.
Definition: txindex.h:18
BaseIndex::DB & GetDB() const override
Definition: txindex.cpp:74
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
Definition: txindex.cpp:58
bool FindTx(const Txid &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Definition: txindex.cpp:76
TxIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
Definition: txindex.cpp:52
virtual ~TxIndex() override
const std::unique_ptr< DB > m_db
Definition: txindex.h:23
bool IsBlockPruned(const CBlockIndex &block) const EXCLUSIVE_LOCKS_REQUIRED(void UpdatePruneLock(const std::string &name, const PruneLockInfo &lock_info) EXCLUSIVE_LOCKS_REQUIRED(AutoFile OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) const
Check whether the block associated with this index entry is pruned or not.
Definition: blockstorage.h:403
const uint256 & ToUint256() const LIFETIMEBOUND
256-bit opaque blob.
Definition: uint256.h:196
#define LogError(...)
Definition: logging.h:358
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1094
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
Definition: serialize.h:288
unsigned int nTxOffset
Definition: disktxpos.h:13
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:79
unsigned data_pos
Definition: chain.h:84
const CBlock * data
Definition: chain.h:85
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
Definition: txindex.cpp:17
constexpr uint8_t DB_TXINDEX
Definition: txindex.cpp:15
assert(!tx.IsCoinBase())