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