LCOV - code coverage report
Current view: top level - src/test/util - txmempool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 81.4 % 113 92
Test Date: 2024-12-04 04:00:22 Functions: 85.7 % 7 6
Branches: 50.4 % 238 120

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <test/util/txmempool.h>
       6                 :             : 
       7                 :             : #include <chainparams.h>
       8                 :             : #include <node/context.h>
       9                 :             : #include <node/mempool_args.h>
      10                 :             : #include <policy/rbf.h>
      11                 :             : #include <policy/truc_policy.h>
      12                 :             : #include <txmempool.h>
      13                 :             : #include <util/check.h>
      14                 :             : #include <util/time.h>
      15                 :             : #include <util/translation.h>
      16                 :             : #include <validation.h>
      17                 :             : 
      18                 :             : using node::NodeContext;
      19                 :             : 
      20                 :       13521 : CTxMemPool::Options MemPoolOptionsForTest(const NodeContext& node)
      21                 :             : {
      22                 :       13521 :     CTxMemPool::Options mempool_opts{
      23                 :             :         // Default to always checking mempool regardless of
      24                 :             :         // chainparams.DefaultConsistencyChecks for tests
      25                 :             :         .check_ratio = 1,
      26                 :       13521 :         .signals = node.validation_signals.get(),
      27                 :       13521 :     };
      28                 :       13521 :     const auto result{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
      29         [ +  - ]:       13521 :     Assert(result);
      30                 :       13521 :     return mempool_opts;
      31                 :       13521 : }
      32                 :             : 
      33                 :           0 : CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
      34                 :             : {
      35         [ #  # ]:           0 :     return FromTx(MakeTransactionRef(tx));
      36                 :             : }
      37                 :             : 
      38                 :      151169 : CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
      39                 :             : {
      40                 :      151169 :     return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
      41                 :             : }
      42                 :             : 
      43                 :       53425 : std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
      44                 :             :                                                            const PackageMempoolAcceptResult& result,
      45                 :             :                                                            bool expect_valid,
      46                 :             :                                                            const CTxMemPool* mempool)
      47                 :             : {
      48         [ +  + ]:       53425 :     if (expect_valid) {
      49         [ -  + ]:        6041 :         if (result.m_state.IsInvalid()) {
      50         [ #  # ]:           0 :             return strprintf("Package validation unexpectedly failed: %s", result.m_state.ToString());
      51                 :             :         }
      52                 :             :     } else {
      53         [ -  + ]:       47384 :         if (result.m_state.IsValid()) {
      54         [ #  # ]:           0 :             return strprintf("Package validation unexpectedly succeeded. %s", result.m_state.ToString());
      55                 :             :         }
      56                 :             :     }
      57   [ +  -  -  + ]:       53425 :     if (result.m_state.GetResult() != PackageValidationResult::PCKG_POLICY && txns.size() != result.m_tx_results.size()) {
      58                 :           0 :         return strprintf("txns size %u does not match tx results size %u", txns.size(), result.m_tx_results.size());
      59                 :             :     }
      60         [ +  + ]:      158974 :     for (const auto& tx : txns) {
      61                 :      105549 :         const auto& wtxid = tx->GetWitnessHash();
      62         [ -  + ]:      105549 :         if (result.m_tx_results.count(wtxid) == 0) {
      63         [ #  # ]:           0 :             return strprintf("result not found for tx %s", wtxid.ToString());
      64                 :             :         }
      65                 :             : 
      66                 :      105549 :         const auto& atmp_result = result.m_tx_results.at(wtxid);
      67                 :      105549 :         const bool valid{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID};
      68   [ +  +  +  - ]:      105549 :         if (expect_valid && atmp_result.m_state.IsInvalid()) {
      69   [ #  #  #  # ]:           0 :             return strprintf("tx %s unexpectedly failed: %s", wtxid.ToString(), atmp_result.m_state.ToString());
      70                 :             :         }
      71                 :             : 
      72                 :             :         // Each subpackage is allowed MAX_REPLACEMENT_CANDIDATES replacements (only checking individually here)
      73         [ -  + ]:      105549 :         if (atmp_result.m_replaced_transactions.size() > MAX_REPLACEMENT_CANDIDATES) {
      74                 :           0 :             return strprintf("tx %s result replaced too many transactions",
      75         [ #  # ]:           0 :                                 wtxid.ToString());
      76                 :             :         }
      77                 :             : 
      78                 :             :         // Replacements can't happen for subpackages larger than 2
      79   [ +  +  +  - ]:      105549 :         if (!atmp_result.m_replaced_transactions.empty() &&
      80   [ +  +  +  -  :      105549 :             atmp_result.m_wtxids_fee_calculations.has_value() && atmp_result.m_wtxids_fee_calculations.value().size() > 2) {
                   +  - ]
      81                 :           0 :              return strprintf("tx %s was part of a too-large package RBF subpackage",
      82         [ #  # ]:           0 :                                 wtxid.ToString());
      83                 :             :         }
      84                 :             : 
      85   [ +  +  +  - ]:      105549 :         if (!atmp_result.m_replaced_transactions.empty() && mempool) {
      86                 :        2018 :             LOCK(mempool->cs);
      87                 :             :             // If replacements occurred and it used 2 transactions, this is a package RBF and should result in a cluster of size 2
      88   [ +  -  +  + ]:        2018 :             if (atmp_result.m_wtxids_fee_calculations.has_value() && atmp_result.m_wtxids_fee_calculations.value().size() == 2) {
      89   [ +  -  +  -  :         652 :                 const auto cluster = mempool->GatherClusters({tx->GetHash()});
                   -  + ]
      90   [ -  +  -  -  :         326 :                 if (cluster.size() != 2) return strprintf("tx %s has too many ancestors or descendants for a package rbf", wtxid.ToString());
                   -  - ]
      91         [ -  - ]:         326 :             }
      92                 :        2018 :         }
      93                 :             : 
      94                 :             :         // m_vsize and m_base_fees should exist iff the result was VALID or MEMPOOL_ENTRY
      95                 :      105549 :         const bool mempool_entry{atmp_result.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY};
      96         [ -  + ]:      105549 :         if (atmp_result.m_base_fees.has_value() != (valid || mempool_entry)) {
      97   [ #  #  #  # ]:           0 :             return strprintf("tx %s result should %shave m_base_fees", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
      98                 :             :         }
      99         [ -  + ]:      105549 :         if (atmp_result.m_vsize.has_value() != (valid || mempool_entry)) {
     100   [ #  #  #  # ]:           0 :             return strprintf("tx %s result should %shave m_vsize", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
     101                 :             :         }
     102                 :             : 
     103                 :             :         // m_other_wtxid should exist iff the result was DIFFERENT_WITNESS
     104                 :      105549 :         const bool diff_witness{atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS};
     105         [ -  + ]:      105549 :         if (atmp_result.m_other_wtxid.has_value() != diff_witness) {
     106   [ #  #  #  # ]:           0 :             return strprintf("tx %s result should %shave m_other_wtxid", wtxid.ToString(), diff_witness ? "" : "not ");
     107                 :             :         }
     108                 :             : 
     109                 :             :         // m_effective_feerate and m_wtxids_fee_calculations should exist iff the result was valid
     110                 :             :         // or if the failure was TX_RECONSIDERABLE
     111   [ +  +  +  + ]:      105549 :         const bool valid_or_reconsiderable{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID ||
     112         [ +  + ]:       90772 :                     atmp_result.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
     113         [ -  + ]:      105549 :         if (atmp_result.m_effective_feerate.has_value() != valid_or_reconsiderable) {
     114                 :           0 :             return strprintf("tx %s result should %shave m_effective_feerate",
     115   [ #  #  #  # ]:           0 :                                     wtxid.ToString(), valid ? "" : "not ");
     116                 :             :         }
     117         [ -  + ]:      105549 :         if (atmp_result.m_wtxids_fee_calculations.has_value() != valid_or_reconsiderable) {
     118                 :           0 :             return strprintf("tx %s result should %shave m_effective_feerate",
     119   [ #  #  #  # ]:           0 :                                     wtxid.ToString(), valid ? "" : "not ");
     120                 :             :         }
     121                 :             : 
     122         [ +  - ]:      105549 :         if (mempool) {
     123                 :             :             // The tx by txid should be in the mempool iff the result was not INVALID.
     124                 :      105549 :             const bool txid_in_mempool{atmp_result.m_result_type != MempoolAcceptResult::ResultType::INVALID};
     125         [ -  + ]:      105549 :             if (mempool->exists(GenTxid::Txid(tx->GetHash())) != txid_in_mempool) {
     126   [ #  #  #  # ]:           0 :                 return strprintf("tx %s should %sbe in mempool", wtxid.ToString(), txid_in_mempool ? "" : "not ");
     127                 :             :             }
     128                 :             :             // Additionally, if the result was DIFFERENT_WITNESS, we shouldn't be able to find the tx in mempool by wtxid.
     129   [ +  -  +  + ]:      105549 :             if (tx->HasWitness() && atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS) {
     130         [ -  + ]:          21 :                 if (mempool->exists(GenTxid::Wtxid(wtxid))) {
     131         [ #  # ]:           0 :                     return strprintf("wtxid %s should not be in mempool", wtxid.ToString());
     132                 :             :                 }
     133                 :             :             }
     134         [ +  + ]:      109143 :             for (const auto& tx_ref : atmp_result.m_replaced_transactions) {
     135         [ -  + ]:        3594 :                 if (mempool->exists(GenTxid::Txid(tx_ref->GetHash()))) {
     136         [ #  # ]:           0 :                     return strprintf("tx %s should not be in mempool as it was replaced", tx_ref->GetWitnessHash().ToString());
     137                 :             :                 }
     138                 :             :             }
     139                 :             :         }
     140                 :             :     }
     141                 :       53425 :     return std::nullopt;
     142                 :             : }
     143                 :             : 
     144                 :       97705 : void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool)
     145                 :             : {
     146                 :       97705 :     LOCK(tx_pool.cs);
     147   [ +  -  +  + ]:     2329693 :     for (const auto& tx_info : tx_pool.infoAll()) {
     148   [ +  -  +  - ]:     2231988 :         const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
     149                 :             : 
     150         [ +  - ]:     2231988 :         std::vector<uint32_t> dust_indexes = GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate);
     151                 :             : 
     152         [ +  - ]:     2231988 :         Assert(dust_indexes.size() < 2);
     153                 :             : 
     154         [ +  + ]:     2231988 :         if (dust_indexes.empty()) continue;
     155                 :             : 
     156                 :             :         // Transaction must have no base fee
     157   [ +  -  -  +  :       15308 :         Assert(entry.GetFee() == 0 && entry.GetModifiedFee() == 0);
                   +  - ]
     158                 :             : 
     159                 :             :         // Transaction has single dust; make sure it's swept or will not be mined
     160         [ +  - ]:       15308 :         const auto& children = entry.GetMemPoolChildrenConst();
     161                 :             : 
     162                 :             :         // Multiple children should never happen as non-dust-spending child
     163                 :             :         // can get mined as package
     164         [ +  - ]:       15308 :         Assert(children.size() < 2);
     165                 :             : 
     166         [ +  + ]:       15308 :         if (children.empty()) {
     167                 :             :             // No children and no fees; modified fees aside won't get mined so it's fine
     168                 :             :             // Happens naturally if child spend is RBF cycled away.
     169                 :        5559 :             continue;
     170                 :             :         }
     171                 :             : 
     172                 :             :         // Only-child should be spending the dust
     173         [ +  - ]:        9749 :         const auto& only_child = children.begin()->get().GetTx();
     174         [ +  - ]:        9749 :         COutPoint dust_outpoint{tx_info.tx->GetHash(), dust_indexes[0]};
     175   [ +  +  +  +  :       25351 :         Assert(std::any_of(only_child.vin.begin(), only_child.vin.end(), [&dust_outpoint](const CTxIn& txin) {
          +  +  +  +  +  
          +  +  +  +  -  
                   +  - ]
     176                 :             :             return txin.prevout == dust_outpoint;
     177                 :             :             }));
     178         [ +  - ]:     2329693 :     }
     179                 :       97705 : }
     180                 :             : 
     181                 :      130519 : void CheckMempoolTRUCInvariants(const CTxMemPool& tx_pool)
     182                 :             : {
     183                 :      130519 :     LOCK(tx_pool.cs);
     184   [ +  -  +  + ]:     1019814 :     for (const auto& tx_info : tx_pool.infoAll()) {
     185   [ +  -  +  - ]:      889295 :         const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
     186         [ +  + ]:      889295 :         if (tx_info.tx->version == TRUC_VERSION) {
     187                 :             :             // Check that special maximum virtual size is respected
     188   [ +  -  +  - ]:       85757 :             Assert(entry.GetTxSize() <= TRUC_MAX_VSIZE);
     189                 :             : 
     190                 :             :             // Check that special TRUC ancestor/descendant limits and rules are always respected
     191         [ +  - ]:       85757 :             Assert(entry.GetCountWithDescendants() <= TRUC_DESCENDANT_LIMIT);
     192         [ +  - ]:       85757 :             Assert(entry.GetCountWithAncestors() <= TRUC_ANCESTOR_LIMIT);
     193         [ +  - ]:       85757 :             Assert(entry.GetSizeWithDescendants() <= TRUC_MAX_VSIZE + TRUC_CHILD_MAX_VSIZE);
     194         [ +  - ]:       85757 :             Assert(entry.GetSizeWithAncestors() <= TRUC_MAX_VSIZE + TRUC_CHILD_MAX_VSIZE);
     195                 :             : 
     196                 :             :             // If this transaction has at least 1 ancestor, it's a "child" and has restricted weight.
     197         [ +  + ]:       85757 :             if (entry.GetCountWithAncestors() > 1) {
     198   [ +  -  +  - ]:       15040 :                 Assert(entry.GetTxSize() <= TRUC_CHILD_MAX_VSIZE);
     199                 :             :                 // All TRUC transactions must only have TRUC unconfirmed parents.
     200         [ +  - ]:       15040 :                 const auto& parents = entry.GetMemPoolParentsConst();
     201   [ +  -  +  - ]:       45120 :                 Assert(parents.begin()->get().GetSharedTx()->version == TRUC_VERSION);
     202                 :             :             }
     203         [ +  + ]:      803538 :         } else if (entry.GetCountWithAncestors() > 1) {
     204                 :             :             // All non-TRUC transactions must only have non-TRUC unconfirmed parents.
     205         [ +  + ]:      304637 :             for (const auto& parent : entry.GetMemPoolParentsConst()) {
     206   [ +  -  +  - ]:      466284 :                 Assert(parent.get().GetSharedTx()->version != TRUC_VERSION);
     207                 :             :             }
     208                 :             :         }
     209         [ +  - ]:      130519 :     }
     210                 :      130519 : }
     211                 :             : 
     212                 :      535800 : void AddToMempool(CTxMemPool& tx_pool, const CTxMemPoolEntry& entry)
     213                 :             : {
     214         [ +  - ]:      535800 :     LOCK2(cs_main, tx_pool.cs);
     215         [ +  - ]:      535800 :     auto changeset = tx_pool.GetChangeSet();
     216   [ +  -  +  - ]:     1071600 :     changeset->StageAddition(entry.GetSharedTx(), entry.GetFee(),
     217         [ +  - ]:      535800 :             entry.GetTime().count(), entry.GetHeight(), entry.GetSequence(),
     218         [ +  - ]:      535800 :             entry.GetSpendsCoinbase(), entry.GetSigOpCost(), entry.GetLockPoints());
     219         [ +  - ]:      535800 :     changeset->Apply();
     220   [ +  -  +  - ]:     1607400 : }
        

Generated by: LCOV version 2.0-1