Bitcoin Core  27.99.0
P2P Digital Currency
rpc_mempool.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 <kernel/cs_main.h>
7 #include <kernel/mempool_entry.h>
8 #include <rpc/mempool.h>
10 #include <txmempool.h>
11 #include <util/chaintype.h>
12 
13 #include <univalue.h>
14 
15 
16 static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
17 {
18  LockPoints lp;
19  pool.addUnchecked(CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
20 }
21 
22 static void RpcMempool(benchmark::Bench& bench)
23 {
24  const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
25  CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
26  LOCK2(cs_main, pool.cs);
27 
28  for (int i = 0; i < 1000; ++i) {
30  tx.vin.resize(1);
31  tx.vin[0].scriptSig = CScript() << OP_1;
32  tx.vin[0].scriptWitness.stack.push_back({1});
33  tx.vout.resize(1);
34  tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
35  tx.vout[0].nValue = i;
36  const CTransactionRef tx_r{MakeTransactionRef(tx)};
37  AddTx(tx_r, /*fee=*/i, pool);
38  }
39 
40  bench.run([&] {
41  (void)MempoolToJSON(pool, /*verbose=*/true);
42  });
43 }
44 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define Assert(val)
Identity function.
Definition: check.h:77
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
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:304
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:391
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
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
@ HIGH
Definition: bench.h:47
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
UniValue MempoolToJSON(const CTxMemPool &pool, bool verbose, bool include_mempool_sequence)
Mempool to JSON.
Definition: mempool.cpp:339
static void RpcMempool(benchmark::Bench &bench)
Definition: rpc_mempool.cpp:22
BENCHMARK(RpcMempool, benchmark::PriorityLevel::HIGH)
static void AddTx(const CTransactionRef &tx, const CAmount &fee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
@ OP_EQUAL
Definition: script.h:145
@ OP_1
Definition: script.h:82
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
#define LOCK2(cs1, cs2)
Definition: sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49