LCOV - code coverage report
Current view: top level - src - txmempool.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.4 % 638 628
Test Date: 2025-11-28 05:09:59 Functions: 100.0 % 65 65
Branches: 66.7 % 742 495

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <txmempool.h>
       7                 :             : 
       8                 :             : #include <chain.h>
       9                 :             : #include <coins.h>
      10                 :             : #include <common/system.h>
      11                 :             : #include <consensus/consensus.h>
      12                 :             : #include <consensus/tx_verify.h>
      13                 :             : #include <consensus/validation.h>
      14                 :             : #include <logging.h>
      15                 :             : #include <policy/policy.h>
      16                 :             : #include <policy/settings.h>
      17                 :             : #include <random.h>
      18                 :             : #include <tinyformat.h>
      19                 :             : #include <util/check.h>
      20                 :             : #include <util/feefrac.h>
      21                 :             : #include <util/moneystr.h>
      22                 :             : #include <util/overflow.h>
      23                 :             : #include <util/result.h>
      24                 :             : #include <util/time.h>
      25                 :             : #include <util/trace.h>
      26                 :             : #include <util/translation.h>
      27                 :             : #include <validationinterface.h>
      28                 :             : 
      29                 :             : #include <algorithm>
      30                 :             : #include <cmath>
      31                 :             : #include <numeric>
      32                 :             : #include <optional>
      33                 :             : #include <ranges>
      34                 :             : #include <string_view>
      35                 :             : #include <utility>
      36                 :             : 
      37                 :             : TRACEPOINT_SEMAPHORE(mempool, added);
      38                 :             : TRACEPOINT_SEMAPHORE(mempool, removed);
      39                 :             : 
      40                 :        2760 : bool TestLockPointValidity(CChain& active_chain, const LockPoints& lp)
      41                 :             : {
      42                 :        2760 :     AssertLockHeld(cs_main);
      43                 :             :     // If there are relative lock times then the maxInputBlock will be set
      44                 :             :     // If there are no relative lock times, the LockPoints don't depend on the chain
      45         [ +  - ]:        2760 :     if (lp.maxInputBlock) {
      46                 :             :         // Check whether active_chain is an extension of the block at which the LockPoints
      47                 :             :         // calculation was valid.  If not LockPoints are no longer valid
      48         [ +  + ]:        2760 :         if (!active_chain.Contains(lp.maxInputBlock)) {
      49                 :         225 :             return false;
      50                 :             :         }
      51                 :             :     }
      52                 :             : 
      53                 :             :     // LockPoints still valid
      54                 :             :     return true;
      55                 :             : }
      56                 :             : 
      57                 :    11850949 : std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> CTxMemPool::GetChildren(const CTxMemPoolEntry& entry) const
      58                 :             : {
      59                 :    11850949 :     LOCK(cs);
      60                 :    11850949 :     std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> ret;
      61                 :    11850949 :     setEntries children;
      62                 :    11850949 :     auto iter = mapNextTx.lower_bound(COutPoint(entry.GetTx().GetHash(), 0));
      63   [ +  +  +  + ]:    12071040 :     for (; iter != mapNextTx.end() && iter->first->hash == entry.GetTx().GetHash(); ++iter) {
      64         [ +  - ]:      220091 :         children.insert(iter->second);
      65                 :             :     }
      66         [ +  + ]:    12070919 :     for (const auto& child : children) {
      67         [ +  - ]:      219970 :         ret.emplace_back(*child);
      68                 :             :     }
      69                 :    11850949 :     return ret;
      70         [ +  - ]:    23701898 : }
      71                 :             : 
      72                 :    11875397 : std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> CTxMemPool::GetParents(const CTxMemPoolEntry& entry) const
      73                 :             : {
      74                 :    11875397 :     LOCK(cs);
      75                 :    11875397 :     std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> ret;
      76                 :    11875397 :     std::set<Txid> inputs;
      77         [ +  + ]:    27351073 :     for (const auto& txin : entry.GetTx().vin) {
      78         [ +  - ]:    15475676 :         inputs.insert(txin.prevout.hash);
      79                 :             :     }
      80         [ +  + ]:    27339651 :     for (const auto& hash : inputs) {
      81         [ +  - ]:    15464254 :         std::optional<txiter> piter = GetIter(hash);
      82         [ +  + ]:    15464254 :         if (piter) {
      83         [ +  - ]:      219996 :             ret.emplace_back(**piter);
      84                 :             :         }
      85                 :             :     }
      86                 :    11875397 :     return ret;
      87         [ +  - ]:    23750794 : }
      88                 :             : 
      89                 :        3125 : void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<Txid>& vHashesToUpdate)
      90                 :             : {
      91                 :        3125 :     AssertLockHeld(cs);
      92                 :             : 
      93                 :             :     // Iterate in reverse, so that whenever we are looking at a transaction
      94                 :             :     // we are sure that all in-mempool descendants have already been processed.
      95         [ +  + ]:        3888 :     for (const Txid& hash : vHashesToUpdate | std::views::reverse) {
      96                 :             :         // calculate children from mapNextTx
      97                 :         763 :         txiter it = mapTx.find(hash);
      98         [ -  + ]:         763 :         if (it == mapTx.end()) {
      99                 :           0 :             continue;
     100                 :             :         }
     101                 :         763 :         auto iter = mapNextTx.lower_bound(COutPoint(hash, 0));
     102                 :         763 :         {
     103   [ +  +  +  + ]:        3210 :             for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) {
     104         [ -  + ]:        2447 :                 txiter childIter = iter->second;
     105         [ -  + ]:        2447 :                 assert(childIter != mapTx.end());
     106                 :             :                 // Add dependencies that are discovered between transactions in the
     107                 :             :                 // block and transactions that were in the mempool to txgraph.
     108                 :        2447 :                 m_txgraph->AddDependency(/*parent=*/*it, /*child=*/*childIter);
     109                 :             :             }
     110                 :             :         }
     111                 :             :     }
     112                 :             : 
     113                 :        3125 :     auto txs_to_remove = m_txgraph->Trim(); // Enforce cluster size limits.
     114         [ -  + ]:        3125 :     for (auto txptr : txs_to_remove) {
     115                 :           0 :         const CTxMemPoolEntry& entry = *(static_cast<const CTxMemPoolEntry*>(txptr));
     116         [ #  # ]:           0 :         removeUnchecked(mapTx.iterator_to(entry), MemPoolRemovalReason::SIZELIMIT);
     117                 :             :     }
     118                 :        3125 : }
     119                 :             : 
     120                 :         223 : bool CTxMemPool::HasDescendants(const Txid& txid) const
     121                 :             : {
     122                 :         223 :     LOCK(cs);
     123         [ +  - ]:         223 :     auto entry = GetEntry(txid);
     124         [ +  + ]:         223 :     if (!entry) return false;
     125         [ -  + ]:         222 :     return m_txgraph->GetDescendants(*entry, TxGraph::Level::MAIN).size() > 1;
     126                 :         223 : }
     127                 :             : 
     128                 :        3181 : CTxMemPool::setEntries CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry) const
     129                 :             : {
     130                 :        3181 :     auto ancestors = m_txgraph->GetAncestors(entry, TxGraph::Level::MAIN);
     131         [ -  + ]:        3181 :     setEntries ret;
     132   [ -  +  +  + ]:        3181 :     if (ancestors.size() > 0) {
     133         [ +  + ]:       17359 :         for (auto ancestor : ancestors) {
     134         [ +  + ]:       15431 :             if (ancestor != &entry) {
     135         [ +  - ]:       13503 :                 ret.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ancestor)));
     136                 :             :             }
     137                 :             :         }
     138                 :             :         return ret;
     139                 :             :     }
     140                 :             : 
     141                 :             :     // If we didn't get anything back, the transaction is not in the graph.
     142                 :             :     // Find each parent and call GetAncestors on each.
     143                 :        1253 :     setEntries staged_parents;
     144                 :        1253 :     const CTransaction &tx = entry.GetTx();
     145                 :             : 
     146                 :             :     // Get parents of this transaction that are in the mempool
     147   [ -  +  +  + ]:        2838 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     148         [ +  - ]:        1585 :         std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
     149         [ +  + ]:        1585 :         if (piter) {
     150         [ +  - ]:         233 :             staged_parents.insert(*piter);
     151                 :             :         }
     152                 :             :     }
     153                 :             : 
     154         [ +  + ]:        1460 :     for (const auto& parent : staged_parents) {
     155                 :         207 :         auto parent_ancestors = m_txgraph->GetAncestors(*parent, TxGraph::Level::MAIN);
     156         [ +  + ]:         692 :         for (auto ancestor : parent_ancestors) {
     157         [ +  - ]:         485 :             ret.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ancestor)));
     158                 :             :         }
     159                 :         207 :     }
     160                 :             : 
     161                 :        1253 :     return ret;
     162                 :        4434 : }
     163                 :             : 
     164                 :        1243 : static CTxMemPool::Options&& Flatten(CTxMemPool::Options&& opts, bilingual_str& error)
     165                 :             : {
     166         [ +  - ]:        1243 :     opts.check_ratio = std::clamp<int>(opts.check_ratio, 0, 1'000'000);
     167                 :        1243 :     int64_t cluster_limit_bytes = opts.limits.cluster_size_vbytes * 40;
     168   [ +  -  +  -  :        1243 :     if (opts.max_size_bytes < 0 || (opts.max_size_bytes > 0 && opts.max_size_bytes < cluster_limit_bytes)) {
                   +  + ]
     169                 :           2 :         error = strprintf(_("-maxmempool must be at least %d MB"), std::ceil(cluster_limit_bytes / 1'000'000.0));
     170                 :             :     }
     171                 :        1243 :     return std::move(opts);
     172                 :             : }
     173                 :             : 
     174                 :        1243 : CTxMemPool::CTxMemPool(Options opts, bilingual_str& error)
     175   [ +  -  +  - ]:        1243 :     : m_opts{Flatten(std::move(opts), error)}
     176                 :             : {
     177                 :        1243 :     m_txgraph = MakeTxGraph(m_opts.limits.cluster_count, m_opts.limits.cluster_size_vbytes * WITNESS_SCALE_FACTOR, ACCEPTABLE_ITERS);
     178                 :        1243 : }
     179                 :             : 
     180                 :          54 : bool CTxMemPool::isSpent(const COutPoint& outpoint) const
     181                 :             : {
     182                 :          54 :     LOCK(cs);
     183         [ +  - ]:          54 :     return mapNextTx.count(outpoint);
     184                 :          54 : }
     185                 :             : 
     186                 :        2082 : unsigned int CTxMemPool::GetTransactionsUpdated() const
     187                 :             : {
     188                 :        2082 :     return nTransactionsUpdated;
     189                 :             : }
     190                 :             : 
     191                 :      143655 : void CTxMemPool::AddTransactionsUpdated(unsigned int n)
     192                 :             : {
     193                 :      143655 :     nTransactionsUpdated += n;
     194                 :      143655 : }
     195                 :             : 
     196                 :       51904 : void CTxMemPool::Apply(ChangeSet* changeset)
     197                 :             : {
     198                 :       51904 :     AssertLockHeld(cs);
     199                 :       51904 :     m_txgraph->CommitStaging();
     200                 :             : 
     201                 :       51904 :     RemoveStaged(changeset->m_to_remove, false, MemPoolRemovalReason::REPLACED);
     202                 :             : 
     203   [ -  +  +  + ]:      103862 :     for (size_t i=0; i<changeset->m_entry_vec.size(); ++i) {
     204                 :       51958 :         auto tx_entry = changeset->m_entry_vec[i];
     205                 :             :         // First splice this entry into mapTx.
     206                 :       51958 :         auto node_handle = changeset->m_to_add.extract(tx_entry);
     207         [ +  - ]:       51958 :         auto result = mapTx.insert(std::move(node_handle));
     208                 :             : 
     209         [ +  - ]:       51958 :         Assume(result.inserted);
     210                 :       51958 :         txiter it = result.position;
     211                 :             : 
     212         [ +  - ]:       51958 :         addNewTransaction(it);
     213         [ -  + ]:       51958 :     }
     214                 :       51904 :     m_txgraph->DoWork(POST_CHANGE_WORK);
     215                 :       51904 : }
     216                 :             : 
     217                 :       51958 : void CTxMemPool::addNewTransaction(CTxMemPool::txiter newit)
     218                 :             : {
     219                 :       51958 :     const CTxMemPoolEntry& entry = *newit;
     220                 :             : 
     221                 :             :     // Update cachedInnerUsage to include contained transaction's usage.
     222                 :             :     // (When we update the entry for in-mempool parents, memory usage will be
     223                 :             :     // further updated.)
     224                 :       51958 :     cachedInnerUsage += entry.DynamicMemoryUsage();
     225                 :             : 
     226                 :       51958 :     const CTransaction& tx = newit->GetTx();
     227   [ -  +  +  + ]:      117326 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     228                 :       65368 :         mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, newit));
     229                 :             :     }
     230                 :             :     // Don't bother worrying about child transactions of this one.
     231                 :             :     // Normal case of a new transaction arriving is that there can't be any
     232                 :             :     // children, because such children would be orphans.
     233                 :             :     // An exception to that is if a transaction enters that used to be in a block.
     234                 :             :     // In that case, our disconnect block logic will call UpdateTransactionsFromBlock
     235                 :             :     // to clean up the mess we're leaving here.
     236                 :             : 
     237                 :       51958 :     nTransactionsUpdated++;
     238                 :       51958 :     totalTxSize += entry.GetTxSize();
     239                 :       51958 :     m_total_fee += entry.GetFee();
     240                 :             : 
     241                 :       51958 :     txns_randomized.emplace_back(tx.GetWitnessHash(), newit);
     242         [ -  + ]:       51958 :     newit->idx_randomized = txns_randomized.size() - 1;
     243                 :             : 
     244                 :             :     TRACEPOINT(mempool, added,
     245                 :             :         entry.GetTx().GetHash().data(),
     246                 :             :         entry.GetTxSize(),
     247                 :             :         entry.GetFee()
     248                 :       51958 :     );
     249                 :       51958 : }
     250                 :             : 
     251                 :       48156 : void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
     252                 :             : {
     253                 :             :     // We increment mempool sequence value no matter removal reason
     254                 :             :     // even if not directly reported below.
     255         [ +  + ]:       48156 :     uint64_t mempool_sequence = GetAndIncrementSequence();
     256                 :             : 
     257   [ +  +  +  - ]:       48156 :     if (reason != MemPoolRemovalReason::BLOCK && m_opts.signals) {
     258                 :             :         // Notify clients that a transaction has been removed from the mempool
     259                 :             :         // for any reason except being included in a block. Clients interested
     260                 :             :         // in transactions included in blocks can subscribe to the BlockConnected
     261                 :             :         // notification.
     262   [ +  -  +  - ]:        6186 :         m_opts.signals->TransactionRemovedFromMempool(it->GetSharedTx(), reason, mempool_sequence);
     263                 :             :     }
     264                 :             :     TRACEPOINT(mempool, removed,
     265                 :             :         it->GetTx().GetHash().data(),
     266                 :             :         RemovalReasonToString(reason).c_str(),
     267                 :             :         it->GetTxSize(),
     268                 :             :         it->GetFee(),
     269                 :             :         std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count()
     270                 :       48156 :     );
     271                 :             : 
     272         [ +  + ]:      108290 :     for (const CTxIn& txin : it->GetTx().vin)
     273                 :       60134 :         mapNextTx.erase(txin.prevout);
     274                 :             : 
     275                 :       48156 :     RemoveUnbroadcastTx(it->GetTx().GetHash(), true /* add logging because unchecked */);
     276                 :             : 
     277   [ -  +  +  + ]:       48156 :     if (txns_randomized.size() > 1) {
     278                 :             :         // Remove entry from txns_randomized by replacing it with the back and deleting the back.
     279         [ -  + ]:       46090 :         txns_randomized[it->idx_randomized] = std::move(txns_randomized.back());
     280         [ -  + ]:       46090 :         txns_randomized[it->idx_randomized].second->idx_randomized = it->idx_randomized;
     281         [ -  + ]:       46090 :         txns_randomized.pop_back();
     282   [ -  +  -  +  :       46090 :         if (txns_randomized.size() * 2 < txns_randomized.capacity()) {
                   +  + ]
     283                 :        3192 :             txns_randomized.shrink_to_fit();
     284                 :             :         }
     285                 :             :     } else {
     286         [ +  - ]:        2066 :         txns_randomized.clear();
     287                 :             :     }
     288                 :             : 
     289                 :       48156 :     totalTxSize -= it->GetTxSize();
     290                 :       48156 :     m_total_fee -= it->GetFee();
     291                 :       48156 :     cachedInnerUsage -= it->DynamicMemoryUsage();
     292                 :       48156 :     mapTx.erase(it);
     293                 :       48156 :     nTransactionsUpdated++;
     294                 :       48156 : }
     295                 :             : 
     296                 :             : // Calculates descendants of given entry and adds to setDescendants.
     297                 :       80173 : void CTxMemPool::CalculateDescendants(txiter entryit, setEntries& setDescendants) const
     298                 :             : {
     299                 :       80173 :     (void)CalculateDescendants(*entryit, setDescendants);
     300                 :       80173 :     return;
     301                 :             : }
     302                 :             : 
     303                 :       80233 : CTxMemPool::txiter CTxMemPool::CalculateDescendants(const CTxMemPoolEntry& entry, setEntries& setDescendants) const
     304                 :             : {
     305         [ +  + ]:      363286 :     for (auto tx : m_txgraph->GetDescendants(entry, TxGraph::Level::MAIN)) {
     306         [ +  - ]:      283053 :         setDescendants.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx)));
     307                 :             :     }
     308                 :       80233 :     return mapTx.iterator_to(entry);
     309                 :             : }
     310                 :             : 
     311                 :       18627 : void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReason reason)
     312                 :             : {
     313                 :             :     // Remove transaction from memory pool
     314                 :       18627 :     AssertLockHeld(cs);
     315         [ +  - ]:       18627 :     Assume(!m_have_changeset);
     316         [ +  - ]:       18627 :         setEntries txToRemove;
     317         [ +  - ]:       18627 :         txiter origit = mapTx.find(origTx.GetHash());
     318         [ +  + ]:       18627 :         if (origit != mapTx.end()) {
     319         [ +  - ]:         163 :             txToRemove.insert(origit);
     320                 :             :         } else {
     321                 :             :             // When recursively removing but origTx isn't in the mempool
     322                 :             :             // be sure to remove any children that are in the pool. This can
     323                 :             :             // happen during chain re-orgs if origTx isn't re-accepted into
     324                 :             :             // the mempool for any reason.
     325   [ -  +  +  + ]:       46708 :             for (unsigned int i = 0; i < origTx.vout.size(); i++) {
     326                 :       28244 :                 auto it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
     327         [ +  + ]:       28244 :                 if (it == mapNextTx.end())
     328                 :       28166 :                     continue;
     329         [ -  + ]:          78 :                 txiter nextit = it->second;
     330         [ -  + ]:          78 :                 assert(nextit != mapTx.end());
     331         [ +  - ]:          78 :                 txToRemove.insert(nextit);
     332                 :             :             }
     333                 :             :         }
     334                 :       18627 :         setEntries setAllRemoves;
     335         [ +  + ]:       18868 :         for (txiter it : txToRemove) {
     336         [ +  - ]:         241 :             CalculateDescendants(it, setAllRemoves);
     337                 :             :         }
     338                 :             : 
     339         [ +  - ]:       18627 :         RemoveStaged(setAllRemoves, false, reason);
     340                 :       18627 : }
     341                 :             : 
     342                 :        3125 : void CTxMemPool::removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature)
     343                 :             : {
     344                 :             :     // Remove transactions spending a coinbase which are now immature and no-longer-final transactions
     345                 :        3125 :     AssertLockHeld(cs);
     346                 :        3125 :     AssertLockHeld(::cs_main);
     347                 :        3125 :     Assume(!m_have_changeset);
     348                 :             : 
     349                 :        3125 :     setEntries txToRemove;
     350         [ +  + ]:        4511 :     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
     351   [ +  -  +  +  :        1386 :         if (check_final_and_mature(it)) txToRemove.insert(it);
                   +  - ]
     352                 :             :     }
     353                 :        3125 :     setEntries setAllRemoves;
     354         [ +  + ]:        3134 :     for (txiter it : txToRemove) {
     355         [ +  - ]:           9 :         CalculateDescendants(it, setAllRemoves);
     356                 :             :     }
     357         [ +  - ]:        3125 :     RemoveStaged(setAllRemoves, false, MemPoolRemovalReason::REORG);
     358         [ +  + ]:        4502 :     for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
     359   [ +  -  -  + ]:        1377 :         assert(TestLockPointValidity(chain, it->GetLockPoints()));
     360                 :             :     }
     361                 :        3125 :     m_txgraph->DoWork(POST_CHANGE_WORK);
     362                 :        3125 : }
     363                 :             : 
     364                 :       57558 : void CTxMemPool::removeConflicts(const CTransaction &tx)
     365                 :             : {
     366                 :             :     // Remove transactions which depend on inputs of tx, recursively
     367                 :       57558 :     AssertLockHeld(cs);
     368         [ +  + ]:      127161 :     for (const CTxIn &txin : tx.vin) {
     369                 :       69603 :         auto it = mapNextTx.find(txin.prevout);
     370         [ +  + ]:       69603 :         if (it != mapNextTx.end()) {
     371         [ +  - ]:         156 :             const CTransaction &txConflict = it->second->GetTx();
     372         [ +  - ]:         156 :             if (Assume(txConflict.GetHash() != tx.GetHash()))
     373                 :             :             {
     374                 :         156 :                 ClearPrioritisation(txConflict.GetHash());
     375                 :         156 :                 removeRecursive(txConflict, MemPoolRemovalReason::CONFLICT);
     376                 :             :             }
     377                 :             :         }
     378                 :             :     }
     379                 :       57558 : }
     380                 :             : 
     381                 :      129909 : void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight)
     382                 :             : {
     383                 :             :     // Remove confirmed txs and conflicts when a new block is connected, updating the fee logic
     384                 :      129909 :     AssertLockHeld(cs);
     385         [ +  + ]:      129909 :     Assume(!m_have_changeset);
     386                 :      129909 :     std::vector<RemovedMempoolTransactionInfo> txs_removed_for_block;
     387   [ +  +  +  -  :      129909 :     if (mapTx.size() || mapNextTx.size() || mapDeltas.size()) {
                   +  + ]
     388   [ -  +  +  - ]:        7062 :         txs_removed_for_block.reserve(vtx.size());
     389         [ +  + ]:       64620 :         for (const auto& tx : vtx) {
     390         [ +  - ]:       57558 :             txiter it = mapTx.find(tx->GetHash());
     391         [ +  + ]:       57558 :             if (it != mapTx.end()) {
     392         [ +  - ]:       46094 :                 setEntries stage;
     393         [ +  - ]:       46094 :                 stage.insert(it);
     394         [ +  - ]:       46094 :                 txs_removed_for_block.emplace_back(*it);
     395         [ +  - ]:       46094 :                 RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
     396                 :       46094 :             }
     397         [ +  - ]:       57558 :             removeConflicts(*tx);
     398         [ +  - ]:       57558 :             ClearPrioritisation(tx->GetHash());
     399                 :             :         }
     400                 :             :     }
     401         [ +  - ]:      129909 :     if (m_opts.signals) {
     402         [ +  - ]:      129909 :         m_opts.signals->MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight);
     403                 :             :     }
     404         [ +  - ]:      129909 :     lastRollingFeeUpdate = GetTime();
     405                 :      129909 :     blockSinceLastRollingFeeBump = true;
     406                 :      129909 :     m_txgraph->DoWork(POST_CHANGE_WORK);
     407                 :      129909 : }
     408                 :             : 
     409                 :      156222 : void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const
     410                 :             : {
     411         [ +  + ]:      156222 :     if (m_opts.check_ratio == 0) return;
     412                 :             : 
     413         [ +  - ]:      154174 :     if (FastRandomContext().randrange(m_opts.check_ratio) >= 1) return;
     414                 :             : 
     415                 :      154174 :     AssertLockHeld(::cs_main);
     416                 :      154174 :     LOCK(cs);
     417   [ +  -  +  -  :      154174 :     LogDebug(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
                   +  - ]
     418                 :             : 
     419                 :      154174 :     uint64_t checkTotal = 0;
     420                 :      154174 :     CAmount check_total_fee{0};
     421                 :      154174 :     uint64_t innerUsage = 0;
     422                 :             : 
     423         [ -  + ]:      154174 :     assert(!m_txgraph->IsOversized(TxGraph::Level::MAIN));
     424         [ +  - ]:      154174 :     m_txgraph->SanityCheck();
     425                 :             : 
     426         [ +  - ]:      154174 :     CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip));
     427                 :             : 
     428         [ +  - ]:      154174 :     const auto score_with_topo{GetSortedScoreWithTopology()};
     429                 :             : 
     430                 :             :     // Number of chunks is bounded by number of transactions.
     431         [ +  - ]:      154174 :     const auto diagram{GetFeerateDiagram()};
     432   [ -  +  -  + ]:      154174 :     Assume(diagram.size() <= score_with_topo.size() + 1);
     433                 :             : 
     434                 :      154174 :     std::optional<Wtxid> last_wtxid = std::nullopt;
     435                 :             : 
     436         [ +  + ]:    11996439 :     for (const auto& it : score_with_topo) {
     437         [ +  - ]:    11842265 :         checkTotal += it->GetTxSize();
     438         [ +  + ]:    11842265 :         check_total_fee += it->GetFee();
     439         [ +  + ]:    11842265 :         innerUsage += it->DynamicMemoryUsage();
     440         [ +  + ]:    11842265 :         const CTransaction& tx = it->GetTx();
     441                 :             : 
     442                 :             :         // CompareMiningScoreWithTopology should agree with GetSortedScoreWithTopology()
     443         [ +  + ]:    11842265 :         if (last_wtxid) {
     444   [ +  -  -  + ]:    11810669 :             assert(CompareMiningScoreWithTopology(*last_wtxid, tx.GetWitnessHash()));
     445                 :             :         }
     446         [ +  + ]:    11842265 :         last_wtxid = tx.GetWitnessHash();
     447                 :             : 
     448                 :    11842265 :         std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setParentCheck;
     449                 :    11842265 :         std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setParentsStored;
     450         [ +  + ]:    27265323 :         for (const CTxIn &txin : tx.vin) {
     451                 :             :             // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
     452         [ +  - ]:    15423058 :             indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
     453         [ +  + ]:    15423058 :             if (it2 != mapTx.end()) {
     454         [ -  + ]:      212987 :                 const CTransaction& tx2 = it2->GetTx();
     455   [ -  +  +  -  :      212987 :                 assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
                   -  + ]
     456         [ +  - ]:      212987 :                 setParentCheck.insert(*it2);
     457                 :             :             }
     458                 :             :             // We are iterating through the mempool entries sorted
     459                 :             :             // topologically and by mining score. All parents must have been
     460                 :             :             // checked before their children and their coins added to the
     461                 :             :             // mempoolDuplicate coins cache.
     462   [ +  -  -  + ]:    15423058 :             assert(mempoolDuplicate.HaveCoin(txin.prevout));
     463                 :             :             // Check whether its inputs are marked in mapNextTx.
     464                 :    15423058 :             auto it3 = mapNextTx.find(txin.prevout);
     465         [ -  + ]:    15423058 :             assert(it3 != mapNextTx.end());
     466         [ -  + ]:    15423058 :             assert(it3->first == &txin.prevout);
     467         [ -  + ]:    15423058 :             assert(&it3->second->GetTx() == &tx);
     468                 :             :         }
     469                 :    12267997 :         auto comp = [](const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) -> bool {
     470         [ +  - ]:      425732 :             return a.GetTx().GetHash() == b.GetTx().GetHash();
     471                 :             :         };
     472   [ +  -  +  + ]:    12055131 :         for (auto &txentry : GetParents(*it)) {
     473         [ +  - ]:      212866 :             setParentsStored.insert(dynamic_cast<const CTxMemPoolEntry&>(txentry.get()));
     474                 :           0 :         }
     475         [ -  + ]:    11842265 :         assert(setParentCheck.size() == setParentsStored.size());
     476         [ -  + ]:    11842265 :         assert(std::equal(setParentCheck.begin(), setParentCheck.end(), setParentsStored.begin(), comp));
     477                 :             : 
     478                 :             :         // Check children against mapNextTx
     479                 :    11842265 :         std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setChildrenCheck;
     480                 :    11842265 :         std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash> setChildrenStored;
     481                 :    11842265 :         auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
     482   [ +  +  +  + ]:    12055252 :         for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
     483         [ -  + ]:      212987 :             txiter childit = iter->second;
     484         [ -  + ]:      212987 :             assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions
     485         [ +  - ]:      212987 :             setChildrenCheck.insert(*childit);
     486                 :             :         }
     487   [ +  -  +  + ]:    12055131 :         for (auto &txentry : GetChildren(*it)) {
     488         [ +  - ]:      212866 :             setChildrenStored.insert(dynamic_cast<const CTxMemPoolEntry&>(txentry.get()));
     489                 :           0 :         }
     490         [ -  + ]:    11842265 :         assert(setChildrenCheck.size() == setChildrenStored.size());
     491         [ -  + ]:    11842265 :         assert(std::equal(setChildrenCheck.begin(), setChildrenCheck.end(), setChildrenStored.begin(), comp));
     492                 :             : 
     493         [ -  + ]:    11842265 :         TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass
     494                 :    11842265 :         CAmount txfee = 0;
     495         [ -  + ]:    11842265 :         assert(!tx.IsCoinBase());
     496   [ +  -  -  + ]:    11842265 :         assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee));
     497   [ +  -  +  + ]:    27265323 :         for (const auto& input: tx.vin) mempoolDuplicate.SpendCoin(input.prevout);
     498         [ +  - ]:    11842265 :         AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
     499                 :    11842265 :     }
     500         [ +  + ]:    15577232 :     for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
     501         [ -  + ]:    15423058 :         indexed_transaction_set::const_iterator it2 = it->second;
     502         [ -  + ]:    15423058 :         assert(it2 != mapTx.end());
     503                 :             :     }
     504                 :             : 
     505         [ -  + ]:      154174 :     assert(totalTxSize == checkTotal);
     506         [ -  + ]:      154174 :     assert(m_total_fee == check_total_fee);
     507         [ -  + ]:      154174 :     assert(innerUsage == cachedInnerUsage);
     508         [ +  - ]:      308348 : }
     509                 :             : 
     510                 :    11836378 : bool CTxMemPool::CompareMiningScoreWithTopology(const Wtxid& hasha, const Wtxid& hashb) const
     511                 :             : {
     512                 :             :     /* Return `true` if hasha should be considered sooner than hashb, namely when:
     513                 :             :      *     a is not in the mempool but b is, or
     514                 :             :      *     both are in the mempool but a is sorted before b in the total mempool ordering
     515                 :             :      *     (which takes dependencies and (chunk) feerates into account).
     516                 :             :      */
     517                 :    11836378 :     LOCK(cs);
     518         [ +  - ]:    11836378 :     auto j{GetIter(hashb)};
     519         [ +  + ]:    11836378 :     if (!j.has_value()) return false;
     520         [ +  - ]:    11832859 :     auto i{GetIter(hasha)};
     521         [ +  + ]:    11832859 :     if (!i.has_value()) return true;
     522                 :             : 
     523                 :    11832641 :     return m_txgraph->CompareMainOrder(*i.value(), *j.value()) < 0;
     524                 :    11836378 : }
     525                 :             : 
     526                 :      163960 : std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedScoreWithTopology() const
     527                 :             : {
     528                 :      163960 :     std::vector<indexed_transaction_set::const_iterator> iters;
     529                 :      163960 :     AssertLockHeld(cs);
     530                 :             : 
     531         [ +  - ]:      163960 :     iters.reserve(mapTx.size());
     532                 :             : 
     533   [ +  +  +  + ]:    24501558 :     for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) {
     534         [ +  - ]:    12168799 :         iters.push_back(mi);
     535                 :             :     }
     536                 :      163960 :     std::sort(iters.begin(), iters.end(), [this](const auto& a, const auto& b) EXCLUSIVE_LOCKS_REQUIRED(cs) noexcept {
     537                 :   142033368 :         return m_txgraph->CompareMainOrder(*a, *b) < 0;
     538                 :             :     });
     539                 :      163960 :     return iters;
     540                 :           0 : }
     541                 :             : 
     542                 :        8843 : std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const
     543                 :             : {
     544                 :        8843 :     AssertLockHeld(cs);
     545                 :             : 
     546                 :        8843 :     std::vector<CTxMemPoolEntryRef> ret;
     547         [ +  - ]:        8843 :     ret.reserve(mapTx.size());
     548   [ +  -  +  + ]:      333856 :     for (const auto& it : GetSortedScoreWithTopology()) {
     549         [ +  - ]:      325013 :         ret.emplace_back(*it);
     550                 :             :     }
     551                 :        8843 :     return ret;
     552                 :           0 : }
     553                 :             : 
     554                 :         943 : std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
     555                 :             : {
     556                 :         943 :     LOCK(cs);
     557         [ +  - ]:         943 :     auto iters = GetSortedScoreWithTopology();
     558                 :             : 
     559                 :         943 :     std::vector<TxMempoolInfo> ret;
     560         [ +  - ]:         943 :     ret.reserve(mapTx.size());
     561         [ +  + ]:        2464 :     for (auto it : iters) {
     562   [ +  -  -  + ]:        3042 :         ret.push_back(GetInfo(it));
     563                 :             :     }
     564                 :             : 
     565                 :         943 :     return ret;
     566         [ +  - ]:        1886 : }
     567                 :             : 
     568                 :        3036 : const CTxMemPoolEntry* CTxMemPool::GetEntry(const Txid& txid) const
     569                 :             : {
     570                 :        3036 :     AssertLockHeld(cs);
     571                 :        3036 :     const auto i = mapTx.find(txid);
     572         [ +  + ]:        3036 :     return i == mapTx.end() ? nullptr : &(*i);
     573                 :             : }
     574                 :             : 
     575                 :      213922 : CTransactionRef CTxMemPool::get(const Txid& hash) const
     576                 :             : {
     577                 :      213922 :     LOCK(cs);
     578         [ +  - ]:      213922 :     indexed_transaction_set::const_iterator i = mapTx.find(hash);
     579         [ +  + ]:      213922 :     if (i == mapTx.end())
     580                 :      153327 :         return nullptr;
     581   [ +  -  +  - ]:      274517 :     return i->GetSharedTx();
     582                 :      213922 : }
     583                 :             : 
     584                 :         767 : void CTxMemPool::PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta)
     585                 :             : {
     586                 :         767 :     {
     587                 :         767 :         LOCK(cs);
     588         [ +  - ]:         767 :         CAmount &delta = mapDeltas[hash];
     589                 :         767 :         delta = SaturatingAdd(delta, nFeeDelta);
     590         [ +  - ]:         767 :         txiter it = mapTx.find(hash);
     591         [ +  + ]:         767 :         if (it != mapTx.end()) {
     592                 :             :             // PrioritiseTransaction calls stack on previous ones. Set the new
     593                 :             :             // transaction fee to be current modified fee + feedelta.
     594         [ +  - ]:         261 :             mapTx.modify(it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(nFeeDelta); });
     595                 :         261 :             m_txgraph->SetTransactionFee(*it, it->GetModifiedFee());
     596                 :         261 :             ++nTransactionsUpdated;
     597                 :             :         }
     598         [ +  + ]:         767 :         if (delta == 0) {
     599                 :           9 :             mapDeltas.erase(hash);
     600   [ +  +  +  -  :          25 :             LogPrintf("PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.ToString(), it == mapTx.end() ? "not " : "");
                   +  - ]
     601                 :             :         } else {
     602   [ +  -  +  -  :        1770 :             LogPrintf("PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",
          +  +  +  -  +  
                      - ]
     603                 :             :                       hash.ToString(),
     604                 :             :                       it == mapTx.end() ? "not " : "",
     605                 :             :                       FormatMoney(nFeeDelta),
     606                 :             :                       FormatMoney(delta));
     607                 :             :         }
     608                 :         767 :     }
     609                 :         767 : }
     610                 :             : 
     611                 :       63327 : void CTxMemPool::ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const
     612                 :             : {
     613                 :       63327 :     AssertLockHeld(cs);
     614                 :       63327 :     std::map<Txid, CAmount>::const_iterator pos = mapDeltas.find(hash);
     615         [ +  + ]:       63327 :     if (pos == mapDeltas.end())
     616                 :             :         return;
     617                 :          38 :     const CAmount &delta = pos->second;
     618                 :          38 :     nFeeDelta += delta;
     619                 :             : }
     620                 :             : 
     621                 :       57715 : void CTxMemPool::ClearPrioritisation(const Txid& hash)
     622                 :             : {
     623                 :       57715 :     AssertLockHeld(cs);
     624                 :       57715 :     mapDeltas.erase(hash);
     625                 :       57715 : }
     626                 :             : 
     627                 :          28 : std::vector<CTxMemPool::delta_info> CTxMemPool::GetPrioritisedTransactions() const
     628                 :             : {
     629                 :          28 :     AssertLockNotHeld(cs);
     630                 :          28 :     LOCK(cs);
     631                 :          28 :     std::vector<delta_info> result;
     632         [ +  - ]:          28 :     result.reserve(mapDeltas.size());
     633   [ +  -  +  + ]:          56 :     for (const auto& [txid, delta] : mapDeltas) {
     634         [ +  - ]:          28 :         const auto iter{mapTx.find(txid)};
     635         [ +  + ]:          28 :         const bool in_mempool{iter != mapTx.end()};
     636                 :          28 :         std::optional<CAmount> modified_fee;
     637         [ +  + ]:          28 :         if (in_mempool) modified_fee = iter->GetModifiedFee();
     638         [ +  - ]:          28 :         result.emplace_back(delta_info{in_mempool, delta, modified_fee, txid});
     639                 :             :     }
     640         [ +  - ]:          28 :     return result;
     641                 :          28 : }
     642                 :             : 
     643                 :      113605 : const CTransaction* CTxMemPool::GetConflictTx(const COutPoint& prevout) const
     644                 :             : {
     645                 :      113605 :     const auto it = mapNextTx.find(prevout);
     646         [ +  + ]:      113605 :     return it == mapNextTx.end() ? nullptr : &(it->second->GetTx());
     647                 :             : }
     648                 :             : 
     649                 :    15595988 : std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Txid& txid) const
     650                 :             : {
     651                 :    15595988 :     AssertLockHeld(cs);
     652                 :    15595988 :     auto it = mapTx.find(txid);
     653         [ +  + ]:    15595988 :     return it != mapTx.end() ? std::make_optional(it) : std::nullopt;
     654                 :             : }
     655                 :             : 
     656                 :    23703718 : std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Wtxid& wtxid) const
     657                 :             : {
     658                 :    23703718 :     AssertLockHeld(cs);
     659         [ +  + ]:    23703718 :     auto it{mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid))};
     660         [ +  + ]:    23703718 :     return it != mapTx.end() ? std::make_optional(it) : std::nullopt;
     661                 :             : }
     662                 :             : 
     663                 :       33118 : CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<Txid>& hashes) const
     664                 :             : {
     665                 :       33118 :     CTxMemPool::setEntries ret;
     666         [ +  + ]:       35386 :     for (const auto& h : hashes) {
     667         [ +  - ]:        2268 :         const auto mi = GetIter(h);
     668   [ +  -  +  - ]:        2268 :         if (mi) ret.insert(*mi);
     669                 :             :     }
     670                 :       33118 :     return ret;
     671                 :           0 : }
     672                 :             : 
     673                 :           2 : std::vector<CTxMemPool::txiter> CTxMemPool::GetIterVec(const std::vector<Txid>& txids) const
     674                 :             : {
     675                 :           2 :     AssertLockHeld(cs);
     676                 :           2 :     std::vector<txiter> ret;
     677   [ -  +  +  - ]:           2 :     ret.reserve(txids.size());
     678         [ +  + ]:         565 :     for (const auto& txid : txids) {
     679         [ +  - ]:         563 :         const auto it{GetIter(txid)};
     680         [ -  + ]:         563 :         if (!it) return {};
     681         [ +  - ]:         563 :         ret.push_back(*it);
     682                 :             :     }
     683                 :           2 :     return ret;
     684                 :           2 : }
     685                 :             : 
     686                 :       25375 : bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
     687                 :             : {
     688   [ -  +  +  + ]:       58263 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     689         [ +  + ]:       36326 :         if (exists(tx.vin[i].prevout.hash))
     690                 :             :             return false;
     691                 :             :     return true;
     692                 :             : }
     693                 :             : 
     694   [ +  -  +  - ]:       41179 : CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
     695                 :             : 
     696                 :       65063 : std::optional<Coin> CCoinsViewMemPool::GetCoin(const COutPoint& outpoint) const
     697                 :             : {
     698                 :             :     // Check to see if the inputs are made available by another tx in the package.
     699                 :             :     // These Coins would not be available in the underlying CoinsView.
     700         [ +  + ]:       65063 :     if (auto it = m_temp_added.find(outpoint); it != m_temp_added.end()) {
     701                 :         594 :         return it->second;
     702                 :             :     }
     703                 :             : 
     704                 :             :     // If an entry in the mempool exists, always return that one, as it's guaranteed to never
     705                 :             :     // conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
     706                 :             :     // transactions. First checking the underlying cache risks returning a pruned entry instead.
     707                 :       64469 :     CTransactionRef ptx = mempool.get(outpoint.hash);
     708         [ +  + ]:       64469 :     if (ptx) {
     709   [ -  +  +  - ]:        8438 :         if (outpoint.n < ptx->vout.size()) {
     710                 :        8438 :             Coin coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
     711         [ +  - ]:        8438 :             m_non_base_coins.emplace(outpoint);
     712                 :        8438 :             return coin;
     713                 :        8438 :         }
     714                 :           0 :         return std::nullopt;
     715                 :             :     }
     716         [ +  - ]:       56031 :     return base->GetCoin(outpoint);
     717                 :       64469 : }
     718                 :             : 
     719                 :         745 : void CCoinsViewMemPool::PackageAddTransaction(const CTransactionRef& tx)
     720                 :             : {
     721   [ -  +  +  + ]:        1521 :     for (unsigned int n = 0; n < tx->vout.size(); ++n) {
     722         [ +  - ]:         776 :         m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
     723                 :         776 :         m_non_base_coins.emplace(tx->GetHash(), n);
     724                 :             :     }
     725                 :         745 : }
     726                 :       64521 : void CCoinsViewMemPool::Reset()
     727                 :             : {
     728                 :       64521 :     m_temp_added.clear();
     729                 :       64521 :     m_non_base_coins.clear();
     730                 :       64521 : }
     731                 :             : 
     732                 :      524888 : size_t CTxMemPool::DynamicMemoryUsage() const {
     733                 :      524888 :     LOCK(cs);
     734                 :             :     // Estimate the overhead of mapTx to be 9 pointers (3 pointers per index) + an allocation, as no exact formula for boost::multi_index_contained is implemented.
     735   [ -  +  +  - ]:     1049776 :     return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 9 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(txns_randomized) + m_txgraph->GetMainMemoryUsage() + cachedInnerUsage;
     736                 :      524888 : }
     737                 :             : 
     738                 :       61508 : void CTxMemPool::RemoveUnbroadcastTx(const Txid& txid, const bool unchecked) {
     739                 :       61508 :     LOCK(cs);
     740                 :             : 
     741         [ +  + ]:       61508 :     if (m_unbroadcast_txids.erase(txid))
     742                 :             :     {
     743   [ +  -  +  -  :       29073 :         LogDebug(BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n", txid.GetHex(), (unchecked ? " before confirmation that txn was sent out" : ""));
          +  +  +  -  +  
                      - ]
     744                 :             :     }
     745                 :       61508 : }
     746                 :             : 
     747                 :      147402 : void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) {
     748                 :      147402 :     AssertLockHeld(cs);
     749         [ +  + ]:      195491 :     for (txiter it : stage) {
     750                 :       48089 :         removeUnchecked(it, reason);
     751                 :             :     }
     752                 :      147402 : }
     753                 :             : 
     754                 :        3385 : bool CTxMemPool::CheckPolicyLimits(const CTransactionRef& tx)
     755                 :             : {
     756                 :        3385 :     LOCK(cs);
     757                 :             :     // Use ChangeSet interface to check whether the chain
     758                 :             :     // limits would be violated. Note that the changeset will be destroyed
     759                 :             :     // when it goes out of scope.
     760         [ +  - ]:        3385 :     auto changeset = GetChangeSet();
     761         [ +  - ]:        3385 :     (void) changeset->StageAddition(tx, /*fee=*/0, /*time=*/0, /*entry_height=*/0, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/0, LockPoints{});
     762         [ +  - ]:        3385 :     return changeset->CheckMemPoolPolicyLimits();
     763         [ +  - ]:        6770 : }
     764                 :             : 
     765                 :       27652 : int CTxMemPool::Expire(std::chrono::seconds time)
     766                 :             : {
     767                 :       27652 :     AssertLockHeld(cs);
     768                 :       27652 :     Assume(!m_have_changeset);
     769                 :       27652 :     indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin();
     770                 :       27652 :     setEntries toremove;
     771   [ +  +  +  + ]:       27656 :     while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) {
     772         [ +  - ]:           4 :         toremove.insert(mapTx.project<0>(it));
     773                 :           4 :         it++;
     774                 :             :     }
     775                 :       27652 :     setEntries stage;
     776         [ +  + ]:       27656 :     for (txiter removeit : toremove) {
     777         [ +  - ]:           4 :         CalculateDescendants(removeit, stage);
     778                 :             :     }
     779         [ +  - ]:       27652 :     RemoveStaged(stage, false, MemPoolRemovalReason::EXPIRY);
     780                 :       27652 :     return stage.size();
     781                 :       27652 : }
     782                 :             : 
     783                 :      473418 : CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
     784                 :      473418 :     LOCK(cs);
     785   [ +  +  +  + ]:      473418 :     if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
     786                 :      473354 :         return CFeeRate(llround(rollingMinimumFeeRate));
     787                 :             : 
     788         [ +  - ]:          64 :     int64_t time = GetTime();
     789         [ +  + ]:          64 :     if (time > lastRollingFeeUpdate + 10) {
     790                 :           6 :         double halflife = ROLLING_FEE_HALFLIFE;
     791   [ +  -  +  + ]:           6 :         if (DynamicMemoryUsage() < sizelimit / 4)
     792                 :             :             halflife /= 4;
     793   [ +  -  +  + ]:           5 :         else if (DynamicMemoryUsage() < sizelimit / 2)
     794                 :           1 :             halflife /= 2;
     795                 :             : 
     796                 :           6 :         rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
     797                 :           6 :         lastRollingFeeUpdate = time;
     798                 :             : 
     799         [ +  + ]:           6 :         if (rollingMinimumFeeRate < (double)m_opts.incremental_relay_feerate.GetFeePerK() / 2) {
     800                 :           1 :             rollingMinimumFeeRate = 0;
     801                 :           1 :             return CFeeRate(0);
     802                 :             :         }
     803                 :             :     }
     804                 :          63 :     return std::max(CFeeRate(llround(rollingMinimumFeeRate)), m_opts.incremental_relay_feerate);
     805                 :      473418 : }
     806                 :             : 
     807                 :          61 : void CTxMemPool::trackPackageRemoved(const CFeeRate& rate) {
     808                 :          61 :     AssertLockHeld(cs);
     809         [ +  + ]:          61 :     if (rate.GetFeePerK() > rollingMinimumFeeRate) {
     810                 :          59 :         rollingMinimumFeeRate = rate.GetFeePerK();
     811                 :          59 :         blockSinceLastRollingFeeBump = false;
     812                 :             :     }
     813                 :          61 : }
     814                 :             : 
     815                 :       27661 : void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining) {
     816                 :       27661 :     AssertLockHeld(cs);
     817                 :       27661 :     Assume(!m_have_changeset);
     818                 :             : 
     819                 :       27661 :     unsigned nTxnRemoved = 0;
     820                 :       27661 :     CFeeRate maxFeeRateRemoved(0);
     821                 :             : 
     822   [ +  +  +  + ]:       27722 :     while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
     823         [ +  - ]:          61 :         const auto &[worst_chunk, feeperweight] = m_txgraph->GetWorstMainChunk();
     824         [ +  - ]:          61 :         FeePerVSize feerate = ToFeePerVSize(feeperweight);
     825         [ +  - ]:          61 :         CFeeRate removed{feerate.fee, feerate.size};
     826                 :             : 
     827                 :             :         // We set the new mempool min fee to the feerate of the removed set, plus the
     828                 :             :         // "minimum reasonable fee rate" (ie some value under which we consider txn
     829                 :             :         // to have 0 fee). This way, we don't allow txn to enter mempool with feerate
     830                 :             :         // equal to txn which were removed with no block in between.
     831                 :          61 :         removed += m_opts.incremental_relay_feerate;
     832         [ +  - ]:          61 :         trackPackageRemoved(removed);
     833                 :          61 :         maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
     834                 :             : 
     835         [ -  + ]:          61 :         nTxnRemoved += worst_chunk.size();
     836                 :             : 
     837                 :          61 :         std::vector<CTransaction> txn;
     838         [ +  + ]:          61 :         if (pvNoSpendsRemaining) {
     839         [ +  - ]:          53 :             txn.reserve(worst_chunk.size());
     840         [ +  + ]:         107 :             for (auto ref : worst_chunk) {
     841         [ +  - ]:          54 :                 txn.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref).GetTx());
     842                 :             :             }
     843                 :             :         }
     844                 :             : 
     845                 :          61 :         setEntries stage;
     846         [ +  + ]:         128 :         for (auto ref : worst_chunk) {
     847         [ +  - ]:          67 :             stage.insert(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref)));
     848                 :             :         }
     849         [ +  + ]:         128 :         for (auto e : stage) {
     850         [ +  - ]:          67 :             removeUnchecked(e, MemPoolRemovalReason::SIZELIMIT);
     851                 :             :         }
     852         [ +  + ]:          61 :         if (pvNoSpendsRemaining) {
     853         [ +  + ]:         107 :             for (const CTransaction& tx : txn) {
     854         [ +  + ]:         108 :                 for (const CTxIn& txin : tx.vin) {
     855   [ +  -  +  + ]:          54 :                     if (exists(txin.prevout.hash)) continue;
     856         [ +  - ]:          53 :                     pvNoSpendsRemaining->push_back(txin.prevout);
     857                 :             :                 }
     858                 :             :             }
     859                 :             :         }
     860                 :         122 :     }
     861                 :             : 
     862         [ +  + ]:       27661 :     if (maxFeeRateRemoved > CFeeRate(0)) {
     863   [ +  -  +  - ]:          96 :         LogDebug(BCLog::MEMPOOL, "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString());
     864                 :             :     }
     865                 :       27661 : }
     866                 :             : 
     867                 :      124550 : std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateAncestorData(const CTxMemPoolEntry& entry) const
     868                 :             : {
     869                 :      124550 :     auto ancestors = m_txgraph->GetAncestors(entry, TxGraph::Level::MAIN);
     870                 :             : 
     871         [ -  + ]:      124550 :     size_t ancestor_count = ancestors.size();
     872                 :      124550 :     size_t ancestor_size = 0;
     873                 :      124550 :     CAmount ancestor_fees = 0;
     874         [ +  + ]:      444377 :     for (auto tx: ancestors) {
     875                 :      319827 :         const CTxMemPoolEntry& anc = static_cast<const CTxMemPoolEntry&>(*tx);
     876         [ +  - ]:      319827 :         ancestor_size += anc.GetTxSize();
     877                 :      319827 :         ancestor_fees += anc.GetModifiedFee();
     878                 :             :     }
     879                 :      124550 :     return {ancestor_count, ancestor_size, ancestor_fees};
     880                 :      124550 : }
     881                 :             : 
     882                 :        8684 : std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateDescendantData(const CTxMemPoolEntry& entry) const
     883                 :             : {
     884                 :        8684 :     auto descendants = m_txgraph->GetDescendants(entry, TxGraph::Level::MAIN);
     885         [ -  + ]:        8684 :     size_t descendant_count = descendants.size();
     886                 :        8684 :     size_t descendant_size = 0;
     887                 :        8684 :     CAmount descendant_fees = 0;
     888                 :             : 
     889         [ +  + ]:      163147 :     for (auto tx: descendants) {
     890                 :      154463 :         const CTxMemPoolEntry &desc = static_cast<const CTxMemPoolEntry&>(*tx);
     891         [ +  - ]:      154463 :         descendant_size += desc.GetTxSize();
     892                 :      154463 :         descendant_fees += desc.GetModifiedFee();
     893                 :             :     }
     894                 :        8684 :     return {descendant_count, descendant_size, descendant_fees};
     895                 :        8684 : }
     896                 :             : 
     897                 :      580385 : void CTxMemPool::GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* const ancestorsize, CAmount* const ancestorfees) const {
     898                 :      580385 :     LOCK(cs);
     899         [ +  - ]:      580385 :     auto it = mapTx.find(txid);
     900                 :      580385 :     ancestors = cluster_count = 0;
     901         [ +  + ]:      580385 :     if (it != mapTx.end()) {
     902   [ +  -  +  + ]:       47766 :         auto [ancestor_count, ancestor_size, ancestor_fees] = CalculateAncestorData(*it);
     903                 :       47766 :         ancestors = ancestor_count;
     904         [ +  + ]:       47766 :         if (ancestorsize) *ancestorsize = ancestor_size;
     905         [ +  + ]:       47766 :         if (ancestorfees) *ancestorfees = ancestor_fees;
     906         [ -  + ]:       47766 :         cluster_count = m_txgraph->GetCluster(*it, TxGraph::Level::MAIN).size();
     907                 :             :     }
     908                 :      580385 : }
     909                 :             : 
     910                 :        2410 : bool CTxMemPool::GetLoadTried() const
     911                 :             : {
     912                 :        2410 :     LOCK(cs);
     913         [ +  - ]:        2410 :     return m_load_tried;
     914                 :        2410 : }
     915                 :             : 
     916                 :        1020 : void CTxMemPool::SetLoadTried(bool load_tried)
     917                 :             : {
     918                 :        1020 :     LOCK(cs);
     919         [ +  - ]:        1020 :     m_load_tried = load_tried;
     920                 :        1020 : }
     921                 :             : 
     922                 :        3049 : std::vector<CTxMemPool::txiter> CTxMemPool::GatherClusters(const std::vector<Txid>& txids) const
     923                 :             : {
     924                 :        3049 :     AssertLockHeld(cs);
     925                 :             : 
     926                 :        3049 :     std::vector<CTxMemPool::txiter> ret;
     927                 :        3049 :     std::set<const CTxMemPoolEntry*> unique_cluster_representatives;
     928         [ +  + ]:       52691 :     for (auto txid : txids) {
     929         [ +  - ]:       49642 :         auto it = mapTx.find(txid);
     930         [ +  - ]:       49642 :         if (it != mapTx.end()) {
     931                 :       49642 :             auto cluster = m_txgraph->GetCluster(*it, TxGraph::Level::MAIN);
     932   [ +  -  +  + ]:       49642 :             if (unique_cluster_representatives.insert(static_cast<const CTxMemPoolEntry*>(&(**cluster.begin()))).second) {
     933         [ +  + ]:      118947 :                 for (auto tx : cluster) {
     934         [ +  - ]:       69466 :                     ret.emplace_back(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx)));
     935                 :             :                 }
     936                 :             :             }
     937                 :       49642 :         }
     938                 :             :     }
     939   [ -  +  +  + ]:        3049 :     if (ret.size() > 500) {
     940                 :           1 :         return {};
     941                 :             :     }
     942                 :        3048 :     return ret;
     943                 :        3049 : }
     944                 :             : 
     945                 :        1280 : util::Result<std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>>> CTxMemPool::ChangeSet::CalculateChunksForRBF()
     946                 :             : {
     947                 :        1280 :     LOCK(m_pool->cs);
     948                 :             : 
     949   [ +  -  -  + ]:        1280 :     if (!CheckMemPoolPolicyLimits()) {
     950   [ #  #  #  # ]:           0 :         return util::Error{Untranslated("cluster size limit exceeded")};
     951                 :             :     }
     952                 :             : 
     953                 :        2560 :     return m_pool->m_txgraph->GetMainStagingDiagrams();
     954                 :        1280 : }
     955                 :             : 
     956                 :       63327 : CTxMemPool::ChangeSet::TxHandle CTxMemPool::ChangeSet::StageAddition(const CTransactionRef& tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
     957                 :             : {
     958                 :       63327 :     LOCK(m_pool->cs);
     959   [ +  -  +  - ]:       63327 :     Assume(m_to_add.find(tx->GetHash()) == m_to_add.end());
     960                 :       63327 :     Assume(!m_dependencies_processed);
     961                 :             : 
     962                 :             :     // We need to process dependencies after adding a new transaction.
     963                 :       63327 :     m_dependencies_processed = false;
     964                 :             : 
     965                 :       63327 :     CAmount delta{0};
     966         [ +  - ]:       63327 :     m_pool->ApplyDelta(tx->GetHash(), delta);
     967                 :             : 
     968         [ +  - ]:       63327 :     TxGraph::Ref ref(m_pool->m_txgraph->AddTransaction(FeePerWeight(fee, GetSigOpsAdjustedWeight(GetTransactionWeight(*tx), sigops_cost, ::nBytesPerSigOp))));
     969         [ +  - ]:       63327 :     auto newit = m_to_add.emplace(std::move(ref), tx, fee, time, entry_height, entry_sequence, spends_coinbase, sigops_cost, lp).first;
     970         [ +  + ]:       63327 :     if (delta) {
     971         [ +  - ]:          38 :         m_to_add.modify(newit, [&delta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(delta); });
     972                 :          38 :         m_pool->m_txgraph->SetTransactionFee(*newit, newit->GetModifiedFee());
     973                 :             :     }
     974                 :             : 
     975         [ +  - ]:       63327 :     m_entry_vec.push_back(newit);
     976                 :             : 
     977                 :      126654 :     return newit;
     978         [ +  - ]:      126654 : }
     979                 :             : 
     980                 :        2122 : void CTxMemPool::ChangeSet::StageRemoval(CTxMemPool::txiter it)
     981                 :             : {
     982                 :        2122 :     LOCK(m_pool->cs);
     983                 :        2122 :     m_pool->m_txgraph->RemoveTransaction(*it);
     984         [ +  - ]:        2122 :     m_to_remove.insert(it);
     985                 :        2122 : }
     986                 :             : 
     987                 :       51904 : void CTxMemPool::ChangeSet::Apply()
     988                 :             : {
     989                 :       51904 :     LOCK(m_pool->cs);
     990         [ +  + ]:       51904 :     if (!m_dependencies_processed) {
     991         [ +  - ]:           3 :         ProcessDependencies();
     992                 :             :     }
     993         [ +  - ]:       51904 :     m_pool->Apply(this);
     994                 :       51904 :     m_to_add.clear();
     995                 :       51904 :     m_to_remove.clear();
     996         [ +  - ]:       51904 :     m_entry_vec.clear();
     997         [ +  - ]:       51904 :     m_ancestors.clear();
     998                 :       51904 : }
     999                 :             : 
    1000                 :       62414 : void CTxMemPool::ChangeSet::ProcessDependencies()
    1001                 :             : {
    1002                 :       62414 :     LOCK(m_pool->cs);
    1003                 :       62414 :     Assume(!m_dependencies_processed); // should only call this once.
    1004         [ +  + ]:      125400 :     for (const auto& entryptr : m_entry_vec) {
    1005   [ +  -  +  -  :      278646 :         for (const auto &txin : entryptr->GetSharedTx()->vin) {
                   +  + ]
    1006         [ +  - ]:       89688 :             std::optional<txiter> piter = m_pool->GetIter(txin.prevout.hash);
    1007         [ +  + ]:       89688 :             if (!piter) {
    1008         [ +  - ]:       79747 :                 auto it = m_to_add.find(txin.prevout.hash);
    1009         [ +  + ]:       79747 :                 if (it != m_to_add.end()) {
    1010                 :         565 :                     piter = std::make_optional(it);
    1011                 :             :                 }
    1012                 :             :             }
    1013         [ +  + ]:       89688 :             if (piter) {
    1014                 :       10506 :                 m_pool->m_txgraph->AddDependency(/*parent=*/**piter, /*child=*/*entryptr);
    1015                 :             :             }
    1016                 :             :         }
    1017                 :             :     }
    1018                 :       62414 :     m_dependencies_processed = true;
    1019         [ +  - ]:       62414 :     return;
    1020                 :       62414 :  }
    1021                 :             : 
    1022                 :       64942 : bool CTxMemPool::ChangeSet::CheckMemPoolPolicyLimits()
    1023                 :             : {
    1024                 :       64942 :     LOCK(m_pool->cs);
    1025         [ +  + ]:       64942 :     if (!m_dependencies_processed) {
    1026         [ +  - ]:       62411 :         ProcessDependencies();
    1027                 :             :     }
    1028                 :             : 
    1029         [ +  - ]:       64942 :     return !m_pool->m_txgraph->IsOversized(TxGraph::Level::TOP);
    1030                 :       64942 : }
    1031                 :             : 
    1032                 :      154179 : std::vector<FeePerWeight> CTxMemPool::GetFeerateDiagram() const
    1033                 :             : {
    1034                 :      154179 :     FeePerWeight zero{};
    1035                 :      154179 :     std::vector<FeePerWeight> ret;
    1036                 :             : 
    1037         [ +  - ]:      154179 :     ret.emplace_back(zero);
    1038                 :             : 
    1039                 :      154179 :     StartBlockBuilding();
    1040                 :             : 
    1041                 :      154179 :     std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> dummy;
    1042                 :             : 
    1043         [ +  - ]:      154179 :     FeePerWeight last_selection = GetBlockBuilderChunk(dummy);
    1044         [ +  + ]:    11980615 :     while (last_selection != FeePerWeight{}) {
    1045         [ +  - ]:    11826436 :         last_selection += ret.back();
    1046         [ +  - ]:    11826436 :         ret.emplace_back(last_selection);
    1047                 :    11826436 :         IncludeBuilderChunk();
    1048         [ +  - ]:    11826436 :         last_selection = GetBlockBuilderChunk(dummy);
    1049                 :             :     }
    1050                 :      154179 :     StopBlockBuilding();
    1051                 :      154179 :     return ret;
    1052                 :      154179 : }
        

Generated by: LCOV version 2.0-1