Skip to content

Commit bcbd7eb

Browse files
committed
bench: basic block filter index initial sync
Introduce benchmark for the block filter index sync. And makes synchronous 'Sync()' mechanism accessible.
1 parent d14c728 commit bcbd7eb

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/Makefile.bench.include

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ bench_bench_bitcoin_SOURCES = \
3434
bench/examples.cpp \
3535
bench/gcs_filter.cpp \
3636
bench/hashpadding.cpp \
37+
bench/index_blockfilter.cpp \
3738
bench/load_external.cpp \
3839
bench/lockedpool.cpp \
3940
bench/logging.cpp \

src/bench/index_blockfilter.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023-present The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
7+
#include <addresstype.h>
8+
#include <index/blockfilterindex.h>
9+
#include <node/chainstate.h>
10+
#include <node/context.h>
11+
#include <test/util/setup_common.h>
12+
#include <util/strencodings.h>
13+
14+
// Very simple block filter index sync benchmark, only using coinbase outputs.
15+
static void BlockFilterIndexSync(benchmark::Bench& bench)
16+
{
17+
const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
18+
19+
// Create more blocks
20+
int CHAIN_SIZE = 600;
21+
CPubKey pubkey{ParseHex("02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9")};
22+
CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
23+
std::vector<CMutableTransaction> noTxns;
24+
for (int i = 0; i < CHAIN_SIZE - 100; i++) {
25+
test_setup->CreateAndProcessBlock(noTxns, script);
26+
SetMockTime(GetTime() + 1);
27+
}
28+
assert(WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveHeight() == CHAIN_SIZE));
29+
30+
bench.minEpochIterations(5).run([&] {
31+
BlockFilterIndex filter_index(interfaces::MakeChain(test_setup->m_node), BlockFilterType::BASIC,
32+
/*n_cache_size=*/0, /*f_memory=*/false, /*f_wipe=*/true);
33+
assert(filter_index.Init());
34+
assert(!filter_index.BlockUntilSyncedToCurrentChain());
35+
filter_index.Sync();
36+
37+
IndexSummary summary = filter_index.GetSummary();
38+
assert(summary.synced);
39+
assert(summary.best_block_hash == WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveTip()->GetBlockHash()));
40+
});
41+
}
42+
43+
BENCHMARK(BlockFilterIndexSync, benchmark::PriorityLevel::HIGH);

src/index/base.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain&
141141
return chain.Next(chain.FindFork(pindex_prev));
142142
}
143143

144-
void BaseIndex::ThreadSync()
144+
void BaseIndex::Sync()
145145
{
146146
const CBlockIndex* pindex = m_best_block_index.load();
147147
if (!m_synced) {
@@ -394,7 +394,7 @@ bool BaseIndex::StartBackgroundSync()
394394
{
395395
if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index");
396396

397-
m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
397+
m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { Sync(); });
398398
return true;
399399
}
400400

src/index/base.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ class BaseIndex : public CValidationInterface
7878
std::thread m_thread_sync;
7979
CThreadInterrupt m_interrupt;
8080

81-
/// Sync the index with the block index starting from the current best block.
82-
/// Intended to be run in its own thread, m_thread_sync, and can be
83-
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
84-
/// flag is set and the BlockConnected ValidationInterface callback takes
85-
/// over and the sync thread exits.
86-
void ThreadSync();
87-
8881
/// Write the current index state (eg. chain block locator and subclass-specific items) to disk.
8982
///
9083
/// Recommendations for error handling:
@@ -152,9 +145,16 @@ class BaseIndex : public CValidationInterface
152145
/// validation interface so that it stays in sync with blockchain updates.
153146
[[nodiscard]] bool Init();
154147

155-
/// Starts the initial sync process.
148+
/// Starts the initial sync process on a background thread.
156149
[[nodiscard]] bool StartBackgroundSync();
157150

151+
/// Sync the index with the block index starting from the current best block.
152+
/// Intended to be run in its own thread, m_thread_sync, and can be
153+
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
154+
/// flag is set and the BlockConnected ValidationInterface callback takes
155+
/// over and the sync thread exits.
156+
void Sync();
157+
158158
/// Stops the instance from staying in sync with blockchain updates.
159159
void Stop();
160160

0 commit comments

Comments
 (0)