5#ifndef BITCOIN_TEST_UTIL_CHAINSTATE_H
6#define BITCOIN_TEST_UTIL_CHAINSTATE_H
31template<
typename F = decltype(NoMalleation)>
36 bool reset_chainstate =
false,
37 bool in_memory_chainstate =
false)
51 node,
node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
60 auto_infile >> metadata;
62 malleation(auto_infile, metadata);
64 if (reset_chainstate) {
74 CBlockIndex *orig_tip =
node.chainman->ActiveChainstate().m_chain.Tip();
75 uint256 gen_hash =
node.chainman->ActiveChainstate().m_chain[0]->GetBlockHash();
76 node.chainman->ResetChainstates();
77 node.chainman->InitializeChainstate(
node.mempool.get());
82 chain.InitCoinsCache(1 << 20);
86 node.chainman->MaybeRebalanceCaches();
93 while (pindex && pindex != chain.
m_chain.
Tip()) {
104 pindex = pindex->
pprev;
108 if (!
node.chainman->ActiveChainstate().ActivateBestChain(state)) {
109 throw std::runtime_error(
strprintf(
"ActivateBestChain failed. (%s)", state.
ToString()));
112 0 ==
WITH_LOCK(
node.chainman->GetMutex(),
return node.chainman->ActiveHeight()));
115 auto& new_active =
node.chainman->ActiveChainstate();
116 auto* tip = new_active.m_chain.Tip();
124 new_active.m_chain.SetTip(*(tip->pprev));
127 auto res =
node.chainman->ActivateSnapshot(auto_infile, metadata, in_memory_chainstate);
130 new_active.m_chain.SetTip(*tip);
UniValue CreateUTXOSnapshot(node::NodeContext &node, Chainstate &chainstate, AutoFile &afile, const fs::path &path, const fs::path &tmppath)
Test-only helper to create UTXO snapshots given a chainstate and a file handle.
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
#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,...
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.
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetBestBlock(const uint256 &hashBlock)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
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.
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Return the current role of the chainstate.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string ToString() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
static path u8path(const std::string &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.