Changeset View
Changeset View
Standalone View
Standalone View
src/test/blockfilter_index_tests.cpp
Show All 12 Lines | |||||
#include <test/util/blockfilter.h> | #include <test/util/blockfilter.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
BOOST_AUTO_TEST_SUITE(blockfilter_index_tests) | BOOST_AUTO_TEST_SUITE(blockfilter_index_tests) | ||||
struct BuildChainTestingSetup : public TestChain100Setup { | |||||
CBlock CreateBlock(const CBlockIndex *prev, | |||||
const std::vector<CMutableTransaction> &txns, | |||||
const CScript &scriptPubKey); | |||||
bool BuildChain(const CBlockIndex *pindex, | |||||
const CScript &coinbase_script_pub_key, size_t length, | |||||
std::vector<std::shared_ptr<CBlock>> &chain); | |||||
}; | |||||
static bool CheckFilterLookups(BlockFilterIndex &filter_index, | static bool CheckFilterLookups(BlockFilterIndex &filter_index, | ||||
const CBlockIndex *block_index, | const CBlockIndex *block_index, | ||||
uint256 &last_header) { | uint256 &last_header) { | ||||
BlockFilter expected_filter; | BlockFilter expected_filter; | ||||
if (!ComputeFilter(filter_index.GetFilterType(), block_index, | if (!ComputeFilter(filter_index.GetFilterType(), block_index, | ||||
expected_filter)) { | expected_filter)) { | ||||
BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight); | BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight); | ||||
return false; | return false; | ||||
Show All 21 Lines | static bool CheckFilterLookups(BlockFilterIndex &filter_index, | ||||
BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash()); | BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash()); | ||||
filters.clear(); | filters.clear(); | ||||
filter_hashes.clear(); | filter_hashes.clear(); | ||||
last_header = filter_header; | last_header = filter_header; | ||||
return true; | return true; | ||||
} | } | ||||
static CBlock CreateBlock(const CBlockIndex *prev, | CBlock BuildChainTestingSetup::CreateBlock( | ||||
const std::vector<CMutableTransaction> &txns, | const CBlockIndex *prev, const std::vector<CMutableTransaction> &txns, | ||||
const CScript &scriptPubKey, | const CScript &scriptPubKey) { | ||||
const CTxMemPool &mempool) { | |||||
const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
std::unique_ptr<CBlockTemplate> pblocktemplate = | std::unique_ptr<CBlockTemplate> pblocktemplate = | ||||
BlockAssembler(config, mempool).CreateNewBlock(scriptPubKey); | BlockAssembler(config, *m_node.mempool).CreateNewBlock(scriptPubKey); | ||||
CBlock &block = pblocktemplate->block; | CBlock &block = pblocktemplate->block; | ||||
block.hashPrevBlock = prev->GetBlockHash(); | block.hashPrevBlock = prev->GetBlockHash(); | ||||
block.nTime = prev->nTime + 1; | block.nTime = prev->nTime + 1; | ||||
// Replace mempool-selected txns with just coinbase plus passed-in txns: | // Replace mempool-selected txns with just coinbase plus passed-in txns: | ||||
block.vtx.resize(1); | block.vtx.resize(1); | ||||
for (const CMutableTransaction &tx : txns) { | for (const CMutableTransaction &tx : txns) { | ||||
block.vtx.push_back(MakeTransactionRef(tx)); | block.vtx.push_back(MakeTransactionRef(tx)); | ||||
} | } | ||||
// IncrementExtraNonce creates a valid coinbase and merkleRoot | // IncrementExtraNonce creates a valid coinbase and merkleRoot | ||||
unsigned int extraNonce = 0; | unsigned int extraNonce = 0; | ||||
IncrementExtraNonce(&block, prev, config.GetMaxBlockSize(), extraNonce); | IncrementExtraNonce(&block, prev, config.GetMaxBlockSize(), extraNonce); | ||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, | while (!CheckProofOfWork(block.GetHash(), block.nBits, | ||||
config.GetChainParams().GetConsensus())) { | config.GetChainParams().GetConsensus())) { | ||||
++block.nNonce; | ++block.nNonce; | ||||
} | } | ||||
return block; | return block; | ||||
} | } | ||||
static bool BuildChain(const CBlockIndex *pindex, | bool BuildChainTestingSetup::BuildChain( | ||||
const CScript &coinbase_script_pub_key, size_t length, | const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, | ||||
std::vector<std::shared_ptr<CBlock>> &chain, | size_t length, std::vector<std::shared_ptr<CBlock>> &chain) { | ||||
const CTxMemPool &mempool) { | |||||
std::vector<CMutableTransaction> no_txns; | std::vector<CMutableTransaction> no_txns; | ||||
chain.resize(length); | chain.resize(length); | ||||
for (auto &block : chain) { | for (auto &block : chain) { | ||||
block = std::make_shared<CBlock>( | block = std::make_shared<CBlock>( | ||||
CreateBlock(pindex, no_txns, coinbase_script_pub_key, mempool)); | CreateBlock(pindex, no_txns, coinbase_script_pub_key)); | ||||
CBlockHeader header = block->GetBlockHeader(); | CBlockHeader header = block->GetBlockHeader(); | ||||
BlockValidationState state; | BlockValidationState state; | ||||
if (!ProcessNewBlockHeaders(GetConfig(), {header}, state, &pindex)) { | if (!ProcessNewBlockHeaders(GetConfig(), {header}, state, &pindex)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, | ||||
BuildChainTestingSetup) { | |||||
BlockFilterIndex filter_index(BlockFilterType::BASIC, 1 << 20, true); | BlockFilterIndex filter_index(BlockFilterType::BASIC, 1 << 20, true); | ||||
uint256 last_header; | uint256 last_header; | ||||
// Filter should not be found in the index before it is started. | // Filter should not be found in the index before it is started. | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, | ||||
CKey coinbase_key_A, coinbase_key_B; | CKey coinbase_key_A, coinbase_key_B; | ||||
coinbase_key_A.MakeNewKey(true); | coinbase_key_A.MakeNewKey(true); | ||||
coinbase_key_B.MakeNewKey(true); | coinbase_key_B.MakeNewKey(true); | ||||
CScript coinbase_script_pub_key_A = | CScript coinbase_script_pub_key_A = | ||||
GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey())); | GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey())); | ||||
CScript coinbase_script_pub_key_B = | CScript coinbase_script_pub_key_B = | ||||
GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey())); | GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey())); | ||||
std::vector<std::shared_ptr<CBlock>> chainA, chainB; | std::vector<std::shared_ptr<CBlock>> chainA, chainB; | ||||
BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA, | BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA)); | ||||
*m_node.mempool)); | BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB)); | ||||
BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB, | |||||
*m_node.mempool)); | |||||
// Check that new blocks on chain A get indexed. | // Check that new blocks on chain A get indexed. | ||||
uint256 chainA_last_header = last_header; | uint256 chainA_last_header = last_header; | ||||
for (size_t i = 0; i < 2; i++) { | for (size_t i = 0; i < 2; i++) { | ||||
const auto &block = chainA[i]; | const auto &block = chainA[i]; | ||||
BOOST_REQUIRE(ProcessNewBlock(GetConfig(), block, true, nullptr)); | BOOST_REQUIRE(ProcessNewBlock(GetConfig(), block, true, nullptr)); | ||||
} | } | ||||
for (size_t i = 0; i < 2; i++) { | for (size_t i = 0; i < 2; i++) { | ||||
▲ Show 20 Lines • Show All 132 Lines • Show Last 20 Lines |