Bitcoin Core  27.99.0
P2P Digital Currency
txmempool.cpp
Go to the documentation of this file.
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/v3_policy.h>
11 #include <txmempool.h>
12 #include <util/check.h>
13 #include <util/time.h>
14 #include <util/translation.h>
15 #include <validation.h>
16 
17 using node::NodeContext;
18 
20 {
21  CTxMemPool::Options mempool_opts{
22  // Default to always checking mempool regardless of
23  // chainparams.DefaultConsistencyChecks for tests
24  .check_ratio = 1,
25  .signals = node.validation_signals.get(),
26  };
27  const auto result{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
28  Assert(result);
29  return mempool_opts;
30 }
31 
33 {
34  return FromTx(MakeTransactionRef(tx));
35 }
36 
38 {
39  return CTxMemPoolEntry{tx, nFee, TicksSinceEpoch<std::chrono::seconds>(time), nHeight, m_sequence, spendsCoinbase, sigOpCost, lp};
40 }
41 
42 std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
43  const PackageMempoolAcceptResult& result,
44  bool expect_valid,
45  const CTxMemPool* mempool)
46 {
47  if (expect_valid) {
48  if (result.m_state.IsInvalid()) {
49  return strprintf("Package validation unexpectedly failed: %s", result.m_state.ToString());
50  }
51  } else {
52  if (result.m_state.IsValid()) {
53  return strprintf("Package validation unexpectedly succeeded. %s", result.m_state.ToString());
54  }
55  }
56  if (result.m_state.GetResult() != PackageValidationResult::PCKG_POLICY && txns.size() != result.m_tx_results.size()) {
57  return strprintf("txns size %u does not match tx results size %u", txns.size(), result.m_tx_results.size());
58  }
59  for (const auto& tx : txns) {
60  const auto& wtxid = tx->GetWitnessHash();
61  if (result.m_tx_results.count(wtxid) == 0) {
62  return strprintf("result not found for tx %s", wtxid.ToString());
63  }
64 
65  const auto& atmp_result = result.m_tx_results.at(wtxid);
66  const bool valid{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID};
67  if (expect_valid && atmp_result.m_state.IsInvalid()) {
68  return strprintf("tx %s unexpectedly failed: %s", wtxid.ToString(), atmp_result.m_state.ToString());
69  }
70 
71  //m_replaced_transactions should exist iff the result was VALID
72  if (atmp_result.m_replaced_transactions.has_value() != valid) {
73  return strprintf("tx %s result should %shave m_replaced_transactions",
74  wtxid.ToString(), valid ? "" : "not ");
75  }
76 
77  // m_vsize and m_base_fees should exist iff the result was VALID or MEMPOOL_ENTRY
78  const bool mempool_entry{atmp_result.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY};
79  if (atmp_result.m_base_fees.has_value() != (valid || mempool_entry)) {
80  return strprintf("tx %s result should %shave m_base_fees", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
81  }
82  if (atmp_result.m_vsize.has_value() != (valid || mempool_entry)) {
83  return strprintf("tx %s result should %shave m_vsize", wtxid.ToString(), valid || mempool_entry ? "" : "not ");
84  }
85 
86  // m_other_wtxid should exist iff the result was DIFFERENT_WITNESS
87  const bool diff_witness{atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS};
88  if (atmp_result.m_other_wtxid.has_value() != diff_witness) {
89  return strprintf("tx %s result should %shave m_other_wtxid", wtxid.ToString(), diff_witness ? "" : "not ");
90  }
91 
92  // m_effective_feerate and m_wtxids_fee_calculations should exist iff the result was valid
93  // or if the failure was TX_RECONSIDERABLE
94  const bool valid_or_reconsiderable{atmp_result.m_result_type == MempoolAcceptResult::ResultType::VALID ||
95  atmp_result.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
96  if (atmp_result.m_effective_feerate.has_value() != valid_or_reconsiderable) {
97  return strprintf("tx %s result should %shave m_effective_feerate",
98  wtxid.ToString(), valid ? "" : "not ");
99  }
100  if (atmp_result.m_wtxids_fee_calculations.has_value() != valid_or_reconsiderable) {
101  return strprintf("tx %s result should %shave m_effective_feerate",
102  wtxid.ToString(), valid ? "" : "not ");
103  }
104 
105  if (mempool) {
106  // The tx by txid should be in the mempool iff the result was not INVALID.
107  const bool txid_in_mempool{atmp_result.m_result_type != MempoolAcceptResult::ResultType::INVALID};
108  if (mempool->exists(GenTxid::Txid(tx->GetHash())) != txid_in_mempool) {
109  return strprintf("tx %s should %sbe in mempool", wtxid.ToString(), txid_in_mempool ? "" : "not ");
110  }
111  // Additionally, if the result was DIFFERENT_WITNESS, we shouldn't be able to find the tx in mempool by wtxid.
112  if (tx->HasWitness() && atmp_result.m_result_type == MempoolAcceptResult::ResultType::DIFFERENT_WITNESS) {
113  if (mempool->exists(GenTxid::Wtxid(wtxid))) {
114  return strprintf("wtxid %s should not be in mempool", wtxid.ToString());
115  }
116  }
117  }
118  }
119  return std::nullopt;
120 }
121 
123 {
124  LOCK(tx_pool.cs);
125  for (const auto& tx_info : tx_pool.infoAll()) {
126  const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
127  if (tx_info.tx->nVersion == 3) {
128  // Check that special v3 ancestor/descendant limits and rules are always respected
129  Assert(entry.GetCountWithDescendants() <= V3_DESCENDANT_LIMIT);
130  Assert(entry.GetCountWithAncestors() <= V3_ANCESTOR_LIMIT);
131  // If this transaction has at least 1 ancestor, it's a "child" and has restricted weight.
132  if (entry.GetCountWithAncestors() > 1) {
133  Assert(entry.GetTxSize() <= V3_CHILD_MAX_VSIZE);
134  // All v3 transactions must only have v3 unconfirmed parents.
135  const auto& parents = entry.GetMemPoolParentsConst();
136  Assert(parents.begin()->get().GetSharedTx()->nVersion == 3);
137  }
138  } else if (entry.GetCountWithAncestors() > 1) {
139  // All non-v3 transactions must only have non-v3 unconfirmed parents.
140  for (const auto& parent : entry.GetMemPoolParentsConst()) {
141  Assert(parent.get().GetSharedTx()->nVersion != 3);
142  }
143  }
144  }
145 }
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:77
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:302
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:390
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:826
bool exists(const GenTxid &gtxid) const
Definition: txmempool.h:678
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:840
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:435
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
bool IsValid() const
Definition: validation.h:122
Result GetResult() const
Definition: validation.h:125
std::string ToString() const
Definition: validation.h:128
bool IsInvalid() const
Definition: validation.h:123
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
Definition: init.h:25
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
A mutable version of CTransaction.
Definition: transaction.h:378
@ DIFFERENT_WITNESS
Valid, transaction was already in the mempool.
@ INVALID
Fully validated, valid.
Validation result for package mempool acceptance.
Definition: validation.h:233
PackageValidationState m_state
Definition: validation.h:234
std::map< uint256, MempoolAcceptResult > m_tx_results
Map from wtxid to finished MempoolAcceptResults.
Definition: validation.h:241
CAmount nFee
Definition: txmempool.h:21
bool spendsCoinbase
Definition: txmempool.h:25
LockPoints lp
Definition: txmempool.h:27
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:32
unsigned int nHeight
Definition: txmempool.h:23
unsigned int sigOpCost
Definition: txmempool.h:26
NodeSeconds time
Definition: txmempool.h:22
uint64_t m_sequence
Definition: txmempool.h:24
Options struct containing options for constructing a CTxMemPool.
NodeContext struct containing references to chain state and connection state.
Definition: context.h:49
#define LOCK(cs)
Definition: sync.h:257
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition: txmempool.cpp:19
std::optional< std::string > CheckPackageMempoolAcceptResult(const Package &txns, const PackageMempoolAcceptResult &result, bool expect_valid, const CTxMemPool *mempool)
Check expected properties for every PackageMempoolAcceptResult, regardless of value.
Definition: txmempool.cpp:42
void CheckMempoolV3Invariants(const CTxMemPool &tx_pool)
For every transaction in tx_pool, check v3 invariants:
Definition: txmempool.cpp:122
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
static constexpr unsigned int V3_DESCENDANT_LIMIT
Maximum number of transactions including an unconfirmed tx and its descendants.
Definition: v3_policy.h:23
static constexpr int64_t V3_CHILD_MAX_VSIZE
Maximum sigop-adjusted virtual size of a tx which spends from an unconfirmed v3 transaction.
Definition: v3_policy.h:28
static constexpr unsigned int V3_ANCESTOR_LIMIT
Maximum number of transactions including a V3 tx and all its mempool ancestors.
Definition: v3_policy.h:25