6#include <blockfilter.h>
8#include <chainparams.h>
34#include <boost/test/unit_test.hpp>
62BOOST_AUTO_TEST_SUITE(baseindex_tests)
72 BOOST_TEST_INFO_SCOPE(index_name);
73 const int tip_height{
WITH_LOCK(
cs_main,
return m_node.chainman->ActiveChain().Tip()->nHeight)};
74 auto sync_index = [&](
bool do_flush,
int expected_sync_height,
int expected_commit_height) {
75 auto index{make_index(m_node)};
76 BOOST_REQUIRE(index->Init());
80 m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
85 BOOST_REQUIRE(index->Init());
89 m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
98 sync_index(
false, tip_height, 0);
103 sync_index(
true, tip_height, tip_height);
110 sync_index(
false, tip_height + 1, tip_height);
123 m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
125 BOOST_TEST_INFO_SCOPE(index_name);
127 auto index{make_index(m_node)};
128 BOOST_REQUIRE(index->Init());
130 std::shared_ptr<const CBlock> new_block;
134 const CBlock block = this->CreateBlock({}, script_pub_key);
136 new_block = std::make_shared<CBlock>(block);
141 BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index,
true,
nullptr,
nullptr,
true));
153 auto index{make_index(m_node)};
154 BOOST_REQUIRE(index->Init());
157 BOOST_REQUIRE(index->StartBackgroundSync());
167 std::unique_ptr<BaseIndex::DB>
m_db;
176 fs::create_directories(path);
177 m_db = std::make_unique<BaseIndex::DB>(path /
"db", 0,
true,
false);
199 std::promise<void> promise;
200 std::shared_future<void> blocker(promise.get_future());
201 int blocking_height =
WITH_LOCK(
cs_main,
return m_node.chainman->ActiveChain().Tip()->nHeight);
204 BOOST_REQUIRE(index.
Init());
207 auto func_wait_until = [&](
int height, std::chrono::milliseconds timeout) {
208 auto deadline = std::chrono::steady_clock::now() + timeout;
210 if (std::chrono::steady_clock::now() > deadline) {
214 std::this_thread::sleep_for(100ms);
219 func_wait_until(blocking_height - 1, 5s);
222 std::vector<std::shared_ptr<CBlock>> fork;
226 for (
const auto& block : fork) {
227 BOOST_REQUIRE(m_node.chainman->ProcessNewBlock(block,
true,
true,
nullptr));
236 func_wait_until(blocking_height + 2, 5s);
239 m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
std::function< std::unique_ptr< BaseIndex >(node::NodeContext &)> IndexFactory
static const std::vector< std::pair< std::string, IndexFactory > > INDEX_FACTORIES
const CChainParams & Params()
Return the currently selected parameters.
#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...
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...
IndexSummary GetSummary() const
Get a summary of the index and its state.
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name, std::string thread_name)
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
The block chain is a tree shaped structure starting with the genesis block at the root,...
int Height() const
Return the maximal height in the chain.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const Consensus::Params & GetConsensus() const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Serialized script, used inside transaction inputs and outputs.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(bool ConnectBlock(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the best known block, and make it the tip of the block chain.
void ForceFlushStateToDisk(bool wipe_cache=true)
Flush all changes to disk.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
IndexReorgCrash(std::unique_ptr< interfaces::Chain > chain, std::shared_future< void > blocker, int blocking_height, FakeNodeClock &clock)
std::unique_ptr< BaseIndex::DB > m_db
BaseIndex::DB & GetDB() const override
bool AllowPrune() const override
std::shared_future< void > m_blocker
static void BlockConnected(const kernel::ChainstateRole &role, CValidationInterface &obj, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
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
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::vector< unsigned char > ToByteVector(const T &in)
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Block data sent with blockConnected, blockDisconnected notifications.
Information about chainstate that notifications are sent from.
#define WITH_LOCK(cs, code)
bool BuildChain(const NodeContext &node, const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, size_t length, std::vector< std::shared_ptr< CBlock > > &chain)
Build a chain of length coinbase-only blocks on top of pindex (which need not be the active tip,...
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.