19 #include <boost/test/unit_test.hpp> 27 CBlock CreateBlock(
const CBlockIndex* prev,
const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
28 bool BuildChain(
const CBlockIndex* pindex,
const CScript& coinbase_script_pub_key,
size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
36 BOOST_ERROR(
"ComputeFilter failed on block " << block_index->
nHeight);
42 std::vector<BlockFilter> filters;
43 std::vector<uint256> filter_hashes;
60 filter_hashes.clear();
61 last_header = filter_header;
66 const std::vector<CMutableTransaction>& txns,
70 CBlock& block = pblocktemplate->block;
92 const CScript& coinbase_script_pub_key,
94 std::vector<std::shared_ptr<CBlock>>& chain)
96 std::vector<CMutableTransaction> no_txns;
99 for (
auto& block : chain) {
100 block = std::make_shared<CBlock>(
CreateBlock(pindex, no_txns, coinbase_script_pub_key));
124 std::vector<BlockFilter> filters;
125 std::vector<uint256> filter_hashes;
128 block_index !=
nullptr;
139 BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
141 BOOST_REQUIRE(filter_index.
Start());
144 constexpr int64_t timeout_ms = 10 * 1000;
146 while (!filter_index.BlockUntilSyncedToCurrentChain()) {
156 block_index !=
nullptr;
168 CKey coinbase_key_A, coinbase_key_B;
173 std::vector<std::shared_ptr<CBlock>> chainA, chainB;
174 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
175 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
178 uint256 chainA_last_header = last_header;
179 for (
size_t i = 0; i < 2; i++) {
180 const auto& block = chainA[i];
183 for (
size_t i = 0; i < 2; i++) {
184 const auto& block = chainA[i];
188 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
191 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
196 uint256 chainB_last_header = last_header;
197 for (
size_t i = 0; i < 3; i++) {
198 const auto& block = chainB[i];
201 for (
size_t i = 0; i < 3; i++) {
202 const auto& block = chainB[i];
206 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
209 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
214 chainA_last_header = last_header;
215 for (
size_t i = 0; i < 2; i++) {
216 const auto& block = chainA[i];
220 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
223 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
228 for (
size_t i = 2; i < 4; i++) {
229 const auto& block = chainA[i];
234 chainA_last_header = last_header;
235 chainB_last_header = last_header;
236 for (
size_t i = 0; i < 3; i++) {
241 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
243 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
248 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
250 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
255 std::vector<BlockFilter> filters;
256 std::vector<uint256> filter_hashes;
270 filter_hashes.clear();
272 filter_index.Interrupt();
bool Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
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.
int64_t GetTimeMillis()
Returns the system time (not mockable)
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.
std::unique_ptr< CTxMemPool > mempool
uint256 GetBlockHash() const
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.
static bool CheckFilterLookups(BlockFilterIndex &filter_index, const CBlockIndex *block_index, uint256 &last_header)
BOOST_AUTO_TEST_SUITE_END()
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
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.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
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.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
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.
void UninterruptibleSleep(const std::chrono::microseconds &n)
#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.
BlockFilterType GetFilterType() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
CBlock CreateBlock(const CBlockIndex *prev, const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
A mutable version of CTransaction.
An encapsulated private key.
int nHeight
height of the entry in the chain. The genesis block has height 0
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
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex *block_index, BlockFilter &filter)
#define Assert(val)
Identity function.
#define BOOST_CHECK(expr)