14 #include <txmempool.h> 20 #include <validation.h> 41 if (load_path.empty())
return false;
43 FILE* filestr{opts.mockable_fopen_function(load_path,
"rb")};
46 LogPrintf(
"Failed to open mempool file from disk. Continuing anyway.\n");
53 int64_t already_there = 0;
54 int64_t unbroadcast = 0;
63 uint64_t total_txns_to_load;
64 file >> total_txns_to_load;
65 uint64_t txns_tried = 0;
66 LogPrintf(
"Loading %u mempool transactions from disk...\n", total_txns_to_load);
67 int next_tenth_to_report = 0;
68 while (txns_tried < total_txns_to_load) {
69 const int percentage_done(100.0 * txns_tried / total_txns_to_load);
70 if (next_tenth_to_report < percentage_done / 10) {
71 LogPrintf(
"Progress loading mempool transactions from disk: %d%% (tried %u, %u remaining)\n",
72 percentage_done, txns_tried, total_txns_to_load - txns_tried);
73 next_tenth_to_report = percentage_done / 10;
84 if (opts.use_current_time) {
85 nTime = TicksSinceEpoch<std::chrono::seconds>(now);
88 CAmount amountdelta = nFeeDelta;
89 if (amountdelta && opts.apply_fee_delta_priority) {
92 if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.
m_expiry)) {
114 std::map<uint256, CAmount> mapDeltas;
117 if (opts.apply_fee_delta_priority) {
118 for (
const auto& i : mapDeltas) {
123 std::set<uint256> unbroadcast_txids;
124 file >> unbroadcast_txids;
125 if (opts.apply_unbroadcast_set) {
126 unbroadcast = unbroadcast_txids.size();
127 for (
const auto& txid : unbroadcast_txids) {
133 }
catch (
const std::exception& e) {
134 LogPrintf(
"Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
138 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);
144 auto start = SteadyClock::now();
146 std::map<uint256, CAmount> mapDeltas;
147 std::vector<TxMempoolInfo> vinfo;
148 std::set<uint256> unbroadcast_txids;
150 static Mutex dump_mutex;
155 for (
const auto &i : pool.mapDeltas) {
156 mapDeltas[i.first] = i.second;
162 auto mid = SteadyClock::now();
165 FILE* filestr{mockable_fopen_function(dump_path +
".new",
"wb")};
175 file << (uint64_t)vinfo.size();
176 for (
const auto& i : vinfo) {
179 file << int64_t{i.nFeeDelta};
180 mapDeltas.erase(i.tx->GetHash());
185 LogPrintf(
"Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
186 file << unbroadcast_txids;
188 if (!skip_file_commit && !
FileCommit(file.Get()))
189 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());
std::shared_ptr< const CTransaction > CTransactionRef
std::vector< TxMempoolInfo > infoAll() const
bool exists(const GenTxid >xid) const
bool DumpMempool(const CTxMemPool &pool, const fs::path &dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
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.
const util::SignalInterrupt & m_interrupt
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
int64_t CAmount
Amount in satoshis (Can be negative)
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.
std::function< FILE *(const fs::path &, const char *)> FopenFn
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Chainstate stores and provides an API to update our local knowledge of the current best chain...
const std::chrono::seconds m_expiry
constexpr int64_t count_seconds(std::chrono::seconds t)
static time_point now() noexcept
Return current system time or mocked time, if set.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
CTransactionRef get(const uint256 &hash) const
static const uint64_t MEMPOOL_DUMP_VERSION
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
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...
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
static GenTxid Txid(const uint256 &hash)
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...