LCOV - code coverage report
Current view: top level - src/index - txindex.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 85.4 % 48 41
Test Date: 2024-08-28 04:44:32 Functions: 88.9 % 9 8
Branches: 48.2 % 56 27

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

Generated by: LCOV version 2.0-1