Bitcoin Core 29.99.0
P2P Digital Currency
package_eval.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 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
6#include <node/context.h>
7#include <node/mempool_args.h>
8#include <node/miner.h>
11#include <test/fuzz/fuzz.h>
12#include <test/fuzz/util.h>
14#include <test/util/mining.h>
15#include <test/util/script.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
26
27namespace {
28
29const TestingSetup* g_setup;
30std::vector<COutPoint> g_outpoints_coinbase_init_mature;
31
32struct MockedTxPool : public CTxMemPool {
33 void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
34 {
35 LOCK(cs);
36 lastRollingFeeUpdate = GetTime();
37 blockSinceLastRollingFeeBump = true;
38 }
39};
40
41void initialize_tx_pool()
42{
43 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
44 g_setup = testing_setup.get();
45 SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
46
47 BlockAssembler::Options options;
48 options.coinbase_output_script = P2WSH_EMPTY;
49
50 for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
51 COutPoint prevout{MineBlock(g_setup->m_node, options)};
52 if (i < COINBASE_MATURITY) {
53 // Remember the txids to avoid expensive disk access later on
54 g_outpoints_coinbase_init_mature.push_back(prevout);
55 }
56 }
57 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
58}
59
60struct OutpointsUpdater final : public CValidationInterface {
61 std::set<COutPoint>& m_mempool_outpoints;
62
63 explicit OutpointsUpdater(std::set<COutPoint>& r)
64 : m_mempool_outpoints{r} {}
65
66 void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
67 {
68 // for coins spent we always want to be able to rbf so they're not removed
69
70 // outputs from this tx can now be spent
71 for (uint32_t index{0}; index < tx.info.m_tx->vout.size(); ++index) {
72 m_mempool_outpoints.insert(COutPoint{tx.info.m_tx->GetHash(), index});
73 }
74 }
75
76 void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
77 {
78 // outpoints spent by this tx are now available
79 for (const auto& input : tx->vin) {
80 // Could already exist if this was a replacement
81 m_mempool_outpoints.insert(input.prevout);
82 }
83 // outpoints created by this tx no longer exist
84 for (uint32_t index{0}; index < tx->vout.size(); ++index) {
85 m_mempool_outpoints.erase(COutPoint{tx->GetHash(), index});
86 }
87 }
88};
89
90struct TransactionsDelta final : public CValidationInterface {
91 std::set<CTransactionRef>& m_added;
92
93 explicit TransactionsDelta(std::set<CTransactionRef>& a)
94 : m_added{a} {}
95
96 void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
97 {
98 // Transactions may be entered and booted any number of times
99 m_added.insert(tx.info.m_tx);
100 }
101
102 void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
103 {
104 // Transactions may be entered and booted any number of times
105 m_added.erase(tx);
106 }
107};
108
109void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
110{
111 const auto time = ConsumeTime(fuzzed_data_provider,
112 chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
113 std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
114 SetMockTime(time);
115}
116
117std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
118{
119 // Take the default options for tests...
121
122
123 // ...override specific options for this specific fuzz suite
124 mempool_opts.limits.ancestor_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50);
125 mempool_opts.limits.ancestor_size_vbytes = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202) * 1'000;
126 mempool_opts.limits.descendant_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50);
127 mempool_opts.limits.descendant_size_vbytes = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202) * 1'000;
128 mempool_opts.max_size_bytes = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200) * 1'000'000;
129 mempool_opts.expiry = std::chrono::hours{fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)};
130 // Only interested in 2 cases: sigop cost 0 or when single legacy sigop cost is >> 1KvB
131 nBytesPerSigOp = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 1) * 10'000;
132
133 mempool_opts.check_ratio = 1;
134 mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
135
136 bilingual_str error;
137 // ...and construct a CTxMemPool from it
138 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
139 // ... ignore the error since it might be beneficial to fuzz even when the
140 // mempool size is unreasonably small
141 Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
142 return mempool;
143}
144
145std::unique_ptr<CTxMemPool> MakeEphemeralMempool(const NodeContext& node)
146{
147 // Take the default options for tests...
149
150 mempool_opts.check_ratio = 1;
151
152 // Require standardness rules otherwise ephemeral dust is no-op
153 mempool_opts.require_standard = true;
154
155 // And set minrelay to 0 to allow ephemeral parent tx even with non-TRUC
156 mempool_opts.min_relay_feerate = CFeeRate(0);
157
158 bilingual_str error;
159 // ...and construct a CTxMemPool from it
160 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
161 Assert(error.empty());
162 return mempool;
163}
164
165// Scan mempool for a tx that has spent dust and return a
166// prevout of the child that isn't the dusty parent itself.
167// This is used to double-spend the child out of the mempool,
168// leaving the parent childless.
169// This assumes CheckMempoolEphemeralInvariants has passed for tx_pool.
170std::optional<COutPoint> GetChildEvictingPrevout(const CTxMemPool& tx_pool)
171{
172 LOCK(tx_pool.cs);
173 for (const auto& tx_info : tx_pool.infoAll()) {
174 const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
175 std::vector<uint32_t> dust_indexes{GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate)};
176 if (!dust_indexes.empty()) {
177 const auto& children = entry.GetMemPoolChildrenConst();
178 if (!children.empty()) {
179 Assert(children.size() == 1);
180 // Find an input that doesn't spend from parent's txid
181 const auto& only_child = children.begin()->get().GetTx();
182 for (const auto& tx_input : only_child.vin) {
183 if (tx_input.prevout.hash != tx_info.tx->GetHash()) {
184 return tx_input.prevout;
185 }
186 }
187 }
188 }
189 }
190
191 return std::nullopt;
192}
193
194FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
195{
197 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
198 const auto& node = g_setup->m_node;
199 auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
200
201 MockTime(fuzzed_data_provider, chainstate);
202
203 // All RBF-spendable outpoints outside of the unsubmitted package
204 std::set<COutPoint> mempool_outpoints;
205 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
206 for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
207 Assert(mempool_outpoints.insert(outpoint).second);
208 outpoints_value[outpoint] = 50 * COIN;
209 }
210
211 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
212 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
213
214 auto tx_pool_{MakeEphemeralMempool(node)};
215 MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
216
217 chainstate.SetMempool(&tx_pool);
218
219 LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
220 {
221 Assert(!mempool_outpoints.empty());
222
223 std::vector<CTransactionRef> txs;
224
225 // Find something we may want to double-spend with two input single tx
226 std::optional<COutPoint> outpoint_to_rbf{fuzzed_data_provider.ConsumeBool() ? GetChildEvictingPrevout(tx_pool) : std::nullopt};
227
228 // Make small packages
229 const auto num_txs = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4);
230
231 std::set<COutPoint> package_outpoints;
232 while (txs.size() < num_txs) {
233 // Create transaction to add to the mempool
234 txs.emplace_back([&] {
235 CMutableTransaction tx_mut;
237 tx_mut.nLockTime = 0;
238 // Last transaction in a package needs to be a child of parents to get further in validation
239 // so the last transaction to be generated(in a >1 package) must spend all package-made outputs
240 // Note that this test currently only spends package outputs in last transaction.
241 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
242 const auto num_in = outpoint_to_rbf ? 2 :
243 last_tx ? fuzzed_data_provider.ConsumeIntegralInRange<int>(package_outpoints.size()/2 + 1, package_outpoints.size()) :
244 fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
245 const auto num_out = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
246
247 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
248
249 Assert((int)outpoints.size() >= num_in && num_in > 0);
250
251 CAmount amount_in{0};
252 for (int i = 0; i < num_in; ++i) {
253 // Pop random outpoint. We erase them to avoid double-spending
254 // while in this loop, but later add them back (unless last_tx).
255 auto pop = outpoints.begin();
256 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
257 auto outpoint = *pop;
258
259 if (i == 0 && outpoint_to_rbf) {
260 outpoint = *outpoint_to_rbf;
261 outpoints.erase(outpoint);
262 } else {
263 outpoints.erase(pop);
264 }
265 // no need to update or erase from outpoints_value
266 amount_in += outpoints_value.at(outpoint);
267
268 // Create input
269 CTxIn in;
270 in.prevout = outpoint;
272
273 tx_mut.vin.push_back(in);
274 }
275
276 const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, amount_in);
277 const auto amount_out = (amount_in - amount_fee) / num_out;
278 for (int i = 0; i < num_out; ++i) {
279 tx_mut.vout.emplace_back(amount_out, P2WSH_EMPTY);
280 }
281
282 // Note output amounts can naturally drop to dust on their own.
283 if (!outpoint_to_rbf && fuzzed_data_provider.ConsumeBool()) {
284 uint32_t dust_index = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, num_out);
285 tx_mut.vout.insert(tx_mut.vout.begin() + dust_index, CTxOut(0, P2WSH_EMPTY));
286 }
287
288 auto tx = MakeTransactionRef(tx_mut);
289 // Restore previously removed outpoints, except in-package outpoints (to allow RBF)
290 if (!last_tx) {
291 for (const auto& in : tx->vin) {
292 Assert(outpoints.insert(in.prevout).second);
293 }
294 // Cache the in-package outpoints being made
295 for (size_t i = 0; i < tx->vout.size(); ++i) {
296 package_outpoints.emplace(tx->GetHash(), i);
297 }
298 }
299 // We need newly-created values for the duration of this run
300 for (size_t i = 0; i < tx->vout.size(); ++i) {
301 outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
302 }
303 return tx;
304 }());
305 }
306
307 if (fuzzed_data_provider.ConsumeBool()) {
308 const auto& txid = fuzzed_data_provider.ConsumeBool() ?
309 txs.back()->GetHash() :
310 PickValue(fuzzed_data_provider, mempool_outpoints).hash;
311 const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
312 // We only prioritise out of mempool transactions since PrioritiseTransaction doesn't
313 // filter for ephemeral dust
314 if (tx_pool.exists(txid)) {
315 const auto tx_info{tx_pool.info(txid)};
316 if (GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
317 tx_pool.PrioritiseTransaction(txid, delta);
318 }
319 }
320 }
321
322 auto single_submit = txs.size() == 1;
323
324 const auto result_package = WITH_LOCK(::cs_main,
325 return ProcessNewPackage(chainstate, tx_pool, txs, /*test_accept=*/single_submit, /*client_maxfeerate=*/{}));
326
327 const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, txs.back(), GetTime(),
328 /*bypass_limits=*/fuzzed_data_provider.ConsumeBool(), /*test_accept=*/!single_submit));
329
330 if (!single_submit && result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
331 // We don't know anything about the validity since transactions were randomly generated, so
332 // just use result_package.m_state here. This makes the expect_valid check meaningless, but
333 // we can still verify that the contents of m_tx_results are consistent with m_state.
334 const bool expect_valid{result_package.m_state.IsValid()};
335 Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, &tx_pool));
336 }
337
338 node.validation_signals->SyncWithValidationInterfaceQueue();
339
341 }
342
343 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
344
345 WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
346}
347
348
349FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
350{
352 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
353 const auto& node = g_setup->m_node;
354 auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
355
356 MockTime(fuzzed_data_provider, chainstate);
357
358 // All RBF-spendable outpoints outside of the unsubmitted package
359 std::set<COutPoint> mempool_outpoints;
360 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
361 for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
362 Assert(mempool_outpoints.insert(outpoint).second);
363 outpoints_value[outpoint] = 50 * COIN;
364 }
365
366 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
367 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
368
369 auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
370 MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
371
372 chainstate.SetMempool(&tx_pool);
373
374 LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
375 {
376 Assert(!mempool_outpoints.empty());
377
378 std::vector<CTransactionRef> txs;
379
380 // Make packages of 1-to-26 transactions
381 const auto num_txs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 26);
382 std::set<COutPoint> package_outpoints;
383 while (txs.size() < num_txs) {
384 // Create transaction to add to the mempool
385 txs.emplace_back([&] {
386 CMutableTransaction tx_mut;
387 tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
388 tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
389 // Last transaction in a package needs to be a child of parents to get further in validation
390 // so the last transaction to be generated(in a >1 package) must spend all package-made outputs
391 // Note that this test currently only spends package outputs in last transaction.
392 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
393 const auto num_in = last_tx ? package_outpoints.size() : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size());
394 auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size() * 2);
395
396 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
397
398 Assert(!outpoints.empty());
399
400 CAmount amount_in{0};
401 for (size_t i = 0; i < num_in; ++i) {
402 // Pop random outpoint. We erase them to avoid double-spending
403 // while in this loop, but later add them back (unless last_tx).
404 auto pop = outpoints.begin();
405 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
406 const auto outpoint = *pop;
407 outpoints.erase(pop);
408 // no need to update or erase from outpoints_value
409 amount_in += outpoints_value.at(outpoint);
410
411 // Create input
412 const auto sequence = ConsumeSequence(fuzzed_data_provider);
413 const auto script_sig = CScript{};
414 const auto script_wit_stack = fuzzed_data_provider.ConsumeBool() ? P2WSH_EMPTY_TRUE_STACK : P2WSH_EMPTY_TWO_STACK;
415
416 CTxIn in;
417 in.prevout = outpoint;
418 in.nSequence = sequence;
419 in.scriptSig = script_sig;
420 in.scriptWitness.stack = script_wit_stack;
421
422 tx_mut.vin.push_back(in);
423 }
424
425 // Duplicate an input
426 bool dup_input = fuzzed_data_provider.ConsumeBool();
427 if (dup_input) {
428 tx_mut.vin.push_back(tx_mut.vin.back());
429 }
430
431 // Refer to a non-existent input
432 if (fuzzed_data_provider.ConsumeBool()) {
433 tx_mut.vin.emplace_back();
434 }
435
436 // Make a p2pk output to make sigops adjusted vsize to violate TRUC rules, potentially, which is never spent
437 if (last_tx && amount_in > 1000 && fuzzed_data_provider.ConsumeBool()) {
438 tx_mut.vout.emplace_back(1000, CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG);
439 // Don't add any other outputs.
440 num_out = 1;
441 amount_in -= 1000;
442 }
443
444 const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, amount_in);
445 const auto amount_out = (amount_in - amount_fee) / num_out;
446 for (int i = 0; i < num_out; ++i) {
447 tx_mut.vout.emplace_back(amount_out, P2WSH_EMPTY);
448 }
449 auto tx = MakeTransactionRef(tx_mut);
450 // Restore previously removed outpoints, except in-package outpoints
451 if (!last_tx) {
452 for (const auto& in : tx->vin) {
453 // It's a fake input, or a new input, or a duplicate
454 Assert(in == CTxIn() || outpoints.insert(in.prevout).second || dup_input);
455 }
456 // Cache the in-package outpoints being made
457 for (size_t i = 0; i < tx->vout.size(); ++i) {
458 package_outpoints.emplace(tx->GetHash(), i);
459 }
460 }
461 // We need newly-created values for the duration of this run
462 for (size_t i = 0; i < tx->vout.size(); ++i) {
463 outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
464 }
465 return tx;
466 }());
467 }
468
469 if (fuzzed_data_provider.ConsumeBool()) {
470 MockTime(fuzzed_data_provider, chainstate);
471 }
472 if (fuzzed_data_provider.ConsumeBool()) {
473 tx_pool.RollingFeeUpdate();
474 }
475 if (fuzzed_data_provider.ConsumeBool()) {
476 const auto& txid = fuzzed_data_provider.ConsumeBool() ?
477 txs.back()->GetHash() :
478 PickValue(fuzzed_data_provider, mempool_outpoints).hash;
479 const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
480 tx_pool.PrioritiseTransaction(txid, delta);
481 }
482
483 // Remember all added transactions
484 std::set<CTransactionRef> added;
485 auto txr = std::make_shared<TransactionsDelta>(added);
486 node.validation_signals->RegisterSharedValidationInterface(txr);
487
488 // When there are multiple transactions in the package, we call ProcessNewPackage(txs, test_accept=false)
489 // and AcceptToMemoryPool(txs.back(), test_accept=true). When there is only 1 transaction, we might flip it
490 // (the package is a test accept and ATMP is a submission).
491 auto single_submit = txs.size() == 1 && fuzzed_data_provider.ConsumeBool();
492
493 // Exercise client_maxfeerate logic
494 std::optional<CFeeRate> client_maxfeerate{};
495 if (fuzzed_data_provider.ConsumeBool()) {
496 client_maxfeerate = CFeeRate(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1, 50 * COIN), 100);
497 }
498
499 const auto result_package = WITH_LOCK(::cs_main,
500 return ProcessNewPackage(chainstate, tx_pool, txs, /*test_accept=*/single_submit, client_maxfeerate));
501
502 // Always set bypass_limits to false because it is not supported in ProcessNewPackage and
503 // can be a source of divergence.
504 const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, txs.back(), GetTime(),
505 /*bypass_limits=*/false, /*test_accept=*/!single_submit));
506 const bool passed = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
507
508 node.validation_signals->SyncWithValidationInterfaceQueue();
509 node.validation_signals->UnregisterSharedValidationInterface(txr);
510
511 // There is only 1 transaction in the package. We did a test-package-accept and a ATMP
512 if (single_submit) {
513 Assert(passed != added.empty());
514 Assert(passed == res.m_state.IsValid());
515 if (passed) {
516 Assert(added.size() == 1);
517 Assert(txs.back() == *added.begin());
518 }
519 } else if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
520 // We don't know anything about the validity since transactions were randomly generated, so
521 // just use result_package.m_state here. This makes the expect_valid check meaningless, but
522 // we can still verify that the contents of m_tx_results are consistent with m_state.
523 const bool expect_valid{result_package.m_state.IsValid()};
524 Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, &tx_pool));
525 } else {
526 // This is empty if it fails early checks, or "full" if transactions are looked at deeper
527 Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());
528 }
529
531
532 // Dust checks only make sense when dust is enforced
533 if (tx_pool.m_opts.require_standard) {
535 }
536 }
537
538 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
539
540 WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
541}
542} // namespace
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
static void pool cs
#define Assert(val)
Identity function.
Definition: check.h:106
uint32_t nTime
Definition: chain.h:189
int64_t GetMedianTimePast() const
Definition: chain.h:278
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:433
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:35
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
static const uint32_t CURRENT_VERSION
Definition: transaction.h:299
An input of a transaction.
Definition: transaction.h:67
uint32_t nSequence
Definition: transaction.h:71
CScript scriptSig
Definition: transaction.h:70
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:72
COutPoint prevout
Definition: transaction.h:69
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:281
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:367
const Options m_opts
Definition: txmempool.h:421
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:858
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:872
An output of a transaction.
Definition: transaction.h:150
Implement this to subscribe to events generated in validation and mempool.
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:531
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:613
T ConsumeIntegralInRange(T min, T max)
Generate a new block, without valid proof-of-work.
Definition: miner.h:151
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
#define FUZZ_TARGET(...)
Definition: fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
uint64_t sequence
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: messages.h:20
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
unsigned int nBytesPerSigOp
Definition: settings.cpp:10
std::vector< uint32_t > GetDust(const CTransaction &tx, CFeeRate dust_relay_rate)
Get the vout index numbers of all dust outputs.
Definition: policy.cpp:70
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_CHECKSIG
Definition: script.h:190
node::NodeContext m_node
Definition: setup_common.h:66
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
std::vector< std::vector< unsigned char > > stack
Definition: script.h:588
Testing setup that configures a complete environment.
Definition: setup_common.h:121
const CTransactionRef m_tx
Bilingual messages:
Definition: translation.h:24
bool empty() const
Definition: translation.h:35
std::string original
Definition: translation.h:25
int64_t ancestor_count
The maximum allowed number of transactions in a package including the entry and its ancestors.
Options struct containing options for constructing a CTxMemPool.
NodeContext struct containing references to chain state and connection state.
Definition: context.h:56
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define LOCK(cs)
Definition: sync.h:259
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:290
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:155
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:47
COutPoint MineBlock(const NodeContext &node, const node::BlockAssembler::Options &assembler_options)
Returns the generated coin.
Definition: mining.cpp:70
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
static const std::vector< std::vector< uint8_t > > P2WSH_EMPTY_TRUE_STACK
Definition: script.h:30
static const std::vector< std::vector< uint8_t > > P2WSH_EMPTY_TWO_STACK
Definition: script.h:31
static const CScript P2WSH_EMPTY
Definition: script.h:22
void CheckMempoolTRUCInvariants(const CTxMemPool &tx_pool)
For every transaction in tx_pool, check TRUC invariants:
Definition: txmempool.cpp:181
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition: txmempool.cpp:20
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:43
void CheckMempoolEphemeralInvariants(const CTxMemPool &tx_pool)
Check that we never get into a state where an ephemeral dust transaction would be mined without the s...
Definition: txmempool.cpp:144
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
static constexpr decltype(CTransaction::version) TRUC_VERSION
Definition: truc_policy.h:20
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:77
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept, const std::optional< CFeeRate > &client_maxfeerate)
Validate (and maybe submit) a package to the mempool.
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept)
Try to add a transaction to the mempool.