LCOV - code coverage report
Current view: top level - src/index - txindex.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 87.2 % 47 41
Test Date: 2025-08-25 05:11:47 Functions: 100.0 % 9 9
Branches: 53.4 % 58 31

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

Generated by: LCOV version 2.0-1