/Users/eugenesiegel/btc/bitcoin/src/index/txindex.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 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 <clientversion.h> | 
| 8 |  | #include <common/args.h> | 
| 9 |  | #include <index/disktxpos.h> | 
| 10 |  | #include <logging.h> | 
| 11 |  | #include <node/blockstorage.h> | 
| 12 |  | #include <primitives/transaction_identifier.h> | 
| 13 |  | #include <validation.h> | 
| 14 |  |  | 
| 15 |  | constexpr uint8_t DB_TXINDEX{'t'}; | 
| 16 |  |  | 
| 17 |  | std::unique_ptr<TxIndex> g_txindex; | 
| 18 |  |  | 
| 19 |  |  | 
| 20 |  | /** Access to the txindex database (indexes/txindex/) */ | 
| 21 |  | class TxIndex::DB : public BaseIndex::DB | 
| 22 |  | { | 
| 23 |  | public: | 
| 24 |  |     explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); | 
| 25 |  |  | 
| 26 |  |     /// Read the disk location of the transaction data with the given hash. Returns false if the | 
| 27 |  |     /// transaction hash is not indexed. | 
| 28 |  |     bool ReadTxPos(const Txid& txid, CDiskTxPos& pos) const; | 
| 29 |  |  | 
| 30 |  |     /// Write a batch of transaction positions to the DB. | 
| 31 |  |     [[nodiscard]] bool WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos); | 
| 32 |  | }; | 
| 33 |  |  | 
| 34 |  | TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) : | 
| 35 | 0 |     BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe) | 
| 36 | 0 | {} | 
| 37 |  |  | 
| 38 |  | bool TxIndex::DB::ReadTxPos(const Txid& txid, CDiskTxPos& pos) const | 
| 39 | 0 | { | 
| 40 | 0 |     return Read(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos); | 
| 41 | 0 | } | 
| 42 |  |  | 
| 43 |  | bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos) | 
| 44 | 0 | { | 
| 45 | 0 |     CDBBatch batch(*this); | 
| 46 | 0 |     for (const auto& [txid, pos] : v_pos) { | 
| 47 | 0 |         batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos); | 
| 48 | 0 |     } | 
| 49 | 0 |     return WriteBatch(batch); | 
| 50 | 0 | } | 
| 51 |  |  | 
| 52 |  | TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe) | 
| 53 | 0 |     : BaseIndex(std::move(chain), "txindex"), m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe)) | 
| 54 | 0 | {} | 
| 55 |  |  | 
| 56 | 0 | TxIndex::~TxIndex() = default; | 
| 57 |  |  | 
| 58 |  | bool TxIndex::CustomAppend(const interfaces::BlockInfo& block) | 
| 59 | 0 | { | 
| 60 |  |     // Exclude genesis block transaction because outputs are not spendable. | 
| 61 | 0 |     if (block.height == 0) return true; | 
| 62 |  |  | 
| 63 | 0 |     assert(block.data); | 
| 64 | 0 |     CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size())); | 
| 65 | 0 |     std::vector<std::pair<Txid, CDiskTxPos>> vPos; | 
| 66 | 0 |     vPos.reserve(block.data->vtx.size()); | 
| 67 | 0 |     for (const auto& tx : block.data->vtx) { | 
| 68 | 0 |         vPos.emplace_back(tx->GetHash(), pos); | 
| 69 | 0 |         pos.nTxOffset += ::GetSerializeSize(TX_WITH_WITNESS(*tx)); | 
| 70 | 0 |     } | 
| 71 | 0 |     return m_db->WriteTxs(vPos); | 
| 72 | 0 | } | 
| 73 |  |  | 
| 74 | 0 | BaseIndex::DB& TxIndex::GetDB() const { return *m_db; } | 
| 75 |  |  | 
| 76 |  | bool TxIndex::FindTx(const Txid& tx_hash, uint256& block_hash, CTransactionRef& tx) const | 
| 77 | 0 | { | 
| 78 | 0 |     CDiskTxPos postx; | 
| 79 | 0 |     if (!m_db->ReadTxPos(tx_hash, postx)) { | 
| 80 | 0 |         return false; | 
| 81 | 0 |     } | 
| 82 |  |  | 
| 83 | 0 |     AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)}; | 
| 84 | 0 |     if (file.IsNull()) { | 
| 85 | 0 |         LogError("OpenBlockFile failed");| Line | Count | Source |  | 358 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) | Line | Count | Source |  | 350 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) | 
 | 
 | 
| 86 | 0 |         return false; | 
| 87 | 0 |     } | 
| 88 | 0 |     CBlockHeader header; | 
| 89 | 0 |     try { | 
| 90 | 0 |         file >> header; | 
| 91 | 0 |         file.seek(postx.nTxOffset, SEEK_CUR); | 
| 92 | 0 |         file >> TX_WITH_WITNESS(tx); | 
| 93 | 0 |     } catch (const std::exception& e) { | 
| 94 | 0 |         LogError("Deserialize or I/O error - %s", e.what());| Line | Count | Source |  | 358 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) | Line | Count | Source |  | 350 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) | 
 | 
 | 
| 95 | 0 |         return false; | 
| 96 | 0 |     } | 
| 97 | 0 |     if (tx->GetHash() != tx_hash) { | 
| 98 | 0 |         LogError("txid mismatch");| Line | Count | Source |  | 358 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) | Line | Count | Source |  | 350 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) | 
 | 
 | 
| 99 | 0 |         return false; | 
| 100 | 0 |     } | 
| 101 | 0 |     block_hash = header.GetHash(); | 
| 102 | 0 |     return true; | 
| 103 | 0 | } |