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