Bitcoin ABC  0.22.12
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::State &state) {
18  const Config &config = GetConfig();
19 
20  const CScript redeemScript = CScript() << OP_DROP << OP_TRUE;
21  const CScript SCRIPT_PUB =
22  CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemScript))
23  << OP_EQUAL;
24 
25  const CScript scriptSig = CScript() << std::vector<uint8_t>(100, 0xff)
26  << ToByteVector(redeemScript);
27 
28  // Collect some loose transactions that spend the coinbases of our mined
29  // blocks
30  constexpr size_t NUM_BLOCKS{200};
31  std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
32  for (size_t b = 0; b < NUM_BLOCKS; ++b) {
34  tx.vin.push_back(
35  MineBlock(config, g_testing_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  // Required for ::AcceptToMemoryPool.
45  LOCK(::cs_main);
46 
47  for (const auto &txr : txs) {
48  TxValidationState vstate;
49  bool ret{::AcceptToMemoryPool(config, ::g_mempool, vstate, txr,
50  false /* bypass_limits */,
51  /* nAbsurdFee */ Amount::zero())};
52  assert(ret);
53  }
54  }
55 
56  while (state.KeepRunning()) {
57  PrepareBlock(config, g_testing_setup->m_node, SCRIPT_PUB);
58  }
59 }
60 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
static constexpr Amount zero()
Definition: amount.h:35
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:339
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)...
Definition: consensus.h:32
bool KeepRunning()
Definition: bench.h:75
static constexpr Amount SATOSHI
Definition: amount.h:151
std::vector< CTxOut > vout
Definition: transaction.h:300
std::vector< CTxIn > vin
Definition: transaction.h:299
const RegTestingSetup * g_testing_setup
A pointer to the current testing setup.
Definition: bench.cpp:19
Definition: config.h:19
static void AssembleBlock(benchmark::State &state)
BENCHMARK(AssembleBlock, 700)
#define LOCK(cs)
Definition: sync.h:230
CTxMemPool g_mempool
Definition: validation.cpp:117
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
const Config & GetConfig()
Definition: config.cpp:34
std::vector< uint8_t > ToByteVector(const T &in)
Definition: script.h:41
Definition: script.h:96
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:429
A mutable version of CTransaction.
Definition: transaction.h:297
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
Definition: script.h:56
bool AcceptToMemoryPool(const Config &config, CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, bool bypass_limits, const Amount nAbsurdFee, bool test_accept)
(try to) add transaction to memory pool
Definition: validation.cpp:781