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