1
- // Copyright (c) 2016-2022 The Bitcoin Core developers
1
+ // Copyright (c) 2025 The Bitcoin Core developers
2
2
// Distributed under the MIT software license, see the accompanying
3
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
@@ -58,50 +58,9 @@ CBlock CreateTestBlock(TestChain100Setup* test_setup, std::vector<CKey>& keys, s
58
58
return test_setup->CreateBlock (txs, coinbase_spk, chainstate);
59
59
}
60
60
61
- static void ConnectBlockAllSchnorr (benchmark::Bench& bench)
62
- {
63
- const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
64
-
65
- size_t num_keys{4 };
66
- std::vector<CKey> keys{test_setup->coinbaseKey };
67
- keys.reserve (num_keys + 1 );
68
-
69
- std::vector<CScript> taproot_spks;
70
- taproot_spks.reserve (num_keys);
71
-
72
- for (size_t i{0 }; i < num_keys; i++) {
73
- const CKey key{GenerateRandomKey ()};
74
- keys.push_back (key);
75
- const CScript scriptpubkey{GetScriptForDestination (WitnessV1Taproot{XOnlyPubKey (key.GetPubKey ())})};
76
- taproot_spks.push_back (scriptpubkey);
77
- }
78
-
79
- const auto test_block{CreateTestBlock (test_setup.get (), keys, taproot_spks)};
80
- auto pindex{std::make_unique<CBlockIndex>(test_block)};
81
- auto test_blockhash{std::make_unique<uint256>(test_block.GetHash ())};
82
-
83
- Chainstate& chainstate{test_setup->m_node .chainman ->ActiveChainstate ()};
84
-
85
- pindex->nHeight = chainstate.m_chain .Height () + 1 ;
86
- pindex->phashBlock = test_blockhash.get ();
87
- pindex->pprev = chainstate.m_chain .Tip ();
88
-
89
- BlockValidationState test_block_state;
90
- bench.unit (" block" ).run ([&] {
91
- LOCK (cs_main);
92
- CCoinsViewCache viewNew{&chainstate.CoinsTip ()};
93
- assert (chainstate.ConnectBlock (test_block, test_block_state, pindex.get (), viewNew, false ));
94
- });
95
- }
96
-
97
- static void ConnectBlockMixed (benchmark::Bench& bench)
61
+ std::pair<std::vector<CKey>, std::vector<CScript>> CreateKeysAndScripts (const CKey& coinbaseKey, size_t num_taproot, size_t num_nontaproot, bool use_schnorr)
98
62
{
99
- const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
100
-
101
- size_t num_taproot{2 };
102
- size_t num_nontaproot{2 };
103
-
104
- std::vector<CKey> keys{test_setup->coinbaseKey };
63
+ std::vector<CKey> keys{coinbaseKey};
105
64
keys.reserve (num_taproot + num_nontaproot + 1 );
106
65
107
66
std::vector<CScript> spks;
@@ -114,16 +73,23 @@ static void ConnectBlockMixed(benchmark::Bench& bench)
114
73
spks.push_back (scriptpubkey);
115
74
}
116
75
117
- for (size_t i{0 }; i < num_taproot; i++) {
118
- const CKey key{GenerateRandomKey ()};
119
- keys.push_back (key);
120
- const CScript scriptpubkey{GetScriptForDestination (WitnessV1Taproot{XOnlyPubKey (key.GetPubKey ())})};
121
- spks.push_back (scriptpubkey);
76
+ if (use_schnorr) {
77
+ for (size_t i{0 }; i < num_taproot; i++) {
78
+ CKey key{GenerateRandomKey ()};
79
+ keys.push_back (key);
80
+ const CScript scriptpubkey{GetScriptForDestination (WitnessV1Taproot{XOnlyPubKey (key.GetPubKey ())})};
81
+ spks.push_back (scriptpubkey);
82
+ }
122
83
}
123
84
124
- const auto test_block{CreateTestBlock (test_setup.get (), keys, spks)};
125
- auto pindex{std::make_unique<CBlockIndex>(test_block)};
126
- auto test_blockhash{std::make_unique<uint256>(test_block.GetHash ())};
85
+ return {keys, spks};
86
+ }
87
+
88
+ void BenchmarkConnectBlock (benchmark::Bench& bench, std::vector<CKey>& keys, std::vector<CScript>& spks, TestChain100Setup* test_setup)
89
+ {
90
+ const auto test_block = CreateTestBlock (test_setup, keys, spks);
91
+ auto pindex = std::make_unique<CBlockIndex>(test_block);
92
+ auto test_blockhash = std::make_unique<uint256>(test_block.GetHash ());
127
93
128
94
Chainstate& chainstate{test_setup->m_node .chainman ->ActiveChainstate ()};
129
95
@@ -139,40 +105,25 @@ static void ConnectBlockMixed(benchmark::Bench& bench)
139
105
});
140
106
}
141
107
142
- static void ConnectBlockNoSchnorr (benchmark::Bench& bench)
108
+ static void ConnectBlockAllSchnorr (benchmark::Bench& bench)
143
109
{
144
- const auto test_setup{MakeNoLogFileContext<TestChain100Setup>()};
145
-
146
- size_t num_keys{4 };
147
- std::vector<CKey> keys{test_setup->coinbaseKey };
148
- keys.reserve (num_keys + 1 );
149
-
150
- std::vector<CScript> spks;
151
- spks.reserve (num_keys);
152
-
153
- for (size_t i{0 }; i < num_keys; i++) {
154
- const CKey key{GenerateRandomKey ()};
155
- keys.push_back (key);
156
- const CScript scriptpubkey{GetScriptForDestination (WitnessV0KeyHash{key.GetPubKey ()})};
157
- spks.push_back (scriptpubkey);
158
- }
159
-
160
- const auto test_block{CreateTestBlock (test_setup.get (), keys, spks)};
161
- auto pindex{std::make_unique<CBlockIndex>(test_block)};
162
- auto test_blockhash{std::make_unique<uint256>(test_block.GetHash ())};
163
-
164
- Chainstate& chainstate{test_setup->m_node .chainman ->ActiveChainstate ()};
110
+ const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
111
+ auto [keys, spks] = CreateKeysAndScripts (test_setup->coinbaseKey , 4 , 0 , true );
112
+ BenchmarkConnectBlock (bench, keys, spks, test_setup.get ());
113
+ }
165
114
166
- pindex->nHeight = chainstate.m_chain .Height () + 1 ;
167
- pindex->phashBlock = test_blockhash.get ();
168
- pindex->pprev = chainstate.m_chain .Tip ();
115
+ static void ConnectBlockMixed (benchmark::Bench& bench)
116
+ {
117
+ const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
118
+ auto [keys, spks] = CreateKeysAndScripts (test_setup->coinbaseKey , 2 , 2 , true );
119
+ BenchmarkConnectBlock (bench, keys, spks, test_setup.get ());
120
+ }
169
121
170
- BlockValidationState test_block_state;
171
- bench.unit (" block" ).run ([&] {
172
- LOCK (cs_main);
173
- CCoinsViewCache viewNew{&chainstate.CoinsTip ()};
174
- assert (chainstate.ConnectBlock (test_block, test_block_state, pindex.get (), viewNew, false ));
175
- });
122
+ static void ConnectBlockNoSchnorr (benchmark::Bench& bench)
123
+ {
124
+ const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
125
+ auto [keys, spks] = CreateKeysAndScripts (test_setup->coinbaseKey , 0 , 4 , false );
126
+ BenchmarkConnectBlock (bench, keys, spks, test_setup.get ());
176
127
}
177
128
178
129
BENCHMARK (ConnectBlockAllSchnorr, benchmark::PriorityLevel::HIGH);
0 commit comments