Branch data Line data Source code
1 : : // Copyright (c) 2021-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 : : #include <chain.h>
6 : : #include <coins.h>
7 : : #include <consensus/amount.h>
8 : : #include <consensus/consensus.h>
9 : : #include <consensus/validation.h>
10 : : #include <node/miner.h>
11 : : #include <node/mining_types.h>
12 : : #include <policy/feerate.h>
13 : : #include <policy/packages.h>
14 : : #include <policy/policy.h>
15 : : #include <policy/truc_policy.h>
16 : : #include <primitives/block.h>
17 : : #include <primitives/transaction.h>
18 : : #include <script/script.h>
19 : : #include <sync.h>
20 : : #include <test/fuzz/FuzzedDataProvider.h>
21 : : #include <test/fuzz/fuzz.h>
22 : : #include <test/fuzz/util.h>
23 : : #include <test/fuzz/util/mempool.h>
24 : : #include <test/util/mining.h>
25 : : #include <test/util/random.h>
26 : : #include <test/util/script.h>
27 : : #include <test/util/setup_common.h>
28 : : #include <test/util/txmempool.h>
29 : : #include <txmempool.h>
30 : : #include <util/check.h>
31 : : #include <util/string.h>
32 : : #include <util/time.h>
33 : : #include <util/translation.h>
34 : : #include <validation.h>
35 : : #include <validationinterface.h>
36 : :
37 : : #include <cstddef>
38 : : #include <cstdint>
39 : : #include <functional>
40 : : #include <iterator>
41 : : #include <limits>
42 : : #include <map>
43 : : #include <memory>
44 : : #include <optional>
45 : : #include <set>
46 : : #include <span>
47 : : #include <string>
48 : : #include <utility>
49 : : #include <vector>
50 : : using node::BlockAssembler;
51 : : using node::BlockCreateOptions;
52 : : using node::NodeContext;
53 : : using util::ToString;
54 : :
55 : : namespace {
56 : :
57 : : const TestingSetup* g_setup;
58 : : std::vector<COutPoint> g_outpoints_coinbase_init_mature;
59 : : std::vector<COutPoint> g_outpoints_coinbase_init_immature;
60 : :
61 : : struct MockedTxPool : public CTxMemPool {
62 : 385192 : void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
63 : : {
64 : 385192 : LOCK(cs);
65 [ + - ]: 385192 : lastRollingFeeUpdate = GetTime();
66 [ + - ]: 385192 : blockSinceLastRollingFeeBump = true;
67 : 385192 : }
68 : : };
69 : :
70 : 2 : void initialize_tx_pool()
71 : : {
72 [ + - + - : 2 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
+ - ]
73 : 2 : g_setup = testing_setup.get();
74 [ + - + - ]: 6 : SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
75 : :
76 [ + + ]: 402 : for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
77 : 1200 : COutPoint prevout{MineBlock(g_setup->m_node, {
78 : : .coinbase_output_script = P2WSH_OP_TRUE,
79 : : })};
80 : : // Remember the txids to avoid expensive disk access later on
81 [ + + ]: 400 : auto& outpoints = i < COINBASE_MATURITY ?
82 : : g_outpoints_coinbase_init_mature :
83 : : g_outpoints_coinbase_init_immature;
84 : 400 : outpoints.push_back(prevout);
85 : : }
86 : 2 : g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
87 [ + - ]: 402 : }
88 : :
89 : : struct TransactionsDelta final : public CValidationInterface {
90 : : std::set<CTransactionRef>& m_removed;
91 : : std::set<CTransactionRef>& m_added;
92 : :
93 : 87753 : explicit TransactionsDelta(std::set<CTransactionRef>& r, std::set<CTransactionRef>& a)
94 : 87753 : : m_removed{r}, m_added{a} {}
95 : :
96 : 25549 : void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
97 : : {
98 [ - + ]: 25549 : Assert(m_added.insert(tx.info.m_tx).second);
99 : 25549 : }
100 : :
101 : 9109 : void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
102 : : {
103 [ - + ]: 9109 : Assert(m_removed.insert(tx).second);
104 : 9109 : }
105 : : };
106 : :
107 : 10175 : void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_provider)
108 : : {
109 [ + - + - ]: 20350 : args.ForceSetArg("-limitclustercount",
110 : 10175 : ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(1, 64)));
111 [ + - + - ]: 20350 : args.ForceSetArg("-limitclustersize",
112 : 10175 : ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(1, 250)));
113 [ + - + - ]: 20350 : args.ForceSetArg("-maxmempool",
114 : 10175 : ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200)));
115 [ + - + - ]: 20350 : args.ForceSetArg("-mempoolexpiry",
116 : 10175 : ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)));
117 : 10175 : }
118 : :
119 : 10175 : void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, Chainstate& chainstate)
120 : : {
121 [ - + + - : 30525 : WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
+ - ]
122 : 10175 : {
123 : 10175 : BlockCreateOptions options{
124 : 10175 : .block_min_fee_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)},
125 [ + - ]: 10175 : .block_max_weight = fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(DEFAULT_BLOCK_RESERVED_WEIGHT, MAX_BLOCK_WEIGHT),
126 : 10175 : };
127 [ + - ]: 10175 : auto assembler = BlockAssembler{chainstate, &tx_pool, options};
128 [ + - ]: 10175 : auto block_template = assembler.CreateNewBlock();
129 [ - + - + ]: 10175 : Assert(block_template->block.vtx.size() >= 1);
130 : :
131 : : // Try updating the mempool for this block, as though it were mined.
132 [ + - + - ]: 10175 : LOCK2(::cs_main, tx_pool.cs);
133 [ - + + - ]: 10175 : tx_pool.removeForBlock(block_template->block.vtx, chainstate.m_chain.Height() + 1);
134 : :
135 : : // Now try to add those transactions back, as though a reorg happened.
136 : 10175 : std::vector<Txid> hashes_to_update;
137 [ + + ]: 68765 : for (const auto& tx : block_template->block.vtx) {
138 [ + - + - ]: 58590 : const auto res = AcceptToMemoryPool(chainstate, tx, GetTime(), true, /*test_accept=*/false);
139 [ + + ]: 58590 : if (res.m_result_type == MempoolAcceptResult::ResultType::VALID) {
140 [ + - ]: 48415 : hashes_to_update.push_back(tx->GetHash());
141 : : } else {
142 [ + - ]: 10175 : tx_pool.removeRecursive(*tx, MemPoolRemovalReason::REORG);
143 : : }
144 : 58590 : }
145 [ + - ]: 10175 : tx_pool.UpdateTransactionsFromBlock(hashes_to_update);
146 [ + - + - ]: 40700 : }
147 : 10175 : const auto info_all = tx_pool.infoAll();
148 [ + + ]: 10175 : if (!info_all.empty()) {
149 [ + - ]: 7375 : const auto& tx_to_remove = *PickValue(fuzzed_data_provider, info_all).tx;
150 [ + - + - ]: 22125 : WITH_LOCK(tx_pool.cs, tx_pool.removeRecursive(tx_to_remove, MemPoolRemovalReason::BLOCK /* dummy */));
151 [ + - - + : 7375 : assert(tx_pool.size() < info_all.size());
- + ]
152 : : }
153 : :
154 [ + + ]: 10175 : if (fuzzed_data_provider.ConsumeBool()) {
155 : : // Try eviction
156 [ + - + - ]: 2253 : LOCK2(::cs_main, tx_pool.cs);
157 [ + - + - ]: 2253 : tx_pool.TrimToSize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0U, tx_pool.DynamicMemoryUsage() * 2));
158 [ + - ]: 4506 : }
159 [ + + ]: 10175 : if (fuzzed_data_provider.ConsumeBool()) {
160 : : // Try expiry
161 [ + - + - ]: 2031 : LOCK2(::cs_main, tx_pool.cs);
162 [ + - + - ]: 2031 : tx_pool.Expire(GetMockTime() - std::chrono::seconds(fuzzed_data_provider.ConsumeIntegral<uint32_t>()));
163 [ + - ]: 4062 : }
164 [ + - - + : 30525 : WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
+ - + - ]
165 [ + - ]: 10175 : g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
166 : 10175 : }
167 : :
168 : 374240 : void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
169 : : {
170 : 374240 : const auto time = ConsumeTime(fuzzed_data_provider,
171 : 374240 : chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
172 [ - + ]: 374240 : std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
173 : 374240 : SetMockTime(time);
174 : 374240 : }
175 : :
176 : 10175 : std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
177 : : {
178 : : // Take the default options for tests...
179 : 10175 : CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
180 : :
181 : : // ...override specific options for this specific fuzz suite
182 : 10175 : mempool_opts.check_ratio = 1;
183 : 10175 : mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
184 : :
185 : : // ...and construct a CTxMemPool from it
186 [ + - ]: 10175 : bilingual_str error;
187 [ + - ]: 10175 : auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
188 : : // ... ignore the error since it might be beneficial to fuzz even when the
189 : : // mempool size is unreasonably small
190 [ + + + - : 10639 : Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
- + ]
191 : 10175 : return mempool;
192 : 10175 : }
193 : :
194 : 87753 : void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
195 : : {
196 : :
197 [ + + - - : 87753 : switch (res.m_result_type) {
- ]
198 : 25549 : case MempoolAcceptResult::ResultType::VALID:
199 : 25549 : {
200 [ - + ]: 25549 : Assert(txid_in_mempool);
201 [ - + ]: 25549 : Assert(wtxid_in_mempool);
202 [ - + ]: 25549 : Assert(res.m_state.IsValid());
203 [ - + ]: 25549 : Assert(!res.m_state.IsInvalid());
204 [ - + ]: 25549 : Assert(res.m_vsize);
205 [ - + ]: 25549 : Assert(res.m_base_fees);
206 [ - + ]: 25549 : Assert(res.m_effective_feerate);
207 [ - + ]: 25549 : Assert(res.m_wtxids_fee_calculations);
208 [ - + ]: 25549 : Assert(!res.m_other_wtxid);
209 : : break;
210 : : }
211 : 62204 : case MempoolAcceptResult::ResultType::INVALID:
212 : 62204 : {
213 : : // It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
214 [ - + ]: 62204 : Assert(!res.m_state.IsValid());
215 [ - + ]: 62204 : Assert(res.m_state.IsInvalid());
216 : :
217 [ - + ]: 62204 : const bool is_reconsiderable{res.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
218 [ - + ]: 62204 : Assert(!res.m_vsize);
219 [ - + ]: 62204 : Assert(!res.m_base_fees);
220 : : // Fee information is provided if the failure is TX_RECONSIDERABLE.
221 : : // In other cases, validation may be unable or unwilling to calculate the fees.
222 [ - + ]: 62204 : Assert(res.m_effective_feerate.has_value() == is_reconsiderable);
223 [ - + ]: 62204 : Assert(res.m_wtxids_fee_calculations.has_value() == is_reconsiderable);
224 [ - + ]: 62204 : Assert(!res.m_other_wtxid);
225 : : break;
226 : : }
227 : 0 : case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
228 : 0 : {
229 : : // ATMP never sets this; only set in package settings
230 : 0 : Assert(false);
231 : 0 : break;
232 : : }
233 : 0 : case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
234 : 0 : {
235 : : // ATMP never sets this; only set in package settings
236 : 0 : Assert(false);
237 : 0 : break;
238 : : }
239 : : }
240 : 87753 : }
241 : :
242 [ + - ]: 3331 : FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
243 : : {
244 : 2857 : SeedRandomStateForTest(SeedRand::ZEROS);
245 : 2857 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
246 : 2857 : const auto& node = g_setup->m_node;
247 : 2857 : auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
248 : :
249 : 2857 : MockTime(fuzzed_data_provider, chainstate);
250 : :
251 : : // All RBF-spendable outpoints
252 : 2857 : std::set<COutPoint> outpoints_rbf;
253 : : // All outpoints counting toward the total supply (subset of outpoints_rbf)
254 : 2857 : std::set<COutPoint> outpoints_supply;
255 [ + + ]: 288557 : for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
256 [ + - ]: 571400 : Assert(outpoints_supply.insert(outpoint).second);
257 : : }
258 [ + - ]: 2857 : outpoints_rbf = outpoints_supply;
259 : :
260 : : // The sum of the values of all spendable outpoints
261 : 2857 : constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
262 : :
263 [ + - ]: 2857 : SetMempoolConstraints(*node.args, fuzzed_data_provider);
264 [ + - ]: 2857 : auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
265 [ + - ]: 2857 : MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
266 : :
267 [ + - ]: 2857 : chainstate.SetMempool(&tx_pool);
268 : :
269 : : // Helper to query an amount
270 [ + - + - ]: 8571 : const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
271 : 15105979 : const auto GetAmount = [&](const COutPoint& outpoint) {
272 [ + - ]: 15103122 : auto coin{amount_view.GetCoin(outpoint).value()};
273 : 15103122 : return coin.out.nValue;
274 : 15105979 : };
275 : :
276 [ + + + + ]: 90610 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {
277 : 87753 : {
278 : : // Total supply is the mempool fee + all outpoints
279 [ + - ]: 175506 : CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
280 [ + + ]: 13401488 : for (const auto& op : outpoints_supply) {
281 [ + - ]: 13313735 : supply_now += GetAmount(op);
282 : : }
283 [ - + ]: 87753 : Assert(supply_now == SUPPLY_TOTAL);
284 : : }
285 [ - + ]: 87753 : Assert(!outpoints_supply.empty());
286 : :
287 : : // Create transaction to add to the mempool
288 : 175506 : const CTransactionRef tx = [&] {
289 : 87753 : CMutableTransaction tx_mut;
290 [ + + ]: 87753 : tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
291 [ + + ]: 87753 : tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
292 : 87753 : const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
293 : 87753 : const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
294 : :
295 : 87753 : CAmount amount_in{0};
296 [ + + ]: 1877140 : for (int i = 0; i < num_in; ++i) {
297 : : // Pop random outpoint
298 : 1789387 : auto pop = outpoints_rbf.begin();
299 : 1789387 : std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints_rbf.size() - 1));
300 : 1789387 : const auto outpoint = *pop;
301 : 1789387 : outpoints_rbf.erase(pop);
302 [ + - ]: 1789387 : amount_in += GetAmount(outpoint);
303 : :
304 : : // Create input
305 : 1789387 : const auto sequence = ConsumeSequence(fuzzed_data_provider);
306 : 1789387 : const auto script_sig = CScript{};
307 [ - + + + : 3578774 : const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
- - ]
308 : 1789387 : CTxIn in;
309 : 1789387 : in.prevout = outpoint;
310 : 1789387 : in.nSequence = sequence;
311 : 1789387 : in.scriptSig = script_sig;
312 [ + - ]: 1789387 : in.scriptWitness.stack = script_wit_stack;
313 : :
314 [ + - ]: 1789387 : tx_mut.vin.push_back(in);
315 : 1789387 : }
316 : :
317 : : // Check sigops in mempool + block template creation
318 : 87753 : bool add_sigops{fuzzed_data_provider.ConsumeBool()};
319 : :
320 : 87753 : const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1000, amount_in);
321 : 87753 : const auto amount_out = (amount_in - amount_fee) / num_out;
322 [ + + ]: 5757118 : for (int i = 0; i < num_out; ++i) {
323 [ + + ]: 5669365 : if (i == 0 && add_sigops) {
324 [ + - + - : 231066 : tx_mut.vout.emplace_back(amount_out, CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG);
+ - ]
325 : : } else {
326 [ + - ]: 5592343 : tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
327 : : }
328 : : }
329 : :
330 [ + - ]: 87753 : auto tx = MakeTransactionRef(tx_mut);
331 : : // Restore previously removed outpoints
332 [ + + ]: 1877140 : for (const auto& in : tx->vin) {
333 [ + - ]: 3578774 : Assert(outpoints_rbf.insert(in.prevout).second);
334 : : }
335 : 87753 : return tx;
336 [ + - ]: 1964893 : }();
337 : :
338 [ + + ]: 87753 : if (fuzzed_data_provider.ConsumeBool()) {
339 [ + - ]: 62728 : MockTime(fuzzed_data_provider, chainstate);
340 : : }
341 [ + + ]: 87753 : if (fuzzed_data_provider.ConsumeBool()) {
342 [ + - ]: 62913 : tx_pool.RollingFeeUpdate();
343 : : }
344 [ + + ]: 87753 : if (fuzzed_data_provider.ConsumeBool()) {
345 [ + + ]: 61439 : const auto& txid = fuzzed_data_provider.ConsumeBool() ?
346 : 54606 : tx->GetHash() :
347 : 6833 : PickValue(fuzzed_data_provider, outpoints_rbf).hash;
348 : 61439 : const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
349 [ + - ]: 61439 : tx_pool.PrioritiseTransaction(txid, delta);
350 : : }
351 : :
352 : : // Remember all removed and added transactions
353 [ + - ]: 87753 : std::set<CTransactionRef> removed;
354 : 87753 : std::set<CTransactionRef> added;
355 [ + - ]: 87753 : auto txr = std::make_shared<TransactionsDelta>(removed, added);
356 [ + - + - ]: 175506 : node.validation_signals->RegisterSharedValidationInterface(txr);
357 : :
358 : : // Make sure ProcessNewPackage on one transaction works.
359 : : // The result is not guaranteed to be the same as what is returned by ATMP.
360 [ + - + - : 438765 : const auto result_package = WITH_LOCK(::cs_main,
+ - + + +
- - - -
- ]
361 : : return ProcessNewPackage(chainstate, tx_pool, {tx}, true, /*client_maxfeerate=*/{}));
362 : : // If something went wrong due to a package-specific policy, it might not return a
363 : : // validation result for the transaction.
364 [ + + ]: 87753 : if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
365 : 86301 : auto it = result_package.m_tx_results.find(tx->GetWitnessHash());
366 [ - + ]: 86301 : Assert(it != result_package.m_tx_results.end());
367 [ + + + - : 147005 : Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
- + ]
368 : : it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
369 : : }
370 : :
371 [ + - + - ]: 263259 : const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), /*bypass_limits=*/false, /*test_accept=*/false));
372 : 87753 : const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
373 [ + - ]: 87753 : node.validation_signals->SyncWithValidationInterfaceQueue();
374 [ + - + - ]: 175506 : node.validation_signals->UnregisterSharedValidationInterface(txr);
375 : :
376 [ + - ]: 87753 : bool txid_in_mempool = tx_pool.exists(tx->GetHash());
377 [ + - ]: 87753 : bool wtxid_in_mempool = tx_pool.exists(tx->GetWitnessHash());
378 [ + - ]: 87753 : CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
379 : :
380 [ - + ]: 87753 : Assert(accepted != added.empty());
381 [ + + ]: 87753 : if (accepted) {
382 [ - + ]: 25549 : Assert(added.size() == 1); // For now, no package acceptance
383 [ - + ]: 25549 : Assert(tx == *added.begin());
384 [ + - ]: 25549 : CheckMempoolTRUCInvariants(tx_pool);
385 : : } else {
386 : : // Do not consider rejected transaction removed
387 : 62204 : removed.erase(tx);
388 : : }
389 : :
390 : : // Helper to insert spent and created outpoints of a tx into collections
391 : 87753 : using Sets = std::vector<std::reference_wrapper<std::set<COutPoint>>>;
392 : 119788 : const auto insert_tx = [](Sets created_by_tx, Sets consumed_by_tx, const auto& tx) {
393 [ - + + + ]: 839778 : for (size_t i{0}; i < tx.vout.size(); ++i) {
394 [ + + ]: 2374508 : for (auto& set : created_by_tx) {
395 [ - + ]: 1566765 : Assert(set.get().emplace(tx.GetHash(), i).second);
396 : : }
397 : : }
398 [ + + ]: 117948 : for (const auto& in : tx.vin) {
399 [ + + ]: 171826 : for (auto& set : consumed_by_tx) {
400 [ - + ]: 85913 : Assert(set.get().insert(in.prevout).second);
401 : : }
402 : : }
403 : 32035 : };
404 : : // Add created outpoints, remove spent outpoints
405 : 87753 : {
406 : : // Outpoints that no longer exist at all
407 : 87753 : std::set<COutPoint> consumed_erased;
408 : : // Outpoints that no longer count toward the total supply
409 : 87753 : std::set<COutPoint> consumed_supply;
410 [ + + ]: 94239 : for (const auto& removed_tx : removed) {
411 [ + - + - : 12972 : insert_tx(/*created_by_tx=*/{consumed_erased}, /*consumed_by_tx=*/{outpoints_supply}, /*tx=*/*removed_tx);
+ - ]
412 : : }
413 [ + + ]: 113302 : for (const auto& added_tx : added) {
414 [ + - + - : 51098 : insert_tx(/*created_by_tx=*/{outpoints_supply, outpoints_rbf}, /*consumed_by_tx=*/{consumed_supply}, /*tx=*/*added_tx);
+ - ]
415 : : }
416 [ + + ]: 136474 : for (const auto& p : consumed_erased) {
417 [ - + ]: 48721 : Assert(outpoints_supply.erase(p) == 1);
418 [ - + ]: 48721 : Assert(outpoints_rbf.erase(p) == 1);
419 : : }
420 [ + + ]: 154125 : for (const auto& p : consumed_supply) {
421 [ - + ]: 66372 : Assert(outpoints_supply.erase(p) == 1);
422 : : }
423 : 87753 : }
424 [ + - + - ]: 263259 : }
425 [ + - ]: 2857 : Finish(fuzzed_data_provider, tx_pool, chainstate);
426 : 2857 : }
427 : :
428 [ + - ]: 7792 : FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
429 : : {
430 : 7318 : SeedRandomStateForTest(SeedRand::ZEROS);
431 : 7318 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
432 : 7318 : const auto& node = g_setup->m_node;
433 : 7318 : auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
434 : :
435 : 7318 : MockTime(fuzzed_data_provider, chainstate);
436 : :
437 : 7318 : std::vector<Txid> txids;
438 [ - + + - ]: 7318 : txids.reserve(g_outpoints_coinbase_init_mature.size());
439 [ + + ]: 739118 : for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
440 [ + - ]: 731800 : txids.push_back(outpoint.hash);
441 : : }
442 [ + + ]: 36590 : for (int i{0}; i <= 3; ++i) {
443 : : // Add some immature and non-existent outpoints
444 [ + - + - ]: 29272 : txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash);
445 [ + - ]: 29272 : txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
446 : : }
447 : :
448 [ + - ]: 7318 : SetMempoolConstraints(*node.args, fuzzed_data_provider);
449 [ + - ]: 7318 : auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
450 : 7318 : MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
451 : :
452 : 7318 : chainstate.SetMempool(&tx_pool);
453 : :
454 : : // If we ever bypass limits, do not do TRUC invariants checks
455 : 7318 : bool ever_bypassed_limits{false};
456 : :
457 [ + + + + ]: 503869 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 300) {
458 [ + - ]: 496551 : const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
459 : :
460 [ + + ]: 496551 : if (fuzzed_data_provider.ConsumeBool()) {
461 [ + - ]: 301337 : MockTime(fuzzed_data_provider, chainstate);
462 : : }
463 [ + + ]: 496551 : if (fuzzed_data_provider.ConsumeBool()) {
464 [ + - ]: 322279 : tx_pool.RollingFeeUpdate();
465 : : }
466 [ + + ]: 496551 : if (fuzzed_data_provider.ConsumeBool()) {
467 [ + + ]: 269986 : const auto txid = fuzzed_data_provider.ConsumeBool() ?
468 [ + - ]: 219917 : mut_tx.GetHash() :
469 : 50069 : PickValue(fuzzed_data_provider, txids);
470 : 269986 : const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
471 [ + - ]: 269986 : tx_pool.PrioritiseTransaction(txid, delta);
472 : : }
473 : :
474 : 496551 : const bool bypass_limits{fuzzed_data_provider.ConsumeBool()};
475 : 496551 : ever_bypassed_limits |= bypass_limits;
476 : :
477 [ + - ]: 496551 : const auto tx = MakeTransactionRef(mut_tx);
478 [ + - + - ]: 1489653 : const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
479 : 496551 : const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
480 [ + + ]: 496551 : if (accepted) {
481 [ + - ]: 102155 : txids.push_back(tx->GetHash());
482 [ + + ]: 102155 : if (!ever_bypassed_limits) {
483 [ + - ]: 6334 : CheckMempoolTRUCInvariants(tx_pool);
484 : : }
485 : : }
486 [ + - ]: 1489653 : }
487 [ + - ]: 7318 : Finish(fuzzed_data_provider, tx_pool, chainstate);
488 : 7318 : }
489 : : } // namespace
|