Bitcoin Core  24.99.0
P2P Digital Currency
mempool_persist.cpp
Go to the documentation of this file.
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 
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, Chainstate& 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
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:121
void fclose()
Definition: streams.h:491
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:513
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:509
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:432
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:900
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:788
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:521
const std::chrono::seconds m_expiry
Definition: txmempool.h:570
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:882
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:868
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:800
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:767
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:438
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
#define LogPrintf(...)
Definition: logging.h:234
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:207
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
Definition: transaction.h:414
@ SER_DISK
Definition: serialize.h:132
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:89
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:72
#define LOCK(cs)
Definition: sync.h:261
static int count
Definition: tests.c:33
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:54
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
Definition: system.cpp:1136
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: system.cpp:1173
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.