1 // Copyright (c) 2022-present 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 #ifndef BITCOIN_TEST_UTIL_TXMEMPOOL_H
6 #define BITCOIN_TEST_UTIL_TXMEMPOOL_H
7 8 #include <policy/packages.h>
9 #include <txmempool.h>
10 #include <util/time.h>
11 12 namespace node {
13 struct NodeContext;
14 }
15 struct PackageMempoolAcceptResult;
16 17 CTxMemPool::Options MemPoolOptionsForTest(const node::NodeContext& node);
18 19 struct TestMemPoolEntryHelper {
20 // Default values
21 CAmount nFee{0};
22 NodeSeconds time{};
23 unsigned int nHeight{1};
24 uint64_t m_sequence{0};
25 bool spendsCoinbase{false};
26 unsigned int sigOpCost{4};
27 LockPoints lp;
28 29 CTxMemPoolEntry FromTx(const CMutableTransaction& tx) const;
30 CTxMemPoolEntry FromTx(const CTransactionRef& tx) const;
31 32 // Change the default value
33 TestMemPoolEntryHelper& Fee(CAmount _fee) { nFee = _fee; return *this; }
34 TestMemPoolEntryHelper& Time(NodeSeconds tp) { time = tp; return *this; }
35 TestMemPoolEntryHelper& Height(unsigned int _height) { nHeight = _height; return *this; }
36 TestMemPoolEntryHelper& Sequence(uint64_t _seq) { m_sequence = _seq; return *this; }
37 TestMemPoolEntryHelper& SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
38 TestMemPoolEntryHelper& SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
39 };
40 41 /** Check expected properties for every PackageMempoolAcceptResult, regardless of value. Returns
42 * a string if an error occurs with error populated, nullopt otherwise. If mempool is provided,
43 * checks that the expected transactions are in mempool (this should be set to nullptr for a test_accept).
44 */
45 std::optional<std::string> CheckPackageMempoolAcceptResult(const Package& txns,
46 const PackageMempoolAcceptResult& result,
47 bool expect_valid,
48 const CTxMemPool* mempool);
49 50 /** Check that we never get into a state where an ephemeral dust
51 * transaction would be mined without the spend of the dust
52 * also being mined. This assumes standardness checks are being
53 * enforced.
54 */
55 void CheckMempoolEphemeralInvariants(const CTxMemPool& tx_pool);
56 57 /** For every transaction in tx_pool, check TRUC invariants:
58 * - a TRUC tx's ancestor count must be within TRUC_ANCESTOR_LIMIT
59 * - a TRUC tx's descendant count must be within TRUC_DESCENDANT_LIMIT
60 * - if a TRUC tx has ancestors, its sigop-adjusted vsize must be within TRUC_CHILD_MAX_VSIZE
61 * - any non-TRUC tx must only have non-TRUC parents
62 * - any TRUC tx must only have TRUC parents
63 * */
64 void CheckMempoolTRUCInvariants(const CTxMemPool& tx_pool);
65 66 /** One-line wrapper for creating a mempool changeset with a single transaction
67 * and applying it if the policy limits are respected. */
68 void TryAddToMempool(CTxMemPool& tx_pool, const CTxMemPoolEntry& entry);
69 70 /** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),
71 * simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that
72 * this clears the mempool, and the new minimum feerate will depend on the maximum feerate of
73 * transactions removed, so this must be called while the mempool is empty.
74 *
75 * @param target_feerate The new mempool minimum feerate after this function returns.
76 * Must be above max(incremental feerate, min relay feerate),
77 * or 1sat/vB with default settings.
78 * @param mempool The mempool to mock the minimum feerate for. Must be empty
79 * when called.
80 */
81 void MockMempoolMinFee(const CFeeRate& target_feerate, CTxMemPool& mempool);
82 83 #endif // BITCOIN_TEST_UTIL_TXMEMPOOL_H
84