Bitcoin ABC  0.29.2
P2P Digital Currency
block_assemble.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 <config.h>
7 #include <consensus/validation.h>
8 #include <script/standard.h>
9 #include <test/util/mining.h>
10 #include <test/util/setup_common.h>
11 #include <test/util/wallet.h>
12 #include <txmempool.h>
13 #include <validation.h>
14 
15 #include <vector>
16 
17 static void AssembleBlock(benchmark::Bench &bench) {
18  const Config &config = GetConfig();
19  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
20 
21  const CScript redeemScript = CScript() << OP_DROP << OP_TRUE;
22  const CScript SCRIPT_PUB =
23  CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemScript))
24  << OP_EQUAL;
25 
26  const CScript scriptSig = CScript() << std::vector<uint8_t>(100, 0xff)
27  << ToByteVector(redeemScript);
28 
29  // Collect some loose transactions that spend the coinbases of our mined
30  // 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(config, test_setup->m_node, SCRIPT_PUB));
36  tx.vin.back().scriptSig = scriptSig;
37  tx.vout.emplace_back(1337 * SATOSHI, SCRIPT_PUB);
38  if (NUM_BLOCKS - b >= COINBASE_MATURITY) {
39  txs.at(b) = MakeTransactionRef(tx);
40  }
41  }
42 
43  {
44  LOCK(::cs_main);
45 
46  for (const auto &txr : txs) {
47  const MempoolAcceptResult res =
48  test_setup->m_node.chainman->ProcessTransaction(txr);
50  }
51  }
52 
53  bench.run([&] { PrepareBlock(config, test_setup->m_node, SCRIPT_PUB); });
54 }
55 
static constexpr Amount SATOSHI
Definition: amount.h:143
static void AssembleBlock(benchmark::Bench &bench)
BENCHMARK(AssembleBlock)
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxOut > vout
Definition: transaction.h:277
std::vector< CTxIn > vin
Definition: transaction.h:276
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:24
Definition: config.h:17
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
const Config & GetConfig()
Definition: config.cpp:34
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule).
Definition: consensus.h:32
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
std::vector< uint8_t > ToByteVector(const T &in)
Definition: script.h:42
@ OP_EQUAL
Definition: script.h:119
@ OP_HASH160
Definition: script.h:160
@ OP_TRUE
Definition: script.h:57
@ OP_DROP
Definition: script.h:97
Validation result for a single transaction mempool acceptance.
Definition: validation.h:186
const ResultType m_result_type
Definition: validation.h:196
@ VALID
Fully validated, valid.
#define LOCK(cs)
Definition: sync.h:306
assert(!tx.IsCoinBase())