LCOV - code coverage report
Current view: top level - src/test/util - txmempool.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 32 60 53.3 %
Date: 2023-11-12 01:39:15 Functions: 2 6 33.3 %
Branches: 27 84 32.1 %

           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 <txmempool.h>
      11                 :            : #include <util/check.h>
      12                 :            : #include <util/time.h>
      13                 :            : #include <util/translation.h>
      14                 :            : #include <validation.h>
      15                 :            : 
      16                 :            : using node::NodeContext;
      17         [ +  - ]:          2 : 
      18         [ +  - ]:        545 : CTxMemPool::Options MemPoolOptionsForTest(const NodeContext& node)
      19                 :            : {
      20                 :       3801 :     CTxMemPool::Options mempool_opts{
      21                 :        543 :         .estimator = node.fee_estimator.get(),
      22                 :            :         // Default to always checking mempool regardless of
      23                 :            :         // chainparams.DefaultConsistencyChecks for tests
      24                 :            :         .check_ratio = 1,
      25                 :            :     };
      26                 :        543 :     const auto result{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
      27         [ +  - ]:        543 :     Assert(result);
      28                 :            :     return mempool_opts;
      29                 :        543 : }
      30                 :            : 
      31                 :          0 : CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
      32                 :            : {
      33         [ #  # ]:          0 :     return FromTx(MakeTransactionRef(tx));
      34                 :          0 : }
      35                 :            : 
      36                 :          0 : CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx) const
      37                 :            : {
      38                 :          0 :     return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
      39                 :            : }
      40                 :            : 
      41                 :       8153 : std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
      42                 :            :                                                            const PackageMempoolAcceptResult& result,
      43                 :        543 :                                                            bool expect_valid,
      44                 :        543 :                                                            const CTxMemPool* mempool)
      45                 :            : {
      46         [ +  + ]:       8696 :     if (expect_valid) {
      47         [ -  + ]:        948 :         if (result.m_state.IsInvalid()) {
      48         [ #  # ]:          0 :             return strprintf("Package validation unexpectedly failed: %s", result.m_state.ToString());
      49                 :            :         }
      50                 :        405 :     } else {
      51         [ -  + ]:       7748 :         if (result.m_state.IsValid()) {
      52         [ #  # ]:          0 :             return strprintf("Package validation unexpectedly succeeded. %s", result.m_state.ToString());
      53                 :            :         }
      54                 :            :     }
      55 [ +  - ][ +  - ]:       8696 :     if (result.m_state.GetResult() != PackageValidationResult::PCKG_POLICY && txns.size() != result.m_tx_results.size()) {
                 [ +  - ]
      56                 :          0 :         return strprintf("txns size %u does not match tx results size %u", txns.size(), result.m_tx_results.size());
      57                 :            :     }
      58         [ +  + ]:      24882 :     for (const auto& tx : txns) {
      59                 :      17272 :         const auto& wtxid = tx->GetWitnessHash();
      60         [ +  - ]:      16729 :         if (result.m_tx_results.count(wtxid) == 0) {
      61         [ #  # ]:          0 :             return strprintf("result not found for tx %s", wtxid.ToString());
      62                 :            :         }
      63                 :            : 
      64                 :      16729 :         const auto& atmp_result = result.m_tx_results.at(wtxid);
      65                 :      16729 :         const bool valid{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID};
      66 [ +  + ][ +  - ]:      16729 :         if (expect_valid && atmp_result.m_state.IsInvalid()) {
      67 [ #  # ][ #  # ]:          0 :             return strprintf("tx %s unexpectedly failed: %s", wtxid.ToString(), atmp_result.m_state.ToString());
      68                 :            :         }
      69                 :            : 
      70                 :            :         //m_replaced_transactions should exist iff the result was VALID
      71         [ +  - ]:      16729 :         if (atmp_result.m_replaced_transactions.has_value() != valid) {
      72         [ #  # ]:          0 :             return strprintf("tx %s result should %shave m_replaced_transactions",
      73                 :          0 :                                     wtxid.ToString(), valid ? "" : "not ");
      74                 :            :         }
      75                 :            : 
      76                 :            :         // m_vsize and m_base_fees should exist iff the result was VALID or MEMPOOL_ENTRY
      77                 :      16729 :         const bool mempool_entry{atmp_result.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY};
      78 [ +  + ][ +  - ]:      16729 :         if (atmp_result.m_base_fees.has_value() != (valid || mempool_entry)) {
      79 [ #  # ][ #  # ]:          0 :             return strprintf("tx %s result should %shave m_base_fees", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
      80                 :            :         }
      81 [ +  + ][ +  - ]:      16729 :         if (atmp_result.m_vsize.has_value() != (valid || mempool_entry)) {
      82 [ #  # ][ #  # ]:          0 :             return strprintf("tx %s result should %shave m_vsize", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
      83                 :            :         }
      84                 :            : 
      85                 :            :         // m_other_wtxid should exist iff the result was DIFFERENT_WITNESS
      86                 :      16729 :         const bool diff_witness{atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS};
      87         [ +  - ]:      16729 :         if (atmp_result.m_other_wtxid.has_value() != diff_witness) {
      88         [ #  # ]:          0 :             return strprintf("tx %s result should %shave m_other_wtxid", wtxid.ToString(), diff_witness ? "" : "not ");
      89                 :            :         }
      90                 :            : 
      91                 :            :         // m_effective_feerate and m_wtxids_fee_calculations should exist iff the result was valid
      92         [ +  - ]:      16729 :         if (atmp_result.m_effective_feerate.has_value() != valid) {
      93         [ #  # ]:          0 :             return strprintf("tx %s result should %shave m_effective_feerate",
      94                 :          0 :                                     wtxid.ToString(), valid ? "" : "not ");
      95                 :            :         }
      96         [ +  - ]:      16729 :         if (atmp_result.m_wtxids_fee_calculations.has_value() != valid) {
      97         [ #  # ]:          0 :             return strprintf("tx %s result should %shave m_effective_feerate",
      98                 :          0 :                                     wtxid.ToString(), valid ? "" : "not ");
      99                 :            :         }
     100                 :            : 
     101         [ +  - ]:      16729 :         if (mempool) {
     102                 :            :             // The tx by txid should be in the mempool iff the result was not INVALID.
     103                 :          0 :             const bool txid_in_mempool{atmp_result.m_result_type != MempoolAcceptResult::ResultType::INVALID};
     104         [ #  # ]:          0 :             if (mempool->exists(GenTxid::Txid(tx->GetHash())) != txid_in_mempool) {
     105         [ #  # ]:          0 :                 return strprintf("tx %s should %sbe in mempool", wtxid.ToString(), txid_in_mempool ? "" : "not ");
     106                 :            :             }
     107                 :            :             // Additionally, if the result was DIFFERENT_WITNESS, we shouldn't be able to find the tx in mempool by wtxid.
     108 [ #  # ][ #  # ]:          0 :             if (tx->HasWitness() && atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS) {
     109         [ #  # ]:          0 :                 if (mempool->exists(GenTxid::Wtxid(wtxid))) {
     110         [ #  # ]:          0 :                     return strprintf("wtxid %s should not be in mempool", wtxid.ToString());
     111                 :            :                 }
     112                 :          0 :             }
     113                 :          0 :         }
     114                 :            :     }
     115                 :       8153 :     return std::nullopt;
     116                 :       8153 : }

Generated by: LCOV version 1.14