5#ifndef BITCOIN_TEST_UTIL_CHAINSTATE_H
6#define BITCOIN_TEST_UTIL_CHAINSTATE_H
32template<
typename F = decltype(NoMalleation)>
37 bool reset_chainstate =
false,
38 bool in_memory_chainstate =
false)
52 node.chainman->ActiveChainstate(),
53 std::move(auto_outfile),
56 LogInfo(
"Wrote UTXO snapshot to %s: %s",
64 auto_infile >> metadata;
66 malleation(auto_infile, metadata);
68 if (reset_chainstate) {
78 CBlockIndex *orig_tip =
node.chainman->ActiveChainstate().m_chain.Tip();
79 uint256 gen_hash =
node.chainman->ActiveChainstate().m_chain[0]->GetBlockHash();
80 node.chainman->ResetChainstates();
81 node.chainman->InitializeChainstate(
node.mempool.get());
86 chain.InitCoinsCache(1_MiB);
89 node.chainman->MaybeRebalanceCaches();
96 while (pindex && pindex != chain.
m_chain.
Tip()) {
107 pindex = pindex->
pprev;
109 chain.PopulateBlockIndexCandidates();
112 if (!
node.chainman->ActiveChainstate().ActivateBestChain(state)) {
113 throw std::runtime_error(
strprintf(
"ActivateBestChain failed. (%s)", state.
ToString()));
116 0 ==
WITH_LOCK(
node.chainman->GetMutex(),
return node.chainman->ActiveHeight()));
119 auto& new_active =
node.chainman->ActiveChainstate();
120 auto* tip = new_active.m_chain.Tip();
128 new_active.m_chain.SetTip(*(tip->pprev));
131 auto res =
node.chainman->ActivateSnapshot(auto_infile, metadata, in_memory_chainstate);
134 new_active.m_chain.SetTip(*tip);
UniValue CreateUTXOSnapshot(node::NodeContext &node, Chainstate &chainstate, AutoFile &&afile, const fs::path &path, const fs::path &tmppath)
Helper to create UTXO snapshots given a chainstate and a file handle.
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous.
#define Assert(val)
Identity function.
Non-refcounted RAII wrapper for FILE*.
The block chain is a tree shaped structure starting with the genesis block at the root,...
bool IsValid(enum BlockStatus nUpTo) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
unsigned int nTx
Number of transactions in this block.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetBestBlock(const uint256 &block_hash)
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 LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
kernel::ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe)
Return the current role of the chainstate.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string ToString() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
static path u8path(std::string_view utf8_str)
static std::string PathToString(const path &path)
Convert path object to a byte string.
FILE * fopen(const fs::path &p, const char *mode)
Testing setup that configures a complete environment.
NodeContext struct containing references to chain state and connection state.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static bool CreateAndActivateUTXOSnapshot(TestingSetup *fixture, F malleation=NoMalleation, bool reset_chainstate=false, bool in_memory_chainstate=false)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot.