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());
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);
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());
204 BOOST_REQUIRE(index.Init());
205 BOOST_REQUIRE(index.StartBackgroundSync());
207 auto func_wait_until = [&](
int height, std::chrono::milliseconds timeout) {
208 auto deadline = std::chrono::steady_clock::now() + timeout;
209 while (index.GetSummary().best_block_height < height) {
210 if (std::chrono::steady_clock::now() > deadline) {
211 BOOST_FAIL(
strprintf(
"Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
214 std::this_thread::sleep_for(100ms);
219 func_wait_until(blocking_height - 1, 5
s);
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, 5
s);
239 m_node.
chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
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.