/Users/brunogarcia/projects/bitcoin-core-dev/src/index/txindex.cpp
Line | Count | Source |
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 | | 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 | | 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 | | bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const |
57 | 0 | { |
58 | 0 | return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos); |
59 | 0 | } |
60 | | |
61 | | void TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos) |
62 | 0 | { |
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 | 0 | } |
67 | 0 | WriteBatch(batch); |
68 | 0 | } |
69 | | |
70 | | 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 | | bool TxIndex::CustomAppend(const interfaces::BlockInfo& block) |
77 | 0 | { |
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 | 0 | } |
89 | 0 | m_db->WriteTxs(vPos); |
90 | 0 | return true; |
91 | 0 | } |
92 | | |
93 | 0 | BaseIndex::DB& TxIndex::GetDB() const { return *m_db; } |
94 | | |
95 | | bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const |
96 | 0 | { |
97 | 0 | CDiskTxPos postx; |
98 | 0 | if (!m_db->ReadTxPos(tx_hash, postx)) { |
99 | 0 | return false; |
100 | 0 | } |
101 | | |
102 | 0 | AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)}; |
103 | 0 | if (file.IsNull()) { |
104 | 0 | LogError("OpenBlockFile failed");Line | Count | Source | 370 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
105 | 0 | return false; |
106 | 0 | } |
107 | 0 | CBlockHeader header; |
108 | 0 | try { |
109 | 0 | file >> header; |
110 | 0 | file.seek(postx.nTxOffset, SEEK_CUR); |
111 | 0 | file >> TX_WITH_WITNESS(tx); |
112 | 0 | } catch (const std::exception& e) { |
113 | 0 | LogError("Deserialize or I/O error - %s", e.what());Line | Count | Source | 370 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
114 | 0 | return false; |
115 | 0 | } |
116 | 0 | if (tx->GetHash() != tx_hash) { |
117 | 0 | LogError("txid mismatch");Line | Count | Source | 370 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
118 | 0 | return false; |
119 | 0 | } |
120 | 0 | block_hash = header.GetHash(); |
121 | 0 | return true; |
122 | 0 | } |