LCOV - code coverage report
Current view: top level - src/kernel - mempool_persist.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 116 0.0 %
Date: 2024-01-03 14:57:27 Functions: 0 2 0.0 %
Branches: 0 250 0.0 %

           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 <kernel/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 kernel {
      37                 :            : 
      38                 :            : static const uint64_t MEMPOOL_DUMP_VERSION_NO_XOR_KEY{1};
      39                 :            : static const uint64_t MEMPOOL_DUMP_VERSION{2};
      40                 :            : 
      41                 :          0 : bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts)
      42                 :            : {
      43         [ #  # ]:          0 :     if (load_path.empty()) return false;
      44                 :            : 
      45         [ #  # ]:          0 :     AutoFile file{opts.mockable_fopen_function(load_path, "rb")};
      46 [ #  # ][ #  # ]:          0 :     if (file.IsNull()) {
      47 [ #  # ][ #  # ]:          0 :         LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
                 [ #  # ]
      48                 :          0 :         return false;
      49                 :            :     }
      50                 :            : 
      51                 :          0 :     int64_t count = 0;
      52                 :          0 :     int64_t expired = 0;
      53                 :          0 :     int64_t failed = 0;
      54                 :          0 :     int64_t already_there = 0;
      55                 :          0 :     int64_t unbroadcast = 0;
      56                 :          0 :     const auto now{NodeClock::now()};
      57                 :            : 
      58                 :            :     try {
      59                 :            :         uint64_t version;
      60         [ #  # ]:          0 :         file >> version;
      61                 :          0 :         std::vector<std::byte> xor_key;
      62         [ #  # ]:          0 :         if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
      63                 :            :             // Leave XOR-key empty
      64         [ #  # ]:          0 :         } else if (version == MEMPOOL_DUMP_VERSION) {
      65         [ #  # ]:          0 :             file >> xor_key;
      66                 :          0 :         } else {
      67                 :          0 :             return false;
      68                 :            :         }
      69 [ #  # ][ #  # ]:          0 :         file.SetXor(xor_key);
      70                 :            :         uint64_t num;
      71         [ #  # ]:          0 :         file >> num;
      72         [ #  # ]:          0 :         while (num) {
      73                 :          0 :             --num;
      74                 :          0 :             CTransactionRef tx;
      75                 :            :             int64_t nTime;
      76                 :            :             int64_t nFeeDelta;
      77 [ #  # ][ #  # ]:          0 :             file >> TX_WITH_WITNESS(tx);
      78         [ #  # ]:          0 :             file >> nTime;
      79         [ #  # ]:          0 :             file >> nFeeDelta;
      80                 :            : 
      81         [ #  # ]:          0 :             if (opts.use_current_time) {
      82         [ #  # ]:          0 :                 nTime = TicksSinceEpoch<std::chrono::seconds>(now);
      83                 :          0 :             }
      84                 :            : 
      85                 :          0 :             CAmount amountdelta = nFeeDelta;
      86 [ #  # ][ #  # ]:          0 :             if (amountdelta && opts.apply_fee_delta_priority) {
      87 [ #  # ][ #  # ]:          0 :                 pool.PrioritiseTransaction(tx->GetHash(), amountdelta);
                 [ #  # ]
      88                 :          0 :             }
      89 [ #  # ][ #  # ]:          0 :             if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.m_expiry)) {
                 [ #  # ]
      90 [ #  # ][ #  # ]:          0 :                 LOCK(cs_main);
      91         [ #  # ]:          0 :                 const auto& accepted = AcceptToMemoryPool(active_chainstate, tx, nTime, /*bypass_limits=*/false, /*test_accept=*/false);
      92         [ #  # ]:          0 :                 if (accepted.m_result_type == MempoolAcceptResult::ResultType::VALID) {
      93                 :          0 :                     ++count;
      94                 :          0 :                 } else {
      95                 :            :                     // mempool may contain the transaction already, e.g. from
      96                 :            :                     // wallet(s) having loaded it while we were processing
      97                 :            :                     // mempool transactions; consider these as valid, instead of
      98                 :            :                     // failed, but mark them as 'already there'
      99 [ #  # ][ #  # ]:          0 :                     if (pool.exists(GenTxid::Txid(tx->GetHash()))) {
         [ #  # ][ #  # ]
                 [ #  # ]
     100                 :          0 :                         ++already_there;
     101                 :          0 :                     } else {
     102                 :          0 :                         ++failed;
     103                 :            :                     }
     104                 :            :                 }
     105                 :          0 :             } else {
     106                 :          0 :                 ++expired;
     107                 :            :             }
     108 [ #  # ][ #  # ]:          0 :             if (active_chainstate.m_chainman.m_interrupt)
     109                 :          0 :                 return false;
     110         [ #  # ]:          0 :         }
     111                 :          0 :         std::map<uint256, CAmount> mapDeltas;
     112         [ #  # ]:          0 :         file >> mapDeltas;
     113                 :            : 
     114         [ #  # ]:          0 :         if (opts.apply_fee_delta_priority) {
     115         [ #  # ]:          0 :             for (const auto& i : mapDeltas) {
     116         [ #  # ]:          0 :                 pool.PrioritiseTransaction(i.first, i.second);
     117                 :            :             }
     118                 :          0 :         }
     119                 :            : 
     120                 :          0 :         std::set<uint256> unbroadcast_txids;
     121         [ #  # ]:          0 :         file >> unbroadcast_txids;
     122         [ #  # ]:          0 :         if (opts.apply_unbroadcast_set) {
     123                 :          0 :             unbroadcast = unbroadcast_txids.size();
     124         [ #  # ]:          0 :             for (const auto& txid : unbroadcast_txids) {
     125                 :            :                 // Ensure transactions were accepted to mempool then add to
     126                 :            :                 // unbroadcast set.
     127 [ #  # ][ #  # ]:          0 :                 if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
                 [ #  # ]
     128                 :            :             }
     129                 :          0 :         }
     130 [ #  # ][ #  # ]:          0 :     } catch (const std::exception& e) {
     131 [ #  # ][ #  # ]:          0 :         LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
                 [ #  # ]
     132                 :          0 :         return false;
     133 [ #  # ][ #  # ]:          0 :     }
     134                 :            : 
     135 [ #  # ][ #  # ]:          0 :     LogPrintf("Imported mempool transactions from disk: %i succeeded, %i failed, %i expired, %i already there, %i waiting for initial broadcast\n", count, failed, expired, already_there, unbroadcast);
                 [ #  # ]
     136                 :          0 :     return true;
     137                 :          0 : }
     138                 :            : 
     139                 :          0 : bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
     140                 :            : {
     141                 :          0 :     auto start = SteadyClock::now();
     142                 :            : 
     143                 :          0 :     std::map<uint256, CAmount> mapDeltas;
     144                 :          0 :     std::vector<TxMempoolInfo> vinfo;
     145                 :          0 :     std::set<uint256> unbroadcast_txids;
     146                 :            : 
     147 [ #  # ][ #  # ]:          0 :     static Mutex dump_mutex;
     148 [ #  # ][ #  # ]:          0 :     LOCK(dump_mutex);
     149                 :            : 
     150                 :            :     {
     151 [ #  # ][ #  # ]:          0 :         LOCK(pool.cs);
     152         [ #  # ]:          0 :         for (const auto &i : pool.mapDeltas) {
     153         [ #  # ]:          0 :             mapDeltas[i.first] = i.second;
     154                 :            :         }
     155         [ #  # ]:          0 :         vinfo = pool.infoAll();
     156         [ #  # ]:          0 :         unbroadcast_txids = pool.GetUnbroadcastTxs();
     157                 :          0 :     }
     158                 :            : 
     159                 :          0 :     auto mid = SteadyClock::now();
     160                 :            : 
     161 [ #  # ][ #  # ]:          0 :     AutoFile file{mockable_fopen_function(dump_path + ".new", "wb")};
         [ #  # ][ #  # ]
     162 [ #  # ][ #  # ]:          0 :     if (file.IsNull()) {
     163                 :          0 :         return false;
     164                 :            :     }
     165                 :            : 
     166                 :            :     try {
     167                 :          0 :         const uint64_t version{pool.m_persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION};
     168         [ #  # ]:          0 :         file << version;
     169                 :            : 
     170         [ #  # ]:          0 :         std::vector<std::byte> xor_key(8);
     171         [ #  # ]:          0 :         if (!pool.m_persist_v1_dat) {
     172 [ #  # ][ #  # ]:          0 :             FastRandomContext{}.fillrand(xor_key);
     173         [ #  # ]:          0 :             file << xor_key;
     174                 :          0 :         }
     175 [ #  # ][ #  # ]:          0 :         file.SetXor(xor_key);
     176                 :            : 
     177         [ #  # ]:          0 :         file << (uint64_t)vinfo.size();
     178         [ #  # ]:          0 :         for (const auto& i : vinfo) {
     179 [ #  # ][ #  # ]:          0 :             file << TX_WITH_WITNESS(*(i.tx));
     180 [ #  # ][ #  # ]:          0 :             file << int64_t{count_seconds(i.m_time)};
     181         [ #  # ]:          0 :             file << int64_t{i.nFeeDelta};
     182 [ #  # ][ #  # ]:          0 :             mapDeltas.erase(i.tx->GetHash());
                 [ #  # ]
     183                 :            :         }
     184                 :            : 
     185         [ #  # ]:          0 :         file << mapDeltas;
     186                 :            : 
     187 [ #  # ][ #  # ]:          0 :         LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
                 [ #  # ]
     188         [ #  # ]:          0 :         file << unbroadcast_txids;
     189                 :            : 
     190 [ #  # ][ #  # ]:          0 :         if (!skip_file_commit && !FileCommit(file.Get()))
         [ #  # ][ #  # ]
     191         [ #  # ]:          0 :             throw std::runtime_error("FileCommit failed");
     192         [ #  # ]:          0 :         file.fclose();
     193 [ #  # ][ #  # ]:          0 :         if (!RenameOver(dump_path + ".new", dump_path)) {
         [ #  # ][ #  # ]
                 [ #  # ]
     194         [ #  # ]:          0 :             throw std::runtime_error("Rename failed");
     195                 :            :         }
     196                 :          0 :         auto last = SteadyClock::now();
     197                 :            : 
     198 [ #  # ][ #  # ]:          0 :         LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     199                 :            :                   Ticks<SecondsDouble>(mid - start),
     200                 :            :                   Ticks<SecondsDouble>(last - mid));
     201         [ #  # ]:          0 :     } catch (const std::exception& e) {
     202 [ #  # ][ #  # ]:          0 :         LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
                 [ #  # ]
     203                 :          0 :         return false;
     204 [ #  # ][ #  # ]:          0 :     }
     205                 :          0 :     return true;
     206                 :          0 : }
     207                 :            : 
     208                 :            : } // namespace kernel

Generated by: LCOV version 1.14