Branch data Line data Source code
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>
16 : : #include <primitives/transaction.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 : :
33 : : constexpr uint8_t DB_TXINDEX{'t'};
34 : :
35 : : std::unique_ptr<TxIndex> g_txindex;
36 : :
37 : :
38 : : /** Access to the txindex database (indexes/txindex/) */
39 : 30 : class TxIndex::DB : public BaseIndex::DB
40 : : {
41 : : public:
42 : : explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
43 : :
44 : : /// Read the disk location of the transaction data with the given hash. Returns false if the
45 : : /// transaction hash is not indexed.
46 : : bool ReadTxPos(const Txid& txid, CDiskTxPos& pos) const;
47 : :
48 : : /// Write a batch of transaction positions to the DB.
49 : : void WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos);
50 : : };
51 : :
52 : 33 : TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
53 [ + - + - : 165 : BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
+ + ]
54 : 30 : {}
55 : :
56 : 244 : bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const
57 : : {
58 : 244 : return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
59 : : }
60 : :
61 : 3434 : void TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos)
62 : : {
63 : 3434 : CDBBatch batch(*this);
64 [ + - + + ]: 7078 : for (const auto& [txid, pos] : v_pos) {
65 [ + - ]: 3644 : batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
66 : : }
67 [ + - ]: 3434 : WriteBatch(batch);
68 : 3434 : }
69 : :
70 : 33 : TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
71 [ + - + + ]: 33 : : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
72 : 33 : {}
73 : :
74 : 59 : TxIndex::~TxIndex() = default;
75 : :
76 : 3445 : bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
77 : : {
78 : : // Exclude genesis block transaction because outputs are not spendable.
79 [ + + ]: 3445 : if (block.height == 0) return true;
80 : :
81 [ - + ]: 3434 : assert(block.data);
82 [ - + - + : 3434 : CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
+ - ]
83 : 3434 : std::vector<std::pair<Txid, CDiskTxPos>> vPos;
84 [ + - ]: 3434 : vPos.reserve(block.data->vtx.size());
85 [ + + ]: 7078 : for (const auto& tx : block.data->vtx) {
86 [ + - ]: 3644 : vPos.emplace_back(tx->GetHash(), pos);
87 : 3644 : pos.nTxOffset += ::GetSerializeSize(TX_WITH_WITNESS(*tx));
88 : : }
89 [ + - ]: 3434 : m_db->WriteTxs(vPos);
90 : 3434 : return true;
91 : 3434 : }
92 : :
93 : 173 : BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
94 : :
95 : 244 : bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const
96 : : {
97 : 244 : CDiskTxPos postx;
98 [ + + ]: 244 : if (!m_db->ReadTxPos(tx_hash, postx)) {
99 : : return false;
100 : : }
101 : :
102 : 143 : AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
103 [ - + ]: 143 : if (file.IsNull()) {
104 [ # # ]: 0 : LogError("OpenBlockFile failed");
105 : : return false;
106 : : }
107 : 143 : CBlockHeader header;
108 : 143 : try {
109 [ + - ]: 143 : file >> header;
110 [ + - ]: 143 : file.seek(postx.nTxOffset, SEEK_CUR);
111 [ + - ]: 143 : file >> TX_WITH_WITNESS(tx);
112 [ - - ]: 0 : } catch (const std::exception& e) {
113 [ - - ]: 0 : LogError("Deserialize or I/O error - %s", e.what());
114 : 0 : return false;
115 : 0 : }
116 [ - + ]: 143 : if (tx->GetHash() != tx_hash) {
117 [ # # ]: 0 : LogError("txid mismatch");
118 : : return false;
119 : : }
120 [ + - ]: 143 : block_hash = header.GetHash();
121 : : return true;
122 : 143 : }
|