Bitcoin Core 22.99.0
P2P Digital Currency
txmempool.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 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 <coins.h>
10#include <consensus/tx_verify.h>
12#include <policy/fees.h>
13#include <policy/policy.h>
14#include <policy/settings.h>
15#include <reverse_iterator.h>
16#include <util/moneystr.h>
17#include <util/system.h>
18#include <util/time.h>
19#include <validation.h>
20#include <validationinterface.h>
21
22#include <cmath>
23#include <optional>
24
25// Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
27{
28 update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
29 modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
30 {}
31
34
35 private:
36 int64_t modifySize;
38 int64_t modifyCount;
39};
40
42{
43 update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
44 modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
45 {}
46
49
50 private:
51 int64_t modifySize;
53 int64_t modifyCount;
55};
56
58{
59 explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
60
62
63private:
64 int64_t feeDelta;
65};
66
68{
69 explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
70
72
73private:
74 const LockPoints& lp;
75};
76
78 int64_t time, unsigned int entry_height,
79 bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
80 : tx{tx},
81 nFee{fee},
82 nTxWeight(GetTransactionWeight(*tx)),
83 nUsageSize{RecursiveDynamicUsage(tx)},
84 nTime{time},
85 entryHeight{entry_height},
86 spendsCoinbase{spends_coinbase},
87 sigOpCost{sigops_cost},
88 lockPoints{lp},
89 nSizeWithDescendants{GetTxSize()},
90 nModFeesWithDescendants{nFee},
91 nSizeWithAncestors{GetTxSize()},
92 nModFeesWithAncestors{nFee},
93 nSigOpCostWithAncestors{sigOpCost} {}
94
95void CTxMemPoolEntry::UpdateFeeDelta(int64_t newFeeDelta)
96{
97 nModFeesWithDescendants += newFeeDelta - feeDelta;
98 nModFeesWithAncestors += newFeeDelta - feeDelta;
99 feeDelta = newFeeDelta;
100}
101
103{
104 lockPoints = lp;
105}
106
108{
110}
111
112// Update the given tx for any in-mempool descendants.
113// Assumes that CTxMemPool::m_children is correct for the given tx and all
114// descendants.
115void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
116{
117 CTxMemPoolEntry::Children stageEntries, descendants;
118 stageEntries = updateIt->GetMemPoolChildrenConst();
119
120 while (!stageEntries.empty()) {
121 const CTxMemPoolEntry& descendant = *stageEntries.begin();
122 descendants.insert(descendant);
123 stageEntries.erase(descendant);
124 const CTxMemPoolEntry::Children& children = descendant.GetMemPoolChildrenConst();
125 for (const CTxMemPoolEntry& childEntry : children) {
126 cacheMap::iterator cacheIt = cachedDescendants.find(mapTx.iterator_to(childEntry));
127 if (cacheIt != cachedDescendants.end()) {
128 // We've already calculated this one, just add the entries for this set
129 // but don't traverse again.
130 for (txiter cacheEntry : cacheIt->second) {
131 descendants.insert(*cacheEntry);
132 }
133 } else if (!descendants.count(childEntry)) {
134 // Schedule for later processing
135 stageEntries.insert(childEntry);
136 }
137 }
138 }
139 // descendants now contains all in-mempool descendants of updateIt.
140 // Update and add to cached descendant map
141 int64_t modifySize = 0;
142 CAmount modifyFee = 0;
143 int64_t modifyCount = 0;
144 for (const CTxMemPoolEntry& descendant : descendants) {
145 if (!setExclude.count(descendant.GetTx().GetHash())) {
146 modifySize += descendant.GetTxSize();
147 modifyFee += descendant.GetModifiedFee();
148 modifyCount++;
149 cachedDescendants[updateIt].insert(mapTx.iterator_to(descendant));
150 // Update ancestor state for each descendant
151 mapTx.modify(mapTx.iterator_to(descendant), update_ancestor_state(updateIt->GetTxSize(), updateIt->GetModifiedFee(), 1, updateIt->GetSigOpCost()));
152 }
153 }
154 mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
155}
156
157// vHashesToUpdate is the set of transaction hashes from a disconnected block
158// which has been re-added to the mempool.
159// for each entry, look for descendants that are outside vHashesToUpdate, and
160// add fee/size information for such descendants to the parent.
161// for each such descendant, also update the ancestor state to include the parent.
162void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate)
163{
165 // For each entry in vHashesToUpdate, store the set of in-mempool, but not
166 // in-vHashesToUpdate transactions, so that we don't have to recalculate
167 // descendants when we come across a previously seen entry.
168 cacheMap mapMemPoolDescendantsToUpdate;
169
170 // Use a set for lookups into vHashesToUpdate (these entries are already
171 // accounted for in the state of their ancestors)
172 std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
173
174 // Iterate in reverse, so that whenever we are looking at a transaction
175 // we are sure that all in-mempool descendants have already been processed.
176 // This maximizes the benefit of the descendant cache and guarantees that
177 // CTxMemPool::m_children will be updated, an assumption made in
178 // UpdateForDescendants.
179 for (const uint256 &hash : reverse_iterate(vHashesToUpdate)) {
180 // calculate children from mapNextTx
181 txiter it = mapTx.find(hash);
182 if (it == mapTx.end()) {
183 continue;
184 }
185 auto iter = mapNextTx.lower_bound(COutPoint(hash, 0));
186 // First calculate the children, and update CTxMemPool::m_children to
187 // include them, and update their CTxMemPoolEntry::m_parents to include this tx.
188 // we cache the in-mempool children to avoid duplicate updates
189 {
191 for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) {
192 const uint256 &childHash = iter->second->GetHash();
193 txiter childIter = mapTx.find(childHash);
194 assert(childIter != mapTx.end());
195 // We can skip updating entries we've encountered before or that
196 // are in the block (which are already accounted for).
197 if (!visited(childIter) && !setAlreadyIncluded.count(childHash)) {
198 UpdateChild(it, childIter, true);
199 UpdateParent(childIter, it, true);
200 }
201 }
202 } // release epoch guard for UpdateForDescendants
203 UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
204 }
205}
206
208 size_t entry_count,
209 setEntries& setAncestors,
210 CTxMemPoolEntry::Parents& staged_ancestors,
211 uint64_t limitAncestorCount,
212 uint64_t limitAncestorSize,
213 uint64_t limitDescendantCount,
214 uint64_t limitDescendantSize,
215 std::string &errString) const
216{
217 size_t totalSizeWithAncestors = entry_size;
218
219 while (!staged_ancestors.empty()) {
220 const CTxMemPoolEntry& stage = staged_ancestors.begin()->get();
221 txiter stageit = mapTx.iterator_to(stage);
222
223 setAncestors.insert(stageit);
224 staged_ancestors.erase(stage);
225 totalSizeWithAncestors += stageit->GetTxSize();
226
227 if (stageit->GetSizeWithDescendants() + entry_size > limitDescendantSize) {
228 errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
229 return false;
230 } else if (stageit->GetCountWithDescendants() + entry_count > limitDescendantCount) {
231 errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
232 return false;
233 } else if (totalSizeWithAncestors > limitAncestorSize) {
234 errString = strprintf("exceeds ancestor size limit [limit: %u]", limitAncestorSize);
235 return false;
236 }
237
238 const CTxMemPoolEntry::Parents& parents = stageit->GetMemPoolParentsConst();
239 for (const CTxMemPoolEntry& parent : parents) {
240 txiter parent_it = mapTx.iterator_to(parent);
241
242 // If this is a new ancestor, add it.
243 if (setAncestors.count(parent_it) == 0) {
244 staged_ancestors.insert(parent);
245 }
246 if (staged_ancestors.size() + setAncestors.size() + entry_count > limitAncestorCount) {
247 errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
248 return false;
249 }
250 }
251 }
252
253 return true;
254}
255
257 uint64_t limitAncestorCount,
258 uint64_t limitAncestorSize,
259 uint64_t limitDescendantCount,
260 uint64_t limitDescendantSize,
261 std::string &errString) const
262{
263 CTxMemPoolEntry::Parents staged_ancestors;
264 size_t total_size = 0;
265 for (const auto& tx : package) {
266 total_size += GetVirtualTransactionSize(*tx);
267 for (const auto& input : tx->vin) {
268 std::optional<txiter> piter = GetIter(input.prevout.hash);
269 if (piter) {
270 staged_ancestors.insert(**piter);
271 if (staged_ancestors.size() + package.size() > limitAncestorCount) {
272 errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
273 return false;
274 }
275 }
276 }
277 }
278 // When multiple transactions are passed in, the ancestors and descendants of all transactions
279 // considered together must be within limits even if they are not interdependent. This may be
280 // stricter than the limits for each individual transaction.
281 setEntries setAncestors;
282 const auto ret = CalculateAncestorsAndCheckLimits(total_size, package.size(),
283 setAncestors, staged_ancestors,
284 limitAncestorCount, limitAncestorSize,
285 limitDescendantCount, limitDescendantSize, errString);
286 // It's possible to overestimate the ancestor/descendant totals.
287 if (!ret) errString.insert(0, "possibly ");
288 return ret;
289}
290
292 setEntries &setAncestors,
293 uint64_t limitAncestorCount,
294 uint64_t limitAncestorSize,
295 uint64_t limitDescendantCount,
296 uint64_t limitDescendantSize,
297 std::string &errString,
298 bool fSearchForParents /* = true */) const
299{
300 CTxMemPoolEntry::Parents staged_ancestors;
301 const CTransaction &tx = entry.GetTx();
302
303 if (fSearchForParents) {
304 // Get parents of this transaction that are in the mempool
305 // GetMemPoolParents() is only valid for entries in the mempool, so we
306 // iterate mapTx to find parents.
307 for (unsigned int i = 0; i < tx.vin.size(); i++) {
308 std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
309 if (piter) {
310 staged_ancestors.insert(**piter);
311 if (staged_ancestors.size() + 1 > limitAncestorCount) {
312 errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
313 return false;
314 }
315 }
316 }
317 } else {
318 // If we're not searching for parents, we require this to already be an
319 // entry in the mempool and use the entry's cached parents.
320 txiter it = mapTx.iterator_to(entry);
321 staged_ancestors = it->GetMemPoolParentsConst();
322 }
323
324 return CalculateAncestorsAndCheckLimits(entry.GetTxSize(), /* entry_count */ 1,
325 setAncestors, staged_ancestors,
326 limitAncestorCount, limitAncestorSize,
327 limitDescendantCount, limitDescendantSize, errString);
328}
329
330void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
331{
332 const CTxMemPoolEntry::Parents& parents = it->GetMemPoolParentsConst();
333 // add or remove this tx as a child of each parent
334 for (const CTxMemPoolEntry& parent : parents) {
335 UpdateChild(mapTx.iterator_to(parent), it, add);
336 }
337 const int64_t updateCount = (add ? 1 : -1);
338 const int64_t updateSize = updateCount * it->GetTxSize();
339 const CAmount updateFee = updateCount * it->GetModifiedFee();
340 for (txiter ancestorIt : setAncestors) {
341 mapTx.modify(ancestorIt, update_descendant_state(updateSize, updateFee, updateCount));
342 }
343}
344
346{
347 int64_t updateCount = setAncestors.size();
348 int64_t updateSize = 0;
349 CAmount updateFee = 0;
350 int64_t updateSigOpsCost = 0;
351 for (txiter ancestorIt : setAncestors) {
352 updateSize += ancestorIt->GetTxSize();
353 updateFee += ancestorIt->GetModifiedFee();
354 updateSigOpsCost += ancestorIt->GetSigOpCost();
355 }
356 mapTx.modify(it, update_ancestor_state(updateSize, updateFee, updateCount, updateSigOpsCost));
357}
358
360{
361 const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
362 for (const CTxMemPoolEntry& updateIt : children) {
363 UpdateParent(mapTx.iterator_to(updateIt), it, false);
364 }
365}
366
367void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
368{
369 // For each entry, walk back all ancestors and decrement size associated with this
370 // transaction
371 const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
372 if (updateDescendants) {
373 // updateDescendants should be true whenever we're not recursively
374 // removing a tx and all its descendants, eg when a transaction is
375 // confirmed in a block.
376 // Here we only update statistics and not data in CTxMemPool::Parents
377 // and CTxMemPoolEntry::Children (which we need to preserve until we're
378 // finished with all operations that need to traverse the mempool).
379 for (txiter removeIt : entriesToRemove) {
380 setEntries setDescendants;
381 CalculateDescendants(removeIt, setDescendants);
382 setDescendants.erase(removeIt); // don't update state for self
383 int64_t modifySize = -((int64_t)removeIt->GetTxSize());
384 CAmount modifyFee = -removeIt->GetModifiedFee();
385 int modifySigOps = -removeIt->GetSigOpCost();
386 for (txiter dit : setDescendants) {
387 mapTx.modify(dit, update_ancestor_state(modifySize, modifyFee, -1, modifySigOps));
388 }
389 }
390 }
391 for (txiter removeIt : entriesToRemove) {
392 setEntries setAncestors;
393 const CTxMemPoolEntry &entry = *removeIt;
394 std::string dummy;
395 // Since this is a tx that is already in the mempool, we can call CMPA
396 // with fSearchForParents = false. If the mempool is in a consistent
397 // state, then using true or false should both be correct, though false
398 // should be a bit faster.
399 // However, if we happen to be in the middle of processing a reorg, then
400 // the mempool can be in an inconsistent state. In this case, the set
401 // of ancestors reachable via GetMemPoolParents()/GetMemPoolChildren()
402 // will be the same as the set of ancestors whose packages include this
403 // transaction, because when we add a new transaction to the mempool in
404 // addUnchecked(), we assume it has no children, and in the case of a
405 // reorg where that assumption is false, the in-mempool children aren't
406 // linked to the in-block tx's until UpdateTransactionsFromBlock() is
407 // called.
408 // So if we're being called during a reorg, ie before
409 // UpdateTransactionsFromBlock() has been called, then
410 // GetMemPoolParents()/GetMemPoolChildren() will differ from the set of
411 // mempool parents we'd calculate by searching, and it's important that
412 // we use the cached notion of ancestor transactions as the set of
413 // things to update for removal.
414 CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
415 // Note that UpdateAncestorsOf severs the child links that point to
416 // removeIt in the entries for the parents of removeIt.
417 UpdateAncestorsOf(false, removeIt, setAncestors);
418 }
419 // After updating all the ancestor sizes, we can now sever the link between each
420 // transaction being removed and any mempool children (ie, update CTxMemPoolEntry::m_parents
421 // for each direct child of a transaction being removed).
422 for (txiter removeIt : entriesToRemove) {
423 UpdateChildrenForRemoval(removeIt);
424 }
425}
426
427void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
428{
429 nSizeWithDescendants += modifySize;
430 assert(int64_t(nSizeWithDescendants) > 0);
431 nModFeesWithDescendants += modifyFee;
432 nCountWithDescendants += modifyCount;
433 assert(int64_t(nCountWithDescendants) > 0);
434}
435
436void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
437{
438 nSizeWithAncestors += modifySize;
439 assert(int64_t(nSizeWithAncestors) > 0);
440 nModFeesWithAncestors += modifyFee;
441 nCountWithAncestors += modifyCount;
442 assert(int64_t(nCountWithAncestors) > 0);
443 nSigOpCostWithAncestors += modifySigOps;
445}
446
448 : m_check_ratio(check_ratio), minerPolicyEstimator(estimator)
449{
450 _clear(); //lock free clear
451}
452
453bool CTxMemPool::isSpent(const COutPoint& outpoint) const
454{
455 LOCK(cs);
456 return mapNextTx.count(outpoint);
457}
458
460{
462}
463
465{
467}
468
469void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate)
470{
471 // Add to memory pool without checking anything.
472 // Used by AcceptToMemoryPool(), which DOES do
473 // all the appropriate checks.
474 indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
475
476 // Update transaction for any feeDelta created by PrioritiseTransaction
477 // TODO: refactor so that the fee delta is calculated before inserting
478 // into mapTx.
479 CAmount delta{0};
480 ApplyDelta(entry.GetTx().GetHash(), delta);
481 if (delta) {
482 mapTx.modify(newit, update_fee_delta(delta));
483 }
484
485 // Update cachedInnerUsage to include contained transaction's usage.
486 // (When we update the entry for in-mempool parents, memory usage will be
487 // further updated.)
488 cachedInnerUsage += entry.DynamicMemoryUsage();
489
490 const CTransaction& tx = newit->GetTx();
491 std::set<uint256> setParentTransactions;
492 for (unsigned int i = 0; i < tx.vin.size(); i++) {
493 mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, &tx));
494 setParentTransactions.insert(tx.vin[i].prevout.hash);
495 }
496 // Don't bother worrying about child transactions of this one.
497 // Normal case of a new transaction arriving is that there can't be any
498 // children, because such children would be orphans.
499 // An exception to that is if a transaction enters that used to be in a block.
500 // In that case, our disconnect block logic will call UpdateTransactionsFromBlock
501 // to clean up the mess we're leaving here.
502
503 // Update ancestors with information about this tx
504 for (const auto& pit : GetIterSet(setParentTransactions)) {
505 UpdateParent(newit, pit, true);
506 }
507 UpdateAncestorsOf(true, newit, setAncestors);
508 UpdateEntryForAncestors(newit, setAncestors);
509
511 totalTxSize += entry.GetTxSize();
512 m_total_fee += entry.GetFee();
514 minerPolicyEstimator->processTransaction(entry, validFeeEstimate);
515 }
516
517 vTxHashes.emplace_back(tx.GetWitnessHash(), newit);
518 newit->vTxHashesIdx = vTxHashes.size() - 1;
519}
520
522{
523 // We increment mempool sequence value no matter removal reason
524 // even if not directly reported below.
525 uint64_t mempool_sequence = GetAndIncrementSequence();
526
527 if (reason != MemPoolRemovalReason::BLOCK) {
528 // Notify clients that a transaction has been removed from the mempool
529 // for any reason except being included in a block. Clients interested
530 // in transactions included in blocks can subscribe to the BlockConnected
531 // notification.
532 GetMainSignals().TransactionRemovedFromMempool(it->GetSharedTx(), reason, mempool_sequence);
533 }
534
535 const uint256 hash = it->GetTx().GetHash();
536 for (const CTxIn& txin : it->GetTx().vin)
537 mapNextTx.erase(txin.prevout);
538
539 RemoveUnbroadcastTx(hash, true /* add logging because unchecked */ );
540
541 if (vTxHashes.size() > 1) {
542 vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
543 vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
544 vTxHashes.pop_back();
545 if (vTxHashes.size() * 2 < vTxHashes.capacity())
546 vTxHashes.shrink_to_fit();
547 } else
548 vTxHashes.clear();
549
550 totalTxSize -= it->GetTxSize();
551 m_total_fee -= it->GetFee();
552 cachedInnerUsage -= it->DynamicMemoryUsage();
553 cachedInnerUsage -= memusage::DynamicUsage(it->GetMemPoolParentsConst()) + memusage::DynamicUsage(it->GetMemPoolChildrenConst());
554 mapTx.erase(it);
557}
558
559// Calculates descendants of entry that are not already in setDescendants, and adds to
560// setDescendants. Assumes entryit is already a tx in the mempool and CTxMemPoolEntry::m_children
561// is correct for tx and all descendants.
562// Also assumes that if an entry is in setDescendants already, then all
563// in-mempool descendants of it are already in setDescendants as well, so that we
564// can save time by not iterating over those entries.
565void CTxMemPool::CalculateDescendants(txiter entryit, setEntries& setDescendants) const
566{
567 setEntries stage;
568 if (setDescendants.count(entryit) == 0) {
569 stage.insert(entryit);
570 }
571 // Traverse down the children of entry, only adding children that are not
572 // accounted for in setDescendants already (because those children have either
573 // already been walked, or will be walked in this iteration).
574 while (!stage.empty()) {
575 txiter it = *stage.begin();
576 setDescendants.insert(it);
577 stage.erase(it);
578
579 const CTxMemPoolEntry::Children& children = it->GetMemPoolChildrenConst();
580 for (const CTxMemPoolEntry& child : children) {
581 txiter childiter = mapTx.iterator_to(child);
582 if (!setDescendants.count(childiter)) {
583 stage.insert(childiter);
584 }
585 }
586 }
587}
588
590{
591 // Remove transaction from memory pool
593 setEntries txToRemove;
594 txiter origit = mapTx.find(origTx.GetHash());
595 if (origit != mapTx.end()) {
596 txToRemove.insert(origit);
597 } else {
598 // When recursively removing but origTx isn't in the mempool
599 // be sure to remove any children that are in the pool. This can
600 // happen during chain re-orgs if origTx isn't re-accepted into
601 // the mempool for any reason.
602 for (unsigned int i = 0; i < origTx.vout.size(); i++) {
603 auto it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
604 if (it == mapNextTx.end())
605 continue;
606 txiter nextit = mapTx.find(it->second->GetHash());
607 assert(nextit != mapTx.end());
608 txToRemove.insert(nextit);
609 }
610 }
611 setEntries setAllRemoves;
612 for (txiter it : txToRemove) {
613 CalculateDescendants(it, setAllRemoves);
614 }
615
616 RemoveStaged(setAllRemoves, false, reason);
617}
618
619void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
620{
621 // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
623 setEntries txToRemove;
624 for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
625 const CTransaction& tx = it->GetTx();
626 LockPoints lp = it->GetLockPoints();
627 bool validLP = TestLockPointValidity(active_chainstate.m_chain, &lp);
628 CCoinsViewMemPool view_mempool(&active_chainstate.CoinsTip(), *this);
629 if (!CheckFinalTx(active_chainstate.m_chain.Tip(), tx, flags)
630 || !CheckSequenceLocks(active_chainstate.m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
631 // Note if CheckSequenceLocks fails the LockPoints may still be invalid
632 // So it's critical that we remove the tx and not depend on the LockPoints.
633 txToRemove.insert(it);
634 } else if (it->GetSpendsCoinbase()) {
635 for (const CTxIn& txin : tx.vin) {
636 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
637 if (it2 != mapTx.end())
638 continue;
639 const Coin &coin = active_chainstate.CoinsTip().AccessCoin(txin.prevout);
640 if (m_check_ratio != 0) assert(!coin.IsSpent());
641 unsigned int nMemPoolHeight = active_chainstate.m_chain.Tip()->nHeight + 1;
642 if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
643 txToRemove.insert(it);
644 break;
645 }
646 }
647 }
648 if (!validLP) {
649 mapTx.modify(it, update_lock_points(lp));
650 }
651 }
652 setEntries setAllRemoves;
653 for (txiter it : txToRemove) {
654 CalculateDescendants(it, setAllRemoves);
655 }
656 RemoveStaged(setAllRemoves, false, MemPoolRemovalReason::REORG);
657}
658
660{
661 // Remove transactions which depend on inputs of tx, recursively
663 for (const CTxIn &txin : tx.vin) {
664 auto it = mapNextTx.find(txin.prevout);
665 if (it != mapNextTx.end()) {
666 const CTransaction &txConflict = *it->second;
667 if (txConflict != tx)
668 {
669 ClearPrioritisation(txConflict.GetHash());
671 }
672 }
673 }
674}
675
679void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight)
680{
682 std::vector<const CTxMemPoolEntry*> entries;
683 for (const auto& tx : vtx)
684 {
685 uint256 hash = tx->GetHash();
686
687 indexed_transaction_set::iterator i = mapTx.find(hash);
688 if (i != mapTx.end())
689 entries.push_back(&*i);
690 }
691 // Before the txs in the new block have been removed from the mempool, update policy estimates
692 if (minerPolicyEstimator) {minerPolicyEstimator->processBlock(nBlockHeight, entries);}
693 for (const auto& tx : vtx)
694 {
695 txiter it = mapTx.find(tx->GetHash());
696 if (it != mapTx.end()) {
697 setEntries stage;
698 stage.insert(it);
700 }
701 removeConflicts(*tx);
703 }
704 lastRollingFeeUpdate = GetTime();
705 blockSinceLastRollingFeeBump = true;
706}
707
709{
710 mapTx.clear();
711 mapNextTx.clear();
712 totalTxSize = 0;
713 m_total_fee = 0;
714 cachedInnerUsage = 0;
715 lastRollingFeeUpdate = GetTime();
716 blockSinceLastRollingFeeBump = false;
717 rollingMinimumFeeRate = 0;
719}
720
722{
723 LOCK(cs);
724 _clear();
725}
726
727void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const
728{
729 if (m_check_ratio == 0) return;
730
731 if (GetRand(m_check_ratio) >= 1) return;
732
734 LOCK(cs);
735 LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
736
737 uint64_t checkTotal = 0;
738 CAmount check_total_fee{0};
739 uint64_t innerUsage = 0;
740 uint64_t prev_ancestor_count{0};
741
742 CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip));
743
744 for (const auto& it : GetSortedDepthAndScore()) {
745 checkTotal += it->GetTxSize();
746 check_total_fee += it->GetFee();
747 innerUsage += it->DynamicMemoryUsage();
748 const CTransaction& tx = it->GetTx();
749 innerUsage += memusage::DynamicUsage(it->GetMemPoolParentsConst()) + memusage::DynamicUsage(it->GetMemPoolChildrenConst());
750 CTxMemPoolEntry::Parents setParentCheck;
751 for (const CTxIn &txin : tx.vin) {
752 // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
753 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
754 if (it2 != mapTx.end()) {
755 const CTransaction& tx2 = it2->GetTx();
756 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
757 setParentCheck.insert(*it2);
758 }
759 // We are iterating through the mempool entries sorted in order by ancestor count.
760 // All parents must have been checked before their children and their coins added to
761 // the mempoolDuplicate coins cache.
762 assert(mempoolDuplicate.HaveCoin(txin.prevout));
763 // Check whether its inputs are marked in mapNextTx.
764 auto it3 = mapNextTx.find(txin.prevout);
765 assert(it3 != mapNextTx.end());
766 assert(it3->first == &txin.prevout);
767 assert(it3->second == &tx);
768 }
769 auto comp = [](const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) -> bool {
770 return a.GetTx().GetHash() == b.GetTx().GetHash();
771 };
772 assert(setParentCheck.size() == it->GetMemPoolParentsConst().size());
773 assert(std::equal(setParentCheck.begin(), setParentCheck.end(), it->GetMemPoolParentsConst().begin(), comp));
774 // Verify ancestor state is correct.
775 setEntries setAncestors;
776 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
777 std::string dummy;
778 CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
779 uint64_t nCountCheck = setAncestors.size() + 1;
780 uint64_t nSizeCheck = it->GetTxSize();
781 CAmount nFeesCheck = it->GetModifiedFee();
782 int64_t nSigOpCheck = it->GetSigOpCost();
783
784 for (txiter ancestorIt : setAncestors) {
785 nSizeCheck += ancestorIt->GetTxSize();
786 nFeesCheck += ancestorIt->GetModifiedFee();
787 nSigOpCheck += ancestorIt->GetSigOpCost();
788 }
789
790 assert(it->GetCountWithAncestors() == nCountCheck);
791 assert(it->GetSizeWithAncestors() == nSizeCheck);
792 assert(it->GetSigOpCostWithAncestors() == nSigOpCheck);
793 assert(it->GetModFeesWithAncestors() == nFeesCheck);
794 // Sanity check: we are walking in ascending ancestor count order.
795 assert(prev_ancestor_count <= it->GetCountWithAncestors());
796 prev_ancestor_count = it->GetCountWithAncestors();
797
798 // Check children against mapNextTx
799 CTxMemPoolEntry::Children setChildrenCheck;
800 auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
801 uint64_t child_sizes = 0;
802 for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
803 txiter childit = mapTx.find(iter->second->GetHash());
804 assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions
805 if (setChildrenCheck.insert(*childit).second) {
806 child_sizes += childit->GetTxSize();
807 }
808 }
809 assert(setChildrenCheck.size() == it->GetMemPoolChildrenConst().size());
810 assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(), it->GetMemPoolChildrenConst().begin(), comp));
811 // Also check to make sure size is greater than sum with immediate children.
812 // just a sanity check, not definitive that this calc is correct...
813 assert(it->GetSizeWithDescendants() >= child_sizes + it->GetTxSize());
814
815 TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass
816 CAmount txfee = 0;
817 assert(!tx.IsCoinBase());
818 assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee));
819 for (const auto& input: tx.vin) mempoolDuplicate.SpendCoin(input.prevout);
820 AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
821 }
822 for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
823 uint256 hash = it->second->GetHash();
824 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
825 const CTransaction& tx = it2->GetTx();
826 assert(it2 != mapTx.end());
827 assert(&tx == it->second);
828 }
829
830 assert(totalTxSize == checkTotal);
831 assert(m_total_fee == check_total_fee);
832 assert(innerUsage == cachedInnerUsage);
833}
834
835bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid)
836{
837 LOCK(cs);
838 indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha);
839 if (i == mapTx.end()) return false;
840 indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb);
841 if (j == mapTx.end()) return true;
842 uint64_t counta = i->GetCountWithAncestors();
843 uint64_t countb = j->GetCountWithAncestors();
844 if (counta == countb) {
845 return CompareTxMemPoolEntryByScore()(*i, *j);
846 }
847 return counta < countb;
848}
849
850namespace {
851class DepthAndScoreComparator
852{
853public:
854 bool operator()(const CTxMemPool::indexed_transaction_set::const_iterator& a, const CTxMemPool::indexed_transaction_set::const_iterator& b)
855 {
856 uint64_t counta = a->GetCountWithAncestors();
857 uint64_t countb = b->GetCountWithAncestors();
858 if (counta == countb) {
859 return CompareTxMemPoolEntryByScore()(*a, *b);
860 }
861 return counta < countb;
862 }
863};
864} // namespace
865
866std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedDepthAndScore() const
867{
868 std::vector<indexed_transaction_set::const_iterator> iters;
870
871 iters.reserve(mapTx.size());
872
873 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) {
874 iters.push_back(mi);
875 }
876 std::sort(iters.begin(), iters.end(), DepthAndScoreComparator());
877 return iters;
878}
879
880void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) const
881{
882 LOCK(cs);
883 auto iters = GetSortedDepthAndScore();
884
885 vtxid.clear();
886 vtxid.reserve(mapTx.size());
887
888 for (auto it : iters) {
889 vtxid.push_back(it->GetTx().GetHash());
890 }
891}
892
893static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
894 return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
895}
896
897std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
898{
899 LOCK(cs);
900 auto iters = GetSortedDepthAndScore();
901
902 std::vector<TxMempoolInfo> ret;
903 ret.reserve(mapTx.size());
904 for (auto it : iters) {
905 ret.push_back(GetInfo(it));
906 }
907
908 return ret;
909}
910
912{
913 LOCK(cs);
914 indexed_transaction_set::const_iterator i = mapTx.find(hash);
915 if (i == mapTx.end())
916 return nullptr;
917 return i->GetSharedTx();
918}
919
921{
922 LOCK(cs);
923 indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash()));
924 if (i == mapTx.end())
925 return TxMempoolInfo();
926 return GetInfo(i);
927}
928
929void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta)
930{
931 {
932 LOCK(cs);
933 CAmount &delta = mapDeltas[hash];
934 delta += nFeeDelta;
935 txiter it = mapTx.find(hash);
936 if (it != mapTx.end()) {
937 mapTx.modify(it, update_fee_delta(delta));
938 // Now update all ancestors' modified fees with descendants
939 setEntries setAncestors;
940 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
941 std::string dummy;
942 CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
943 for (txiter ancestorIt : setAncestors) {
944 mapTx.modify(ancestorIt, update_descendant_state(0, nFeeDelta, 0));
945 }
946 // Now update all descendants' modified fees with ancestors
947 setEntries setDescendants;
948 CalculateDescendants(it, setDescendants);
949 setDescendants.erase(it);
950 for (txiter descendantIt : setDescendants) {
951 mapTx.modify(descendantIt, update_ancestor_state(0, nFeeDelta, 0, 0));
952 }
954 }
955 }
956 LogPrintf("PrioritiseTransaction: %s fee += %s\n", hash.ToString(), FormatMoney(nFeeDelta));
957}
958
959void CTxMemPool::ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const
960{
962 std::map<uint256, CAmount>::const_iterator pos = mapDeltas.find(hash);
963 if (pos == mapDeltas.end())
964 return;
965 const CAmount &delta = pos->second;
966 nFeeDelta += delta;
967}
968
970{
972 mapDeltas.erase(hash);
973}
974
976{
977 const auto it = mapNextTx.find(prevout);
978 return it == mapNextTx.end() ? nullptr : it->second;
979}
980
981std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const uint256& txid) const
982{
983 auto it = mapTx.find(txid);
984 if (it != mapTx.end()) return it;
985 return std::nullopt;
986}
987
988CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<uint256>& hashes) const
989{
991 for (const auto& h : hashes) {
992 const auto mi = GetIter(h);
993 if (mi) ret.insert(*mi);
994 }
995 return ret;
996}
997
999{
1000 for (unsigned int i = 0; i < tx.vin.size(); i++)
1001 if (exists(GenTxid::Txid(tx.vin[i].prevout.hash)))
1002 return false;
1003 return true;
1004}
1005
1006CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
1007
1008bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
1009 // Check to see if the inputs are made available by another tx in the package.
1010 // These Coins would not be available in the underlying CoinsView.
1011 if (auto it = m_temp_added.find(outpoint); it != m_temp_added.end()) {
1012 coin = it->second;
1013 return true;
1014 }
1015
1016 // If an entry in the mempool exists, always return that one, as it's guaranteed to never
1017 // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
1018 // transactions. First checking the underlying cache risks returning a pruned entry instead.
1019 CTransactionRef ptx = mempool.get(outpoint.hash);
1020 if (ptx) {
1021 if (outpoint.n < ptx->vout.size()) {
1022 coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
1023 return true;
1024 } else {
1025 return false;
1026 }
1027 }
1028 return base->GetCoin(outpoint, coin);
1029}
1030
1032{
1033 for (unsigned int n = 0; n < tx->vout.size(); ++n) {
1034 m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
1035 }
1036}
1037
1039 LOCK(cs);
1040 // Estimate the overhead of mapTx to be 15 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
1041 return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
1042}
1043
1044void CTxMemPool::RemoveUnbroadcastTx(const uint256& txid, const bool unchecked) {
1045 LOCK(cs);
1046
1047 if (m_unbroadcast_txids.erase(txid))
1048 {
1049 LogPrint(BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n", txid.GetHex(), (unchecked ? " before confirmation that txn was sent out" : ""));
1050 }
1051}
1052
1053void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) {
1055 UpdateForRemoveFromMempool(stage, updateDescendants);
1056 for (txiter it : stage) {
1057 removeUnchecked(it, reason);
1058 }
1059}
1060
1061int CTxMemPool::Expire(std::chrono::seconds time)
1062{
1064 indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin();
1065 setEntries toremove;
1066 while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) {
1067 toremove.insert(mapTx.project<0>(it));
1068 it++;
1069 }
1070 setEntries stage;
1071 for (txiter removeit : toremove) {
1072 CalculateDescendants(removeit, stage);
1073 }
1075 return stage.size();
1076}
1077
1078void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate)
1079{
1080 setEntries setAncestors;
1081 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
1082 std::string dummy;
1083 CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
1084 return addUnchecked(entry, setAncestors, validFeeEstimate);
1085}
1086
1087void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
1088{
1091 if (add && entry->GetMemPoolChildren().insert(*child).second) {
1092 cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
1093 } else if (!add && entry->GetMemPoolChildren().erase(*child)) {
1094 cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
1095 }
1096}
1097
1098void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add)
1099{
1102 if (add && entry->GetMemPoolParents().insert(*parent).second) {
1103 cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
1104 } else if (!add && entry->GetMemPoolParents().erase(*parent)) {
1105 cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
1106 }
1107}
1108
1109CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
1110 LOCK(cs);
1111 if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
1112 return CFeeRate(llround(rollingMinimumFeeRate));
1113
1114 int64_t time = GetTime();
1115 if (time > lastRollingFeeUpdate + 10) {
1116 double halflife = ROLLING_FEE_HALFLIFE;
1117 if (DynamicMemoryUsage() < sizelimit / 4)
1118 halflife /= 4;
1119 else if (DynamicMemoryUsage() < sizelimit / 2)
1120 halflife /= 2;
1121
1122 rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
1123 lastRollingFeeUpdate = time;
1124
1125 if (rollingMinimumFeeRate < (double)incrementalRelayFee.GetFeePerK() / 2) {
1126 rollingMinimumFeeRate = 0;
1127 return CFeeRate(0);
1128 }
1129 }
1130 return std::max(CFeeRate(llround(rollingMinimumFeeRate)), incrementalRelayFee);
1131}
1132
1135 if (rate.GetFeePerK() > rollingMinimumFeeRate) {
1136 rollingMinimumFeeRate = rate.GetFeePerK();
1137 blockSinceLastRollingFeeBump = false;
1138 }
1139}
1140
1141void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining) {
1143
1144 unsigned nTxnRemoved = 0;
1145 CFeeRate maxFeeRateRemoved(0);
1146 while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
1147 indexed_transaction_set::index<descendant_score>::type::iterator it = mapTx.get<descendant_score>().begin();
1148
1149 // We set the new mempool min fee to the feerate of the removed set, plus the
1150 // "minimum reasonable fee rate" (ie some value under which we consider txn
1151 // to have 0 fee). This way, we don't allow txn to enter mempool with feerate
1152 // equal to txn which were removed with no block in between.
1153 CFeeRate removed(it->GetModFeesWithDescendants(), it->GetSizeWithDescendants());
1154 removed += incrementalRelayFee;
1155 trackPackageRemoved(removed);
1156 maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
1157
1158 setEntries stage;
1159 CalculateDescendants(mapTx.project<0>(it), stage);
1160 nTxnRemoved += stage.size();
1161
1162 std::vector<CTransaction> txn;
1163 if (pvNoSpendsRemaining) {
1164 txn.reserve(stage.size());
1165 for (txiter iter : stage)
1166 txn.push_back(iter->GetTx());
1167 }
1169 if (pvNoSpendsRemaining) {
1170 for (const CTransaction& tx : txn) {
1171 for (const CTxIn& txin : tx.vin) {
1172 if (exists(GenTxid::Txid(txin.prevout.hash))) continue;
1173 pvNoSpendsRemaining->push_back(txin.prevout);
1174 }
1175 }
1176 }
1177 }
1178
1179 if (maxFeeRateRemoved > CFeeRate(0)) {
1180 LogPrint(BCLog::MEMPOOL, "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString());
1181 }
1182}
1183
1185 // find parent with highest descendant count
1186 std::vector<txiter> candidates;
1187 setEntries counted;
1188 candidates.push_back(entry);
1189 uint64_t maximum = 0;
1190 while (candidates.size()) {
1191 txiter candidate = candidates.back();
1192 candidates.pop_back();
1193 if (!counted.insert(candidate).second) continue;
1194 const CTxMemPoolEntry::Parents& parents = candidate->GetMemPoolParentsConst();
1195 if (parents.size() == 0) {
1196 maximum = std::max(maximum, candidate->GetCountWithDescendants());
1197 } else {
1198 for (const CTxMemPoolEntry& i : parents) {
1199 candidates.push_back(mapTx.iterator_to(i));
1200 }
1201 }
1202 }
1203 return maximum;
1204}
1205
1206void CTxMemPool::GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* const ancestorsize, CAmount* const ancestorfees) const {
1207 LOCK(cs);
1208 auto it = mapTx.find(txid);
1209 ancestors = descendants = 0;
1210 if (it != mapTx.end()) {
1211 ancestors = it->GetCountWithAncestors();
1212 if (ancestorsize) *ancestorsize = it->GetSizeWithAncestors();
1213 if (ancestorfees) *ancestorfees = it->GetModFeesWithAncestors();
1214 descendants = CalculateDescendantMaximum(it);
1215 }
1216}
1217
1219{
1220 LOCK(cs);
1221 return m_is_loaded;
1222}
1223
1225{
1226 LOCK(cs);
1227 m_is_loaded = loaded;
1228}
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int flags
Definition: bitcoin-tx.cpp:525
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:158
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:132
bool removeTx(uint256 hash, bool inBlock)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:493
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:538
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry * > &entries)
Process all the transactions that have been included in a block.
Definition: fees.cpp:604
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:421
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:544
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:638
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:620
CCoinsView backed by another CCoinsView.
Definition: coins.h:195
CCoinsView * base
Definition: coins.h:197
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:214
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:137
Abstract view on the open txout dataset.
Definition: coins.h:158
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:12
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:852
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:1008
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:857
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:1006
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:1031
const CTxMemPool & mempool
Definition: txmempool.h:859
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
std::string ToString(const FeeEstimateMode &fee_estimate_mode=FeeEstimateMode::BTC_KVB) const
Definition: feerate.cpp:39
CAmount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:57
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:27
uint32_t n
Definition: transaction.h:30
uint256 hash
Definition: transaction.h:29
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
const uint256 & GetWitnessHash() const
Definition: transaction.h:303
const uint256 & GetHash() const
Definition: transaction.h:302
const std::vector< CTxOut > vout
Definition: transaction.h:271
bool IsCoinBase() const
Definition: transaction.h:315
const std::vector< CTxIn > vin
Definition: transaction.h:270
An input of a transaction.
Definition: transaction.h:66
COutPoint prevout
Definition: transaction.h:68
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:80
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
Definition: txmempool.cpp:77
const int64_t sigOpCost
Total sigop cost.
Definition: txmempool.h:97
void UpdateFeeDelta(int64_t feeDelta)
Definition: txmempool.cpp:95
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
Definition: txmempool.h:92
int64_t nSigOpCostWithAncestors
Definition: txmempool.h:112
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Definition: txmempool.h:98
const CTransaction & GetTx() const
Definition: txmempool.h:120
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
Definition: txmempool.cpp:427
void UpdateLockPoints(const LockPoints &lp)
Definition: txmempool.cpp:102
CAmount nModFeesWithAncestors
Definition: txmempool.h:111
const Children & GetMemPoolChildrenConst() const
Definition: txmempool.h:154
uint64_t nCountWithDescendants
number of descendant transactions
Definition: txmempool.h:104
size_t GetTxSize() const
Definition: txmempool.cpp:107
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
Definition: txmempool.cpp:436
uint64_t nSizeWithDescendants
... and size
Definition: txmempool.h:105
size_t DynamicMemoryUsage() const
Definition: txmempool.h:129
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
Definition: txmempool.h:85
CAmount nModFeesWithDescendants
... and total fees (all including us)
Definition: txmempool.h:106
uint64_t nCountWithAncestors
Definition: txmempool.h:109
const CAmount & GetFee() const
Definition: txmempool.h:122
LockPoints lockPoints
Track the height and time at which tx was final.
Definition: txmempool.h:99
uint64_t nSizeWithAncestors
Definition: txmempool.h:110
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: txmempool.h:84
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:424
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:659
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:427
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:1044
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:929
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:998
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:345
bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs
visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently cre...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:511
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:969
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1133
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:1109
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:589
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) 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:582
const int m_check_ratio
Value n means that 1 times in n we check.
Definition: txmempool.h:426
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:1141
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
Definition: txmempool.cpp:162
return !it visited * it
Definition: txmempool.h:834
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:464
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:359
void removeForReorg(CChainState &active_chainstate, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs
Definition: txmempool.cpp:619
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:981
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:911
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1038
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:897
void GetTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr) const
Calculate the ancestor and descendant count for the given transaction.
Definition: txmempool.cpp:1206
void SetIsLoaded(bool loaded)
Sets the current loaded state.
Definition: txmempool.cpp:1224
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1098
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:521
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:1061
void clear()
Definition: txmempool.cpp:721
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:735
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update ancestors of hash to add/remove it as a descendant transaction.
Definition: txmempool.cpp:330
CBlockPolicyEstimator *const minerPolicyEstimator
Definition: txmempool.h:428
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:256
TxMempoolInfo info(const GenTxid &gtxid) const
Definition: txmempool.cpp:920
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:959
CTxMemPool(CBlockPolicyEstimator *estimator=nullptr, int check_ratio=0)
Create a new CTxMemPool.
Definition: txmempool.cpp:447
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:291
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:450
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:517
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:866
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1053
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
Definition: txmempool.cpp:679
void queryHashes(std::vector< uint256 > &vtxid) const
Definition: txmempool.cpp:880
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:514
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:772
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1087
bool IsLoaded() const
Definition: txmempool.cpp:1218
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:725
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:521
bool m_epoch
Definition: txmempool.h:827
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
Definition: txmempool.cpp:115
setEntries GetIterSet(const std::set< uint256 > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
Definition: txmempool.cpp:988
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:975
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
Definition: txmempool.cpp:835
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:565
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
Definition: txmempool.h:582
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:367
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:207
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1184
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:453
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:459
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:708
A UTXO entry.
Definition: coins.h:31
bool IsCoinBase() const
Definition: coins.h:55
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:79
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:40
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay,...
Definition: txmempool.h:243
A generic txid reference (txid or wtxid).
Definition: transaction.h:391
bool IsWtxid() const
Definition: transaction.h:399
const uint256 & GetHash() const
Definition: transaction.h:400
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:397
std::string ToString() const
Definition: uint256.cpp:64
std::string GetHex() const
Definition: uint256.cpp:20
256-bit opaque blob.
Definition: uint256.h:124
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:108
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:146
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
#define WITH_FRESH_EPOCH(epoch)
Definition: epochguard.h:99
#define LogPrint(category,...)
Definition: logging.h:191
#define LogPrintf(...)
Definition: logging.h:187
bool spendsCoinbase
unsigned int sigOpCost
LockPoints lp
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
Definition: moneystr.cpp:15
@ MEMPOOL
Definition: logging.h:40
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
Definition: tx_verify.cpp:169
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:29
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition: memusage.h:105
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:50
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:32
CFeeRate incrementalRelayFee
Definition: settings.cpp:12
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:285
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
Definition: random.cpp:591
reverse_range< T > reverse_iterate(T &x)
Information about a mempool transaction.
Definition: txmempool.h:321
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:47
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
Definition: txmempool.cpp:43
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:32
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
Definition: txmempool.cpp:28
int64_t feeDelta
Definition: txmempool.cpp:64
update_fee_delta(int64_t _feeDelta)
Definition: txmempool.cpp:59
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:61
void operator()(CTxMemPoolEntry &e)
Definition: txmempool.cpp:71
update_lock_points(const LockPoints &_lp)
Definition: txmempool.cpp:69
const LockPoints & lp
Definition: txmempool.cpp:74
#define LOCK(cs)
Definition: sync.h:226
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.cpp:893
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:341
@ 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 Coin to signify they are only in the memory pool (since 0....
Definition: txmempool.h:38
bool CheckSequenceLocks(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx, int flags, LockPoints *lp, bool useExistingLockPoints)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
Definition: validation.cpp:233
AssertLockHeld(pool.cs)
bool CheckFinalTx(const CBlockIndex *active_chain_tip, const CTransaction &tx, int flags)
Transaction validation functions.
Definition: validation.cpp:182
bool TestLockPointValidity(CChain &active_chain, const LockPoints *lp)
Test whether the LockPoints height and time are still valid on the current chain.
Definition: validation.cpp:215
assert(!tx.IsCoinBase())
CMainSignals & GetMainSignals()