Bitcoin ABC  0.26.3
P2P Digital Currency
txmempool.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <txmempool.h>
7 
8 #include <chain.h>
9 #include <chainparams.h> // for GetConsensus.
10 #include <clientversion.h>
11 #include <coins.h>
12 #include <config.h>
13 #include <consensus/consensus.h>
14 #include <consensus/tx_verify.h>
15 #include <consensus/validation.h>
16 #include <policy/fees.h>
17 #include <policy/mempool.h>
18 #include <policy/policy.h>
19 #include <policy/settings.h>
20 #include <reverse_iterator.h>
21 #include <undo.h>
22 #include <util/moneystr.h>
23 #include <util/system.h>
24 #include <util/time.h>
25 #include <validation.h>
26 #include <validationinterface.h>
27 #include <version.h>
28 
29 #include <algorithm>
30 #include <cmath>
31 #include <limits>
32 
35 inline constexpr uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
36 
37 // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
38 // Remove after Wellington
40  update_descendant_state(int64_t _modifySize, Amount _modifyFee,
41  int64_t _modifyCount, int64_t _modifySigChecks)
42  : modifySize(_modifySize), modifyFee(_modifyFee),
43  modifyCount(_modifyCount), modifySigChecks(_modifySigChecks) {}
44 
48  }
49 
50 private:
51  int64_t modifySize;
53  int64_t modifyCount;
54  int64_t modifySigChecks;
55 };
56 
58  update_ancestor_state(int64_t _modifySize, Amount _modifyFee,
59  int64_t _modifyCount, int64_t _modifySigChecks)
60  : modifySize(_modifySize), modifyFee(_modifyFee),
61  modifyCount(_modifyCount), modifySigChecks(_modifySigChecks) {}
62 
66  }
67 
68 private:
69  int64_t modifySize;
71  int64_t modifyCount;
72  int64_t modifySigChecks;
73 };
74 
75 bool TestLockPointValidity(const CChain &active_chain, const LockPoints &lp) {
77  // If there are relative lock times then the maxInputBlock will be set
78  // If there are no relative lock times, the LockPoints don't depend on the
79  // chain
80  if (lp.maxInputBlock) {
81  // Check whether active_chain is an extension of the block at which the
82  // LockPoints calculation was valid. If not LockPoints are no longer
83  // valid
84  if (!active_chain.Contains(lp.maxInputBlock)) {
85  return false;
86  }
87  }
88 
89  // LockPoints still valid
90  return true;
91 }
92 
94  int64_t time, unsigned int entry_height,
95  bool spends_coinbase, int64_t _sigChecks,
96  LockPoints lp)
97  : tx{_tx}, nFee{fee},
98  nTxSize(tx->GetTotalSize()), nUsageSize{RecursiveDynamicUsage(tx)},
99  nTime(time), entryHeight{entry_height}, spendsCoinbase(spends_coinbase),
100  sigChecks(_sigChecks), lockPoints(lp), nSizeWithDescendants{GetTxSize()},
101  nModFeesWithDescendants{nFee}, nSigChecksWithDescendants{sigChecks},
102  nSizeWithAncestors{GetTxSize()}, nModFeesWithAncestors{nFee},
103  nSigChecksWithAncestors{sigChecks} {}
104 
107 }
108 
109 // Remove after wellinggton
111  // note this is distinct from the sum of descendants' individual virtual
112  // sizes, and may be smaller.
115 }
116 
117 // Remove after wellinggton
119  // note this is distinct from the sum of ancestors' individual virtual
120  // sizes, and may be smaller.
123 }
124 
126  // Remove after wellington; this stat is unused after wellington
127  nModFeesWithDescendants += newFeeDelta - feeDelta;
128  nModFeesWithAncestors += newFeeDelta - feeDelta;
129  feeDelta = newFeeDelta;
130 }
131 
133  lockPoints = lp;
134 }
135 
137  size_t entry_size, size_t entry_count, setEntries &setAncestors,
138  CTxMemPoolEntry::Parents &staged_ancestors, uint64_t limitAncestorCount,
139  uint64_t limitAncestorSize, uint64_t limitDescendantCount,
140  uint64_t limitDescendantSize, std::string &errString) const {
141  size_t totalSizeWithAncestors = entry_size;
142 
143  while (!staged_ancestors.empty()) {
144  const CTxMemPoolEntry &stage = staged_ancestors.begin()->get();
145  txiter stageit = mapTx.iterator_to(stage);
146 
147  setAncestors.insert(stageit);
148  staged_ancestors.erase(staged_ancestors.begin());
149  totalSizeWithAncestors += stageit->GetTxSize();
150 
151  if (stageit->GetSizeWithDescendants() + entry_size >
152  limitDescendantSize) {
153  errString = strprintf(
154  "exceeds descendant size limit for tx %s [limit: %u]",
155  stageit->GetTx().GetId().ToString(), limitDescendantSize);
156  return false;
157  }
158 
159  if (stageit->GetCountWithDescendants() + entry_count >
160  limitDescendantCount) {
161  errString = strprintf("too many descendants for tx %s [limit: %u]",
162  stageit->GetTx().GetId().ToString(),
163  limitDescendantCount);
164  return false;
165  }
166 
167  if (totalSizeWithAncestors > limitAncestorSize) {
168  errString = strprintf("exceeds ancestor size limit [limit: %u]",
169  limitAncestorSize);
170  return false;
171  }
172 
173  const CTxMemPoolEntry::Parents &parents =
174  stageit->GetMemPoolParentsConst();
175  for (const CTxMemPoolEntry &parent : parents) {
176  txiter parent_it = mapTx.iterator_to(parent);
177 
178  // If this is a new ancestor, add it.
179  if (setAncestors.count(parent_it) == 0) {
180  staged_ancestors.insert(parent);
181  }
182  if (staged_ancestors.size() + setAncestors.size() + entry_count >
183  limitAncestorCount) {
184  errString =
185  strprintf("too many unconfirmed ancestors [limit: %u]",
186  limitAncestorCount);
187  return false;
188  }
189  }
190  }
191 
192  return true;
193 }
194 
196  uint64_t limitAncestorCount,
197  uint64_t limitAncestorSize,
198  uint64_t limitDescendantCount,
199  uint64_t limitDescendantSize,
200  std::string &errString) const {
201  CTxMemPoolEntry::Parents staged_ancestors;
202  size_t total_size = 0;
203  for (const auto &tx : package) {
204  total_size += GetVirtualTransactionSize(*tx);
205  for (const auto &input : tx->vin) {
206  std::optional<txiter> piter = GetIter(input.prevout.GetTxId());
207  if (piter) {
208  staged_ancestors.insert(**piter);
209  if (staged_ancestors.size() + package.size() >
210  limitAncestorCount) {
211  errString =
212  strprintf("too many unconfirmed parents [limit: %u]",
213  limitAncestorCount);
214  return false;
215  }
216  }
217  }
218  }
219  // When multiple transactions are passed in, the ancestors and descendants
220  // of all transactions considered together must be within limits even if
221  // they are not interdependent. This may be stricter than the limits for
222  // each individual transaction.
223  setEntries setAncestors;
224  const auto ret = CalculateAncestorsAndCheckLimits(
225  total_size, package.size(), setAncestors, staged_ancestors,
226  limitAncestorCount, limitAncestorSize, limitDescendantCount,
227  limitDescendantSize, errString);
228  // It's possible to overestimate the ancestor/descendant totals.
229  if (!ret) {
230  errString.insert(0, "possibly ");
231  }
232  return ret;
233 }
234 
236  const CTxMemPoolEntry &entry, setEntries &setAncestors,
237  uint64_t limitAncestorCount, uint64_t limitAncestorSize,
238  uint64_t limitDescendantCount, uint64_t limitDescendantSize,
239  std::string &errString, bool fSearchForParents /* = true */) const {
240  CTxMemPoolEntry::Parents staged_ancestors;
241  const CTransaction &tx = entry.GetTx();
242 
243  if (fSearchForParents) {
244  // Get parents of this transaction that are in the mempool
245  // GetMemPoolParents() is only valid for entries in the mempool, so we
246  // iterate mapTx to find parents.
247  for (const CTxIn &in : tx.vin) {
248  std::optional<txiter> piter = GetIter(in.prevout.GetTxId());
249  if (!piter) {
250  continue;
251  }
252  staged_ancestors.insert(**piter);
253  if (staged_ancestors.size() + 1 > limitAncestorCount) {
254  errString =
255  strprintf("too many unconfirmed parents [limit: %u]",
256  limitAncestorCount);
257  return false;
258  }
259  }
260  } else {
261  // If we're not searching for parents, we require this to be an entry in
262  // the mempool already.
263  staged_ancestors = entry.GetMemPoolParentsConst();
264  }
265 
267  entry.GetTxSize(), /* entry_count */ 1, setAncestors, staged_ancestors,
268  limitAncestorCount, limitAncestorSize, limitDescendantCount,
269  limitDescendantSize, errString);
270 }
271 
273  const setEntries *setAncestors) {
274  // add or remove this tx as a child of each parent
275  for (const CTxMemPoolEntry &parent : it->GetMemPoolParentsConst()) {
276  UpdateChild(mapTx.iterator_to(parent), it, add);
277  }
278 
279  // Remove this after wellington
280  if (setAncestors && !wellingtonLatched) {
281  const int64_t updateCount = (add ? 1 : -1);
282  const int64_t updateSize = updateCount * it->GetTxSize();
283  const int64_t updateSigChecks = updateCount * it->GetSigChecks();
284  const Amount updateFee = updateCount * it->GetModifiedFee();
285  for (txiter ancestorIt : *setAncestors) {
286  mapTx.modify(ancestorIt,
287  update_descendant_state(updateSize, updateFee,
288  updateCount, updateSigChecks));
289  }
290  }
291 }
292 
294  const setEntries *setAncestors) {
295  if (!setAncestors || wellingtonLatched) {
296  return;
297  }
298 
299  int64_t updateCount = setAncestors->size();
300  int64_t updateSize = 0;
301  int64_t updateSigChecks = 0;
302  Amount updateFee = Amount::zero();
303 
304  for (txiter ancestorIt : *setAncestors) {
305  updateSize += ancestorIt->GetTxSize();
306  updateFee += ancestorIt->GetModifiedFee();
307  updateSigChecks += ancestorIt->GetSigChecks();
308  }
309  mapTx.modify(it, update_ancestor_state(updateSize, updateFee, updateCount,
310  updateSigChecks));
311 }
312 
314  const CTxMemPoolEntry::Children &children = it->GetMemPoolChildrenConst();
315  for (const CTxMemPoolEntry &updateIt : children) {
316  UpdateParent(mapTx.iterator_to(updateIt), it, false);
317  }
318 }
319 
321  bool updateDescendants) {
322  if (!wellingtonLatched) {
323  // remove this branch after wellington
324  // slow quadratic branch, only for pre-activation compatibility
325 
326  // For each entry, walk back all ancestors and decrement size associated
327  // with this transaction.
328  if (updateDescendants) {
329  // updateDescendants should be true whenever we're not recursively
330  // removing a tx and all its descendants, eg when a transaction is
331  // confirmed in a block.
332  // Here we only update statistics and not data in
333  // CTxMemPool::Parents and CTxMemPoolEntry::Children (which we need
334  // to preserve until we're finished with all operations that need to
335  // traverse the mempool).
336  for (txiter removeIt : entriesToRemove) {
337  setEntries setDescendants;
338  CalculateDescendants(removeIt, setDescendants);
339  setDescendants.erase(removeIt); // don't update state for self
340  int64_t modifySize = -int64_t(removeIt->GetTxSize());
341  Amount modifyFee = -1 * removeIt->GetModifiedFee();
342  int modifySigChecks = -removeIt->GetSigChecks();
343  for (txiter dit : setDescendants) {
344  mapTx.modify(dit,
345  update_ancestor_state(modifySize, modifyFee,
346  -1, modifySigChecks));
347  }
348  }
349  }
350 
351  for (txiter removeIt : entriesToRemove) {
352  setEntries setAncestors;
353  const CTxMemPoolEntry &entry = *removeIt;
354  std::string dummy;
355  // Since this is a tx that is already in the mempool, we can call
356  // CMPA with fSearchForParents = false. If the mempool is in a
357  // consistent state, then using true or false should both be
358  // correct, though false should be a bit faster.
359  CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit,
360  nNoLimit, nNoLimit, dummy, false);
361  // Note that UpdateParentsOf severs the child links that point to
362  // removeIt in the entries for the parents of removeIt.
363  UpdateParentsOf(false, removeIt, &setAncestors);
364  }
365  } else {
366  for (txiter removeIt : entriesToRemove) {
367  // Note that UpdateParentsOf severs the child links that point to
368  // removeIt in the entries for the parents of removeIt.
369  UpdateParentsOf(false, removeIt);
370  }
371  }
372  // After updating all the parent links, we can now sever the link between
373  // each transaction being removed and any mempool children (ie, update
374  // CTxMemPoolEntry::m_parents for each direct child of a transaction being
375  // removed).
376  for (txiter removeIt : entriesToRemove) {
377  UpdateChildrenForRemoval(removeIt);
378  }
379 }
380 
382  Amount modifyFee,
383  int64_t modifyCount,
384  int64_t modifySigChecks) {
385  nSizeWithDescendants += modifySize;
386  assert(int64_t(nSizeWithDescendants) > 0);
387  nModFeesWithDescendants += modifyFee;
388  nCountWithDescendants += modifyCount;
389  assert(int64_t(nCountWithDescendants) > 0);
390  nSigChecksWithDescendants += modifySigChecks;
392 }
393 
394 void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, Amount modifyFee,
395  int64_t modifyCount,
396  int64_t modifySigChecks) {
397  nSizeWithAncestors += modifySize;
398  assert(int64_t(nSizeWithAncestors) > 0);
399  nModFeesWithAncestors += modifyFee;
400  nCountWithAncestors += modifyCount;
401  assert(int64_t(nCountWithAncestors) > 0);
402  nSigChecksWithAncestors += modifySigChecks;
404 }
405 
406 CTxMemPool::CTxMemPool(int check_ratio) : m_check_ratio(check_ratio) {
407  // lock free clear
408  _clear();
409 }
410 
412 
413 bool CTxMemPool::isSpent(const COutPoint &outpoint) const {
414  LOCK(cs);
415  return mapNextTx.count(outpoint);
416 }
417 
419  return nTransactionsUpdated;
420 }
421 
424 }
425 
426 void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entryIn,
427  const setEntries &setAncestors) {
428  CTxMemPoolEntry entry{entryIn};
429  // get a guaranteed unique id (in case tests re-use the same object)
430  entry.SetEntryId(nextEntryId++);
431 
432  // Update transaction for any feeDelta created by PrioritiseTransaction
433  {
434  Amount feeDelta = Amount::zero();
435  ApplyDelta(entry.GetTx().GetId(), feeDelta);
436  entry.UpdateFeeDelta(feeDelta);
437  }
438 
439  // Add to memory pool without checking anything.
440  // Used by AcceptToMemoryPool(), which DOES do all the appropriate checks.
441  auto [newit, inserted] = mapTx.insert(entry);
442  // Sanity check: It is a programming error if insertion fails (uniqueness
443  // invariants in mapTx are violated, etc)
444  assert(inserted);
445  // Sanity check: We should always end up inserting at the end of the
446  // entry_id index
447  assert(&*mapTx.get<entry_id>().rbegin() == &*newit);
448 
449  // Update cachedInnerUsage to include contained transaction's usage.
450  // (When we update the entry for in-mempool parents, memory usage will be
451  // further updated.)
452  cachedInnerUsage += entry.DynamicMemoryUsage();
453 
454  const CTransaction &tx = newit->GetTx();
455  std::set<TxId> setParentTransactions;
456  for (const CTxIn &in : tx.vin) {
457  mapNextTx.insert(std::make_pair(&in.prevout, &tx));
458  setParentTransactions.insert(in.prevout.GetTxId());
459  }
460  // Don't bother worrying about child transactions of this one. It is
461  // guaranteed that a new transaction arriving will not have any children,
462  // because such children would be orphans.
463 
464  // Update ancestors with information about this tx
465  for (const auto &pit : GetIterSet(setParentTransactions)) {
466  UpdateParent(newit, pit, true);
467  }
468 
469  const setEntries *pSetEntries = wellingtonLatched ? nullptr : &setAncestors;
470  UpdateParentsOf(true, newit, pSetEntries);
471  UpdateEntryForAncestors(newit, pSetEntries);
472 
474  totalTxSize += entry.GetTxSize();
475  m_total_fee += entry.GetFee();
476 }
477 
479  // We increment mempool sequence value no matter removal reason
480  // even if not directly reported below.
481  uint64_t mempool_sequence = GetAndIncrementSequence();
482 
483  if (reason != MemPoolRemovalReason::BLOCK) {
484  // Notify clients that a transaction has been removed from the mempool
485  // for any reason except being included in a block. Clients interested
486  // in transactions included in blocks can subscribe to the
487  // BlockConnected notification.
489  it->GetSharedTx(), reason, mempool_sequence);
490  }
491 
492  for (const CTxIn &txin : it->GetTx().vin) {
493  mapNextTx.erase(txin.prevout);
494  }
495 
496  /* add logging because unchecked */
497  RemoveUnbroadcastTx(it->GetTx().GetId(), true);
498 
499  totalTxSize -= it->GetTxSize();
500  m_total_fee -= it->GetFee();
501  cachedInnerUsage -= it->DynamicMemoryUsage();
502  cachedInnerUsage -= memusage::DynamicUsage(it->GetMemPoolParentsConst()) +
503  memusage::DynamicUsage(it->GetMemPoolChildrenConst());
504  mapTx.erase(it);
506 }
507 
508 // Calculates descendants of entry that are not already in setDescendants, and
509 // adds to setDescendants. Assumes entryit is already a tx in the mempool and
510 // CTxMemPoolEntry::m_children is correct for tx and all descendants. Also
511 // assumes that if an entry is in setDescendants already, then all in-mempool
512 // descendants of it are already in setDescendants as well, so that we can save
513 // time by not iterating over those entries.
515  setEntries &setDescendants) const {
516  setEntries stage;
517  if (setDescendants.count(entryit) == 0) {
518  stage.insert(entryit);
519  }
520  // Traverse down the children of entry, only adding children that are not
521  // accounted for in setDescendants already (because those children have
522  // either already been walked, or will be walked in this iteration).
523  while (!stage.empty()) {
524  txiter it = *stage.begin();
525  setDescendants.insert(it);
526  stage.erase(stage.begin());
527 
528  const CTxMemPoolEntry::Children &children =
529  it->GetMemPoolChildrenConst();
530  for (const CTxMemPoolEntry &child : children) {
531  txiter childiter = mapTx.iterator_to(child);
532  if (!setDescendants.count(childiter)) {
533  stage.insert(childiter);
534  }
535  }
536  }
537 }
538 
540  MemPoolRemovalReason reason) {
541  // Remove transaction from memory pool.
543  setEntries txToRemove;
544  txiter origit = mapTx.find(origTx.GetId());
545  if (origit != mapTx.end()) {
546  txToRemove.insert(origit);
547  } else {
548  // When recursively removing but origTx isn't in the mempool be sure to
549  // remove any children that are in the pool. This can happen during
550  // chain re-orgs if origTx isn't re-accepted into the mempool for any
551  // reason.
552  auto it = mapNextTx.lower_bound(COutPoint(origTx.GetId(), 0));
553  while (it != mapNextTx.end() &&
554  it->first->GetTxId() == origTx.GetId()) {
555  txiter nextit = mapTx.find(it->second->GetId());
556  assert(nextit != mapTx.end());
557  txToRemove.insert(nextit);
558  ++it;
559  }
560  }
561 
562  setEntries setAllRemoves;
563  for (txiter it : txToRemove) {
564  CalculateDescendants(it, setAllRemoves);
565  }
566 
567  RemoveStaged(setAllRemoves, false, reason);
568 }
569 
571  // Remove transactions which depend on inputs of tx, recursively
573  for (const CTxIn &txin : tx.vin) {
574  auto it = mapNextTx.find(txin.prevout);
575  if (it != mapNextTx.end()) {
576  const CTransaction &txConflict = *it->second;
577  if (txConflict != tx) {
578  ClearPrioritisation(txConflict.GetId());
580  }
581  }
582  }
583 }
584 
589 void CTxMemPool::removeForBlock(const std::vector<CTransactionRef> &vtx) {
591 
592  if (mapTx.empty() && mapDeltas.empty()) {
593  // fast-path for IBD and/or when mempool is empty; there is no need to
594  // do any of the set-up work below which eats precious cycles.
595  // Note that this also skips updating the rolling fee udpate, which is
596  // fine: it is only recomputed when the mempool has to be trimmed down
597  // because it is full which is contradictory with this condition.
598  return;
599  }
600 
601  DisconnectedBlockTransactions disconnectpool;
602  disconnectpool.addForBlock(vtx, *this);
603 
604  for (const CTransactionRef &tx :
605  reverse_iterate(disconnectpool.GetQueuedTx().get<insertion_order>())) {
606  txiter it = mapTx.find(tx->GetId());
607  if (it != mapTx.end()) {
608  setEntries stage;
609  stage.insert(it);
611  } else {
612  // Conflicting txs can only exist if the tx was not in the mempool
613  removeConflicts(*tx);
614  }
615  ClearPrioritisation(tx->GetId());
616  }
617 
618  disconnectpool.clear();
619 
620  lastRollingFeeUpdate = GetTime();
621  blockSinceLastRollingFeeBump = true;
622 }
623 
625  mapTx.clear();
626  mapNextTx.clear();
627  totalTxSize = 0;
628  m_total_fee = Amount::zero();
629  cachedInnerUsage = 0;
630  lastRollingFeeUpdate = GetTime();
631  blockSinceLastRollingFeeBump = false;
632  rollingMinimumFeeRate = 0;
634 }
635 
637  LOCK(cs);
638  _clear();
639 }
640 
641 void CTxMemPool::check(const CCoinsViewCache &active_coins_tip,
642  int64_t spendheight) const {
643  if (m_check_ratio == 0) {
644  return;
645  }
646 
647  if (GetRand(m_check_ratio) >= 1) {
648  return;
649  }
650 
652  LOCK(cs);
654  "Checking mempool with %u transactions and %u inputs\n",
655  (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
656 
657  uint64_t checkTotal = 0;
658  Amount check_total_fee{Amount::zero()};
659  uint64_t innerUsage = 0;
660 
661  CCoinsViewCache mempoolDuplicate(
662  const_cast<CCoinsViewCache *>(&active_coins_tip));
663 
664  for (const CTxMemPoolEntry &entry : mapTx.get<entry_id>()) {
665  checkTotal += entry.GetTxSize();
666  check_total_fee += entry.GetFee();
667  innerUsage += entry.DynamicMemoryUsage();
668  const CTransaction &tx = entry.GetTx();
669  innerUsage += memusage::DynamicUsage(entry.GetMemPoolParentsConst()) +
670  memusage::DynamicUsage(entry.GetMemPoolChildrenConst());
671 
672  CTxMemPoolEntry::Parents setParentCheck;
673  for (const CTxIn &txin : tx.vin) {
674  // Check that every mempool transaction's inputs refer to available
675  // coins, or other mempool tx's.
676  txiter parentIt = mapTx.find(txin.prevout.GetTxId());
677  if (parentIt != mapTx.end()) {
678  const CTransaction &parentTx = parentIt->GetTx();
679  assert(parentTx.vout.size() > txin.prevout.GetN() &&
680  !parentTx.vout[txin.prevout.GetN()].IsNull());
681  setParentCheck.insert(*parentIt);
682  // also check that parents have a topological ordering before
683  // their children
684  assert(parentIt->GetEntryId() < entry.GetEntryId());
685  }
686  // We are iterating through the mempool entries sorted
687  // topologically.
688  // All parents must have been checked before their children and
689  // their coins added to the mempoolDuplicate coins cache.
690  assert(mempoolDuplicate.HaveCoin(txin.prevout));
691  // Check whether its inputs are marked in mapNextTx.
692  auto prevoutNextIt = mapNextTx.find(txin.prevout);
693  assert(prevoutNextIt != mapNextTx.end());
694  assert(prevoutNextIt->first == &txin.prevout);
695  assert(prevoutNextIt->second == &tx);
696  }
697  auto comp = [](const CTxMemPoolEntry &a,
698  const CTxMemPoolEntry &b) -> bool {
699  return a.GetTx().GetId() == b.GetTx().GetId();
700  };
701  assert(setParentCheck.size() == entry.GetMemPoolParentsConst().size());
702  assert(std::equal(setParentCheck.begin(), setParentCheck.end(),
703  entry.GetMemPoolParentsConst().begin(), comp));
704 
705  // Verify ancestor state is correct.
706  setEntries setAncestors;
707  std::string dummy;
708 
709  const bool ok = CalculateMemPoolAncestors(
710  entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
711  assert(ok);
712 
713  if (!wellingtonLatched) {
714  uint64_t nCountCheck = setAncestors.size() + 1;
715  uint64_t nSizeCheck = entry.GetTxSize();
716  Amount nFeesCheck = entry.GetModifiedFee();
717  int64_t nSigChecksCheck = entry.GetSigChecks();
718 
719  for (txiter ancestorIt : setAncestors) {
720  nSizeCheck += ancestorIt->GetTxSize();
721  nFeesCheck += ancestorIt->GetModifiedFee();
722  nSigChecksCheck += ancestorIt->GetSigChecks();
723  }
724 
725  assert(entry.GetCountWithAncestors() == nCountCheck);
726  assert(entry.GetSizeWithAncestors() == nSizeCheck);
727  assert(entry.GetSigChecksWithAncestors() == nSigChecksCheck);
728  assert(entry.GetModFeesWithAncestors() == nFeesCheck);
729  }
730 
731  // all ancestors should have entryId < this tx's entryId
732  for (const auto &ancestor : setAncestors) {
733  assert(ancestor->GetEntryId() < entry.GetEntryId());
734  }
735 
736  // Check children against mapNextTx
737  CTxMemPoolEntry::Children setChildrenCheck;
738  auto iter = mapNextTx.lower_bound(COutPoint(entry.GetTx().GetId(), 0));
739  uint64_t child_sizes = 0;
740  int64_t child_sigChecks = 0;
741  for (; iter != mapNextTx.end() &&
742  iter->first->GetTxId() == entry.GetTx().GetId();
743  ++iter) {
744  txiter childIt = mapTx.find(iter->second->GetId());
745  // mapNextTx points to in-mempool transactions
746  assert(childIt != mapTx.end());
747  if (setChildrenCheck.insert(*childIt).second) {
748  child_sizes += childIt->GetTxSize();
749  child_sigChecks += childIt->GetSigChecks();
750  }
751  }
752  assert(setChildrenCheck.size() ==
753  entry.GetMemPoolChildrenConst().size());
754  assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(),
755  entry.GetMemPoolChildrenConst().begin(), comp));
756  if (!wellingtonLatched) {
757  // Also check to make sure size is greater than sum with immediate
758  // children. Just a sanity check, not definitive that this calc is
759  // correct...
760  assert(entry.GetSizeWithDescendants() >=
761  child_sizes + entry.GetTxSize());
762  assert(entry.GetSigChecksWithDescendants() >=
763  child_sigChecks + entry.GetSigChecks());
764  }
765 
766  // Not used. CheckTxInputs() should always pass
767  TxValidationState dummy_state;
768  Amount txfee{Amount::zero()};
769  assert(!tx.IsCoinBase());
770  assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate,
771  spendheight, txfee));
772  for (const auto &input : tx.vin) {
773  mempoolDuplicate.SpendCoin(input.prevout);
774  }
775  AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
776  }
777 
778  for (auto &[_, nextTx] : mapNextTx) {
779  txiter it = mapTx.find(nextTx->GetId());
780  assert(it != mapTx.end());
781  assert(&it->GetTx() == nextTx);
782  }
783 
784  assert(totalTxSize == checkTotal);
785  assert(m_total_fee == check_total_fee);
786  assert(innerUsage == cachedInnerUsage);
787 }
788 
790  const TxId &txidb) const {
791  LOCK(cs);
792  auto it1 = mapTx.find(txida);
793  if (it1 == mapTx.end()) {
794  return false;
795  }
796  auto it2 = mapTx.find(txidb);
797  if (it2 == mapTx.end()) {
798  return true;
799  }
800  return it1->GetEntryId() < it2->GetEntryId();
801 }
802 
803 void CTxMemPool::getAllTxIds(std::vector<TxId> &vtxid) const {
804  LOCK(cs);
805 
806  vtxid.clear();
807  vtxid.reserve(mapTx.size());
808 
809  for (const auto &entry : mapTx.get<entry_id>()) {
810  vtxid.push_back(entry.GetTx().GetId());
811  }
812 }
813 
814 static TxMempoolInfo
815 GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
816  return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(),
817  it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
818 }
819 
820 std::vector<TxMempoolInfo> CTxMemPool::infoAll() const {
821  LOCK(cs);
822 
823  std::vector<TxMempoolInfo> ret;
824  ret.reserve(mapTx.size());
825 
826  const auto &index = mapTx.get<entry_id>();
827  for (auto it = index.begin(); it != index.end(); ++it) {
828  ret.push_back(GetInfo(mapTx.project<0>(it)));
829  }
830 
831  return ret;
832 }
833 
835  LOCK(cs);
836  indexed_transaction_set::const_iterator i = mapTx.find(txid);
837  if (i == mapTx.end()) {
838  return nullptr;
839  }
840 
841  return i->GetSharedTx();
842 }
843 
844 TxMempoolInfo CTxMemPool::info(const TxId &txid) const {
845  LOCK(cs);
846  indexed_transaction_set::const_iterator i = mapTx.find(txid);
847  if (i == mapTx.end()) {
848  return TxMempoolInfo();
849  }
850 
851  return GetInfo(i);
852 }
853 
855  LOCK(cs);
856 
857  uint64_t maxMempoolSize =
858  gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
859  // minerPolicy uses recent blocks to figure out a reasonable fee. This
860  // may disagree with the rollingMinimumFeerate under certain scenarios
861  // where the mempool increases rapidly, or blocks are being mined which
862  // do not contain propagated transactions.
863  return std::max(::minRelayTxFee, GetMinFee(maxMempoolSize));
864 }
865 
867  const Amount nFeeDelta) {
868  {
869  LOCK(cs);
870  Amount &delta = mapDeltas[txid];
871  delta += nFeeDelta;
872  txiter it = mapTx.find(txid);
873  if (it != mapTx.end()) {
874  mapTx.modify(
875  it, [&delta](CTxMemPoolEntry &e) { e.UpdateFeeDelta(delta); });
876  // Remove after wellington
877  if (!wellingtonLatched) {
878  // Now update all ancestors' modified fees with descendants
879  setEntries setAncestors;
880  std::string dummy;
881  CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit,
882  nNoLimit, nNoLimit, dummy, false);
883  for (txiter ancestorIt : setAncestors) {
884  mapTx.modify(ancestorIt,
885  update_descendant_state(0, nFeeDelta, 0, 0));
886  }
887 
888  // Now update all descendants' modified fees with ancestors
889  setEntries setDescendants;
890  CalculateDescendants(it, setDescendants);
891  setDescendants.erase(it);
892  for (txiter descendantIt : setDescendants) {
893  mapTx.modify(descendantIt,
894  update_ancestor_state(0, nFeeDelta, 0, 0));
895  }
896  }
898  }
899  }
900  LogPrintf("PrioritiseTransaction: %s fee += %s\n", txid.ToString(),
901  FormatMoney(nFeeDelta));
902 }
903 
904 void CTxMemPool::ApplyDelta(const TxId &txid, Amount &nFeeDelta) const {
906  std::map<TxId, Amount>::const_iterator pos = mapDeltas.find(txid);
907  if (pos == mapDeltas.end()) {
908  return;
909  }
910 
911  nFeeDelta += pos->second;
912 }
913 
916  mapDeltas.erase(txid);
917 }
918 
919 const CTransaction *CTxMemPool::GetConflictTx(const COutPoint &prevout) const {
920  const auto it = mapNextTx.find(prevout);
921  return it == mapNextTx.end() ? nullptr : it->second;
922 }
923 
924 std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const TxId &txid) const {
925  auto it = mapTx.find(txid);
926  if (it != mapTx.end()) {
927  return it;
928  }
929  return std::nullopt;
930 }
931 
933 CTxMemPool::GetIterSet(const std::set<TxId> &txids) const {
935  for (const auto &txid : txids) {
936  const auto mi = GetIter(txid);
937  if (mi) {
938  ret.insert(*mi);
939  }
940  }
941  return ret;
942 }
943 
945  for (const CTxIn &in : tx.vin) {
946  if (exists(in.prevout.GetTxId())) {
947  return false;
948  }
949  }
950 
951  return true;
952 }
953 
955  const CTxMemPool &mempoolIn)
956  : CCoinsViewBacked(baseIn), mempool(mempoolIn) {}
957 
958 bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
959  // Check to see if the inputs are made available by another tx in the
960  // package. These Coins would not be available in the underlying CoinsView.
961  if (auto it = m_temp_added.find(outpoint); it != m_temp_added.end()) {
962  coin = it->second;
963  return true;
964  }
965 
966  // If an entry in the mempool exists, always return that one, as it's
967  // guaranteed to never conflict with the underlying cache, and it cannot
968  // have pruned entries (as it contains full) transactions. First checking
969  // the underlying cache risks returning a pruned entry instead.
970  CTransactionRef ptx = mempool.get(outpoint.GetTxId());
971  if (ptx) {
972  if (outpoint.GetN() < ptx->vout.size()) {
973  coin = Coin(ptx->vout[outpoint.GetN()], MEMPOOL_HEIGHT, false);
974  return true;
975  }
976  return false;
977  }
978  return base->GetCoin(outpoint, coin);
979 }
980 
982  for (uint32_t n = 0; n < tx->vout.size(); ++n) {
983  m_temp_added.emplace(COutPoint(tx->GetId(), n),
984  Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
985  }
986 }
987 
989  LOCK(cs);
990  // Estimate the overhead of mapTx to be 12 pointers + an allocation, as no
991  // exact formula for boost::multi_index_contained is implemented.
992  return memusage::MallocUsage(sizeof(CTxMemPoolEntry) +
993  12 * sizeof(void *)) *
994  mapTx.size() +
995  memusage::DynamicUsage(mapNextTx) +
996  memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;
997 }
998 
999 void CTxMemPool::RemoveUnbroadcastTx(const TxId &txid, const bool unchecked) {
1000  LOCK(cs);
1001 
1002  if (m_unbroadcast_txids.erase(txid)) {
1003  LogPrint(
1004  BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n",
1005  txid.GetHex(),
1006  (unchecked ? " before confirmation that txn was sent out" : ""));
1007  }
1008 }
1009 
1010 void CTxMemPool::RemoveStaged(const setEntries &stage, bool updateDescendants,
1011  MemPoolRemovalReason reason) {
1012  AssertLockHeld(cs);
1013  UpdateForRemoveFromMempool(stage, updateDescendants);
1014  for (txiter it : stage) {
1015  removeUnchecked(it, reason);
1016  }
1017 }
1018 
1019 int CTxMemPool::Expire(std::chrono::seconds time) {
1020  AssertLockHeld(cs);
1021  indexed_transaction_set::index<entry_time>::type::iterator it =
1022  mapTx.get<entry_time>().begin();
1023  setEntries toremove;
1024  while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) {
1025  toremove.insert(mapTx.project<0>(it));
1026  it++;
1027  }
1028 
1029  setEntries stage;
1030  for (txiter removeit : toremove) {
1031  CalculateDescendants(removeit, stage);
1032  }
1033 
1035  return stage.size();
1036 }
1037 
1038 void CTxMemPool::LimitSize(CCoinsViewCache &coins_cache, size_t limit,
1039  std::chrono::seconds age) {
1041  AssertLockHeld(cs);
1042  int expired = Expire(GetTime<std::chrono::seconds>() - age);
1043  if (expired != 0) {
1045  "Expired %i transactions from the memory pool\n", expired);
1046  }
1047 
1048  std::vector<COutPoint> vNoSpendsRemaining;
1049  TrimToSize(limit, &vNoSpendsRemaining);
1050  for (const COutPoint &removed : vNoSpendsRemaining) {
1051  coins_cache.Uncache(removed);
1052  }
1053 }
1054 
1055 void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry) {
1056  setEntries setAncestors;
1057  if (!wellingtonLatched) {
1058  std::string dummy;
1059  CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit,
1060  nNoLimit, nNoLimit, dummy);
1061  }
1062  return addUnchecked(entry, setAncestors);
1063 }
1064 
1065 void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add) {
1066  AssertLockHeld(cs);
1068  if (add && entry->GetMemPoolChildren().insert(*child).second) {
1069  cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
1070  } else if (!add && entry->GetMemPoolChildren().erase(*child)) {
1071  cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
1072  }
1073 }
1074 
1075 void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add) {
1076  AssertLockHeld(cs);
1078  if (add && entry->GetMemPoolParents().insert(*parent).second) {
1079  cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
1080  } else if (!add && entry->GetMemPoolParents().erase(*parent)) {
1081  cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
1082  }
1083 }
1084 
1085 CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
1086  LOCK(cs);
1087  if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0) {
1088  return CFeeRate(int64_t(ceill(rollingMinimumFeeRate)) * SATOSHI);
1089  }
1090 
1091  int64_t time = GetTime();
1092  if (time > lastRollingFeeUpdate + 10) {
1093  double halflife = ROLLING_FEE_HALFLIFE;
1094  if (DynamicMemoryUsage() < sizelimit / 4) {
1095  halflife /= 4;
1096  } else if (DynamicMemoryUsage() < sizelimit / 2) {
1097  halflife /= 2;
1098  }
1099 
1100  rollingMinimumFeeRate =
1101  rollingMinimumFeeRate /
1102  pow(2.0, (time - lastRollingFeeUpdate) / halflife);
1103  lastRollingFeeUpdate = time;
1104  }
1105  return CFeeRate(int64_t(ceill(rollingMinimumFeeRate)) * SATOSHI);
1106 }
1107 
1109  AssertLockHeld(cs);
1110  if ((rate.GetFeePerK() / SATOSHI) > rollingMinimumFeeRate) {
1111  rollingMinimumFeeRate = rate.GetFeePerK() / SATOSHI;
1112  blockSinceLastRollingFeeBump = false;
1113  }
1114 }
1115 
1116 void CTxMemPool::TrimToSize(size_t sizelimit,
1117  std::vector<COutPoint> *pvNoSpendsRemaining) {
1118  AssertLockHeld(cs);
1119 
1120  unsigned nTxnRemoved = 0;
1121  CFeeRate maxFeeRateRemoved(Amount::zero());
1122  while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
1123  auto it = mapTx.get<modified_feerate>().end();
1124  --it;
1125 
1126  // We set the new mempool min fee to the feerate of the removed
1127  // transaction, plus the "minimum reasonable fee rate" (ie some value
1128  // under which we consider txn to have 0 fee). This way, we don't allow
1129  // txn to enter mempool with feerate equal to txn which were removed
1130  // with no block in between.
1131  CFeeRate removed = it->GetModifiedFeeRate();
1132  removed += MEMPOOL_FULL_FEE_INCREMENT;
1133 
1134  trackPackageRemoved(removed);
1135  maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
1136 
1137  setEntries stage;
1138  CalculateDescendants(mapTx.project<0>(it), stage);
1139  nTxnRemoved += stage.size();
1140 
1141  if (pvNoSpendsRemaining) {
1142  for (const txiter &iter : stage) {
1143  for (const CTxIn &txin : iter->GetTx().vin) {
1144  if (!exists(txin.prevout.GetTxId())) {
1145  pvNoSpendsRemaining->push_back(txin.prevout);
1146  }
1147  }
1148  }
1149  }
1150 
1152  }
1153 
1154  if (maxFeeRateRemoved > CFeeRate(Amount::zero())) {
1156  "Removed %u txn, rolling minimum fee bumped to %s\n",
1157  nTxnRemoved, maxFeeRateRemoved.ToString());
1158  }
1159 }
1160 
1163  // find parent with highest descendant count
1164  std::vector<txiter> candidates;
1165  setEntries counted;
1166  candidates.push_back(entry);
1167  uint64_t maximum = 0;
1168  while (candidates.size()) {
1169  txiter candidate = candidates.back();
1170  candidates.pop_back();
1171  if (!counted.insert(candidate).second) {
1172  continue;
1173  }
1174  const CTxMemPoolEntry::Parents &parents =
1175  candidate->GetMemPoolParentsConst();
1176  if (parents.size() == 0) {
1177  maximum = std::max(maximum, candidate->GetCountWithDescendants());
1178  } else {
1179  for (const CTxMemPoolEntry &i : parents) {
1180  candidates.push_back(mapTx.iterator_to(i));
1181  }
1182  }
1183  }
1184  return maximum;
1185 }
1186 
1187 void CTxMemPool::GetTransactionAncestry(const TxId &txid, size_t &ancestors,
1188  size_t &descendants,
1189  size_t *const ancestorsize,
1190  Amount *const ancestorfees) const {
1191  LOCK(cs);
1192  auto it = mapTx.find(txid);
1193  ancestors = descendants = 0;
1194  if (it != mapTx.end()) {
1195  ancestors = it->GetCountWithAncestors();
1196  if (ancestorsize) {
1197  *ancestorsize = it->GetSizeWithAncestors();
1198  }
1199  if (ancestorfees) {
1200  *ancestorfees = it->GetModFeesWithAncestors();
1201  }
1202  descendants = CalculateDescendantMaximum(it);
1203  }
1204 }
1205 
1206 bool CTxMemPool::IsLoaded() const {
1207  LOCK(cs);
1208  return m_is_loaded;
1209 }
1210 
1211 void CTxMemPool::SetIsLoaded(bool loaded) {
1212  LOCK(cs);
1213  m_is_loaded = loaded;
1214 }
1215 
1218 
1220  const std::vector<CTransactionRef> &vtx, CTxMemPool &pool) {
1221  AssertLockHeld(pool.cs);
1222  for (const auto &tx : reverse_iterate(vtx)) {
1223  // If we already added it, just skip.
1224  auto it = queuedTx.find(tx->GetId());
1225  if (it != queuedTx.end()) {
1226  continue;
1227  }
1228 
1229  // Insert the transaction into the pool.
1230  addTransaction(tx);
1231 
1232  // Fill in the set of parents.
1233  std::unordered_set<TxId, SaltedTxIdHasher> parents;
1234  for (const CTxIn &in : tx->vin) {
1235  parents.insert(in.prevout.GetTxId());
1236  }
1237 
1238  // In order to make sure we keep things in topological order, we check
1239  // if we already know of the parent of the current transaction. If so,
1240  // we remove them from the set and then add them back.
1241  while (parents.size() > 0) {
1242  std::unordered_set<TxId, SaltedTxIdHasher> worklist(
1243  std::move(parents));
1244  parents.clear();
1245 
1246  for (const TxId &txid : worklist) {
1247  // If we do not have that txid in the set, nothing needs to be
1248  // done.
1249  auto pit = queuedTx.find(txid);
1250  if (pit == queuedTx.end()) {
1251  continue;
1252  }
1253 
1254  // We have parent in our set, we reinsert them at the right
1255  // position.
1256  const CTransactionRef ptx = *pit;
1257  queuedTx.erase(pit);
1258  queuedTx.insert(ptx);
1259 
1260  // And we make sure ancestors are covered.
1261  for (const CTxIn &in : ptx->vin) {
1262  parents.insert(in.prevout.GetTxId());
1263  }
1264  }
1265  }
1266  }
1267 
1268  // Keep the size under control.
1270  // Drop the earliest entry, and remove its children from the
1271  // mempool.
1272  auto it = queuedTx.get<insertion_order>().begin();
1274  removeEntry(it);
1275  }
1276 }
1277 
1279  AssertLockHeld(pool.cs);
1280  // addForBlock's algorithm sorts a vector of transactions back into
1281  // topological order. We use it in a separate object to create a valid
1282  // ordering of all mempool transactions, which we then splice in front of
1283  // the current queuedTx. This results in a valid sequence of transactions to
1284  // be reprocessed in updateMempoolForReorg.
1285 
1286  // We create vtx in order of the entry_id index to facilitate for
1287  // addForBlocks (which iterates in reverse order), as vtx probably end in
1288  // the correct ordering for queuedTx.
1289  std::vector<CTransactionRef> vtx;
1290 
1291  vtx.reserve(pool.mapTx.size());
1292  txInfo.reserve(pool.mapTx.size());
1293  for (const CTxMemPoolEntry &e : pool.mapTx.get<entry_id>()) {
1294  vtx.push_back(e.GetSharedTx());
1295  // save entry time, feeDelta, and height for use in
1296  // updateMempoolForReorg()
1297  txInfo.try_emplace(e.GetTx().GetId(), e.GetTime(),
1298  e.GetModifiedFee() - e.GetFee(), e.GetHeight());
1299  // Notify all observers of this (possibly temporary) removal. This is
1300  // necessary for tracking the transactions that are removed from the
1301  // mempool during a reorg and can't be added back due to missing parent.
1302  // Transactions that are added back to the mempool will trigger another
1303  // notification.
1305  e.GetSharedTx(), MemPoolRemovalReason::REORG,
1306  pool.GetAndIncrementSequence());
1307  }
1308  pool.clear();
1309 
1310  // Use addForBlocks to sort the transactions and then splice them in front
1311  // of queuedTx
1312  DisconnectedBlockTransactions orderedTxnPool;
1313  orderedTxnPool.addForBlock(vtx, pool);
1314  cachedInnerUsage += orderedTxnPool.cachedInnerUsage;
1315  queuedTx.get<insertion_order>().splice(
1316  queuedTx.get<insertion_order>().begin(),
1317  orderedTxnPool.queuedTx.get<insertion_order>());
1318 
1319  // We limit memory usage because we can't know if more blocks will be
1320  // disconnected
1322  // Drop the earliest entry which, by definition, has no children
1323  removeEntry(queuedTx.get<insertion_order>().begin());
1324  }
1325 }
1326 
1328  -> const TxInfo * {
1329  if (auto it = txInfo.find(tx->GetId()); it != txInfo.end()) {
1330  return &it->second;
1331  }
1332 
1333  return nullptr;
1334 }
1335 
1337  const Config &config, Chainstate &active_chainstate, bool fAddToMempool,
1338  CTxMemPool &pool) {
1340  AssertLockHeld(pool.cs);
1341 
1342  if (fAddToMempool) {
1343  // disconnectpool's insertion_order index sorts the entries from oldest
1344  // to newest, but the oldest entry will be the last tx from the latest
1345  // mined block that was disconnected.
1346  // Iterate disconnectpool in reverse, so that we add transactions back
1347  // to the mempool starting with the earliest transaction that had been
1348  // previously seen in a block.
1349  for (const CTransactionRef &tx :
1351  if (tx->IsCoinBase()) {
1352  continue;
1353  }
1354  // restore saved PrioritiseTransaction state and nAcceptTime
1355  const auto ptxInfo = getTxInfo(tx);
1356  bool hasFeeDelta = false;
1357  if (ptxInfo && ptxInfo->feeDelta != Amount::zero()) {
1358  // manipulate mapDeltas directly (faster than calling
1359  // PrioritiseTransaction)
1360  pool.mapDeltas[tx->GetId()] = ptxInfo->feeDelta;
1361  hasFeeDelta = true;
1362  }
1363  // ignore validation errors in resurrected transactions
1364  auto result = AcceptToMemoryPool(
1365  config, active_chainstate, tx,
1366  /*accept_time=*/ptxInfo ? ptxInfo->time.count() : GetTime(),
1367  /*bypass_limits=*/true, /*test_accept=*/false,
1368  /*heightOverride=*/ptxInfo ? ptxInfo->height : 0);
1369  if (result.m_result_type !=
1371  hasFeeDelta) {
1372  // tx not accepted: undo mapDelta insertion from above
1373  pool.mapDeltas.erase(tx->GetId());
1374  }
1375  }
1376  }
1377 
1378  queuedTx.clear();
1379  txInfo.clear();
1380 
1381  // Re-limit mempool size, in case we added any transactions
1382  pool.LimitSize(active_chainstate.CoinsTip(),
1383  gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) *
1384  1000000,
1385  std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry",
1386  DEFAULT_MEMPOOL_EXPIRY)});
1387 }
static constexpr Amount SATOSHI
Definition: amount.h:143
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:591
An in-memory indexed chain of blocks.
Definition: chain.h:141
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:173
CCoinsView backed by another CCoinsView.
Definition: coins.h:184
CCoinsView * base
Definition: coins.h:186
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:203
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
Definition: coins.cpp:290
Abstract view on the open txout dataset.
Definition: coins.h:147
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:13
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:958
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:808
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:954
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:981
const CTxMemPool & mempool
Definition: txmempool.h:811
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
std::string ToString() const
Definition: feerate.cpp:57
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:54
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint32_t GetN() const
Definition: transaction.h:38
const TxId & GetTxId() const
Definition: transaction.h:37
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
const std::vector< CTxOut > vout
Definition: transaction.h:213
bool IsCoinBase() const
Definition: transaction.h:258
const std::vector< CTxIn > vin
Definition: transaction.h:212
const TxId GetId() const
Definition: transaction.h:246
An input of a transaction.
Definition: transaction.h:61
COutPoint prevout
Definition: transaction.h:63
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
std::set< CTxMemPoolEntryRef, CompareIteratorById > Parents
Definition: txmempool.h:92
uint64_t GetVirtualSizeWithDescendants() const
Definition: txmempool.cpp:110
int64_t nSigChecksWithDescendants
... and sichecks
Definition: txmempool.h:136
const CTransaction & GetTx() const
Definition: txmempool.h:154
const Parents & GetMemPoolParentsConst() const
Definition: txmempool.h:201
void UpdateAncestorState(int64_t modifySize, Amount modifyFee, int64_t modifyCount, int64_t modifySigChecks)
Definition: txmempool.cpp:394
const size_t nTxSize
... and avoid recomputing tx size
Definition: txmempool.h:105
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:132
uint64_t GetVirtualSizeWithAncestors() const
Definition: txmempool.cpp:118
Amount nModFeesWithAncestors
Definition: txmempool.h:141
void UpdateFeeDelta(Amount feeDelta)
Definition: txmempool.cpp:125
void SetEntryId(uint64_t eid)
This should only be set by addUnchecked() before entry insertion into mempool.
Definition: txmempool.h:152
int64_t nSigChecksWithAncestors
Definition: txmempool.h:142
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:130
size_t GetTxSize() const
Definition: txmempool.h:157
Amount feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:118
CTxMemPoolEntry(const CTransactionRef &_tx, const Amount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigchecks, LockPoints lp)
Definition: txmempool.cpp:93
Amount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:134
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:132
uint64_t nCountWithAncestors
Definition: txmempool.h:139
size_t GetTxVirtualSize() const
Definition: txmempool.cpp:105
void UpdateDescendantState(int64_t modifySize, Amount modifyFee, int64_t modifyCount, int64_t modifySigChecks)
Definition: txmempool.cpp:381
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:120
uint64_t nSizeWithAncestors
Definition: txmempool.h:140
std::set< CTxMemPoolEntryRef, CompareIteratorById > Children
Definition: txmempool.h:93
const int64_t sigChecks
Total sigChecks.
Definition: txmempool.h:115
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:355
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:570
CFeeRate estimateFee() const
Definition: txmempool.cpp:854
bool HasNoInputsOf(const CTransaction &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
Definition: txmempool.cpp:944
void ClearPrioritisation(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:914
std::set< txiter, CompareIteratorById > setEntries
Definition: txmempool.h:445
void RemoveUnbroadcastTx(const TxId &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:999
void UpdateParentsOf(bool add, txiter it, const setEntries *setAncestors=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update parents of it to add/remove it as a child transaction.
Definition: txmempool.cpp:272
void UpdateEntryForAncestors(txiter it, const setEntries *setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:293
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:441
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1108
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.cpp:1085
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:539
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:358
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1116
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:422
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:313
bool CompareTopologically(const TxId &txida, const TxId &txidb) const
Definition: txmempool.cpp:789
TxMempoolInfo info(const TxId &txid) const
Definition: txmempool.cpp:844
void getAllTxIds(std::vector< TxId > &vtxid) const
Definition: txmempool.cpp:803
std::atomic< uint32_t > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:360
setEntries GetIterSet(const std::set< TxId > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of txids into a set of pool iterators to avoid repeated lookups.
Definition: txmempool.cpp:933
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:988
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:820
void SetIsLoaded(bool loaded)
Sets the current loaded state.
Definition: txmempool.cpp:1211
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1075
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:478
void removeForBlock(const std::vector< CTransactionRef > &vtx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:589
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:1019
void clear()
Definition: txmempool.cpp:636
bool exists(const TxId &txid) const
Definition: txmempool.h:707
bool CheckPackageLimits(const Package &package, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ance...
Definition: txmempool.cpp:195
CTxMemPool(int check_ratio=0)
Create a new CTxMemPool.
Definition: txmempool.cpp:406
void LimitSize(CCoinsViewCache &coins_cache, size_t limit, std::chrono::seconds age) EXCLUSIVE_LOCKS_REQUIRED(cs
Reduce the size of the mempool by expiring and then trimming the mempool.
Definition: txmempool.cpp:1038
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:235
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:390
void GetTransactionAncestry(const TxId &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, Amount *ancestorfees=nullptr) const
Calculate the ancestor and descendant count for the given transaction.
Definition: txmempool.cpp:1187
CTransactionRef get(const TxId &txid) const
Definition: txmempool.cpp:834
void PrioritiseTransaction(const TxId &txid, const Amount nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:866
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:444
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:746
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1065
bool IsLoaded() const
Definition: txmempool.cpp:1206
std::atomic< bool > wellingtonLatched
Wellington activation latch.
Definition: txmempool.h:492
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
Definition: txmempool.cpp:919
void RemoveStaged(const setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1010
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:514
unsigned long size() const
Definition: txmempool.h:692
void ApplyDelta(const TxId &txid, Amount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:904
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:320
std::optional< txiter > GetIter(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given txid, if found.
Definition: txmempool.cpp:924
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:523
bool CalculateAncestorsAndCheckLimits(size_t entry_size, size_t entry_count, setEntries &setAncestors, CTxMemPoolEntry::Parents &staged_ancestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and desc...
Definition: txmempool.cpp:136
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove after wellington activates as this will be inaccurate.
Definition: txmempool.cpp:1162
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:413
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:418
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:522
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:624
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:646
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:770
A UTXO entry.
Definition: coins.h:27
Definition: config.h:17
void updateMempoolForReorg(const Config &config, Chainstate &active_chainstate, bool fAddToMempool, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transac...
Definition: txmempool.cpp:1336
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:936
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:859
TxInfoMap txInfo
populated by importMempool(); the original tx entry times and feeDeltas
Definition: txmempool.h:873
size_t DynamicMemoryUsage() const
Definition: txmempool.h:898
const TxInfo * getTxInfo(const CTransactionRef &tx) const
Definition: txmempool.cpp:1327
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:875
const indexed_disconnected_transactions & GetQueuedTx() const
Definition: txmempool.h:905
void addForBlock(const std::vector< CTransactionRef > &vtx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Definition: txmempool.cpp:1219
void importMempool(CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Definition: txmempool.cpp:1278
std::string ToString() const
Definition: uint256.h:78
std::string GetHex() const
Definition: uint256.cpp:16
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
Definition: coins.cpp:152
static const uint64_t DEFAULT_MAX_BLOCK_SIZE
Default setting for maximum allowed size for a block, in bytes.
Definition: consensus.h:20
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
#define LogPrint(category,...)
Definition: logging.h:208
#define LogPrintf(...)
Definition: logging.h:204
bool spendsCoinbase
LockPoints lp
unsigned int sigChecks
std::string FormatMoney(const Amount amt)
Do not use these functions to represent or parse monetary amounts to or from JSON but use AmountFromV...
Definition: moneystr.cpp:13
@ MEMPOOL
Definition: logging.h:41
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, Amount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts).
Definition: tx_verify.cpp:168
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:26
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition: memusage.h:122
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:72
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:38
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Compute the virtual transaction size (size, or more if sigChecks are too dense).
Definition: policy.cpp:164
static const CFeeRate MEMPOOL_FULL_FEE_INCREMENT(1000 *SATOSHI)
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or BIP...
static const unsigned int DEFAULT_MAX_MEMPOOL_SIZE
Default for -maxmempool, maximum megabytes of mempool memory usage.
Definition: policy.h:48
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:321
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
Definition: random.cpp:650
reverse_range< T > reverse_iterate(T &x)
Definition: amount.h:19
static constexpr Amount zero()
Definition: amount.h:32
CBlockIndex * maxInputBlock
Definition: txmempool.h:54
@ VALID
Fully validated, valid.
A TxId is the identifier of a transaction.
Definition: txid.h:14
Information about a mempool transaction.
Definition: txmempool.h:269
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:63
update_ancestor_state(int64_t _modifySize, Amount _modifyFee, int64_t _modifyCount, int64_t _modifySigChecks)
Definition: txmempool.cpp:58
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:45
update_descendant_state(int64_t _modifySize, Amount _modifyFee, int64_t _modifyCount, int64_t _modifySigChecks)
Definition: txmempool.cpp:40
#define LOCK(cs)
Definition: sync.h:243
ArgsManager gArgs
Definition: system.cpp:75
T GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:71
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
bool TestLockPointValidity(const CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain.
Definition: txmempool.cpp:75
static const size_t MAX_DISCONNECTED_TX_POOL_SIZE
Maximum bytes for transactions to store for processing during reorg.
Definition: txmempool.cpp:1217
constexpr uint64_t nNoLimit
Used in various places in this file to signify "no limit" for CalculateMemPoolAncestors.
Definition: txmempool.cpp:35
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.cpp:815
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:290
@ SIZELIMIT
Removed in size limiting.
@ BLOCK
Removed for block.
@ EXPIRY
Expired from mempool.
@ CONFLICT
Removed for conflict with in-block transaction.
@ REORG
Removed for reorganization.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
Definition: txmempool.h:43
MempoolAcceptResult AcceptToMemoryPool(const Config &config, 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.
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation)
Definition: validation.cpp:126
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
CMainSignals & GetMainSignals()