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 <clientversion.h>
9 #include <coins.h>
10 #include <config.h>
11 #include <consensus/consensus.h>
12 #include <consensus/tx_verify.h>
13 #include <consensus/validation.h>
14 #include <policy/fees.h>
15 #include <policy/policy.h>
16 #include <reverse_iterator.h>
17 #include <undo.h>
18 #include <util/moneystr.h>
19 #include <util/system.h>
20 #include <util/time.h>
21 #include <validationinterface.h>
22 #include <version.h>
23 
24 #include <algorithm>
25 #include <cmath>
26 #include <limits>
27 
29  setEntries &setAncestors,
30  CTxMemPoolEntry::Parents &staged_ancestors) const {
31  while (!staged_ancestors.empty()) {
32  const auto stage = staged_ancestors.begin()->get();
33 
34  txiter stageit = mapTx.find(stage->GetTx().GetId());
35  assert(stageit != mapTx.end());
36  setAncestors.insert(stageit);
37  staged_ancestors.erase(staged_ancestors.begin());
38 
39  const CTxMemPoolEntry::Parents &parents =
40  (*stageit)->GetMemPoolParentsConst();
41  for (const auto &parent : parents) {
42  txiter parent_it = mapTx.find(parent.get()->GetTx().GetId());
43  assert(parent_it != mapTx.end());
44 
45  // If this is a new ancestor, add it.
46  if (setAncestors.count(parent_it) == 0) {
47  staged_ancestors.insert(parent);
48  }
49  }
50  }
51 
52  return true;
53 }
54 
56  const CTxMemPoolEntryRef &entry, setEntries &setAncestors,
57  bool fSearchForParents /* = true */) const {
58  CTxMemPoolEntry::Parents staged_ancestors;
59  const CTransaction &tx = entry->GetTx();
60 
61  if (fSearchForParents) {
62  // Get parents of this transaction that are in the mempool
63  // GetMemPoolParents() is only valid for entries in the mempool, so we
64  // iterate mapTx to find parents.
65  for (const CTxIn &in : tx.vin) {
66  std::optional<txiter> piter = GetIter(in.prevout.GetTxId());
67  if (!piter) {
68  continue;
69  }
70  staged_ancestors.insert(**piter);
71  }
72  } else {
73  // If we're not searching for parents, we require this to be an entry in
74  // the mempool already.
75  staged_ancestors = entry->GetMemPoolParentsConst();
76  }
77 
78  return CalculateAncestors(setAncestors, staged_ancestors);
79 }
80 
81 void CTxMemPool::UpdateParentsOf(bool add, txiter it) {
82  // add or remove this tx as a child of each parent
83  for (const auto &parent : (*it)->GetMemPoolParentsConst()) {
84  auto parent_it = mapTx.find(parent.get()->GetTx().GetId());
85  assert(parent_it != mapTx.end());
86  UpdateChild(parent_it, it, add);
87  }
88 }
89 
91  const CTxMemPoolEntry::Children &children =
92  (*it)->GetMemPoolChildrenConst();
93  for (const auto &child : children) {
94  auto updateIt = mapTx.find(child.get()->GetTx().GetId());
95  assert(updateIt != mapTx.end());
96  UpdateParent(updateIt, it, false);
97  }
98 }
99 
101  for (txiter removeIt : entriesToRemove) {
102  // Note that UpdateParentsOf severs the child links that point to
103  // removeIt in the entries for the parents of removeIt.
104  UpdateParentsOf(false, removeIt);
105  }
106 
107  // After updating all the parent links, we can now sever the link between
108  // each transaction being removed and any mempool children (ie, update
109  // CTxMemPoolEntry::m_parents for each direct child of a transaction being
110  // removed).
111  for (txiter removeIt : entriesToRemove) {
112  UpdateChildrenForRemoval(removeIt);
113  }
114 }
115 
117  : m_check_ratio(opts.check_ratio), m_max_size_bytes{opts.max_size_bytes},
118  m_expiry{opts.expiry}, m_min_relay_feerate{opts.min_relay_feerate},
119  m_dust_relay_feerate{opts.dust_relay_feerate},
120  m_permit_bare_multisig{opts.permit_bare_multisig},
121  m_max_datacarrier_bytes{opts.max_datacarrier_bytes},
122  m_require_standard{opts.require_standard} {
123  // lock free clear
124  _clear();
125 }
126 
128 
129 bool CTxMemPool::isSpent(const COutPoint &outpoint) const {
130  LOCK(cs);
131  return mapNextTx.count(outpoint);
132 }
133 
135  return nTransactionsUpdated;
136 }
137 
140 }
141 
143  // get a guaranteed unique id (in case tests re-use the same object)
144  entry->SetEntryId(nextEntryId++);
145 
146  // Update transaction for any feeDelta created by PrioritiseTransaction
147  {
148  Amount feeDelta = Amount::zero();
149  ApplyDelta(entry->GetTx().GetId(), feeDelta);
150  entry->UpdateFeeDelta(feeDelta);
151  }
152 
153  // Add to memory pool without checking anything.
154  // Used by AcceptToMemoryPool(), which DOES do all the appropriate checks.
155  auto [newit, inserted] = mapTx.insert(entry);
156  // Sanity check: It is a programming error if insertion fails (uniqueness
157  // invariants in mapTx are violated, etc)
158  assert(inserted);
159  // Sanity check: We should always end up inserting at the end of the
160  // entry_id index
161  assert(&*mapTx.get<entry_id>().rbegin() == &*newit);
162 
163  // Update cachedInnerUsage to include contained transaction's usage.
164  // (When we update the entry for in-mempool parents, memory usage will be
165  // further updated.)
166  cachedInnerUsage += entry->DynamicMemoryUsage();
167 
168  const CTransaction &tx = entry->GetTx();
169  std::set<TxId> setParentTransactions;
170  for (const CTxIn &in : tx.vin) {
171  mapNextTx.insert(std::make_pair(&in.prevout, &tx));
172  setParentTransactions.insert(in.prevout.GetTxId());
173  }
174  // Don't bother worrying about child transactions of this one. It is
175  // guaranteed that a new transaction arriving will not have any children,
176  // because such children would be orphans.
177 
178  // Update ancestors with information about this tx
179  for (const auto &pit : GetIterSet(setParentTransactions)) {
180  UpdateParent(newit, pit, true);
181  }
182 
183  UpdateParentsOf(true, newit);
184 
186  totalTxSize += entry->GetTxSize();
187  m_total_fee += entry->GetFee();
188 }
189 
191  // We increment mempool sequence value no matter removal reason
192  // even if not directly reported below.
193  uint64_t mempool_sequence = GetAndIncrementSequence();
194 
195  const TxId &txid = (*it)->GetTx().GetId();
196 
197  if (reason != MemPoolRemovalReason::BLOCK) {
198  // Notify clients that a transaction has been removed from the mempool
199  // for any reason except being included in a block. Clients interested
200  // in transactions included in blocks can subscribe to the
201  // BlockConnected notification.
203  (*it)->GetSharedTx(), reason, mempool_sequence);
204 
205  finalizedTxs.remove(txid);
206  }
207 
208  for (const CTxIn &txin : (*it)->GetTx().vin) {
209  mapNextTx.erase(txin.prevout);
210  }
211 
212  /* add logging because unchecked */
213  RemoveUnbroadcastTx(txid, true);
214 
215  totalTxSize -= (*it)->GetTxSize();
216  m_total_fee -= (*it)->GetFee();
217  cachedInnerUsage -= (*it)->DynamicMemoryUsage();
218  cachedInnerUsage -=
219  memusage::DynamicUsage((*it)->GetMemPoolParentsConst()) +
220  memusage::DynamicUsage((*it)->GetMemPoolChildrenConst());
221  mapTx.erase(it);
223 }
224 
225 // Calculates descendants of entry that are not already in setDescendants, and
226 // adds to setDescendants. Assumes entryit is already a tx in the mempool and
227 // CTxMemPoolEntry::m_children is correct for tx and all descendants. Also
228 // assumes that if an entry is in setDescendants already, then all in-mempool
229 // descendants of it are already in setDescendants as well, so that we can save
230 // time by not iterating over those entries.
232  setEntries &setDescendants) const {
233  setEntries stage;
234  if (setDescendants.count(entryit) == 0) {
235  stage.insert(entryit);
236  }
237  // Traverse down the children of entry, only adding children that are not
238  // accounted for in setDescendants already (because those children have
239  // either already been walked, or will be walked in this iteration).
240  while (!stage.empty()) {
241  txiter it = *stage.begin();
242  setDescendants.insert(it);
243  stage.erase(stage.begin());
244 
245  const CTxMemPoolEntry::Children &children =
246  (*it)->GetMemPoolChildrenConst();
247  for (const auto &child : children) {
248  txiter childiter = mapTx.find(child.get()->GetTx().GetId());
249  assert(childiter != mapTx.end());
250 
251  if (!setDescendants.count(childiter)) {
252  stage.insert(childiter);
253  }
254  }
255  }
256 }
257 
259  MemPoolRemovalReason reason) {
260  // Remove transaction from memory pool.
262  setEntries txToRemove;
263  txiter origit = mapTx.find(origTx.GetId());
264  if (origit != mapTx.end()) {
265  txToRemove.insert(origit);
266  } else {
267  // When recursively removing but origTx isn't in the mempool be sure to
268  // remove any children that are in the pool. This can happen during
269  // chain re-orgs if origTx isn't re-accepted into the mempool for any
270  // reason.
271  auto it = mapNextTx.lower_bound(COutPoint(origTx.GetId(), 0));
272  while (it != mapNextTx.end() &&
273  it->first->GetTxId() == origTx.GetId()) {
274  txiter nextit = mapTx.find(it->second->GetId());
275  assert(nextit != mapTx.end());
276  txToRemove.insert(nextit);
277  ++it;
278  }
279  }
280 
281  setEntries setAllRemoves;
282  for (txiter it : txToRemove) {
283  CalculateDescendants(it, setAllRemoves);
284  }
285 
286  RemoveStaged(setAllRemoves, reason);
287 }
288 
290  // Remove transactions which depend on inputs of tx, recursively
292  for (const CTxIn &txin : tx.vin) {
293  auto it = mapNextTx.find(txin.prevout);
294  if (it != mapNextTx.end()) {
295  const CTransaction &txConflict = *it->second;
296  if (txConflict != tx) {
297  ClearPrioritisation(txConflict.GetId());
299  }
300  }
301  }
302 }
303 
309 
310  lastRollingFeeUpdate = GetTime();
311  blockSinceLastRollingFeeBump = true;
312 }
313 
315  const std::vector<CTransactionRef> &vtx) {
317 
318  for (const auto &tx : vtx) {
319  // If the tx has a parent, it will be in the block as well or the block
320  // is invalid. If the tx has a child, it can remain in the tree for the
321  // next block. So we can simply remove the txs from the block with no
322  // further check.
323  finalizedTxs.remove(tx->GetId());
324  }
325 }
326 
328  mapTx.clear();
329  mapNextTx.clear();
330  totalTxSize = 0;
331  m_total_fee = Amount::zero();
332  cachedInnerUsage = 0;
333  lastRollingFeeUpdate = GetTime();
334  blockSinceLastRollingFeeBump = false;
335  rollingMinimumFeeRate = 0;
337 }
338 
340  LOCK(cs);
341  _clear();
342 }
343 
344 void CTxMemPool::check(const CCoinsViewCache &active_coins_tip,
345  int64_t spendheight) const {
346  if (m_check_ratio == 0) {
347  return;
348  }
349 
350  if (GetRand(m_check_ratio) >= 1) {
351  return;
352  }
353 
355  LOCK(cs);
357  "Checking mempool with %u transactions and %u inputs\n",
358  (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
359 
360  uint64_t checkTotal = 0;
361  Amount check_total_fee{Amount::zero()};
362  uint64_t innerUsage = 0;
363 
364  CCoinsViewCache mempoolDuplicate(
365  const_cast<CCoinsViewCache *>(&active_coins_tip));
366 
367  for (const CTxMemPoolEntryRef &entry : mapTx.get<entry_id>()) {
368  checkTotal += entry->GetTxSize();
369  check_total_fee += entry->GetFee();
370  innerUsage += entry->DynamicMemoryUsage();
371  const CTransaction &tx = entry->GetTx();
372  innerUsage += memusage::DynamicUsage(entry->GetMemPoolParentsConst()) +
373  memusage::DynamicUsage(entry->GetMemPoolChildrenConst());
374 
375  CTxMemPoolEntry::Parents setParentCheck;
376  for (const CTxIn &txin : tx.vin) {
377  // Check that every mempool transaction's inputs refer to available
378  // coins, or other mempool tx's.
379  txiter parentIt = mapTx.find(txin.prevout.GetTxId());
380  if (parentIt != mapTx.end()) {
381  const CTransaction &parentTx = (*parentIt)->GetTx();
382  assert(parentTx.vout.size() > txin.prevout.GetN() &&
383  !parentTx.vout[txin.prevout.GetN()].IsNull());
384  setParentCheck.insert(*parentIt);
385  // also check that parents have a topological ordering before
386  // their children
387  assert((*parentIt)->GetEntryId() < entry->GetEntryId());
388  }
389  // We are iterating through the mempool entries sorted
390  // topologically.
391  // All parents must have been checked before their children and
392  // their coins added to the mempoolDuplicate coins cache.
393  assert(mempoolDuplicate.HaveCoin(txin.prevout));
394  // Check whether its inputs are marked in mapNextTx.
395  auto prevoutNextIt = mapNextTx.find(txin.prevout);
396  assert(prevoutNextIt != mapNextTx.end());
397  assert(prevoutNextIt->first == &txin.prevout);
398  assert(prevoutNextIt->second == &tx);
399  }
400  auto comp = [](const auto &a, const auto &b) -> bool {
401  return a.get()->GetTx().GetId() == b.get()->GetTx().GetId();
402  };
403  assert(setParentCheck.size() == entry->GetMemPoolParentsConst().size());
404  assert(std::equal(setParentCheck.begin(), setParentCheck.end(),
405  entry->GetMemPoolParentsConst().begin(), comp));
406 
407  // Verify ancestor state is correct.
408  setEntries setAncestors;
409  std::string dummy;
410 
411  const bool ok = CalculateMemPoolAncestors(entry, setAncestors);
412  assert(ok);
413 
414  // all ancestors should have entryId < this tx's entryId
415  for (const auto &ancestor : setAncestors) {
416  assert((*ancestor)->GetEntryId() < entry->GetEntryId());
417  }
418 
419  // Check children against mapNextTx
420  CTxMemPoolEntry::Children setChildrenCheck;
421  auto iter = mapNextTx.lower_bound(COutPoint(entry->GetTx().GetId(), 0));
422  for (; iter != mapNextTx.end() &&
423  iter->first->GetTxId() == entry->GetTx().GetId();
424  ++iter) {
425  txiter childIt = mapTx.find(iter->second->GetId());
426  // mapNextTx points to in-mempool transactions
427  assert(childIt != mapTx.end());
428  setChildrenCheck.insert(*childIt);
429  }
430  assert(setChildrenCheck.size() ==
431  entry->GetMemPoolChildrenConst().size());
432  assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(),
433  entry->GetMemPoolChildrenConst().begin(), comp));
434 
435  // Not used. CheckTxInputs() should always pass
436  TxValidationState dummy_state;
437  Amount txfee{Amount::zero()};
438  assert(!tx.IsCoinBase());
439  assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate,
440  spendheight, txfee));
441  for (const auto &input : tx.vin) {
442  mempoolDuplicate.SpendCoin(input.prevout);
443  }
444  AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
445  }
446 
447  for (auto &[_, nextTx] : mapNextTx) {
448  txiter it = mapTx.find(nextTx->GetId());
449  assert(it != mapTx.end());
450  assert(&(*it)->GetTx() == nextTx);
451  }
452 
453  assert(totalTxSize == checkTotal);
454  assert(m_total_fee == check_total_fee);
455  assert(innerUsage == cachedInnerUsage);
456 }
457 
459  const TxId &txidb) const {
460  LOCK(cs);
461  auto it1 = mapTx.find(txida);
462  if (it1 == mapTx.end()) {
463  return false;
464  }
465  auto it2 = mapTx.find(txidb);
466  if (it2 == mapTx.end()) {
467  return true;
468  }
469  return (*it1)->GetEntryId() < (*it2)->GetEntryId();
470 }
471 
472 void CTxMemPool::getAllTxIds(std::vector<TxId> &vtxid) const {
473  LOCK(cs);
474 
475  vtxid.clear();
476  vtxid.reserve(mapTx.size());
477 
478  for (const auto &entry : mapTx.get<entry_id>()) {
479  vtxid.push_back(entry->GetTx().GetId());
480  }
481 }
482 
483 static TxMempoolInfo
484 GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
485  return TxMempoolInfo{(*it)->GetSharedTx(), (*it)->GetTime(),
486  (*it)->GetFee(), (*it)->GetTxSize(),
487  (*it)->GetModifiedFee() - (*it)->GetFee()};
488 }
489 
490 std::vector<TxMempoolInfo> CTxMemPool::infoAll() const {
491  LOCK(cs);
492 
493  std::vector<TxMempoolInfo> ret;
494  ret.reserve(mapTx.size());
495 
496  const auto &index = mapTx.get<entry_id>();
497  for (auto it = index.begin(); it != index.end(); ++it) {
498  ret.push_back(GetInfo(mapTx.project<0>(it)));
499  }
500 
501  return ret;
502 }
503 
505  LOCK(cs);
506  indexed_transaction_set::const_iterator i = mapTx.find(txid);
507  if (i == mapTx.end()) {
508  return nullptr;
509  }
510 
511  return (*i)->GetSharedTx();
512 }
513 
514 TxMempoolInfo CTxMemPool::info(const TxId &txid) const {
515  LOCK(cs);
516  indexed_transaction_set::const_iterator i = mapTx.find(txid);
517  if (i == mapTx.end()) {
518  return TxMempoolInfo();
519  }
520 
521  return GetInfo(i);
522 }
523 
525  LOCK(cs);
526 
527  // minerPolicy uses recent blocks to figure out a reasonable fee. This
528  // may disagree with the rollingMinimumFeerate under certain scenarios
529  // where the mempool increases rapidly, or blocks are being mined which
530  // do not contain propagated transactions.
531  return std::max(m_min_relay_feerate, GetMinFee());
532 }
533 
535  const Amount nFeeDelta) {
536  {
537  LOCK(cs);
538  Amount &delta = mapDeltas[txid];
539  delta += nFeeDelta;
540  txiter it = mapTx.find(txid);
541  if (it != mapTx.end()) {
542  mapTx.modify(it, [&delta](CTxMemPoolEntryRef &e) {
543  e->UpdateFeeDelta(delta);
544  });
546  }
547  }
548  LogPrintf("PrioritiseTransaction: %s fee += %s\n", txid.ToString(),
549  FormatMoney(nFeeDelta));
550 }
551 
552 void CTxMemPool::ApplyDelta(const TxId &txid, Amount &nFeeDelta) const {
554  std::map<TxId, Amount>::const_iterator pos = mapDeltas.find(txid);
555  if (pos == mapDeltas.end()) {
556  return;
557  }
558 
559  nFeeDelta += pos->second;
560 }
561 
564  mapDeltas.erase(txid);
565 }
566 
567 const CTransaction *CTxMemPool::GetConflictTx(const COutPoint &prevout) const {
568  const auto it = mapNextTx.find(prevout);
569  return it == mapNextTx.end() ? nullptr : it->second;
570 }
571 
572 std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const TxId &txid) const {
573  auto it = mapTx.find(txid);
574  if (it != mapTx.end()) {
575  return it;
576  }
577  return std::nullopt;
578 }
579 
581 CTxMemPool::GetIterSet(const std::set<TxId> &txids) const {
583  for (const auto &txid : txids) {
584  const auto mi = GetIter(txid);
585  if (mi) {
586  ret.insert(*mi);
587  }
588  }
589  return ret;
590 }
591 
593  for (const CTxIn &in : tx.vin) {
594  if (exists(in.prevout.GetTxId())) {
595  return false;
596  }
597  }
598 
599  return true;
600 }
601 
603  const CTxMemPool &mempoolIn)
604  : CCoinsViewBacked(baseIn), mempool(mempoolIn) {}
605 
606 bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
607  // Check to see if the inputs are made available by another tx in the
608  // package. These Coins would not be available in the underlying CoinsView.
609  if (auto it = m_temp_added.find(outpoint); it != m_temp_added.end()) {
610  coin = it->second;
611  return true;
612  }
613 
614  // If an entry in the mempool exists, always return that one, as it's
615  // guaranteed to never conflict with the underlying cache, and it cannot
616  // have pruned entries (as it contains full) transactions. First checking
617  // the underlying cache risks returning a pruned entry instead.
618  CTransactionRef ptx = mempool.get(outpoint.GetTxId());
619  if (ptx) {
620  if (outpoint.GetN() < ptx->vout.size()) {
621  coin = Coin(ptx->vout[outpoint.GetN()], MEMPOOL_HEIGHT, false);
622  return true;
623  }
624  return false;
625  }
626  return base->GetCoin(outpoint, coin);
627 }
628 
630  for (uint32_t n = 0; n < tx->vout.size(); ++n) {
631  m_temp_added.emplace(COutPoint(tx->GetId(), n),
632  Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
633  }
634 }
635 
637  LOCK(cs);
638  // Estimate the overhead of mapTx to be 12 pointers + an allocation, as no
639  // exact formula for boost::multi_index_contained is implemented.
640  return memusage::MallocUsage(sizeof(CTxMemPoolEntry) +
641  12 * sizeof(void *)) *
642  mapTx.size() +
643  memusage::DynamicUsage(mapNextTx) +
644  memusage::DynamicUsage(mapDeltas) + cachedInnerUsage;
645 }
646 
647 void CTxMemPool::RemoveUnbroadcastTx(const TxId &txid, const bool unchecked) {
648  LOCK(cs);
649 
650  if (m_unbroadcast_txids.erase(txid)) {
651  LogPrint(
652  BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n",
653  txid.GetHex(),
654  (unchecked ? " before confirmation that txn was sent out" : ""));
655  }
656 }
657 
659  MemPoolRemovalReason reason) {
662 
663  // Remove txs in reverse-topological order
664  const setRevTopoEntries stageRevTopo(stage.begin(), stage.end());
665  for (txiter it : stageRevTopo) {
666  removeUnchecked(it, reason);
667  }
668 }
669 
670 int CTxMemPool::Expire(std::chrono::seconds time) {
672  indexed_transaction_set::index<entry_time>::type::iterator it =
673  mapTx.get<entry_time>().begin();
674  setEntries toremove;
675  while (it != mapTx.get<entry_time>().end() && (*it)->GetTime() < time) {
676  toremove.insert(mapTx.project<0>(it));
677  it++;
678  }
679 
680  setEntries stage;
681  for (txiter removeit : toremove) {
682  CalculateDescendants(removeit, stage);
683  }
684 
686  return stage.size();
687 }
688 
692  int expired = Expire(GetTime<std::chrono::seconds>() - m_expiry);
693  if (expired != 0) {
695  "Expired %i transactions from the memory pool\n", expired);
696  }
697 
698  std::vector<COutPoint> vNoSpendsRemaining;
699  TrimToSize(m_max_size_bytes, &vNoSpendsRemaining);
700  for (const COutPoint &removed : vNoSpendsRemaining) {
701  coins_cache.Uncache(removed);
702  }
703 }
704 
705 void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add) {
708  if (add && (*entry)->GetMemPoolChildren().insert(*child).second) {
709  cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
710  } else if (!add && (*entry)->GetMemPoolChildren().erase(*child)) {
711  cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
712  }
713 }
714 
715 void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add) {
718  if (add && (*entry)->GetMemPoolParents().insert(*parent).second) {
719  cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
720  } else if (!add && (*entry)->GetMemPoolParents().erase(*parent)) {
721  cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
722  }
723 }
724 
725 CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
726  LOCK(cs);
727  if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0) {
728  return CFeeRate(int64_t(ceill(rollingMinimumFeeRate)) * SATOSHI);
729  }
730 
731  int64_t time = GetTime();
732  if (time > lastRollingFeeUpdate + 10) {
733  double halflife = ROLLING_FEE_HALFLIFE;
734  if (DynamicMemoryUsage() < sizelimit / 4) {
735  halflife /= 4;
736  } else if (DynamicMemoryUsage() < sizelimit / 2) {
737  halflife /= 2;
738  }
739 
740  rollingMinimumFeeRate =
741  rollingMinimumFeeRate /
742  pow(2.0, (time - lastRollingFeeUpdate) / halflife);
743  lastRollingFeeUpdate = time;
744  }
745  return CFeeRate(int64_t(ceill(rollingMinimumFeeRate)) * SATOSHI);
746 }
747 
750  if ((rate.GetFeePerK() / SATOSHI) > rollingMinimumFeeRate) {
751  rollingMinimumFeeRate = rate.GetFeePerK() / SATOSHI;
752  blockSinceLastRollingFeeBump = false;
753  }
754 }
755 
756 void CTxMemPool::TrimToSize(size_t sizelimit,
757  std::vector<COutPoint> *pvNoSpendsRemaining) {
759 
760  unsigned nTxnRemoved = 0;
761  CFeeRate maxFeeRateRemoved(Amount::zero());
762  while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
763  auto it = mapTx.get<modified_feerate>().end();
764  --it;
765 
766  // We set the new mempool min fee to the feerate of the removed
767  // transaction, plus the "minimum reasonable fee rate" (ie some value
768  // under which we consider txn to have 0 fee). This way, we don't allow
769  // txn to enter mempool with feerate equal to txn which were removed
770  // with no block in between.
771  CFeeRate removed = (*it)->GetModifiedFeeRate();
772  removed += MEMPOOL_FULL_FEE_INCREMENT;
773 
774  trackPackageRemoved(removed);
775  maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
776 
777  setEntries stage;
778  CalculateDescendants(mapTx.project<0>(it), stage);
779  nTxnRemoved += stage.size();
780 
781  if (pvNoSpendsRemaining) {
782  for (const txiter &iter : stage) {
783  for (const CTxIn &txin : (*iter)->GetTx().vin) {
784  if (!exists(txin.prevout.GetTxId())) {
785  pvNoSpendsRemaining->push_back(txin.prevout);
786  }
787  }
788  }
789  }
790 
792  }
793 
794  if (maxFeeRateRemoved > CFeeRate(Amount::zero())) {
796  "Removed %u txn, rolling minimum fee bumped to %s\n",
797  nTxnRemoved, maxFeeRateRemoved.ToString());
798  }
799 }
800 
802  LOCK(cs);
803  return m_load_tried;
804 }
805 
806 void CTxMemPool::SetLoadTried(bool load_tried) {
807  LOCK(cs);
808  m_load_tried = load_tried;
809 }
static constexpr Amount SATOSHI
Definition: amount.h:143
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:606
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:597
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:602
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:629
const CTxMemPool & mempool
Definition: txmempool.h:600
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:20
uint32_t GetN() const
Definition: transaction.h:36
const TxId & GetTxId() const
Definition: transaction.h:35
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
const std::vector< CTxOut > vout
Definition: transaction.h:207
bool IsCoinBase() const
Definition: transaction.h:252
const std::vector< CTxIn > vin
Definition: transaction.h:206
const TxId GetId() const
Definition: transaction.h:240
An input of a transaction.
Definition: transaction.h:59
COutPoint prevout
Definition: transaction.h:61
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
std::set< std::reference_wrapper< const CTxMemPoolEntryRef >, CompareIteratorById > Children
Definition: mempool_entry.h:73
std::set< std::reference_wrapper< const CTxMemPoolEntryRef >, CompareIteratorById > Parents
Definition: mempool_entry.h:70
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:209
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:289
CFeeRate estimateFee() const
Definition: txmempool.cpp:524
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:592
void ClearPrioritisation(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:562
std::set< txiter, CompareIteratorById > setEntries
Definition: txmempool.h:300
void RemoveUnbroadcastTx(const TxId &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:647
bool GetLoadTried() const
Definition: txmempool.cpp:801
bool CalculateAncestors(setEntries &setAncestors, CTxMemPoolEntry::Parents &staged_ancestors) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors param@[in] staged_ancestors...
Definition: txmempool.cpp:28
void updateFeeForBlock() EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:307
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.h:440
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:296
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:748
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:258
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:100
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:212
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:756
const std::chrono::seconds m_expiry
Definition: txmempool.h:334
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:138
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:90
bool CompareTopologically(const TxId &txida, const TxId &txidb) const
Definition: txmempool.cpp:458
TxMempoolInfo info(const TxId &txid) const
Definition: txmempool.cpp:514
const int64_t m_max_size_bytes
Definition: txmempool.h:333
void getAllTxIds(std::vector< TxId > &vtxid) const
Definition: txmempool.cpp:472
std::atomic< uint32_t > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:214
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:581
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:636
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:490
void LimitSize(CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(cs
Reduce the size of the mempool by expiring and then trimming the mempool.
Definition: txmempool.cpp:689
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:715
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:190
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:670
void clear()
Definition: txmempool.cpp:339
std::set< txiter, CompareIteratorByRevEntryId > setRevTopoEntries
Definition: txmempool.h:301
bool exists(const TxId &txid) const
Definition: txmempool.h:492
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:244
CTransactionRef get(const TxId &txid) const
Definition: txmempool.cpp:504
const CFeeRate m_min_relay_feerate
Definition: txmempool.h:335
void PrioritiseTransaction(const TxId &txid, const Amount nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:534
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:299
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:541
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:705
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(CTxMemPoolEntryRef 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:361
RadixTree< CTxMemPoolEntry, MemPoolEntryRadixTreeAdapter > finalizedTxs
Definition: txmempool.h:303
CTxMemPool(const Options &opts)
Create a new CTxMemPool.
Definition: txmempool.cpp:116
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:567
bool CalculateMemPoolAncestors(const CTxMemPoolEntryRef &entry, setEntries &setAncestors, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:55
void removeForFinalizedBlock(const std::vector< CTransactionRef > &vtx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:314
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:231
void RemoveStaged(const setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:658
void UpdateParentsOf(bool add, txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update parents of it to add/remove it as a child transaction.
Definition: txmempool.cpp:81
void ApplyDelta(const TxId &txid, Amount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:552
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
Definition: txmempool.cpp:806
std::optional< txiter > GetIter(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given txid, if found.
Definition: txmempool.cpp:572
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:362
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:129
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:134
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:327
A UTXO entry.
Definition: coins.h:27
Definition: rcu.h:85
T * get()
Get allows to access the undelying pointer.
Definition: rcu.h:170
std::string ToString() const
Definition: uint256.h:80
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
#define LogPrint(category,...)
Definition: logging.h:210
#define LogPrintf(...)
Definition: logging.h:206
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:42
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
static constexpr CFeeRate MEMPOOL_FULL_FEE_INCREMENT(1000 *SATOSHI)
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or BIP...
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition: random.h:85
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
RCUPtr< T > remove(const KeyType &key)
Remove an element from the tree.
Definition: radix.h:181
A TxId is the identifier of a transaction.
Definition: txid.h:14
Information about a mempool transaction.
Definition: txmempool.h:127
Options struct containing options for constructing a CTxMemPool.
#define LOCK(cs)
Definition: sync.h:306
int64_t GetTime()
Definition: time.cpp:109
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.cpp:484
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:148
@ SIZELIMIT
Removed in size limiting.
@ BLOCK
Removed for block.
@ EXPIRY
Expired from mempool.
@ CONFLICT
Removed for conflict with in-block transaction.
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:45
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
CMainSignals & GetMainSignals()