27 #include <util/threadinterrupt.h> 28 #include <util/time.h> 30 #include <validation.h> 52 template <
typename... Args>
71 .cache_bytes = n_cache_size,
72 .memory_only = f_memory,
74 .obfuscate = f_obfuscate,
96 :
m_chain{std::move(chain)}, m_name{std::move(
name)} {}
114 return &
m_chain->context()->chainman->ValidatedChainstate());
117 m_chain->context()->validation_signals->RegisterValidationInterface(
this);
124 if (locator.IsNull()) {
130 if (!locator_index) {
155 return chain.Genesis();
158 const CBlockIndex* pindex = chain.Next(pindex_prev);
165 return chain.Next(chain.FindFork(pindex_prev));
179 block_info.
data = █
185 FatalErrorf(
"Failed to read undo block data %s from disk",
193 FatalErrorf(
"Failed to write block %s to index database",
206 auto last_locator_write_time{last_log_time};
243 pindex = pindex_next;
251 last_log_time = current_time;
256 last_locator_write_time = current_time;
297 for (
const CBlockIndex* iter_tip = current_tip; iter_tip != new_tip; iter_tip = iter_tip->
pprev) {
301 LogError(
"Failed to read block %s from disk",
302 iter_tip->GetBlockHash().ToString());
305 block_info.
data = █
344 if (!best_block_index) {
346 FatalErrorf(
"First block connected is not the genesis block (height=%d)",
357 LogWarning(
"Block %s does not connect to an ancestor of " 358 "known best chain (tip=%s); not updating index",
363 if (best_block_index != pindex->
pprev && !
Rewind(best_block_index, pindex->
pprev)) {
364 FatalErrorf(
"Failed to rewind %s to a previous chain tip",
391 const uint256& locator_tip_hash = locator.
vHave.front();
398 if (!locator_tip_index) {
399 FatalErrorf(
"First block (hash=%s) in locator was not found",
411 LogWarning(
"Locator contains block (hash=%s) not on known best " 412 "chain (tip=%s); not writing index locator",
424 bool BaseIndex::BlockUntilSyncedToCurrentChain()
const 444 m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
455 if (!
m_init)
throw std::logic_error(
"Error: Cannot start a non-initialized index");
463 if (
m_chain->context()->validation_signals) {
464 m_chain->context()->validation_signals->UnregisterValidationInterface(
this);
478 summary.best_block_height = pindex->nHeight;
479 summary.best_block_hash = pindex->GetBlockHash();
481 summary.best_block_height = 0;
482 summary.best_block_hash =
m_chain->getBlockHash(0);
Helper for findBlock to selectively return pieces of block data.
is a home for simple enum and struct type definitions that can be used internally by functions in the...
fs::path path
Location in the filesystem where leveldb data will be stored.
bool Commit()
Write the current index state (eg.
virtual interfaces::Chain::NotifyOptions CustomOptions()
Return custom notification options for index.
bool IsPruneMode() const
Whether running in -prune mode.
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
CThreadInterrupt m_interrupt
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Batch of changes queued to be written to a CDBWrapper.
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances...
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
An in-memory indexed chain of blocks.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Information about chainstate that notifications are sent from.
Hash/height pair to help track and identify blocks.
bool disconnect_undo_data
Include undo data with block disconnected notifications.
bool connect_undo_data
Include undo data with block connected notifications.
bool ProcessBlock(const CBlockIndex *pindex, const CBlock *block_data=nullptr)
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRemove.
CChain m_chain
The current chain of blockheaders we consult and build on.
User-controlled performance and debug options.
CBlockLocator ReadBestBlock() 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.
std::thread m_thread_sync
bool validated
Whether this is a notification from a chainstate that's been fully validated starting from the genesi...
uint256 GetBlockHash() const
Block data sent with blockConnected, blockDisconnected notifications.
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
constexpr auto SYNC_LOG_INTERVAL
void WriteBatch(CDBBatch &batch, bool fSync=false)
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
CBlockLocator GetLocator(interfaces::Chain &chain, const uint256 &block_hash)
bool InitError(const bilingual_str &str)
Show error message.
std::atomic< bool > m_init
Whether the index has been initialized or not.
void ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
void Sync()
Sync the index with the block index starting from the current best block.
virtual bool findBlock(const uint256 &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
constexpr uint8_t DB_BEST_BLOCK
void Write(const K &key, const V &value)
std::string ToString() const
void ReadDatabaseArgs(const ArgsManager &args, DBOptions &options)
constexpr auto SYNC_LOCATOR_WRITE_INTERVAL
std::vector< uint256 > vHave
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
virtual void reset()
Reset to an non-interrupted state.
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
virtual bool CustomRemove(const interfaces::BlockInfo &block)
Rewind index by one block during a chain reorg.
static time_point now() noexcept
Return current system time or mocked time, if set.
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void AbortNode(const std::function< bool()> &shutdown_request, std::atomic< int > &exit_status, const bilingual_str &message, node::Warnings *warnings)
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
The block chain is a tree shaped structure starting with the genesis block at the root...
Undo information for a CBlock.
Chainstate * m_chainstate
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Application-specific storage settings.
#define AssertLockNotHeld(cs)
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex *index, const CBlock *data)
Return data from block index.
int nHeight
height of the entry in the chain. The genesis block has height 0
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false)
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
const CBlockUndo * undo_data
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
virtual DB & GetDB() const =0
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
void FatalErrorf(util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
virtual bool AllowPrune() const =0
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
std::unique_ptr< interfaces::Chain > m_chain
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
IndexSummary GetSummary() const
Get a summary of the index and its state.