Bitcoin Core  29.1.0
P2P Digital Currency
mempool_ephemeral_spends.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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>
7 #include <kernel/cs_main.h>
9 #include <policy/policy.h>
10 #include <primitives/transaction.h>
11 #include <script/script.h>
12 #include <sync.h>
13 #include <test/util/setup_common.h>
14 #include <test/util/txmempool.h>
15 #include <txmempool.h>
16 #include <util/check.h>
17 
18 #include <cstdint>
19 #include <memory>
20 #include <vector>
21 
22 
23 static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
24 {
25  int64_t nTime{0};
26  unsigned int nHeight{1};
27  uint64_t sequence{0};
28  bool spendsCoinbase{false};
29  unsigned int sigOpCost{4};
30  uint64_t fee{0};
33  tx, fee, nTime, nHeight, sequence,
35 }
36 
38 {
39  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
40 
41  int number_outputs{1000};
42  if (bench.complexityN() > 1) {
43  number_outputs = static_cast<int>(bench.complexityN());
44  }
45 
46  // Tx with many outputs
48  tx1.vin.resize(1);
49  tx1.vout.resize(number_outputs);
50  for (size_t i = 0; i < tx1.vout.size(); i++) {
51  tx1.vout[i].scriptPubKey = CScript();
52  // Each output progressively larger
53  tx1.vout[i].nValue = i * CENT;
54  }
55 
56  const auto& parent_txid = tx1.GetHash();
57 
58  // Spends all outputs of tx1, other details don't matter
60  tx2.vin.resize(tx1.vout.size());
61  for (size_t i = 0; i < tx2.vin.size(); i++) {
62  tx2.vin[0].prevout.hash = parent_txid;
63  tx2.vin[0].prevout.n = i;
64  }
65  tx2.vout.resize(1);
66 
67  CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
68  LOCK2(cs_main, pool.cs);
69  // Create transaction references outside the "hot loop"
70  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
71  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
72 
73  AddTx(tx1_r, pool);
74 
75  uint32_t iteration{0};
76 
77  TxValidationState dummy_state;
78  Wtxid dummy_wtxid;
79 
80  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
81 
82  CheckEphemeralSpends({tx2_r}, /*dust_relay_rate=*/CFeeRate(iteration * COIN / 10), pool, dummy_state, dummy_wtxid);
83  iteration++;
84  });
85 }
86 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51
BENCHMARK(MempoolCheckEphemeralSpends, benchmark::PriorityLevel::HIGH)
Bench & complexityN(T n) noexcept
Definition: nanobench.h:1265
std::vector< CTxIn > vin
Definition: transaction.h:379
static void MempoolCheckEphemeralSpends(benchmark::Bench &bench)
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
unsigned int sigOpCost
#define LOCK2(cs1, cs2)
Definition: sync.h:258
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
AddToMempool(pool, CTxMemPoolEntry(tx, fee, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp))
std::vector< CTxOut > vout
Definition: transaction.h:380
unsigned int nHeight
bool CheckEphemeralSpends(const Package &package, CFeeRate dust_relay_rate, const CTxMemPool &tx_pool, TxValidationState &out_child_state, Wtxid &out_child_wtxid)
Must be called for each transaction(package) if any dust is in the package.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:303
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
bool spendsCoinbase
A mutable version of CTransaction.
Definition: transaction.h:377
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
static constexpr CAmount CENT
Definition: setup_common.h:47
uint64_t sequence
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
LockPoints lp
uint64_t fee
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:390
#define Assert(val)
Identity function.
Definition: check.h:85
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15