24 int64_t _nTime,
unsigned int _entryHeight,
25 bool _spendsCoinbase, int64_t _sigOpsCost,
LockPoints lp)
64 stageEntries = updateIt->GetMemPoolChildrenConst();
66 while (!stageEntries.empty()) {
68 descendants.insert(descendant);
69 stageEntries.erase(descendant);
72 cacheMap::iterator cacheIt = cachedDescendants.find(mapTx.iterator_to(childEntry));
73 if (cacheIt != cachedDescendants.end()) {
76 for (
txiter cacheEntry : cacheIt->second) {
77 descendants.insert(*cacheEntry);
79 }
else if (!descendants.count(childEntry)) {
81 stageEntries.insert(childEntry);
87 int64_t modifySize = 0;
89 int64_t modifyCount = 0;
91 if (!setExclude.count(descendant.GetTx().GetHash())) {
92 modifySize += descendant.GetTxSize();
93 modifyFee += descendant.GetModifiedFee();
95 cachedDescendants[updateIt].insert(mapTx.iterator_to(descendant));
97 mapTx.modify(mapTx.iterator_to(descendant),
update_ancestor_state(updateIt->GetTxSize(), updateIt->GetModifiedFee(), 1, updateIt->GetSigOpCost()));
114 cacheMap mapMemPoolDescendantsToUpdate;
118 std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
128 if (
it == mapTx.end()) {
131 auto iter = mapNextTx.lower_bound(
COutPoint(hash, 0));
137 for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) {
138 const uint256 &childHash = iter->second->GetHash();
139 txiter childIter = mapTx.find(childHash);
140 assert(childIter != mapTx.end());
143 if (!
visited(childIter) && !setAlreadyIncluded.count(childHash)) {
158 if (fSearchForParents) {
162 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
163 std::optional<txiter> piter =
GetIter(tx.
vin[i].prevout.hash);
165 staged_ancestors.insert(**piter);
166 if (staged_ancestors.size() + 1 > limitAncestorCount) {
167 errString =
strprintf(
"too many unconfirmed parents [limit: %u]", limitAncestorCount);
175 txiter it = mapTx.iterator_to(entry);
176 staged_ancestors =
it->GetMemPoolParentsConst();
179 size_t totalSizeWithAncestors = entry.
GetTxSize();
181 while (!staged_ancestors.empty()) {
183 txiter stageit = mapTx.iterator_to(stage);
185 setAncestors.insert(stageit);
186 staged_ancestors.erase(stage);
187 totalSizeWithAncestors += stageit->
GetTxSize();
189 if (stageit->GetSizeWithDescendants() + entry.
GetTxSize() > limitDescendantSize) {
190 errString =
strprintf(
"exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
192 }
else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
193 errString =
strprintf(
"too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
195 }
else if (totalSizeWithAncestors > limitAncestorSize) {
196 errString =
strprintf(
"exceeds ancestor size limit [limit: %u]", limitAncestorSize);
202 txiter parent_it = mapTx.iterator_to(parent);
205 if (setAncestors.count(parent_it) == 0) {
206 staged_ancestors.insert(parent);
208 if (staged_ancestors.size() + setAncestors.size() + 1 > limitAncestorCount) {
209 errString =
strprintf(
"too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
225 const int64_t updateCount = (add ? 1 : -1);
226 const int64_t updateSize = updateCount *
it->GetTxSize();
227 const CAmount updateFee = updateCount *
it->GetModifiedFee();
228 for (
txiter ancestorIt : setAncestors) {
235 int64_t updateCount = setAncestors.size();
236 int64_t updateSize = 0;
238 int64_t updateSigOpsCost = 0;
239 for (
txiter ancestorIt : setAncestors) {
240 updateSize += ancestorIt->GetTxSize();
241 updateFee += ancestorIt->GetModifiedFee();
242 updateSigOpsCost += ancestorIt->GetSigOpCost();
259 const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
260 if (updateDescendants) {
267 for (
txiter removeIt : entriesToRemove) {
270 setDescendants.erase(removeIt);
271 int64_t modifySize = -((int64_t)removeIt->GetTxSize());
272 CAmount modifyFee = -removeIt->GetModifiedFee();
273 int modifySigOps = -removeIt->GetSigOpCost();
274 for (
txiter dit : setDescendants) {
279 for (
txiter removeIt : entriesToRemove) {
310 for (
txiter removeIt : entriesToRemove) {
336 : m_check_ratio(check_ratio), minerPolicyEstimator(estimator)
344 return mapNextTx.count(outpoint);
362 indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
379 std::set<uint256> setParentTransactions;
380 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
381 mapNextTx.insert(std::make_pair(&tx.
vin[i].prevout, &tx));
382 setParentTransactions.insert(tx.
vin[i].prevout.hash);
392 for (
const auto& pit :
GetIterSet(setParentTransactions)) {
400 m_total_fee += entry.
GetFee();
406 newit->vTxHashesIdx = vTxHashes.size() - 1;
423 const uint256 hash =
it->GetTx().GetHash();
424 for (
const CTxIn& txin :
it->GetTx().vin)
425 mapNextTx.erase(txin.prevout);
429 if (vTxHashes.size() > 1) {
430 vTxHashes[
it->vTxHashesIdx] = std::move(vTxHashes.back());
431 vTxHashes[
it->vTxHashesIdx].second->vTxHashesIdx =
it->vTxHashesIdx;
432 vTxHashes.pop_back();
433 if (vTxHashes.size() * 2 < vTxHashes.capacity())
434 vTxHashes.shrink_to_fit();
438 totalTxSize -=
it->GetTxSize();
439 m_total_fee -=
it->GetFee();
440 cachedInnerUsage -=
it->DynamicMemoryUsage();
456 if (setDescendants.count(entryit) == 0) {
457 stage.insert(entryit);
462 while (!stage.empty()) {
464 setDescendants.insert(
it);
469 txiter childiter = mapTx.iterator_to(child);
470 if (!setDescendants.count(childiter)) {
471 stage.insert(childiter);
483 if (origit != mapTx.end()) {
484 txToRemove.insert(origit);
490 for (
unsigned int i = 0; i < origTx.
vout.size(); i++) {
492 if (
it == mapNextTx.end())
494 txiter nextit = mapTx.find(
it->second->GetHash());
495 assert(nextit != mapTx.end());
496 txToRemove.insert(nextit);
512 for (indexed_transaction_set::const_iterator
it = mapTx.begin();
it != mapTx.end();
it++) {
520 txToRemove.insert(
it);
521 }
else if (
it->GetSpendsCoinbase()) {
523 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.
prevout.
hash);
524 if (it2 != mapTx.end())
530 txToRemove.insert(
it);
552 if (
it != mapNextTx.end()) {
554 if (txConflict != tx)
569 std::vector<const CTxMemPoolEntry*> entries;
570 for (
const auto& tx : vtx)
574 indexed_transaction_set::iterator i = mapTx.find(hash);
575 if (i != mapTx.end())
576 entries.push_back(&*i);
580 for (
const auto& tx : vtx)
583 if (
it != mapTx.end()) {
601 cachedInnerUsage = 0;
620 UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
623 void CTxMemPool::check(
CChainState& active_chainstate)
const
631 LogPrint(
BCLog::MEMPOOL,
"Checking mempool with %u transactions and %u inputs\n", (
unsigned int)mapTx.size(), (
unsigned int)mapNextTx.size());
633 uint64_t checkTotal = 0;
635 uint64_t innerUsage = 0;
640 const int64_t spendheight = active_chainstate.
m_chain.
Height() + 1;
641 assert(
g_chainman.m_blockman.GetSpendHeight(mempoolDuplicate) == spendheight);
643 std::list<const CTxMemPoolEntry*> waitingOnDependants;
644 for (indexed_transaction_set::const_iterator
it = mapTx.begin();
it != mapTx.end();
it++) {
646 checkTotal +=
it->GetTxSize();
647 check_total_fee +=
it->GetFee();
648 innerUsage +=
it->DynamicMemoryUsage();
651 bool fDependsWait =
false;
655 indexed_transaction_set::const_iterator it2 = mapTx.find(txin.
prevout.
hash);
656 if (it2 != mapTx.end()) {
660 setParentCheck.insert(*it2);
665 auto it3 = mapNextTx.find(txin.
prevout);
666 assert(it3 != mapNextTx.end());
668 assert(it3->second == &tx);
674 assert(setParentCheck.size() ==
it->GetMemPoolParentsConst().size());
675 assert(std::equal(setParentCheck.begin(), setParentCheck.end(),
it->GetMemPoolParentsConst().begin(), comp));
678 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
681 uint64_t nCountCheck = setAncestors.size() + 1;
682 uint64_t nSizeCheck =
it->GetTxSize();
683 CAmount nFeesCheck =
it->GetModifiedFee();
684 int64_t nSigOpCheck =
it->GetSigOpCost();
686 for (
txiter ancestorIt : setAncestors) {
687 nSizeCheck += ancestorIt->GetTxSize();
688 nFeesCheck += ancestorIt->GetModifiedFee();
689 nSigOpCheck += ancestorIt->GetSigOpCost();
692 assert(
it->GetCountWithAncestors() == nCountCheck);
693 assert(
it->GetSizeWithAncestors() == nSizeCheck);
694 assert(
it->GetSigOpCostWithAncestors() == nSigOpCheck);
695 assert(
it->GetModFeesWithAncestors() == nFeesCheck);
699 auto iter = mapNextTx.lower_bound(
COutPoint(
it->GetTx().GetHash(), 0));
700 uint64_t child_sizes = 0;
701 for (; iter != mapNextTx.end() && iter->first->hash ==
it->GetTx().GetHash(); ++iter) {
702 txiter childit = mapTx.find(iter->second->GetHash());
703 assert(childit != mapTx.end());
704 if (setChildrenCheck.insert(*childit).second) {
705 child_sizes += childit->GetTxSize();
708 assert(setChildrenCheck.size() ==
it->GetMemPoolChildrenConst().size());
709 assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(),
it->GetMemPoolChildrenConst().begin(), comp));
712 assert(
it->GetSizeWithDescendants() >= child_sizes +
it->GetTxSize());
715 waitingOnDependants.push_back(&(*
it));
720 unsigned int stepsSinceLastRemove = 0;
721 while (!waitingOnDependants.empty()) {
723 waitingOnDependants.pop_front();
724 if (!mempoolDuplicate.HaveInputs(entry->
GetTx())) {
725 waitingOnDependants.push_back(entry);
726 stepsSinceLastRemove++;
727 assert(stepsSinceLastRemove < waitingOnDependants.size());
730 stepsSinceLastRemove = 0;
733 for (
auto it = mapNextTx.cbegin();
it != mapNextTx.cend();
it++) {
735 indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
737 assert(it2 != mapTx.end());
741 assert(totalTxSize == checkTotal);
742 assert(m_total_fee == check_total_fee);
743 assert(innerUsage == cachedInnerUsage);
749 indexed_transaction_set::const_iterator i = wtxid ?
get_iter_from_wtxid(hasha) : mapTx.find(hasha);
750 if (i == mapTx.end())
return false;
751 indexed_transaction_set::const_iterator j = wtxid ?
get_iter_from_wtxid(hashb) : mapTx.find(hashb);
752 if (j == mapTx.end())
return true;
753 uint64_t counta = i->GetCountWithAncestors();
754 uint64_t countb = j->GetCountWithAncestors();
755 if (counta == countb) {
758 return counta < countb;
762 class DepthAndScoreComparator
765 bool operator()(
const CTxMemPool::indexed_transaction_set::const_iterator& a,
const CTxMemPool::indexed_transaction_set::const_iterator& b)
767 uint64_t counta = a->GetCountWithAncestors();
768 uint64_t countb = b->GetCountWithAncestors();
769 if (counta == countb) {
772 return counta < countb;
779 std::vector<indexed_transaction_set::const_iterator> iters;
782 iters.reserve(mapTx.size());
784 for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) {
787 std::sort(iters.begin(), iters.end(), DepthAndScoreComparator());
797 vtxid.reserve(mapTx.size());
799 for (
auto it : iters) {
800 vtxid.push_back(
it->GetTx().GetHash());
805 return TxMempoolInfo{
it->GetSharedTx(),
it->GetTime(),
it->GetFee(),
it->GetTxSize(),
it->GetModifiedFee() -
it->GetFee()};
813 std::vector<TxMempoolInfo> ret;
814 ret.reserve(mapTx.size());
815 for (
auto it : iters) {
825 indexed_transaction_set::const_iterator i = mapTx.find(hash);
826 if (i == mapTx.end())
828 return i->GetSharedTx();
835 if (i == mapTx.end())
849 if (
it != mapTx.end()) {
853 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
856 for (
txiter ancestorIt : setAncestors) {
862 setDescendants.erase(
it);
863 for (
txiter descendantIt : setDescendants) {
875 std::map<uint256, CAmount>::const_iterator pos =
mapDeltas.find(hash);
878 const CAmount &delta = pos->second;
890 const auto it = mapNextTx.find(prevout);
891 return it == mapNextTx.end() ? nullptr :
it->second;
896 auto it = mapTx.find(txid);
897 if (
it != mapTx.end())
return it;
904 for (
const auto& h : hashes) {
906 if (mi) ret.insert(*mi);
913 for (
unsigned int i = 0; i < tx.
vin.size(); i++)
927 if (outpoint.
n < ptx->vout.size()) {
946 if (m_unbroadcast_txids.erase(txid))
948 LogPrint(
BCLog::MEMPOOL,
"Removed %i from set of unbroadcast txns%s\n", txid.
GetHex(), (unchecked ?
" before confirmation that txn was sent out" :
""));
963 indexed_transaction_set::index<entry_time>::type::iterator
it = mapTx.get<
entry_time>().begin();
965 while (
it != mapTx.get<
entry_time>().end() &&
it->GetTime() < time) {
966 toremove.insert(mapTx.project<0>(
it));
970 for (
txiter removeit : toremove) {
980 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
983 return addUnchecked(entry, setAncestors, validFeeEstimate);
990 if (add && entry->GetMemPoolChildren().insert(*child).second) {
992 }
else if (!add && entry->GetMemPoolChildren().erase(*child)) {
1001 if (add && entry->GetMemPoolParents().insert(*parent).second) {
1003 }
else if (!add && entry->GetMemPoolParents().erase(*parent)) {
1043 unsigned nTxnRemoved = 0;
1046 indexed_transaction_set::index<descendant_score>::type::iterator
it = mapTx.get<
descendant_score>().begin();
1052 CFeeRate removed(
it->GetModFeesWithDescendants(),
it->GetSizeWithDescendants());
1055 maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
1059 nTxnRemoved += stage.size();
1061 std::vector<CTransaction> txn;
1062 if (pvNoSpendsRemaining) {
1063 txn.reserve(stage.size());
1064 for (
txiter iter : stage)
1065 txn.push_back(iter->GetTx());
1068 if (pvNoSpendsRemaining) {
1072 pvNoSpendsRemaining->push_back(txin.
prevout);
1078 if (maxFeeRateRemoved >
CFeeRate(0)) {
1085 std::vector<txiter> candidates;
1087 candidates.push_back(entry);
1088 uint64_t maximum = 0;
1089 while (candidates.size()) {
1090 txiter candidate = candidates.back();
1091 candidates.pop_back();
1092 if (!counted.insert(candidate).second)
continue;
1094 if (parents.size() == 0) {
1095 maximum = std::max(maximum, candidate->GetCountWithDescendants());
1098 candidates.push_back(mapTx.iterator_to(i));
1107 auto it = mapTx.find(txid);
1108 ancestors = descendants = 0;
1109 if (
it != mapTx.end()) {
1110 ancestors =
it->GetCountWithAncestors();
1124 m_is_loaded = loaded;