Bitcoin Core 30.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 <common/args.h>
8#include <dbwrapper.h>
9#include <flatfile.h>
10#include <index/base.h>
11#include <index/disktxpos.h>
12#include <interfaces/chain.h>
13#include <logging.h>
14#include <node/blockstorage.h>
15#include <primitives/block.h>
17#include <serialize.h>
18#include <streams.h>
19#include <uint256.h>
20#include <util/fs.h>
21#include <validation.h>
22
23#include <cassert>
24#include <cstdint>
25#include <cstdio>
26#include <exception>
27#include <iterator>
28#include <span>
29#include <string>
30#include <utility>
31#include <vector>
32
33constexpr uint8_t DB_TXINDEX{'t'};
34
35std::unique_ptr<TxIndex> g_txindex;
36
37
40{
41public:
42 explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
43
46 bool ReadTxPos(const Txid& txid, CDiskTxPos& pos) const;
47
49 [[nodiscard]] bool WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos);
50};
51
52TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
53 BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
54{}
55
56bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const
57{
58 return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
59}
60
61bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos)
62{
63 CDBBatch batch(*this);
64 for (const auto& [txid, pos] : v_pos) {
65 batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
66 }
67 return WriteBatch(batch);
68}
69
70TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
71 : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
72{}
73
74TxIndex::~TxIndex() = default;
75
77{
78 // Exclude genesis block transaction because outputs are not spendable.
79 if (block.height == 0) return true;
80
81 assert(block.data);
82 CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
83 std::vector<std::pair<Txid, CDiskTxPos>> vPos;
84 vPos.reserve(block.data->vtx.size());
85 for (const auto& tx : block.data->vtx) {
86 vPos.emplace_back(tx->GetHash(), pos);
88 }
89 return m_db->WriteTxs(vPos);
90}
91
92BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
93
94bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const
95{
96 CDiskTxPos postx;
97 if (!m_db->ReadTxPos(tx_hash, postx)) {
98 return false;
99 }
100
101 AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
102 if (file.IsNull()) {
103 LogError("OpenBlockFile failed");
104 return false;
105 }
106 CBlockHeader header;
107 try {
108 file >> header;
109 file.seek(postx.nTxOffset, SEEK_CUR);
110 file >> TX_WITH_WITNESS(tx);
111 } catch (const std::exception& e) {
112 LogError("Deserialize or I/O error - %s", e.what());
113 return false;
114 }
115 if (tx->GetHash() != tx_hash) {
116 LogError("txid mismatch");
117 return false;
118 }
119 block_hash = header.GetHash();
120 return true;
121}
ArgsManager gArgs
Definition: args.cpp:40
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:65
Base class for indices of blockchain data.
Definition: base.h:55
Chainstate * m_chainstate
Definition: base.h:117
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:569
Access to the txindex database (indexes/txindex/)
Definition: txindex.cpp:40
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Definition: txindex.cpp:52
bool WriteTxs(const std::vector< std::pair< Txid, CDiskTxPos > > &v_pos)
Write a batch of transaction positions to the DB.
Definition: txindex.cpp:61
bool ReadTxPos(const Txid &txid, CDiskTxPos &pos) const
Read the disk location of the transaction data with the given hash.
Definition: txindex.cpp:56
TxIndex is used to look up transactions included in the blockchain by hash.
Definition: txindex.h:27
BaseIndex::DB & GetDB() const override
Definition: txindex.cpp:92
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
Definition: txindex.cpp:76
bool FindTx(const Txid &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Definition: txindex.cpp:94
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:70
virtual ~TxIndex() override
const std::unique_ptr< DB > m_db
Definition: txindex.h:32
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:370
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:80
unsigned data_pos
Definition: chain.h:85
const CBlock * data
Definition: chain.h:86
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
Definition: txindex.cpp:35
constexpr uint8_t DB_TXINDEX
Definition: txindex.cpp:33
assert(!tx.IsCoinBase())