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 : 0 : 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 : : [[nodiscard]] bool WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos);
50 : : };
51 : :
52 : 0 : TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
53 [ # # # # : 0 : BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
# # ]
54 : 0 : {}
55 : :
56 : 0 : bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const
57 : : {
58 : 0 : return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
59 : : }
60 : :
61 : 0 : bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos)
62 : : {
63 : 0 : CDBBatch batch(*this);
64 [ # # # # ]: 0 : for (const auto& [txid, pos] : v_pos) {
65 [ # # ]: 0 : batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
66 : : }
67 [ # # ]: 0 : return WriteBatch(batch);
68 : 0 : }
69 : :
70 : 0 : TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
71 [ # # # # ]: 0 : : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
72 : 0 : {}
73 : :
74 : 0 : TxIndex::~TxIndex() = default;
75 : :
76 : 0 : bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
77 : : {
78 : : // Exclude genesis block transaction because outputs are not spendable.
79 [ # # ]: 0 : if (block.height == 0) return true;
80 : :
81 [ # # ]: 0 : assert(block.data);
82 [ # # # # : 0 : CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
# # ]
83 : 0 : std::vector<std::pair<Txid, CDiskTxPos>> vPos;
84 [ # # ]: 0 : vPos.reserve(block.data->vtx.size());
85 [ # # ]: 0 : for (const auto& tx : block.data->vtx) {
86 [ # # ]: 0 : vPos.emplace_back(tx->GetHash(), pos);
87 : 0 : pos.nTxOffset += ::GetSerializeSize(TX_WITH_WITNESS(*tx));
88 : : }
89 [ # # ]: 0 : return m_db->WriteTxs(vPos);
90 : 0 : }
91 : :
92 : 0 : BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
93 : :
94 : 0 : bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const
95 : : {
96 : 0 : CDiskTxPos postx;
97 [ # # ]: 0 : if (!m_db->ReadTxPos(tx_hash, postx)) {
98 : : return false;
99 : : }
100 : :
101 : 0 : AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
102 [ # # ]: 0 : if (file.IsNull()) {
103 [ # # ]: 0 : LogError("OpenBlockFile failed");
104 : : return false;
105 : : }
106 : 0 : CBlockHeader header;
107 : 0 : try {
108 [ # # ]: 0 : file >> header;
109 [ # # ]: 0 : file.seek(postx.nTxOffset, SEEK_CUR);
110 [ # # ]: 0 : file >> TX_WITH_WITNESS(tx);
111 [ - - ]: 0 : } catch (const std::exception& e) {
112 [ - - ]: 0 : LogError("Deserialize or I/O error - %s", e.what());
113 : 0 : return false;
114 : 0 : }
115 [ # # ]: 0 : if (tx->GetHash() != tx_hash) {
116 [ # # ]: 0 : LogError("txid mismatch");
117 : : return false;
118 : : }
119 [ # # ]: 0 : block_hash = header.GetHash();
120 : : return true;
121 : 0 : }
|