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);
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());
94 sync_index(
false, tip_height, 0);
99 sync_index(
true, tip_height, tip_height);
106 sync_index(
false, tip_height + 1, tip_height);
119 m_node.
chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
121 BOOST_TEST_INFO_SCOPE(index_name);
123 auto index{make_index(
m_node)};
124 BOOST_REQUIRE(index->Init());
126 std::shared_ptr<const CBlock> new_block;
130 const CBlock block = this->CreateBlock({}, script_pub_key);
132 new_block = std::make_shared<CBlock>(block);
149 auto index{make_index(
m_node)};
150 BOOST_REQUIRE(index->Init());
153 BOOST_REQUIRE(index->StartBackgroundSync());
163 std::unique_ptr<BaseIndex::DB>
m_db;
172 fs::create_directories(path);
173 m_db = std::make_unique<BaseIndex::DB>(path /
"db", 0,
true,
false);
195 std::promise<void> promise;
196 std::shared_future<void> blocker(promise.get_future());
200 BOOST_REQUIRE(index.Init());
201 BOOST_REQUIRE(index.StartBackgroundSync());
203 auto func_wait_until = [&](
int height, std::chrono::milliseconds timeout) {
204 auto deadline = std::chrono::steady_clock::now() + timeout;
205 while (index.GetSummary().best_block_height < height) {
206 if (std::chrono::steady_clock::now() > deadline) {
207 BOOST_FAIL(
strprintf(
"Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
210 std::this_thread::sleep_for(100ms);
215 func_wait_until(blocking_height - 1, 5
s);
218 std::vector<std::shared_ptr<CBlock>> fork;
222 for (
const auto& block : fork) {
223 BOOST_REQUIRE(
m_node.
chainman->ProcessNewBlock(block,
true,
true,
nullptr));
229 func_wait_until(blocking_height + 2, 5
s);
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::function< std::unique_ptr< BaseIndex >(node::NodeContext &)> IndexFactory
static const std::vector< std::pair< std::string, IndexFactory > > INDEX_FACTORIES
BOOST_FIXTURE_TEST_CASE(baseindex_no_commit_ahead_of_flush, TestChain100Setup)
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...
Base class for indices of blockchain data.
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.
CChain m_chain
The current chain of blockheaders we consult and build on.
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(boo 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()
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
is a home for simple enum and struct type definitions that can be used internally by functions in the...
CKey GenerateRandomKey(bool compressed) noexcept
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK(expr)
std::vector< unsigned char > ToByteVector(const T &in)
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
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.
NodeContext struct containing references to chain state and connection state.
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< interfaces::Chain > chain
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
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.