19#include <validation.h>
40 FopenFn mockable_fopen_function) {
41 if (load_path.empty()) {
45 AutoFile file{mockable_fopen_function(load_path,
"rb")};
48 "Failed to open mempool file from disk. Continuing anyway.\n");
55 int64_t already_there = 0;
56 int64_t unbroadcast = 0;
82 TicksSinceEpoch<std::chrono::seconds>(now - pool.
m_expiry)) {
84 const auto &accepted =
88 if (accepted.m_result_type ==
96 if (pool.
exists(tx->GetId())) {
110 std::map<TxId, Amount> mapDeltas;
113 for (
const auto &i : mapDeltas) {
117 std::set<TxId> unbroadcast_txids;
118 file >> unbroadcast_txids;
119 unbroadcast = unbroadcast_txids.size();
120 for (
const auto &txid : unbroadcast_txids) {
123 if (pool.
get(txid) !=
nullptr) {
127 }
catch (
const std::exception &e) {
128 LogPrintf(
"Failed to deserialize mempool data on disk: %s. Continuing "
134 LogPrintf(
"Imported mempool transactions from disk: %i succeeded, %i "
135 "failed, %i expired, %i already there, %i waiting for initial "
137 count, failed, expired, already_there, unbroadcast);
142 FopenFn mockable_fopen_function,
bool skip_file_commit) {
143 auto start = SteadyClock::now();
145 std::map<uint256, Amount> mapDeltas;
146 std::vector<TxMempoolInfo> vinfo;
147 std::set<TxId> unbroadcast_txids;
149 static Mutex dump_mutex;
154 for (
const auto &i : pool.mapDeltas) {
155 mapDeltas[i.first] = i.second;
162 auto mid = SteadyClock::now();
165 AutoFile file{mockable_fopen_function(dump_path +
".new",
"wb")};
173 file << uint64_t(vinfo.size());
174 for (
const auto &i : vinfo) {
178 mapDeltas.erase(i.tx->GetId());
183 LogPrintf(
"Writing %d unbroadcast transactions to disk.\n",
184 unbroadcast_txids.size());
185 file << unbroadcast_txids;
187 if (!skip_file_commit && !
FileCommit(file.Get())) {
188 throw std::runtime_error(
"FileCommit failed");
191 if (!
RenameOver(dump_path +
".new", dump_path)) {
192 throw std::runtime_error(
"Rename failed");
194 auto last = SteadyClock::now();
196 LogPrintf(
"Dumped mempool: %gs to copy, %gs to dump\n",
197 Ticks<SecondsDouble>(mid - start),
198 Ticks<SecondsDouble>(last - mid));
199 }
catch (
const std::exception &e) {
200 LogPrintf(
"Failed to dump mempool: %s. Continuing anyway.\n", e.what());
static constexpr Amount SATOSHI
Non-refcounted RAII wrapper for FILE*.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
const std::chrono::seconds m_expiry
std::vector< TxMempoolInfo > infoAll() const
bool exists(const TxId &txid) const
std::set< TxId > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
CTransactionRef get(const TxId &txid) const
void PrioritiseTransaction(const TxId &txid, const Amount nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
void AddUnbroadcastTx(const TxId &txid)
Adds a transaction to the unbroadcast set.
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
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)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
std::function< FILE *(const fs::path &, const char *)> FopenFn
bool DumpMempool(const CTxMemPool &pool, const fs::path &dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
static const uint64_t MEMPOOL_DUMP_VERSION
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, FopenFn mockable_fopen_function)
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr Amount zero() noexcept
@ VALID
Fully validated, valid.
static time_point now() noexcept
Return current system time or mocked time, if set.
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, unsigned int heightOverride)
Try to add a transaction to the mempool.