15 #include <txmempool.h>
20 #include <validation.h>
42 if (load_path.empty())
return false;
44 FILE* filestr{mockable_fopen_function(load_path,
"rb")};
47 LogPrintf(
"Failed to open mempool file from disk. Continuing anyway.\n");
54 int64_t already_there = 0;
55 int64_t unbroadcast = 0;
75 CAmount amountdelta = nFeeDelta;
79 if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.
m_expiry)) {
101 std::map<uint256, CAmount> mapDeltas;
104 for (
const auto& i : mapDeltas) {
108 std::set<uint256> unbroadcast_txids;
109 file >> unbroadcast_txids;
110 unbroadcast = unbroadcast_txids.size();
111 for (
const auto& txid : unbroadcast_txids) {
116 }
catch (
const std::exception& e) {
117 LogPrintf(
"Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
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);
127 auto start = SteadyClock::now();
129 std::map<uint256, CAmount> mapDeltas;
130 std::vector<TxMempoolInfo> vinfo;
131 std::set<uint256> unbroadcast_txids;
133 static Mutex dump_mutex;
138 for (
const auto &i : pool.mapDeltas) {
139 mapDeltas[i.first] = i.second;
145 auto mid = SteadyClock::now();
148 FILE* filestr{mockable_fopen_function(dump_path +
".new",
"wb")};
158 file << (uint64_t)vinfo.size();
159 for (
const auto& i : vinfo) {
162 file << int64_t{i.nFeeDelta};
163 mapDeltas.erase(i.tx->GetHash());
168 LogPrintf(
"Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
169 file << unbroadcast_txids;
172 throw std::runtime_error(
"FileCommit failed");
174 if (!
RenameOver(dump_path +
".new", dump_path)) {
175 throw std::runtime_error(
"Rename failed");
177 auto last = SteadyClock::now();
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());
int64_t CAmount
Amount in satoshis (Can be negative)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
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.
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.
const std::chrono::seconds m_expiry
CTransactionRef get(const uint256 &hash) const
std::vector< TxMempoolInfo > infoAll() const
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
bool exists(const GenTxid >xid) const
Chainstate stores and provides an API to update our local knowledge of the current best chain.
static GenTxid Txid(const uint256 &hash)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
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.
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
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
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) EXCLUSIVE_LOCKS_REQUIRED(
Try to add a transaction to the mempool.