MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / CreateTestBlock

Function CreateTestBlock

src/bench/connectblock.cpp:36–75  ·  view source on GitHub ↗

* Creates a test block containing transactions with the following properties: * - Each transaction has the same number of inputs and outputs * - All Taproot inputs use simple key path spends (no script path spends) * - All signatures use SIGHASH_ALL (default sighash) * - Each transaction spends all outputs from the previous transaction */

Source from the content-addressed store, hash-verified

34 * - Each transaction spends all outputs from the previous transaction
35 */
36CBlock CreateTestBlock(
37 TestChain100Setup& test_setup,
38 const std::vector<CKey>& keys,
39 const std::vector<CTxOut>& outputs,
40 int num_txs = 1000)
41{
42 Chainstate& chainstate{test_setup.m_node.chainman->ActiveChainstate()};
43
44 const WitnessV1Taproot coinbase_taproot{XOnlyPubKey(test_setup.coinbaseKey.GetPubKey())};
45
46 // Create the outputs that will be spent in the first transaction of the test block
47 // Doing this in a separate block excludes the validation of its inputs from the benchmark
48 auto& coinbase_to_spend{test_setup.m_coinbase_txns[0]};
49 const auto [first_tx, _]{test_setup.CreateValidTransaction(
50 {coinbase_to_spend},
51 {COutPoint(coinbase_to_spend->GetHash(), 0)},
52 chainstate.m_chain.Height() + 1, keys, outputs, {}, {})};
53 const CScript coinbase_spk{GetScriptForDestination(coinbase_taproot)};
54 test_setup.CreateAndProcessBlock({first_tx}, coinbase_spk);
55
56 std::vector<CMutableTransaction> txs;
57 txs.reserve(num_txs);
58 CTransactionRef tx_to_spend{MakeTransactionRef(first_tx)};
59 for (int i{0}; i < num_txs; i++) {
60 std::vector<COutPoint> inputs;
61 inputs.reserve(outputs.size());
62
63 for (size_t j{0}; j < outputs.size(); j++) {
64 inputs.emplace_back(tx_to_spend->GetHash(), j);
65 }
66
67 const auto [tx, _]{test_setup.CreateValidTransaction(
68 {tx_to_spend}, inputs, chainstate.m_chain.Height() + 1, keys, outputs, {}, {})};
69 txs.emplace_back(tx);
70 tx_to_spend = MakeTransactionRef(tx);
71 }
72
73 // Coinbase output can use any output type as it is not spent and will not change the benchmark
74 return test_setup.CreateBlock(txs, coinbase_spk);
75}
76
77/*
78 * Creates key pairs and corresponding outputs for the benchmark transactions.

Callers 1

BenchmarkConnectBlockFunction · 0.70

Calls 13

GetScriptForDestinationFunction · 0.85
MakeTransactionRefFunction · 0.85
HeightMethod · 0.80
CreateAndProcessBlockMethod · 0.80
XOnlyPubKeyClass · 0.50
COutPointClass · 0.50
GetPubKeyMethod · 0.45
GetHashMethod · 0.45
reserveMethod · 0.45
sizeMethod · 0.45
emplace_backMethod · 0.45

Tested by

no test coverage detected