Bitcoin Core  22.99.0
P2P Digital Currency
block_assemble.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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/validation.h>
7 #include <crypto/sha256.h>
8 #include <test/util/mining.h>
9 #include <test/util/script.h>
10 #include <test/util/setup_common.h>
11 #include <test/util/wallet.h>
12 #include <txmempool.h>
13 #include <validation.h>
14 
15 
16 #include <vector>
17 
18 static void AssembleBlock(benchmark::Bench& bench)
19 {
20  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
21 
22  CScriptWitness witness;
23  witness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
24 
25  // Collect some loose transactions that spend the coinbases of our mined blocks
26  constexpr size_t NUM_BLOCKS{200};
27  std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
28  for (size_t b{0}; b < NUM_BLOCKS; ++b) {
30  tx.vin.push_back(MineBlock(test_setup->m_node, P2WSH_OP_TRUE));
31  tx.vin.back().scriptWitness = witness;
32  tx.vout.emplace_back(1337, P2WSH_OP_TRUE);
33  if (NUM_BLOCKS - b >= COINBASE_MATURITY)
34  txs.at(b) = MakeTransactionRef(tx);
35  }
36  {
37  LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
38 
39  for (const auto& txr : txs) {
40  const MempoolAcceptResult res = ::AcceptToMemoryPool(test_setup->m_node.chainman->ActiveChainstate(), *test_setup->m_node.mempool, txr, false /* bypass_limits */);
42  }
43  }
44 
45  bench.run([&] {
46  PrepareBlock(test_setup->m_node, P2WSH_OP_TRUE);
47  });
48 }
49 
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
assert
assert(!tx.IsCoinBase())
PrepareBlock
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Prepare a block to be mined.
Definition: mining.cpp:74
COINBASE_MATURITY
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
setup_common.h
MempoolAcceptResult::m_result_type
const ResultType m_result_type
Definition: validation.h:155
AcceptToMemoryPool
MempoolAcceptResult AcceptToMemoryPool(CChainState &active_chainstate, CTxMemPool &pool, const CTransactionRef &tx, bool bypass_limits, bool test_accept)
(Try to) add a transaction to the memory pool.
Definition: validation.cpp:1045
validation.h
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
MineBlock
CTxIn MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:59
txmempool.h
CScriptWitness
Definition: script.h:557
WITNESS_STACK_ELEM_OP_TRUE
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
Definition: script.h:11
script.h
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
bench.h
sha256.h
MempoolAcceptResult::ResultType::VALID
@ VALID
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
BENCHMARK
BENCHMARK(AssembleBlock)
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:117
LOCK
#define LOCK(cs)
Definition: sync.h:226
MempoolAcceptResult
Validation result for a single transaction mempool acceptance.
Definition: validation.h:149
AssembleBlock
static void AssembleBlock(benchmark::Bench &bench)
Definition: block_assemble.cpp:18
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition: script.h:561
wallet.h
mining.h
P2WSH_OP_TRUE
static const CScript P2WSH_OP_TRUE
Definition: script.h:12