21#include <validation.h>
23#include <boost/test/unit_test.hpp>
37BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
43 if (!
ComputeFilter(filter_index.GetFilterType(), *block_index, expected_filter, blockman)) {
44 BOOST_ERROR(
"ComputeFilter failed on block " << block_index->nHeight);
50 std::vector<BlockFilter> filters;
51 std::vector<uint256> filter_hashes;
53 BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
54 BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
55 BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
56 BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
68 filter_hashes.clear();
69 last_header = filter_header;
76 BOOST_REQUIRE(filter_index.
Init());
86 std::vector<BlockFilter> filters;
87 std::vector<uint256> filter_hashes;
90 block_index !=
nullptr;
101 BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
110 block_index !=
nullptr;
111 block_index =
m_node.
chainman->ActiveChain().Next(*block_index)) {
126 std::vector<std::shared_ptr<CBlock>> chainA, chainB;
127 BOOST_REQUIRE(
BuildChain(
m_node, tip, coinbase_script_pub_key_A, 10, chainA));
128 BOOST_REQUIRE(
BuildChain(
m_node, tip, coinbase_script_pub_key_B, 10, chainB));
131 uint256 chainA_last_header = last_header;
132 for (
size_t i = 0; i < 2; i++) {
133 const auto& block = chainA[i];
136 for (
size_t i = 0; i < 2; i++) {
137 const auto& block = chainA[i];
141 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
144 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
149 uint256 chainB_last_header = last_header;
150 for (
size_t i = 0; i < 3; i++) {
151 const auto& block = chainB[i];
154 for (
size_t i = 0; i < 3; i++) {
155 const auto& block = chainB[i];
159 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
162 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
167 chainA_last_header = last_header;
168 for (
size_t i = 0; i < 2; i++) {
169 const auto& block = chainA[i];
173 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
176 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
181 for (
size_t i = 2; i < 4; i++) {
182 const auto& block = chainA[i];
187 chainA_last_header = last_header;
188 chainB_last_header = last_header;
189 for (
size_t i = 0; i < 3; i++) {
194 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
196 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
201 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
203 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
208 std::vector<BlockFilter> filters;
209 std::vector<uint256> filter_hashes;
223 filter_hashes.clear();
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static bool CheckFilterLookups(BlockFilterIndex &filter_index, const CBlockIndex *block_index, uint256 &last_header, const BlockManager &blockman)
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
bool InitBlockFilterIndex(std::function< std::unique_ptr< interfaces::Chain >()> make_chain, BlockFilterType filter_type, size_t n_cache_size, bool f_memory, bool f_wipe)
Initialize a block filter index for the given type if one does not already exist.
#define Assert(val)
Identity function.
void Stop()
Stops the instance from staying in sync with blockchain updates.
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(void Interrupt()
Blocks the current thread until the index is caught up to the current state of the block chain.
void Sync()
Sync the index with the block index starting from the current best block.
Complete block filter struct as defined in BIP 157.
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
uint256 GetHash() const
Compute the filter hash.
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, std::vector< BlockFilter > &filters_out) const
Get a range of filters between two heights on a chain.
BlockFilterType GetFilterType() const
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, std::vector< uint256 > &hashes_out) const
Get a range of filter hashes between two heights on a chain.
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
The block chain is a tree shaped structure starting with the genesis block at the root,...
int nHeight
height of the entry in the chain. The genesis block has height 0
An encapsulated private key.
CPubKey GetPubKey() const
Compute the public key from a private key.
Serialized script, used inside transaction inputs and outputs.
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
CKey GenerateRandomKey(bool compressed) noexcept
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK(expr)
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
std::unique_ptr< ChainstateManager > chainman
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex &block_index, BlockFilter &filter, const BlockManager &blockman)
bool BuildChain(const NodeContext &node, const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, size_t length, std::vector< std::shared_ptr< CBlock > > &chain)
Build a chain of length coinbase-only blocks on top of pindex (which need not be the active tip,...