Bitcoin Core 31.99.0
P2P Digital Currency
connectblock.cpp
Go to the documentation of this file.
1// Copyright (c) 2025-present 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 <addresstype.h>
6#include <bench/bench.h>
7#include <chain.h>
8#include <coins.h>
9#include <consensus/amount.h>
11#include <key.h>
12#include <node/blockstorage.h>
13#include <policy/feerate.h>
14#include <primitives/block.h>
16#include <pubkey.h>
17#include <script/script.h>
18#include <sync.h>
20#include <validation.h>
21
22#include <cassert>
23#include <cstddef>
24#include <memory>
25#include <optional>
26#include <string>
27#include <utility>
28#include <vector>
29
30/*
31 * Creates a test block containing transactions with the following properties:
32 * - Each transaction has the same number of inputs and outputs
33 * - All Taproot inputs use simple key path spends (no script path spends)
34 * - All signatures use SIGHASH_ALL (default sighash)
35 * - Each transaction spends all outputs from the previous transaction
36 */
38 TestChain100Setup& test_setup,
39 const std::vector<CKey>& keys,
40 const std::vector<CTxOut>& outputs,
41 int num_txs = 1000)
42{
43 Chainstate& chainstate{test_setup.m_node.chainman->ActiveChainstate()};
44
45 const WitnessV1Taproot coinbase_taproot{XOnlyPubKey(test_setup.coinbaseKey.GetPubKey())};
46
47 // Create the outputs that will be spent in the first transaction of the test block
48 // Doing this in a separate block excludes the validation of its inputs from the benchmark
49 auto& coinbase_to_spend{test_setup.m_coinbase_txns[0]};
50 const auto [first_tx, _]{test_setup.CreateValidTransaction(
51 {coinbase_to_spend},
52 {COutPoint(coinbase_to_spend->GetHash(), 0)},
53 chainstate.m_chain.Height() + 1, keys, outputs, {}, {})};
54 const CScript coinbase_spk{GetScriptForDestination(coinbase_taproot)};
55 test_setup.CreateAndProcessBlock({first_tx}, coinbase_spk);
56
57 std::vector<CMutableTransaction> txs;
58 txs.reserve(num_txs);
59 CTransactionRef tx_to_spend{MakeTransactionRef(first_tx)};
60 for (int i{0}; i < num_txs; i++) {
61 std::vector<COutPoint> inputs;
62 inputs.reserve(outputs.size());
63
64 for (size_t j{0}; j < outputs.size(); j++) {
65 inputs.emplace_back(tx_to_spend->GetHash(), j);
66 }
67
68 const auto [tx, _]{test_setup.CreateValidTransaction(
69 {tx_to_spend}, inputs, chainstate.m_chain.Height() + 1, keys, outputs, {}, {})};
70 txs.emplace_back(tx);
71 tx_to_spend = MakeTransactionRef(tx);
72 }
73
74 // Coinbase output can use any output type as it is not spent and will not change the benchmark
75 return test_setup.CreateBlock(txs, coinbase_spk);
76}
77
78/*
79 * Creates key pairs and corresponding outputs for the benchmark transactions.
80 * - For Schnorr signatures: Creates simple key path spendable outputs
81 * - For Ecdsa signatures: Creates P2WPKH (native SegWit v0) outputs
82 * - All outputs have value of 1 BTC
83 */
84std::pair<std::vector<CKey>, std::vector<CTxOut>> CreateKeysAndOutputs(const CKey& coinbaseKey, size_t num_schnorr, size_t num_ecdsa)
85{
86 std::vector<CKey> keys{coinbaseKey};
87 keys.reserve(num_schnorr + num_ecdsa + 1);
88
89 std::vector<CTxOut> outputs;
90 outputs.reserve(num_schnorr + num_ecdsa);
91
92 for (size_t i{0}; i < num_ecdsa; ++i) {
93 keys.emplace_back(GenerateRandomKey());
94 outputs.emplace_back(COIN, GetScriptForDestination(WitnessV0KeyHash{keys.back().GetPubKey()}));
95 }
96
97 for (size_t i{0}; i < num_schnorr; ++i) {
98 keys.emplace_back(GenerateRandomKey());
99 outputs.emplace_back(COIN, GetScriptForDestination(WitnessV1Taproot{XOnlyPubKey(keys.back().GetPubKey())}));
100 }
101
102 return {keys, outputs};
103}
104
105void BenchmarkConnectBlock(benchmark::Bench& bench, std::vector<CKey>& keys, std::vector<CTxOut>& outputs, TestChain100Setup& test_setup)
106{
107 const auto& test_block{CreateTestBlock(test_setup, keys, outputs)};
108 bench.unit("block").run([&] {
109 LOCK(cs_main);
110 auto& chainman{test_setup.m_node.chainman};
111 auto& chainstate{chainman->ActiveChainstate()};
112 BlockValidationState test_block_state;
113 auto* pindex{chainman->m_blockman.AddToBlockIndex(test_block, chainman->m_best_header)}; // Doing this here doesn't impact the benchmark
114 CCoinsViewCache viewNew{&chainstate.CoinsTip()};
115
116 assert(chainstate.ConnectBlock(test_block, test_block_state, pindex, viewNew));
117 });
118}
119
121{
122 const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
123 auto [keys, outputs]{CreateKeysAndOutputs(test_setup->coinbaseKey, /*num_schnorr=*/5, /*num_ecdsa=*/0)};
124 BenchmarkConnectBlock(bench, keys, outputs, *test_setup);
125}
126
128{
129 const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
130 // Blocks in range 848000 to 868000 have a roughly 20 to 80 ratio of schnorr to ecdsa inputs
131 auto [keys, outputs]{CreateKeysAndOutputs(test_setup->coinbaseKey, /*num_schnorr=*/1, /*num_ecdsa=*/4)};
132 BenchmarkConnectBlock(bench, keys, outputs, *test_setup);
133}
134
136{
137 const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
138 auto [keys, outputs]{CreateKeysAndOutputs(test_setup->coinbaseKey, /*num_schnorr=*/0, /*num_ecdsa=*/5)};
139 BenchmarkConnectBlock(bench, keys, outputs, *test_setup);
140}
141
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
Definition: block.h:74
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:394
An encapsulated private key.
Definition: key.h:37
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:551
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:633
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1292
Bench & unit(char const *unit)
Sets the operation unit.
void BenchmarkConnectBlock(benchmark::Bench &bench, std::vector< CKey > &keys, std::vector< CTxOut > &outputs, TestChain100Setup &test_setup)
BENCHMARK(ConnectBlockAllSchnorr)
static void ConnectBlockMixedEcdsaSchnorr(benchmark::Bench &bench)
static void ConnectBlockAllEcdsa(benchmark::Bench &bench)
static void ConnectBlockAllSchnorr(benchmark::Bench &bench)
std::pair< std::vector< CKey >, std::vector< CTxOut > > CreateKeysAndOutputs(const CKey &coinbaseKey, size_t num_schnorr, size_t num_ecdsa)
CBlock CreateTestBlock(TestChain100Setup &test_setup, const std::vector< CKey > &keys, const std::vector< CTxOut > &outputs, int num_txs=1000)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:237
node::NodeContext m_node
Definition: setup_common.h:58
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:137
std::vector< CTransactionRef > m_coinbase_txns
Definition: setup_common.h:229
std::pair< CMutableTransaction, CAmount > CreateValidTransaction(const std::vector< CTransactionRef > &input_transactions, const std::vector< COutPoint > &inputs, int input_height, const std::vector< CKey > &input_signing_keys, const std::vector< CTxOut > &outputs, const std::optional< CFeeRate > &feerate, const std::optional< uint32_t > &fee_output)
Create a transaction, optionally setting the fee based on the feerate.
CBlock CreateAndProcessBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
Create a new block with just given transactions, coinbase paying to scriptPubKey, and try to add it t...
CBlock CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
Create a new block with just given transactions, coinbase paying to scriptPubKey.
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:76
#define LOCK(cs)
Definition: sync.h:268
std::vector< uint16_t > keys
Definition: dbwrapper.cpp:398
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
assert(!tx.IsCoinBase())