Bitcoin Core  25.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 <logging.h>
10 #include <primitives/transaction.h>
11 #include <serialize.h>
12 #include <streams.h>
13 #include <sync.h>
14 #include <txmempool.h>
15 #include <uint256.h>
16 #include <util/fs.h>
17 #include <util/fs_helpers.h>
18 #include <util/signalinterrupt.h>
19 #include <util/time.h>
20 #include <validation.h>
21 
22 #include <cstdint>
23 #include <cstdio>
24 #include <exception>
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <set>
29 #include <stdexcept>
30 #include <utility>
31 #include <vector>
32 
33 using fsbridge::FopenFn;
34 
35 namespace kernel {
36 
37 static const uint64_t MEMPOOL_DUMP_VERSION = 1;
38 
39 bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts)
40 {
41  if (load_path.empty()) return false;
42 
43  FILE* filestr{opts.mockable_fopen_function(load_path, "rb")};
44  CAutoFile file{filestr, CLIENT_VERSION};
45  if (file.IsNull()) {
46  LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
47  return false;
48  }
49 
50  int64_t count = 0;
51  int64_t expired = 0;
52  int64_t failed = 0;
53  int64_t already_there = 0;
54  int64_t unbroadcast = 0;
55  const auto now{NodeClock::now()};
56 
57  try {
58  uint64_t version;
59  file >> version;
60  if (version != MEMPOOL_DUMP_VERSION) {
61  return false;
62  }
63  uint64_t num;
64  file >> num;
65  while (num) {
66  --num;
67  CTransactionRef tx;
68  int64_t nTime;
69  int64_t nFeeDelta;
70  file >> tx;
71  file >> nTime;
72  file >> nFeeDelta;
73 
74  if (opts.use_current_time) {
75  nTime = TicksSinceEpoch<std::chrono::seconds>(now);
76  }
77 
78  CAmount amountdelta = nFeeDelta;
79  if (amountdelta && opts.apply_fee_delta_priority) {
80  pool.PrioritiseTransaction(tx->GetHash(), amountdelta);
81  }
82  if (nTime > TicksSinceEpoch<std::chrono::seconds>(now - pool.m_expiry)) {
83  LOCK(cs_main);
84  const auto& accepted = AcceptToMemoryPool(active_chainstate, tx, nTime, /*bypass_limits=*/false, /*test_accept=*/false);
85  if (accepted.m_result_type == MempoolAcceptResult::ResultType::VALID) {
86  ++count;
87  } else {
88  // mempool may contain the transaction already, e.g. from
89  // wallet(s) having loaded it while we were processing
90  // mempool transactions; consider these as valid, instead of
91  // failed, but mark them as 'already there'
92  if (pool.exists(GenTxid::Txid(tx->GetHash()))) {
93  ++already_there;
94  } else {
95  ++failed;
96  }
97  }
98  } else {
99  ++expired;
100  }
101  if (active_chainstate.m_chainman.m_interrupt)
102  return false;
103  }
104  std::map<uint256, CAmount> mapDeltas;
105  file >> mapDeltas;
106 
107  if (opts.apply_fee_delta_priority) {
108  for (const auto& i : mapDeltas) {
109  pool.PrioritiseTransaction(i.first, i.second);
110  }
111  }
112 
113  std::set<uint256> unbroadcast_txids;
114  file >> unbroadcast_txids;
115  if (opts.apply_unbroadcast_set) {
116  unbroadcast = unbroadcast_txids.size();
117  for (const auto& txid : unbroadcast_txids) {
118  // Ensure transactions were accepted to mempool then add to
119  // unbroadcast set.
120  if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
121  }
122  }
123  } catch (const std::exception& e) {
124  LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
125  return false;
126  }
127 
128  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);
129  return true;
130 }
131 
132 bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
133 {
134  auto start = SteadyClock::now();
135 
136  std::map<uint256, CAmount> mapDeltas;
137  std::vector<TxMempoolInfo> vinfo;
138  std::set<uint256> unbroadcast_txids;
139 
140  static Mutex dump_mutex;
141  LOCK(dump_mutex);
142 
143  {
144  LOCK(pool.cs);
145  for (const auto &i : pool.mapDeltas) {
146  mapDeltas[i.first] = i.second;
147  }
148  vinfo = pool.infoAll();
149  unbroadcast_txids = pool.GetUnbroadcastTxs();
150  }
151 
152  auto mid = SteadyClock::now();
153 
154  try {
155  FILE* filestr{mockable_fopen_function(dump_path + ".new", "wb")};
156  if (!filestr) {
157  return false;
158  }
159 
160  CAutoFile file{filestr, CLIENT_VERSION};
161 
162  uint64_t version = MEMPOOL_DUMP_VERSION;
163  file << version;
164 
165  file << (uint64_t)vinfo.size();
166  for (const auto& i : vinfo) {
167  file << *(i.tx);
168  file << int64_t{count_seconds(i.m_time)};
169  file << int64_t{i.nFeeDelta};
170  mapDeltas.erase(i.tx->GetHash());
171  }
172 
173  file << mapDeltas;
174 
175  LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
176  file << unbroadcast_txids;
177 
178  if (!skip_file_commit && !FileCommit(file.Get()))
179  throw std::runtime_error("FileCommit failed");
180  file.fclose();
181  if (!RenameOver(dump_path + ".new", dump_path)) {
182  throw std::runtime_error("Rename failed");
183  }
184  auto last = SteadyClock::now();
185 
186  LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n",
187  Ticks<SecondsDouble>(mid - start),
188  Ticks<SecondsDouble>(last - mid));
189  } catch (const std::exception& e) {
190  LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
191  return false;
192  }
193  return true;
194 }
195 
196 } // namespace kernel
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:302
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:882
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:703
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:391
const std::chrono::seconds m_expiry
Definition: txmempool.h:441
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:853
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:839
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:715
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:678
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:466
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
Definition: validation.h:506
const util::SignalInterrupt & m_interrupt
Definition: validation.h:935
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:432
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
Definition: fs_helpers.cpp:262
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:121
#define LogPrintf(...)
Definition: logging.h:237
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:207
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 const uint64_t MEMPOOL_DUMP_VERSION
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:70
#define LOCK(cs)
Definition: sync.h:258
static int count
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:54
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.