17#include <boost/test/unit_test.hpp>
22 CBlock CreateBlock(
const CBlockIndex* prev,
const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
23 bool BuildChain(
const CBlockIndex* pindex,
const CScript& coinbase_script_pub_key,
size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
31 BOOST_ERROR(
"ComputeFilter failed on block " << block_index->
nHeight);
37 std::vector<BlockFilter> filters;
38 std::vector<uint256> filter_hashes;
55 filter_hashes.clear();
56 last_header = filter_header;
61 const std::vector<CMutableTransaction>& txns,
66 CBlock& block = pblocktemplate->block;
76 unsigned int extraNonce = 0;
85 const CScript& coinbase_script_pub_key,
87 std::vector<std::shared_ptr<CBlock>>& chain)
89 std::vector<CMutableTransaction> no_txns;
92 for (
auto& block : chain) {
93 block = std::make_shared<CBlock>(
CreateBlock(pindex, no_txns, coinbase_script_pub_key));
117 std::vector<BlockFilter> filters;
118 std::vector<uint256> filter_hashes;
121 block_index !=
nullptr;
132 BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
137 constexpr int64_t timeout_ms = 10 * 1000;
139 while (!filter_index.BlockUntilSyncedToCurrentChain()) {
149 block_index !=
nullptr;
161 CKey coinbase_key_A, coinbase_key_B;
166 std::vector<std::shared_ptr<CBlock>> chainA, chainB;
167 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
168 BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
171 uint256 chainA_last_header = last_header;
172 for (
size_t i = 0; i < 2; i++) {
173 const auto& block = chainA[i];
176 for (
size_t i = 0; i < 2; i++) {
177 const auto& block = chainA[i];
181 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
184 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
189 uint256 chainB_last_header = last_header;
190 for (
size_t i = 0; i < 3; i++) {
191 const auto& block = chainB[i];
194 for (
size_t i = 0; i < 3; i++) {
195 const auto& block = chainB[i];
199 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
202 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
207 chainA_last_header = last_header;
208 for (
size_t i = 0; i < 2; i++) {
209 const auto& block = chainA[i];
213 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(block->GetHash());
216 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
221 for (
size_t i = 2; i < 4; i++) {
222 const auto& block = chainA[i];
227 chainA_last_header = last_header;
228 chainB_last_header = last_header;
229 for (
size_t i = 0; i < 3; i++) {
234 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
236 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
241 block_index =
m_node.
chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
243 BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
248 std::vector<BlockFilter> filters;
249 std::vector<uint256> filter_hashes;
263 filter_hashes.clear();
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
static bool CheckFilterLookups(BlockFilterIndex &filter_index, const CBlockIndex *block_index, uint256 &last_header)
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(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.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
void Stop()
Stops the instance from staying in sync with blockchain updates.
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.
bool Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Generate a new block, without valid proof-of-work.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
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 LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out)
Get a single filter header by block.
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.
std::vector< CTransactionRef > vtx
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
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const Consensus::Params & GetConsensus() const
An encapsulated private key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
CPubKey GetPubKey() const
Compute the public key from a private key.
Serialized script, used inside transaction inputs and outputs.
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
#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)
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
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.
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< CTxMemPool > mempool
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex *block_index, BlockFilter &filter)
int64_t GetTimeMillis()
Returns the system time (not mockable)
void UninterruptibleSleep(const std::chrono::microseconds &n)