Bitcoin Core  21.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/setup_common.h>
10 #include <test/util/wallet.h>
11 #include <txmempool.h>
12 #include <validation.h>
13 
14 
15 #include <vector>
16 
17 static void AssembleBlock(benchmark::Bench& bench)
18 {
19  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
20 
21  const std::vector<unsigned char> op_true{OP_TRUE};
22  CScriptWitness witness;
23  witness.stack.push_back(op_true);
24 
25  uint256 witness_program;
26  CSHA256().Write(&op_true[0], op_true.size()).Finalize(witness_program.begin());
27 
28  const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};
29 
30  // Collect some loose transactions that spend the coinbases of our mined blocks
31  constexpr size_t NUM_BLOCKS{200};
32  std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
33  for (size_t b{0}; b < NUM_BLOCKS; ++b) {
35  tx.vin.push_back(MineBlock(test_setup->m_node, SCRIPT_PUB));
36  tx.vin.back().scriptWitness = witness;
37  tx.vout.emplace_back(1337, SCRIPT_PUB);
38  if (NUM_BLOCKS - b >= COINBASE_MATURITY)
39  txs.at(b) = MakeTransactionRef(tx);
40  }
41  {
42  LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
43 
44  for (const auto& txr : txs) {
45  const MempoolAcceptResult res = ::AcceptToMemoryPool(::ChainstateActive(), *test_setup->m_node.mempool, txr, false /* bypass_limits */);
47  }
48  }
49 
50  bench.run([&] {
51  PrepareBlock(test_setup->m_node, SCRIPT_PUB);
52  });
53 }
54 
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
OP_0
@ OP_0
Definition: script.h:68
CSHA256::Write
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
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
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:1118
validation.h
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:131
ChainstateActive
CChainState & ChainstateActive()
Please prefer the identical ChainstateManager::ActiveChainstate.
Definition: validation.cpp:108
txmempool.h
CScriptWitness
Definition: script.h:556
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
uint256
256-bit opaque blob.
Definition: uint256.h:124
CSHA256::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
bench.h
sha256.h
MempoolAcceptResult::ResultType::VALID
@ VALID
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
BENCHMARK
BENCHMARK(AssembleBlock)
LOCK
#define LOCK(cs)
Definition: sync.h:232
OP_TRUE
@ OP_TRUE
Definition: script.h:76
MempoolAcceptResult
Validation result for a single transaction mempool acceptance.
Definition: validation.h:190
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
AssembleBlock
static void AssembleBlock(benchmark::Bench &bench)
Definition: block_assemble.cpp:17
MempoolAcceptResult::m_result_type
ResultType m_result_type
Definition: validation.h:198
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition: script.h:560
base_blob::end
unsigned char * end()
Definition: uint256.h:63