Skip to content

Commit

Permalink
index: Access functions for global block filter indexes.
Browse files Browse the repository at this point in the history
Summary:
This is a partial backport of Core [[bitcoin/bitcoin#14121 | PR14121]] : bitcoin/bitcoin@accc8b8

Depends on D6341 and D6344

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D6345
  • Loading branch information
jimpo authored and deadalnix committed Jun 3, 2020
1 parent dfaae26 commit bd0c10d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/index/blockfilterindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ struct DBHashKey {

}; // namespace

static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;

BlockFilterIndex::BlockFilterIndex(BlockFilterType filter_type,
size_t n_cache_size, bool f_memory,
bool f_wipe)
Expand Down Expand Up @@ -462,3 +464,30 @@ bool BlockFilterIndex::LookupFilterHashRange(
}
return true;
}

BlockFilterIndex *GetBlockFilterIndex(BlockFilterType filter_type) {
auto it = g_filter_indexes.find(filter_type);
return it != g_filter_indexes.end() ? &it->second : nullptr;
}

void ForEachBlockFilterIndex(std::function<void(BlockFilterIndex &)> fn) {
for (auto &entry : g_filter_indexes) {
fn(entry.second);
}
}

bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
bool f_memory, bool f_wipe) {
auto result = g_filter_indexes.emplace(
std::piecewise_construct, std::forward_as_tuple(filter_type),
std::forward_as_tuple(filter_type, n_cache_size, f_memory, f_wipe));
return result.second;
}

bool DestroyBlockFilterIndex(BlockFilterType filter_type) {
return g_filter_indexes.erase(filter_type);
}

void DestroyAllBlockFilterIndexes() {
g_filter_indexes.clear();
}
27 changes: 27 additions & 0 deletions src/index/blockfilterindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,31 @@ class BlockFilterIndex final : public BaseIndex {
std::vector<uint256> &hashes_out) const;
};

/**
* Get a block filter index by type. Returns nullptr if index has not been
* initialized or was already destroyed.
*/
BlockFilterIndex *GetBlockFilterIndex(BlockFilterType filter_type);

/** Iterate over all running block filter indexes, invoking fn on each. */
void ForEachBlockFilterIndex(std::function<void(BlockFilterIndex &)> fn);

/**
* Initialize a block filter index for the given type if one does not already
* exist. Returns true if a new index is created and false if one has already
* been initialized.
*/
bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
bool f_memory = false, bool f_wipe = false);

/**
* Destroy the block filter index with the given type. Returns false if no such
* index exists. This just releases the allocated memory and closes the database
* connection, it does not delete the index data.
*/
bool DestroyBlockFilterIndex(BlockFilterType filter_type);

/** Destroy all open block filter indexes. */
void DestroyAllBlockFilterIndexes();

#endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
40 changes: 40 additions & 0 deletions src/test/blockfilter_index_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,44 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup) {
filter_index.Stop();
}

BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup) {
BlockFilterIndex *filter_index;

filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index == nullptr);

BOOST_CHECK(
InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));

filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index != nullptr);
BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);

// Initialize returns false if index already exists.
BOOST_CHECK(
!InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));

int iter_count = 0;
ForEachBlockFilterIndex(
[&iter_count](BlockFilterIndex &_index) { iter_count++; });
BOOST_CHECK_EQUAL(iter_count, 1);

BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));

// Destroy returns false because index was already destroyed.
BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));

filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index == nullptr);

// Reinitialize index.
BOOST_CHECK(
InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));

DestroyAllBlockFilterIndexes();

filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index == nullptr);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit bd0c10d

Please sign in to comment.