Bitcoin Core  0.19.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::State& state)
18 {
19  const std::vector<unsigned char> op_true{OP_TRUE};
20  CScriptWitness witness;
21  witness.stack.push_back(op_true);
22 
23  uint256 witness_program;
24  CSHA256().Write(&op_true[0], op_true.size()).Finalize(witness_program.begin());
25 
26  const CScript SCRIPT_PUB{CScript(OP_0) << std::vector<unsigned char>{witness_program.begin(), witness_program.end()}};
27 
28  // Collect some loose transactions that spend the coinbases of our mined blocks
29  constexpr size_t NUM_BLOCKS{200};
30  std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
31  for (size_t b{0}; b < NUM_BLOCKS; ++b) {
33  tx.vin.push_back(MineBlock(g_testing_setup->m_node, SCRIPT_PUB));
34  tx.vin.back().scriptWitness = witness;
35  tx.vout.emplace_back(1337, SCRIPT_PUB);
36  if (NUM_BLOCKS - b >= COINBASE_MATURITY)
37  txs.at(b) = MakeTransactionRef(tx);
38  }
39  {
40  LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
41 
42  for (const auto& txr : txs) {
43  TxValidationState state;
44  bool ret{::AcceptToMemoryPool(::mempool, state, txr, nullptr /* plTxnReplaced */, false /* bypass_limits */, /* nAbsurdFee */ 0)};
45  assert(ret);
46  }
47  }
48 
49  while (state.KeepRunning()) {
50  PrepareBlock(g_testing_setup->m_node, SCRIPT_PUB);
51  }
52 }
53 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
CTxMemPool mempool
std::vector< CTxIn > vin
Definition: transaction.h:368
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:409
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:574
bool KeepRunning()
Definition: bench.h:72
unsigned char * begin()
Definition: uint256.h:54
unsigned char * end()
Definition: uint256.h:59
const RegTestingSetup * g_testing_setup
A pointer to the current testing setup.
Definition: bench.cpp:18
static void AssembleBlock(benchmark::State &state)
BENCHMARK(AssembleBlock, 700)
bool AcceptToMemoryPool(CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
#define LOCK(cs)
Definition: sync.h:179
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:106
std::vector< CTxOut > vout
Definition: transaction.h:369
256-bit opaque blob.
Definition: uint256.h:120
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A mutable version of CTransaction.
Definition: transaction.h:366
A hasher class for SHA-256.
Definition: sha256.h:13
Definition: script.h:65
Definition: script.h:57