7 #include <chainparams.h> 19 #include <boost/test/unit_test.hpp> 26 BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
29 CBlock CreateBlock(
const CBlockIndex* prev,
const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
30 bool BuildChain(
const CBlockIndex* pindex,
const CScript& coinbase_script_pub_key,
size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
38 BOOST_ERROR(
"ComputeFilter failed on block " << block_index->
nHeight);
44 std::vector<BlockFilter> filters;
45 std::vector<uint256> filter_hashes;
62 filter_hashes.clear();
63 last_header = filter_header;
68 const std::vector<CMutableTransaction>& txns,
71 BlockAssembler::Options options;
72 options.coinbase_output_script = scriptPubKey;
73 options.include_dummy_extranonce =
true;
75 CBlock& block = pblocktemplate->block;
86 tx_coinbase.nLockTime =
static_cast<uint32_t
>(prev->
nHeight);
98 const CScript& coinbase_script_pub_key,
100 std::vector<std::shared_ptr<CBlock>>& chain)
102 std::vector<CMutableTransaction> no_txns;
104 chain.resize(length);
105 for (
auto& block : chain) {
106 block = std::make_shared<CBlock>(
CreateBlock(pindex, no_txns, coinbase_script_pub_key));
120 BOOST_REQUIRE(filter_index.
Init());
130 std::vector<BlockFilter> filters;
131 std::vector<uint256> filter_hashes;
134 block_index !=
nullptr;
145 BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
154 block_index !=
nullptr;
170 std::vector<std::shared_ptr<CBlock>> chainA, chainB;
171 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
172 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
175 uint256 chainA_last_header = last_header;
176 for (
size_t i = 0; i < 2; i++) {
177 const auto& block = chainA[i];
180 for (
size_t i = 0; i < 2; i++) {
181 const auto& block = chainA[i];
185 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
188 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
193 uint256 chainB_last_header = last_header;
194 for (
size_t i = 0; i < 3; i++) {
195 const auto& block = chainB[i];
198 for (
size_t i = 0; i < 3; i++) {
199 const auto& block = chainB[i];
203 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
206 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
211 chainA_last_header = last_header;
212 for (
size_t i = 0; i < 2; i++) {
213 const auto& block = chainA[i];
217 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
220 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
225 for (
size_t i = 2; i < 4; i++) {
226 const auto& block = chainA[i];
231 chainA_last_header = last_header;
232 chainB_last_header = last_header;
233 for (
size_t i = 0; i < 3; i++) {
238 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
240 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
245 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
247 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
252 std::vector<BlockFilter> filters;
253 std::vector<uint256> filter_hashes;
267 filter_hashes.clear();
269 filter_index.Interrupt();
313 std::unique_ptr<BaseIndex::DB>
m_db;
318 explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker,
323 fs::create_directories(path);
324 m_db = std::make_unique<BaseIndex::DB>(path /
"db", 0,
true,
false);
349 std::promise<void> promise;
350 std::shared_future<void> blocker(promise.get_future());
354 BOOST_REQUIRE(index.Init());
355 BOOST_REQUIRE(index.StartBackgroundSync());
357 auto func_wait_until = [&](
int height, std::chrono::milliseconds timeout) {
358 auto deadline = std::chrono::steady_clock::now() + timeout;
359 while (index.GetSummary().best_block_height < height) {
360 if (std::chrono::steady_clock::now() > deadline) {
361 BOOST_FAIL(
strprintf(
"Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
364 std::this_thread::sleep_for(100ms);
369 func_wait_until(blocking_height - 1, 5
s);
372 std::vector<std::shared_ptr<CBlock>> fork;
376 for (
const auto& block : fork) {
377 BOOST_REQUIRE(
m_node.
chainman->ProcessNewBlock(block,
true,
true,
nullptr));
383 func_wait_until(blocking_height + 2, 5
s);
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
Interval between compact filter checkpoints.
IndexReorgCrash(std::unique_ptr< interfaces::Chain > chain, std::shared_future< void > blocker, int blocking_height)
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
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. ...
Generate a new block, without valid proof-of-work.
BOOST_FAIL("Test unconditionally fails.")
CPubKey GetPubKey() const
Compute the public key from a private key.
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.
uint256 GetHash() const
Compute the filter hash.
void Stop()
Stops the instance from staying in sync with blockchain updates.
The database stores a block locator of the chain the database is synced to so that the index can effi...
std::unique_ptr< CTxMemPool > mempool
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
CKey GenerateRandomKey(bool compressed) noexcept
uint256 GetBlockHash() const
static bool CheckFilterLookups(BlockFilterIndex &filter_index, const CBlockIndex *block_index, uint256 &last_header, const BlockManager &blockman)
Block data sent with blockConnected, blockDisconnected notifications.
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex &block_index, BlockFilter &filter, const BlockManager &blockman)
Base class for indices of blockchain data.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
BaseIndex::DB & GetDB() const override
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
Complete block filter struct as defined in BIP 157.
BOOST_AUTO_TEST_SUITE_END()
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
void Sync()
Sync the index with the block index starting from the current best block.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
std::unique_ptr< BaseIndex::DB > m_db
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
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.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
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.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
bool BuildChain(const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, size_t length, std::vector< std::shared_ptr< CBlock >> &chain)
std::vector< CTransactionRef > vtx
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
bool AllowPrune() const override
#define BOOST_CHECK_EQUAL(v1, v2)
The block chain is a tree shaped structure starting with the genesis block at the root...
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
Serialized script, used inside transaction inputs and outputs.
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
BlockFilterType GetFilterType() const
CBlock CreateBlock(const CBlockIndex *prev, const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
A mutable version of CTransaction.
An encapsulated private key.
int nHeight
height of the entry in the chain. The genesis block has height 0
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
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.
std::unique_ptr< ChainstateManager > chainman
std::shared_future< void > m_blocker
#define Assert(val)
Identity function.
#define BOOST_CHECK(expr)