Bitcoin Core 31.99.0
P2P Digital Currency
mempool_ephemeral_spends.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-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 <bench/bench.h>
6#include <consensus/amount.h>
8#include <kernel/cs_main.h>
10#include <policy/feerate.h>
12#include <script/script.h>
13#include <sync.h>
15#include <test/util/txmempool.h>
16#include <txmempool.h>
17#include <util/check.h>
18
19#include <cstddef>
20#include <cstdint>
21#include <memory>
22#include <vector>
23
24
25static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
26{
27 int64_t nTime{0};
28 unsigned int nHeight{1};
29 uint64_t sequence{0};
30 bool spendsCoinbase{false};
31 unsigned int sigOpCost{4};
32 uint64_t fee{0};
35 tx, fee, nTime, nHeight, sequence,
37}
38
40{
41 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
42
43 int number_outputs{1000};
44 if (bench.complexityN() > 1) {
45 number_outputs = static_cast<int>(bench.complexityN());
46 }
47
48 // Tx with many outputs
50 tx1.vin.resize(1);
51 tx1.vout.resize(number_outputs);
52 for (size_t i = 0; i < tx1.vout.size(); i++) {
53 tx1.vout[i].scriptPubKey = CScript();
54 // Each output progressively larger
55 tx1.vout[i].nValue = i * CENT;
56 }
57
58 const auto& parent_txid = tx1.GetHash();
59
60 // Spends all outputs of tx1, other details don't matter
62 tx2.vin.resize(tx1.vout.size());
63 for (size_t i = 0; i < tx2.vin.size(); i++) {
64 tx2.vin[i].prevout.hash = parent_txid;
65 tx2.vin[i].prevout.n = i;
66 }
67 tx2.vout.resize(1);
68
69 CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
70 LOCK2(cs_main, pool.cs);
71 // Create transaction references outside the "hot loop"
72 const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
73 const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
74
75 AddTx(tx1_r, pool);
76 assert(tx2_r->vin.back().prevout == COutPoint(parent_txid, tx1_r->vout.size() - 1));
77
78 uint32_t iteration{0};
79
80 TxValidationState dummy_state;
81 Wtxid dummy_wtxid;
82
83 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
84
85 CheckEphemeralSpends({tx2_r}, /*dust_relay_rate=*/CFeeRate(iteration * COIN / 10), pool, dummy_state, dummy_wtxid);
86 iteration++;
87 });
88}
89
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assert(val)
Identity function.
Definition: check.h:116
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:32
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:406
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:187
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1308
Bench & complexityN(T n) noexcept
Definition: nanobench.h:1339
transaction_identifier represents the two canonical transaction identifier types (txid,...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
bool CheckEphemeralSpends(const Package &package, CFeeRate dust_relay_rate, const CTxMemPool &tx_pool, TxValidationState &out_child_state, Wtxid &out_child_wtxid)
Called for each transaction(package) if any dust is in the package.
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
unsigned int nHeight
static void MempoolCheckEphemeralSpends(benchmark::Bench &bench)
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp))
BENCHMARK(MempoolCheckEphemeralSpends)
uint64_t fee
bool spendsCoinbase
unsigned int sigOpCost
uint64_t sequence
LockPoints lp
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static constexpr CAmount CENT
Definition: setup_common.h:41
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
std::vector< CTxIn > vin
Definition: transaction.h:359
#define LOCK2(cs1, cs2)
Definition: sync.h:269
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51
assert(!tx.IsCoinBase())