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