Bitcoin Core  0.20.99
P2P Digital Currency
block_assemble.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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  TestingSetup test_setup{
21  /* extra_args */ {
22  "-nodebuglogfile",
23  "-nodebug",
24  },
25  };
26 
27  const std::vector<unsigned char> op_true{OP_TRUE};
28  CScriptWitness witness;
29  witness.stack.push_back(op_true);
30 
31  uint256 witness_program;
32  CSHA256().Write(&op_true[0], op_true.size()).Finalize(witness_program.begin());
33 
34  const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};
35 
36  // Collect some loose transactions that spend the coinbases of our mined blocks
37  constexpr size_t NUM_BLOCKS{200};
38  std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
39  for (size_t b{0}; b < NUM_BLOCKS; ++b) {
41  tx.vin.push_back(MineBlock(test_setup.m_node, SCRIPT_PUB));
42  tx.vin.back().scriptWitness = witness;
43  tx.vout.emplace_back(1337, SCRIPT_PUB);
44  if (NUM_BLOCKS - b >= COINBASE_MATURITY)
45  txs.at(b) = MakeTransactionRef(tx);
46  }
47  {
48  LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
49 
50  for (const auto& txr : txs) {
51  TxValidationState state;
52  bool ret{::AcceptToMemoryPool(*test_setup.m_node.mempool, state, txr, nullptr /* plTxnReplaced */, false /* bypass_limits */)};
53  assert(ret);
54  }
55  }
56 
57  bench.run([&] {
58  PrepareBlock(test_setup.m_node, SCRIPT_PUB);
59  });
60 }
61 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:395
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
static const std::string REGTEST
std::vector< CTxIn > vin
Definition: transaction.h:355
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:396
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
Definition: consensus.h:19
std::vector< std::vector< unsigned char > > stack
Definition: script.h:560
BENCHMARK(AssembleBlock)
unsigned char * begin()
Definition: uint256.h:58
unsigned char * end()
Definition: uint256.h:63
bool AcceptToMemoryPool(CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, bool test_accept, CAmount *fee_out)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
#define LOCK(cs)
Definition: sync.h:230
static void AssembleBlock(benchmark::Bench &bench)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:129
std::vector< CTxOut > vout
Definition: transaction.h:356
256-bit opaque blob.
Definition: uint256.h:124
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
A mutable version of CTransaction.
Definition: transaction.h:353
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
A hasher class for SHA-256.
Definition: sha256.h:13
Definition: script.h:76
Definition: script.h:68