5#include <chainparams.h>
21#include <validation.h>
36template <
typename... Args>
52BaseIndex::DB::DB(
const fs::path& path,
size_t n_cache_size,
bool f_memory,
bool f_wipe,
bool f_obfuscate) :
55 .cache_bytes = n_cache_size,
56 .memory_only = f_memory,
58 .obfuscate = f_obfuscate,
95 return &
m_chain->context()->chainman->GetChainstateForIndexing());
98 m_chain->context()->validation_signals->RegisterValidationInterface(
this);
101 if (!
GetDB().ReadBestBlock(locator)) {
114 if (!locator_index) {
139 return chain.Genesis();
142 const CBlockIndex* pindex = chain.Next(pindex_prev);
149 return chain.Next(chain.FindFork(pindex_prev));
159 FatalErrorf(
"%s: Failed to read block %s from disk",
163 block_info.
data = █
169 FatalErrorf(
"%s: Failed to read undo block data %s from disk",
177 FatalErrorf(
"%s: Failed to write block %s to index database",
189 std::chrono::steady_clock::time_point last_log_time{0
s};
190 std::chrono::steady_clock::time_point last_locator_write_time{0
s};
224 FatalErrorf(
"%s: Failed to rewind index %s to a previous chain tip", __func__,
GetName());
227 pindex = pindex_next;
232 auto current_time{std::chrono::steady_clock::now()};
234 LogPrintf(
"Syncing %s with block chain from height %d\n",
236 last_log_time = current_time;
241 last_locator_write_time = current_time;
269 LogError(
"%s: Failed to commit latest %s state\n", __func__,
GetName());
283 for (
const CBlockIndex* iter_tip = current_tip; iter_tip != new_tip; iter_tip = iter_tip->
pprev) {
287 LogError(
"%s: Failed to read block %s from disk",
288 __func__, iter_tip->GetBlockHash().ToString());
291 block_info.
data = █
293 if (
CustomOptions().disconnect_undo_data && iter_tip->nHeight > 0) {
337 if (!best_block_index) {
339 FatalErrorf(
"%s: First block connected is not the genesis block (height=%d)",
350 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor of "
351 "known best chain (tip=%s); not updating index\n",
356 if (best_block_index != pindex->
pprev && !
Rewind(best_block_index, pindex->
pprev)) {
357 FatalErrorf(
"%s: Failed to rewind index %s to a previous chain tip",
385 const uint256& locator_tip_hash = locator.
vHave.front();
392 if (!locator_tip_index) {
393 FatalErrorf(
"%s: First block (hash=%s) in locator was not found",
394 __func__, locator_tip_hash.
ToString());
405 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known best "
406 "chain (tip=%s); not writing index locator\n",
407 __func__, locator_tip_hash.
ToString(),
418bool BaseIndex::BlockUntilSyncedToCurrentChain()
const
437 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
GetName());
438 m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
449 if (!
m_init)
throw std::logic_error(
"Error: Cannot start a non-initialized index");
457 if (
m_chain->context()->validation_signals) {
458 m_chain->context()->validation_signals->UnregisterValidationInterface(
this);
472 summary.best_block_height = pindex->nHeight;
473 summary.best_block_hash = pindex->GetBlockHash();
475 summary.best_block_height = 0;
476 summary.best_block_hash =
m_chain->getBlockHash(0);
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
constexpr uint8_t DB_BEST_BLOCK
constexpr auto SYNC_LOCATOR_WRITE_INTERVAL
constexpr auto SYNC_LOG_INTERVAL
CBlockLocator GetLocator(interfaces::Chain &chain, const uint256 &block_hash)
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false)
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
void Stop()
Stops the instance from staying in sync with blockchain updates.
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(void Interrupt()
Blocks the current thread until the index is caught up to the current state of the block chain.
virtual bool AllowPrune() const =0
void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
CThreadInterrupt m_interrupt
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
IndexSummary GetSummary() const
Get a summary of the index and its state.
virtual DB & GetDB() const =0
void Sync()
Sync the index with the block index starting from the current best block.
std::thread m_thread_sync
bool Commit()
Write the current index state (eg.
virtual interfaces::Chain::NotifyOptions CustomOptions()
Return custom notification options for index.
bool ProcessBlock(const CBlockIndex *pindex, const CBlock *block_data=nullptr)
void FatalErrorf(util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
Chainstate * m_chainstate
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRemove.
virtual bool CustomRemove(const interfaces::BlockInfo &block)
Rewind index by one block during a chain reorg.
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
std::unique_ptr< interfaces::Chain > m_chain
std::atomic< bool > m_init
Whether the index has been initialized or not.
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
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
uint256 GetBlockHash() const
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
Undo information for a CBlock.
An in-memory indexed chain of blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
CChain m_chain
The current chain of blockheaders we consult and build on.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
std::string ToString() const
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
virtual bool findBlock(const uint256 &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
Helper for findBlock to selectively return pieces of block data.
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash={}) const
Functions for disk access for blocks.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
bool IsPruneMode() const
Whether running in -prune mode.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool InitError(const bilingual_str &str)
Show error message.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex *index, const CBlock *data)
Return data from block index.
void AbortNode(const std::function< bool()> &shutdown_request, std::atomic< int > &exit_status, const bilingual_str &message, node::Warnings *warnings)
void ReadDatabaseArgs(const ArgsManager &args, DBOptions &options)
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< uint256 > vHave
User-controlled performance and debug options.
Application-specific storage settings.
Block data sent with blockConnected, blockDisconnected notifications.
const CBlockUndo * undo_data
Hash/height pair to help track and identify blocks.
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.