LCOV - code coverage report
Current view: top level - src/node - mempool_persist.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 88.1 % 134 118
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 2 2
Branches: 50.8 % 236 120

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 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 <node/mempool_persist.h>
       6                 :             : 
       7                 :             : #include <clientversion.h>
       8                 :             : #include <consensus/amount.h>
       9                 :             : #include <logging.h>
      10                 :             : #include <primitives/transaction.h>
      11                 :             : #include <random.h>
      12                 :             : #include <serialize.h>
      13                 :             : #include <streams.h>
      14                 :             : #include <sync.h>
      15                 :             : #include <txmempool.h>
      16                 :             : #include <uint256.h>
      17                 :             : #include <util/fs.h>
      18                 :             : #include <util/fs_helpers.h>
      19                 :             : #include <util/signalinterrupt.h>
      20                 :             : #include <util/time.h>
      21                 :             : #include <validation.h>
      22                 :             : 
      23                 :             : #include <cstdint>
      24                 :             : #include <cstdio>
      25                 :             : #include <exception>
      26                 :             : #include <functional>
      27                 :             : #include <map>
      28                 :             : #include <memory>
      29                 :             : #include <set>
      30                 :             : #include <stdexcept>
      31                 :             : #include <utility>
      32                 :             : #include <vector>
      33                 :             : 
      34                 :             : using fsbridge::FopenFn;
      35                 :             : 
      36                 :             : namespace node {
      37                 :             : 
      38                 :             : static const uint64_t MEMPOOL_DUMP_VERSION_NO_XOR_KEY{1};
      39                 :             : static const uint64_t MEMPOOL_DUMP_VERSION{2};
      40                 :             : 
      41                 :        1694 : bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts)
      42                 :             : {
      43         [ -  + ]:        1694 :     if (load_path.empty()) return false;
      44                 :             : 
      45         [ +  - ]:        1694 :     AutoFile file{opts.mockable_fopen_function(load_path, "rb")};
      46   [ +  -  +  + ]:        1694 :     if (file.IsNull()) {
      47         [ +  - ]:          22 :         LogInfo("Failed to open mempool file. Continuing anyway.\n");
      48                 :          22 :         return false;
      49                 :             :     }
      50                 :             : 
      51                 :        1672 :     int64_t count = 0;
      52                 :        1672 :     int64_t expired = 0;
      53                 :        1672 :     int64_t failed = 0;
      54                 :        1672 :     int64_t already_there = 0;
      55                 :        1672 :     int64_t unbroadcast = 0;
      56                 :        1672 :     const auto now{NodeClock::now()};
      57                 :             : 
      58                 :             :     try {
      59                 :        1672 :         uint64_t version;
      60         [ +  + ]:        1672 :         file >> version;
      61                 :        1638 :         std::vector<std::byte> xor_key;
      62         [ +  + ]:        1638 :         if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
      63                 :             :             // Leave XOR-key empty
      64         [ +  + ]:        1638 :         } else if (version == MEMPOOL_DUMP_VERSION) {
      65         [ +  + ]:          66 :             file >> xor_key;
      66                 :          57 :         } else {
      67                 :           4 :             return false;
      68                 :             :         }
      69   [ +  -  +  - ]:        1625 :         file.SetXor(xor_key);
      70                 :        1625 :         uint64_t total_txns_to_load;
      71         [ +  + ]:        1625 :         file >> total_txns_to_load;
      72                 :        1618 :         uint64_t txns_tried = 0;
      73         [ +  - ]:        1618 :         LogInfo("Loading %u mempool transactions from file...\n", total_txns_to_load);
      74                 :        1618 :         int next_tenth_to_report = 0;
      75         [ +  + ]:      276437 :         while (txns_tried < total_txns_to_load) {
      76                 :      276222 :             const int percentage_done(100.0 * txns_tried / total_txns_to_load);
      77         [ +  + ]:      276222 :             if (next_tenth_to_report < percentage_done / 10) {
      78         [ -  + ]:         114 :                 LogInfo("Progress loading mempool transactions from file: %d%% (tried %u, %u remaining)\n",
      79                 :             :                         percentage_done, txns_tried, total_txns_to_load - txns_tried);
      80                 :         114 :                 next_tenth_to_report = percentage_done / 10;
      81                 :         114 :             }
      82                 :      276222 :             ++txns_tried;
      83                 :             : 
      84                 :      276222 :             CTransactionRef tx;
      85                 :      276222 :             int64_t nTime;
      86                 :      276222 :             int64_t nFeeDelta;
      87   [ +  -  +  + ]:      276222 :             file >> TX_WITH_WITNESS(tx);
      88         [ +  + ]:      274888 :             file >> nTime;
      89         [ +  + ]:      274845 :             file >> nFeeDelta;
      90                 :             : 
      91         [ -  + ]:      274819 :             if (opts.use_current_time) {
      92         [ #  # ]:           0 :                 nTime = TicksSinceEpoch<std::chrono::seconds>(now);
      93                 :           0 :             }
      94                 :             : 
      95                 :      274819 :             CAmount amountdelta = nFeeDelta;
      96   [ +  +  +  - ]:      274819 :             if (amountdelta && opts.apply_fee_delta_priority) {
      97   [ +  -  +  -  :      245029 :                 pool.PrioritiseTransaction(tx->GetHash(), amountdelta);
                   +  - ]
      98                 :      245029 :             }
      99   [ +  -  +  -  :      274819 :             if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.m_opts.expiry)) {
                   +  + ]
     100   [ +  -  +  - ]:      227706 :                 LOCK(cs_main);
     101         [ -  + ]:      227706 :                 const auto& accepted = AcceptToMemoryPool(active_chainstate, tx, nTime, /*bypass_limits=*/false, /*test_accept=*/false);
     102         [ +  - ]:      227706 :                 if (accepted.m_result_type == MempoolAcceptResult::ResultType::VALID) {
     103                 :           0 :                     ++count;
     104                 :           0 :                 } else {
     105                 :             :                     // mempool may contain the transaction already, e.g. from
     106                 :             :                     // wallet(s) having loaded it while we were processing
     107                 :             :                     // mempool transactions; consider these as valid, instead of
     108                 :             :                     // failed, but mark them as 'already there'
     109   [ +  -  +  -  :      227706 :                     if (pool.exists(GenTxid::Txid(tx->GetHash()))) {
          +  -  +  -  -  
                      + ]
     110                 :           0 :                         ++already_there;
     111                 :           0 :                     } else {
     112                 :      227706 :                         ++failed;
     113                 :             :                     }
     114                 :             :                 }
     115                 :      227706 :             } else {
     116                 :       47113 :                 ++expired;
     117                 :             :             }
     118   [ +  -  -  + ]:      274819 :             if (active_chainstate.m_chainman.m_interrupt)
     119                 :           0 :                 return false;
     120         [ -  + ]:      276222 :         }
     121                 :         215 :         std::map<uint256, CAmount> mapDeltas;
     122         [ +  + ]:         215 :         file >> mapDeltas;
     123                 :             : 
     124         [ -  + ]:         188 :         if (opts.apply_fee_delta_priority) {
     125         [ +  + ]:       79692 :             for (const auto& i : mapDeltas) {
     126         [ +  - ]:       79504 :                 pool.PrioritiseTransaction(i.first, i.second);
     127                 :       79504 :             }
     128                 :         188 :         }
     129                 :             : 
     130                 :         188 :         std::set<uint256> unbroadcast_txids;
     131         [ +  + ]:         188 :         file >> unbroadcast_txids;
     132         [ -  + ]:          81 :         if (opts.apply_unbroadcast_set) {
     133                 :          81 :             unbroadcast = unbroadcast_txids.size();
     134         [ +  + ]:        1540 :             for (const auto& txid : unbroadcast_txids) {
     135                 :             :                 // Ensure transactions were accepted to mempool then add to
     136                 :             :                 // unbroadcast set.
     137   [ -  +  +  -  :        1459 :                 if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
                   #  # ]
     138                 :        1459 :             }
     139                 :          81 :         }
     140   [ +  +  -  + ]:        1672 :     } catch (const std::exception& e) {
     141         [ +  - ]:        1587 :         LogInfo("Failed to deserialize mempool data on file: %s. Continuing anyway.\n", e.what());
     142                 :        1587 :         return false;
     143   [ +  -  #  # ]:        1587 :     }
     144                 :             : 
     145         [ +  - ]:          81 :     LogInfo("Imported mempool transactions from file: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
     146                 :          81 :     return true;
     147                 :        3281 : }
     148                 :             : 
     149                 :        1694 : bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
     150                 :             : {
     151                 :        1694 :     auto start = SteadyClock::now();
     152                 :             : 
     153                 :        1694 :     std::map<uint256, CAmount> mapDeltas;
     154                 :        1694 :     std::vector<TxMempoolInfo> vinfo;
     155                 :        1694 :     std::set<uint256> unbroadcast_txids;
     156                 :             : 
     157   [ +  +  -  + ]:        1694 :     static Mutex dump_mutex;
     158   [ +  -  +  - ]:        1694 :     LOCK(dump_mutex);
     159                 :             : 
     160                 :             :     {
     161   [ +  -  +  - ]:        1694 :         LOCK(pool.cs);
     162         [ +  + ]:      118845 :         for (const auto &i : pool.mapDeltas) {
     163         [ +  - ]:      117151 :             mapDeltas[i.first] = i.second;
     164                 :      117151 :         }
     165         [ +  - ]:        1694 :         vinfo = pool.infoAll();
     166         [ +  - ]:        1694 :         unbroadcast_txids = pool.GetUnbroadcastTxs();
     167                 :        1694 :     }
     168                 :             : 
     169                 :        1694 :     auto mid = SteadyClock::now();
     170                 :             : 
     171   [ +  -  +  -  :        1694 :     AutoFile file{mockable_fopen_function(dump_path + ".new", "wb")};
             +  -  +  - ]
     172   [ +  -  +  + ]:        1694 :     if (file.IsNull()) {
     173                 :          38 :         return false;
     174                 :             :     }
     175                 :             : 
     176                 :             :     try {
     177                 :        1656 :         const uint64_t version{pool.m_opts.persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION};
     178         [ +  + ]:        1656 :         file << version;
     179                 :             : 
     180         [ +  - ]:         170 :         std::vector<std::byte> xor_key(8);
     181         [ -  + ]:         170 :         if (!pool.m_opts.persist_v1_dat) {
     182         [ +  - ]:         170 :             FastRandomContext{}.fillrand(xor_key);
     183         [ +  - ]:         170 :             file << xor_key;
     184                 :         170 :         }
     185   [ +  -  +  - ]:         170 :         file.SetXor(xor_key);
     186                 :             : 
     187                 :         170 :         uint64_t mempool_transactions_to_write(vinfo.size());
     188         [ +  + ]:         170 :         file << mempool_transactions_to_write;
     189         [ +  - ]:         116 :         LogInfo("Writing %u mempool transactions to file...\n", mempool_transactions_to_write);
     190         [ +  - ]:         116 :         for (const auto& i : vinfo) {
     191   [ #  #  #  # ]:           0 :             file << TX_WITH_WITNESS(*(i.tx));
     192   [ #  #  #  # ]:           0 :             file << int64_t{count_seconds(i.m_time)};
     193         [ #  # ]:           0 :             file << int64_t{i.nFeeDelta};
     194   [ #  #  #  #  :           0 :             mapDeltas.erase(i.tx->GetHash());
                   #  # ]
     195                 :           0 :         }
     196                 :             : 
     197         [ +  + ]:         116 :         file << mapDeltas;
     198                 :             : 
     199         [ +  - ]:          36 :         LogInfo("Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size());
     200         [ +  + ]:          36 :         file << unbroadcast_txids;
     201                 :             : 
     202   [ -  +  #  #  :          28 :         if (!skip_file_commit && !FileCommit(file.Get()))
             #  #  #  # ]
     203   [ #  #  #  # ]:           0 :             throw std::runtime_error("FileCommit failed");
     204         [ +  - ]:          28 :         file.fclose();
     205   [ +  -  +  -  :          28 :         if (!RenameOver(dump_path + ".new", dump_path)) {
          +  -  +  -  -  
                      + ]
     206         [ +  - ]:          28 :             throw std::runtime_error("Rename failed");
     207                 :             :         }
     208                 :           0 :         auto last = SteadyClock::now();
     209                 :             : 
     210   [ #  #  #  #  :           0 :         LogInfo("Dumped mempool: %.3fs to copy, %.3fs to dump, %d bytes dumped to file\n",
          #  #  #  #  #  
                #  #  # ]
     211                 :             :                   Ticks<SecondsDouble>(mid - start),
     212                 :             :                   Ticks<SecondsDouble>(last - mid),
     213                 :             :                   fs::file_size(dump_path));
     214         [ -  + ]:        1656 :     } catch (const std::exception& e) {
     215         [ +  - ]:        1656 :         LogInfo("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
     216                 :        1656 :         return false;
     217   [ +  -  #  # ]:        1656 :     }
     218                 :           0 :     return true;
     219                 :        3350 : }
     220                 :             : 
     221                 :             : } // namespace node
        

Generated by: LCOV version 2.0-1