LCOV - code coverage report
Current view: top level - src/index - txindex.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 44 52 84.6 %
Date: 2022-04-21 14:51:19 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 11 14 78.6 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2017-2021 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 <index/disktxpos.h>
#       8                 :            : #include <node/blockstorage.h>
#       9                 :            : #include <util/system.h>
#      10                 :            : #include <validation.h>
#      11                 :            : 
#      12                 :            : using node::OpenBlockFile;
#      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                 :            : 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                 :            :     bool WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);
#      31                 :            : };
#      32                 :            : 
#      33                 :            : TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
#      34                 :            :     BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
#      35                 :         21 : {}
#      36                 :            : 
#      37                 :            : bool TxIndex::DB::ReadTxPos(const uint256 &txid, CDiskTxPos& pos) const
#      38                 :        444 : {
#      39                 :        444 :     return Read(std::make_pair(DB_TXINDEX, txid), pos);
#      40                 :        444 : }
#      41                 :            : 
#      42                 :            : bool TxIndex::DB::WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos)
#      43                 :       1620 : {
#      44                 :       1620 :     CDBBatch batch(*this);
#      45         [ +  + ]:       1705 :     for (const auto& tuple : v_pos) {
#      46                 :       1705 :         batch.Write(std::make_pair(DB_TXINDEX, tuple.first), tuple.second);
#      47                 :       1705 :     }
#      48                 :       1620 :     return WriteBatch(batch);
#      49                 :       1620 : }
#      50                 :            : 
#      51                 :            : TxIndex::TxIndex(size_t n_cache_size, bool f_memory, bool f_wipe)
#      52                 :            :     : m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
#      53                 :         21 : {}
#      54                 :            : 
#      55                 :         21 : TxIndex::~TxIndex() {}
#      56                 :            : 
#      57                 :            : bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
#      58                 :       1632 : {
#      59                 :            :     // Exclude genesis block transaction because outputs are not spendable.
#      60         [ +  + ]:       1632 :     if (pindex->nHeight == 0) return true;
#      61                 :            : 
#      62                 :       1620 :     CDiskTxPos pos{
#      63                 :       1620 :         WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
#      64                 :       1620 :         GetSizeOfCompactSize(block.vtx.size())};
#      65                 :       1620 :     std::vector<std::pair<uint256, CDiskTxPos>> vPos;
#      66                 :       1620 :     vPos.reserve(block.vtx.size());
#      67         [ +  + ]:       1705 :     for (const auto& tx : block.vtx) {
#      68                 :       1705 :         vPos.emplace_back(tx->GetHash(), pos);
#      69                 :       1705 :         pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION);
#      70                 :       1705 :     }
#      71                 :       1620 :     return m_db->WriteTxs(vPos);
#      72                 :       1632 : }
#      73                 :            : 
#      74                 :        120 : BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
#      75                 :            : 
#      76                 :            : bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const
#      77                 :        444 : {
#      78                 :        444 :     CDiskTxPos postx;
#      79         [ +  + ]:        444 :     if (!m_db->ReadTxPos(tx_hash, postx)) {
#      80                 :        202 :         return false;
#      81                 :        202 :     }
#      82                 :            : 
#      83                 :        242 :     CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
#      84         [ -  + ]:        242 :     if (file.IsNull()) {
#      85                 :          0 :         return error("%s: OpenBlockFile failed", __func__);
#      86                 :          0 :     }
#      87                 :        242 :     CBlockHeader header;
#      88                 :        242 :     try {
#      89                 :        242 :         file >> header;
#      90         [ -  + ]:        242 :         if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) {
#      91                 :          0 :             return error("%s: fseek(...) failed", __func__);
#      92                 :          0 :         }
#      93                 :        242 :         file >> tx;
#      94                 :        242 :     } catch (const std::exception& e) {
#      95                 :          0 :         return error("%s: Deserialize or I/O error - %s", __func__, e.what());
#      96                 :          0 :     }
#      97         [ -  + ]:        242 :     if (tx->GetHash() != tx_hash) {
#      98                 :          0 :         return error("%s: txid mismatch", __func__);
#      99                 :          0 :     }
#     100                 :        242 :     block_hash = header.GetHash();
#     101                 :        242 :     return true;
#     102                 :        242 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a