31#include <boost/test/unit_test.hpp>
47BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
50 CBlock CreateBlock(
const CBlockIndex* prev,
const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
51 bool BuildChain(
const CBlockIndex* pindex,
const CScript& coinbase_script_pub_key,
size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
59 BOOST_ERROR(
"ComputeFilter failed on block " << block_index->
nHeight);
65 std::vector<BlockFilter> filters;
66 std::vector<uint256> filter_hashes;
83 filter_hashes.clear();
84 last_header = filter_header;
89 const std::vector<CMutableTransaction>& txns,
93 auto block_template{mining->createNewBlock({
94 .coinbase_output_script = scriptPubKey,
96 BOOST_REQUIRE(block_template);
97 CBlock block{block_template->getBlock()};
99 block.nTime = prev->
nTime + 1;
120 const CScript& coinbase_script_pub_key,
122 std::vector<std::shared_ptr<CBlock>>& chain)
124 std::vector<CMutableTransaction> no_txns;
126 chain.resize(length);
127 for (
auto& block : chain) {
128 block = std::make_shared<CBlock>(
CreateBlock(pindex, no_txns, coinbase_script_pub_key));
142 BOOST_REQUIRE(filter_index.
Init());
152 std::vector<BlockFilter> filters;
153 std::vector<uint256> filter_hashes;
156 block_index !=
nullptr;
157 block_index =
m_node.
chainman->ActiveChain().Next(*block_index)) {
167 BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
176 block_index !=
nullptr;
177 block_index =
m_node.
chainman->ActiveChain().Next(*block_index)) {
192 std::vector<std::shared_ptr<CBlock>> chainA, chainB;
193 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
194 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
197 uint256 chainA_last_header = last_header;
198 for (
size_t i = 0; i < 2; i++) {
199 const auto& block = chainA[i];
202 for (
size_t i = 0; i < 2; i++) {
203 const auto& block = chainA[i];
207 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
210 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
215 uint256 chainB_last_header = last_header;
216 for (
size_t i = 0; i < 3; i++) {
217 const auto& block = chainB[i];
220 for (
size_t i = 0; i < 3; i++) {
221 const auto& block = chainB[i];
225 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
228 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
233 chainA_last_header = last_header;
234 for (
size_t i = 0; i < 2; i++) {
235 const auto& block = chainA[i];
239 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
242 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
247 for (
size_t i = 2; i < 4; i++) {
248 const auto& block = chainA[i];
253 chainA_last_header = last_header;
254 chainB_last_header = last_header;
255 for (
size_t i = 0; i < 3; i++) {
260 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
262 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
267 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
269 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
274 std::vector<BlockFilter> filters;
275 std::vector<uint256> filter_hashes;
289 filter_hashes.clear();
335 std::unique_ptr<BaseIndex::DB>
m_db;
340 explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker,
345 fs::create_directories(path);
346 m_db = std::make_unique<BaseIndex::DB>(path /
"db", 0,
true,
false);
371 std::promise<void> promise;
372 std::shared_future<void> blocker(promise.get_future());
376 BOOST_REQUIRE(index.
Init());
379 auto func_wait_until = [&](
int height, std::chrono::milliseconds timeout) {
380 auto deadline = std::chrono::steady_clock::now() + timeout;
382 if (std::chrono::steady_clock::now() > deadline) {
386 std::this_thread::sleep_for(100ms);
391 func_wait_until(blocking_height - 1, 5
s);
394 std::vector<std::shared_ptr<CBlock>> fork;
398 for (
const auto& block : fork) {
399 BOOST_REQUIRE(
m_node.
chainman->ProcessNewBlock(block,
true,
true,
nullptr));
405 func_wait_until(blocking_height + 2, 5
s);
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, BuildChainTestingSetup)
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.
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
The database stores a block locator of the chain the database is synced to so that the index can effi...
Base class for indices of blockchain data.
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.
IndexSummary GetSummary() const
Get a summary of the index and its state.
void Sync()
Sync the index with the block index starting from the current best block.
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
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,...
uint256 GetBlockHash() const
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.
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
std::unique_ptr< BaseIndex::DB > m_db
BaseIndex::DB & GetDB() const override
IndexReorgCrash(std::unique_ptr< interfaces::Chain > chain, std::shared_future< void > blocker, int blocking_height)
bool AllowPrune() const override
std::shared_future< void > m_blocker
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
CKey GenerateRandomKey(bool compressed) noexcept
BOOST_FAIL("Test unconditionally fails.")
std::unique_ptr< Mining > MakeMining(const node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
bool BuildChain(const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, size_t length, std::vector< std::shared_ptr< CBlock > > &chain)
CBlock CreateBlock(const CBlockIndex *prev, const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
A mutable version of CTransaction.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Block data sent with blockConnected, blockDisconnected notifications.
std::unique_ptr< ChainstateManager > chainman
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex &block_index, BlockFilter &filter, const BlockManager &blockman)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.