21#include <validation.h>
43 if (load_path.empty())
return false;
45 AutoFile file{opts.mockable_fopen_function(load_path,
"rb")};
47 LogInfo(
"Failed to open mempool file. Continuing anyway.\n");
54 int64_t already_there = 0;
55 int64_t unbroadcast = 0;
61 std::vector<std::byte> xor_key;
70 uint64_t total_txns_to_load;
71 file >> total_txns_to_load;
72 uint64_t txns_tried = 0;
73 LogInfo(
"Loading %u mempool transactions from file...\n", total_txns_to_load);
74 int next_tenth_to_report = 0;
75 while (txns_tried < total_txns_to_load) {
76 const int percentage_done(100.0 * txns_tried / total_txns_to_load);
77 if (next_tenth_to_report < percentage_done / 10) {
78 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 next_tenth_to_report = percentage_done / 10;
91 if (opts.use_current_time) {
92 nTime = TicksSinceEpoch<std::chrono::seconds>(now);
95 CAmount amountdelta = nFeeDelta;
96 if (amountdelta && opts.apply_fee_delta_priority) {
99 if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.
m_opts.
expiry)) {
101 const auto& accepted =
AcceptToMemoryPool(active_chainstate, tx, nTime,
false,
false);
121 std::map<uint256, CAmount> mapDeltas;
124 if (opts.apply_fee_delta_priority) {
125 for (
const auto& i : mapDeltas) {
130 std::set<uint256> unbroadcast_txids;
131 file >> unbroadcast_txids;
132 if (opts.apply_unbroadcast_set) {
133 unbroadcast = unbroadcast_txids.size();
134 for (
const auto& txid : unbroadcast_txids) {
140 }
catch (
const std::exception& e) {
141 LogInfo(
"Failed to deserialize mempool data on file: %s. Continuing anyway.\n", e.what());
145 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);
151 auto start = SteadyClock::now();
153 std::map<uint256, CAmount> mapDeltas;
154 std::vector<TxMempoolInfo> vinfo;
155 std::set<uint256> unbroadcast_txids;
157 static Mutex dump_mutex;
162 for (
const auto &i : pool.mapDeltas) {
163 mapDeltas[i.first] = i.second;
169 auto mid = SteadyClock::now();
171 AutoFile file{mockable_fopen_function(dump_path +
".new",
"wb")};
180 std::vector<std::byte> xor_key(8);
185 file.SetXor(xor_key);
187 uint64_t mempool_transactions_to_write(vinfo.size());
188 file << mempool_transactions_to_write;
189 LogInfo(
"Writing %u mempool transactions to file...\n", mempool_transactions_to_write);
190 for (
const auto& i : vinfo) {
193 file << int64_t{i.nFeeDelta};
194 mapDeltas.erase(i.tx->GetHash());
199 LogInfo(
"Writing %d unbroadcast transactions to file.\n", unbroadcast_txids.size());
200 file << unbroadcast_txids;
202 if (!skip_file_commit && !file.Commit())
203 throw std::runtime_error(
"Commit failed");
205 if (!
RenameOver(dump_path +
".new", dump_path)) {
206 throw std::runtime_error(
"Rename failed");
208 auto last = SteadyClock::now();
210 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 }
catch (
const std::exception& e) {
215 LogInfo(
"Failed to dump mempool: %s. Continuing anyway.\n", e.what());
int64_t CAmount
Amount in satoshis (Can be negative)
Non-refcounted RAII wrapper for FILE*.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
CTransactionRef get(const uint256 &hash) const
std::vector< TxMempoolInfo > infoAll() const
bool exists(const GenTxid >xid) const
Chainstate stores and provides an API to update our local knowledge of the current best chain.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
const util::SignalInterrupt & m_interrupt
void fillrand(Span< std::byte > output) noexcept
Fill a byte Span with random bytes.
static GenTxid Txid(const uint256 &hash)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
std::function< FILE *(const fs::path &, const char *)> FopenFn
static const uint64_t MEMPOOL_DUMP_VERSION_NO_XOR_KEY
static const uint64_t MEMPOOL_DUMP_VERSION
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, ImportMempoolOptions &&opts)
Import the file and attempt to add its contents to the mempool.
bool DumpMempool(const CTxMemPool &pool, const fs::path &dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
static constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::seconds expiry
constexpr int64_t count_seconds(std::chrono::seconds t)
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept)
Try to add a transaction to the mempool.