Skip to content

Commit accc8b8

Browse files
committed
index: Access functions for global block filter indexes.
1 parent 2bc90e4 commit accc8b8

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/index/blockfilterindex.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ struct DBHashKey {
9494

9595
}; // namespace
9696

97+
static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
98+
9799
BlockFilterIndex::BlockFilterIndex(BlockFilterType filter_type,
98100
size_t n_cache_size, bool f_memory, bool f_wipe)
99101
: m_filter_type(filter_type)
@@ -432,3 +434,34 @@ bool BlockFilterIndex::LookupFilterHashRange(int start_height, const CBlockIndex
432434
}
433435
return true;
434436
}
437+
438+
BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type)
439+
{
440+
auto it = g_filter_indexes.find(filter_type);
441+
return it != g_filter_indexes.end() ? &it->second : nullptr;
442+
}
443+
444+
void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn)
445+
{
446+
for (auto& entry : g_filter_indexes) fn(entry.second);
447+
}
448+
449+
bool InitBlockFilterIndex(BlockFilterType filter_type,
450+
size_t n_cache_size, bool f_memory, bool f_wipe)
451+
{
452+
auto result = g_filter_indexes.emplace(std::piecewise_construct,
453+
std::forward_as_tuple(filter_type),
454+
std::forward_as_tuple(filter_type,
455+
n_cache_size, f_memory, f_wipe));
456+
return result.second;
457+
}
458+
459+
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
460+
{
461+
return g_filter_indexes.erase(filter_type);
462+
}
463+
464+
void DestroyAllBlockFilterIndexes()
465+
{
466+
g_filter_indexes.clear();
467+
}

src/index/blockfilterindex.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,30 @@ class BlockFilterIndex final : public BaseIndex
6565
std::vector<uint256>& hashes_out) const;
6666
};
6767

68+
/**
69+
* Get a block filter index by type. Returns nullptr if index has not been initialized or was
70+
* already destroyed.
71+
*/
72+
BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type);
73+
74+
/** Iterate over all running block filter indexes, invoking fn on each. */
75+
void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
76+
77+
/**
78+
* Initialize a block filter index for the given type if one does not already exist. Returns true if
79+
* a new index is created and false if one has already been initialized.
80+
*/
81+
bool InitBlockFilterIndex(BlockFilterType filter_type,
82+
size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
83+
84+
/**
85+
* Destroy the block filter index with the given type. Returns false if no such index exists. This
86+
* just releases the allocated memory and closes the database connection, it does not delete the
87+
* index data.
88+
*/
89+
bool DestroyBlockFilterIndex(BlockFilterType filter_type);
90+
91+
/** Destroy all open block filter indexes. */
92+
void DestroyAllBlockFilterIndexes();
93+
6894
#endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H

src/test/blockfilter_index_tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,43 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
265265
filter_index.Stop();
266266
}
267267

268+
BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
269+
{
270+
SetDataDir("tempdir");
271+
272+
BlockFilterIndex* filter_index;
273+
274+
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
275+
BOOST_CHECK(filter_index == nullptr);
276+
277+
BOOST_CHECK(InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
278+
279+
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
280+
BOOST_CHECK(filter_index != nullptr);
281+
BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
282+
283+
// Initialize returns false if index already exists.
284+
BOOST_CHECK(!InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
285+
286+
int iter_count = 0;
287+
ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
288+
BOOST_CHECK_EQUAL(iter_count, 1);
289+
290+
BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
291+
292+
// Destroy returns false because index was already destroyed.
293+
BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
294+
295+
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
296+
BOOST_CHECK(filter_index == nullptr);
297+
298+
// Reinitialize index.
299+
BOOST_CHECK(InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
300+
301+
DestroyAllBlockFilterIndexes();
302+
303+
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
304+
BOOST_CHECK(filter_index == nullptr);
305+
}
306+
268307
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)