Skip to content

Commit 12eab5d

Browse files
committed
refactor: Deduplicate connectblock benchmark
1 parent c059732 commit 12eab5d

File tree

1 file changed

+34
-83
lines changed

1 file changed

+34
-83
lines changed

src/bench/check_connectblock.cpp

+34-83
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2022 The Bitcoin Core developers
1+
// Copyright (c) 2025 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -58,50 +58,9 @@ CBlock CreateTestBlock(TestChain100Setup* test_setup, std::vector<CKey>& keys, s
5858
return test_setup->CreateBlock(txs, coinbase_spk, chainstate);
5959
}
6060

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)
9862
{
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};
10564
keys.reserve(num_taproot + num_nontaproot + 1);
10665

10766
std::vector<CScript> spks;
@@ -114,16 +73,23 @@ static void ConnectBlockMixed(benchmark::Bench& bench)
11473
spks.push_back(scriptpubkey);
11574
}
11675

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+
}
12283
}
12384

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());
12793

12894
Chainstate& chainstate{test_setup->m_node.chainman->ActiveChainstate()};
12995

@@ -139,40 +105,25 @@ static void ConnectBlockMixed(benchmark::Bench& bench)
139105
});
140106
}
141107

142-
static void ConnectBlockNoSchnorr(benchmark::Bench& bench)
108+
static void ConnectBlockAllSchnorr(benchmark::Bench& bench)
143109
{
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+
}
165114

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+
}
169121

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());
176127
}
177128

178129
BENCHMARK(ConnectBlockAllSchnorr, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)