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