Bitcoin Core 28.99.0
P2P Digital Currency
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
BaseIndex Class Referenceabstract

Base class for indices of blockchain data. More...

#include <base.h>

Inheritance diagram for BaseIndex:
[legend]
Collaboration diagram for BaseIndex:
[legend]

Classes

class  DB
 The database stores a block locator of the chain the database is synced to so that the index can efficiently determine the point it last stopped at. More...
 

Public Member Functions

 BaseIndex (std::unique_ptr< interfaces::Chain > chain, std::string name)
 
virtual ~BaseIndex ()
 Destructor interrupts sync thread if running and blocks until it exits. More...
 
const std::string & GetName () const LIFETIMEBOUND
 Get the name of the index for display in logs. More...
 
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. More...
 
bool Init ()
 Initializes the sync state and registers the instance to the validation interface so that it stays in sync with blockchain updates. More...
 
bool StartBackgroundSync ()
 Starts the initial sync process on a background thread. More...
 
void Sync ()
 Sync the index with the block index starting from the current best block. More...
 
void Stop ()
 Stops the instance from staying in sync with blockchain updates. More...
 
IndexSummary GetSummary () const
 Get a summary of the index and its state. More...
 

Protected Member Functions

void BlockConnected (ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
 Notifies listeners of a block being connected. More...
 
void ChainStateFlushed (ChainstateRole role, const CBlockLocator &locator) override
 Notifies listeners of the new active block chain on-disk. More...
 
virtual bool CustomInit (const std::optional< interfaces::BlockRef > &block)
 Initialize internal state from the database and block index. More...
 
virtual bool CustomAppend (const interfaces::BlockInfo &block)
 Write update index entries for a newly connected block. More...
 
virtual bool CustomCommit (CDBBatch &batch)
 Virtual method called internally by Commit that can be overridden to atomically commit more index state. More...
 
virtual bool CustomRewind (const interfaces::BlockRef &current_tip, const interfaces::BlockRef &new_tip)
 Rewind index to an earlier chain tip during a chain reorg. More...
 
virtual DBGetDB () const =0
 
void SetBestBlockIndex (const CBlockIndex *block)
 Update the internal best block index as well as the prune lock. More...
 
- Protected Member Functions inherited from CValidationInterface
 ~CValidationInterface ()=default
 Protected destructor so that instances can only be deleted by derived classes. More...
 
virtual void UpdatedBlockTip (const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
 Notifies listeners when the block chain tip advances. More...
 
virtual void ActiveTipChange (const CBlockIndex &new_tip, bool is_ibd)
 Notifies listeners any time the block chain tip changes, synchronously. More...
 
virtual void TransactionAddedToMempool (const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
 Notifies listeners of a transaction having been added to mempool. More...
 
virtual void TransactionRemovedFromMempool (const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
 Notifies listeners of a transaction leaving mempool. More...
 
virtual void MempoolTransactionsRemovedForBlock (const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight)
 
virtual void BlockConnected (ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
 Notifies listeners of a block being connected. More...
 
virtual void BlockDisconnected (const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
 Notifies listeners of a block being disconnected Provides the block that was disconnected. More...
 
virtual void ChainStateFlushed (ChainstateRole role, const CBlockLocator &locator)
 Notifies listeners of the new active block chain on-disk. More...
 
virtual void BlockChecked (const CBlock &, const BlockValidationState &)
 Notifies listeners of a block validation result. More...
 
virtual void NewPoWValidBlock (const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
 Notifies listeners that a block which builds directly on our current tip has been received and connected to the headers tree, though not validated yet. More...
 

Protected Attributes

std::unique_ptr< interfaces::Chainm_chain
 
Chainstatem_chainstate {nullptr}
 
const std::string m_name
 

Private Member Functions

bool Commit ()
 Write the current index state (eg. More...
 
bool Rewind (const CBlockIndex *current_tip, const CBlockIndex *new_tip)
 Loop over disconnected blocks and call CustomRewind. More...
 
virtual bool AllowPrune () const =0
 
template<typename... Args>
void FatalErrorf (util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
 

Private Attributes

std::atomic< bool > m_init {false}
 Whether the index has been initialized or not. More...
 
std::atomic< bool > m_synced {false}
 Whether the index is in sync with the main chain. More...
 
std::atomic< const CBlockIndex * > m_best_block_index {nullptr}
 The last block in the chain that the index is in sync with. More...
 
std::thread m_thread_sync
 
CThreadInterrupt m_interrupt
 

Detailed Description

Base class for indices of blockchain data.

This implements CValidationInterface and ensures blocks are indexed sequentially according to their position in the active chain.

In the presence of multiple chainstates (i.e. if a UTXO snapshot is loaded), only the background "IBD" chainstate will be indexed to avoid building the index out of order. When the background chainstate completes validation, the index will be reinitialized and indexing will continue.

Definition at line 42 of file base.h.

Constructor & Destructor Documentation

◆ BaseIndex()

BaseIndex::BaseIndex ( std::unique_ptr< interfaces::Chain chain,
std::string  name 
)

Definition at line 70 of file base.cpp.

◆ ~BaseIndex()

BaseIndex::~BaseIndex ( )
virtual

Destructor interrupts sync thread if running and blocks until it exits.

Definition at line 73 of file base.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ AllowPrune()

virtual bool BaseIndex::AllowPrune ( ) const
privatepure virtual

Implemented in BlockFilterIndex, CoinStatsIndex, and TxIndex.

Here is the caller graph for this function:

◆ BlockConnected()

void BaseIndex::BlockConnected ( ChainstateRole  role,
const std::shared_ptr< const CBlock > &  block,
const CBlockIndex pindex 
)
overrideprotectedvirtual

Notifies listeners of a block being connected.

Provides a vector of transactions evicted from the mempool as a result.

Called on a background thread.

Reimplemented from CValidationInterface.

Definition at line 271 of file base.cpp.

Here is the call graph for this function:

◆ ChainStateFlushed()

void BaseIndex::ChainStateFlushed ( ChainstateRole  role,
const CBlockLocator locator 
)
overrideprotectedvirtual

Notifies listeners of the new active block chain on-disk.

Prior to this callback, any updates are not guaranteed to persist on disk (ie clients need to handle shutdown/restart safety by being able to understand when some updates were lost due to unclean shutdown).

When this callback is invoked, the validation changes done by any prior callback are guaranteed to exist on disk and survive a restart, including an unclean shutdown.

Provides a locator describing the best chain, which is likely useful for storing current state on disk in client DBs.

Called on a background thread.

Reimplemented from CValidationInterface.

Definition at line 328 of file base.cpp.

Here is the call graph for this function:

◆ Commit()

bool BaseIndex::Commit ( )
private

Write the current index state (eg.

chain block locator and subclass-specific items) to disk.

Recommendations for error handling: If called on a successor of the previous committed best block in the index, the index can continue processing without risk of corruption, though the index state will need to catch up from further behind on reboot. If the new state is not a successor of the previous state (due to a chain reorganization), the index must halt until Commit succeeds or else it could end up getting corrupted.

Definition at line 227 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CustomAppend()

virtual bool BaseIndex::CustomAppend ( const interfaces::BlockInfo block)
inlineprotectedvirtual

Write update index entries for a newly connected block.

Reimplemented in BlockFilterIndex, CoinStatsIndex, and TxIndex.

Definition at line 114 of file base.h.

Here is the caller graph for this function:

◆ CustomCommit()

virtual bool BaseIndex::CustomCommit ( CDBBatch batch)
inlineprotectedvirtual

Virtual method called internally by Commit that can be overridden to atomically commit more index state.

Reimplemented in BlockFilterIndex, and CoinStatsIndex.

Definition at line 118 of file base.h.

Here is the caller graph for this function:

◆ CustomInit()

virtual bool BaseIndex::CustomInit ( const std::optional< interfaces::BlockRef > &  block)
inlineprotectedvirtual

Initialize internal state from the database and block index.

Reimplemented in BlockFilterIndex, and CoinStatsIndex.

Definition at line 111 of file base.h.

Here is the caller graph for this function:

◆ CustomRewind()

virtual bool BaseIndex::CustomRewind ( const interfaces::BlockRef current_tip,
const interfaces::BlockRef new_tip 
)
inlineprotectedvirtual

Rewind index to an earlier chain tip during a chain reorg.

The tip must be an ancestor of the current best block.

Reimplemented in BlockFilterIndex, and CoinStatsIndex.

Definition at line 122 of file base.h.

Here is the caller graph for this function:

◆ FatalErrorf()

template<typename... Args>
void BaseIndex::FatalErrorf ( util::ConstevalFormatString< sizeof...(Args)>  fmt,
const Args &...  args 
)
private

Definition at line 31 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDB()

virtual DB & BaseIndex::GetDB ( ) const
protectedpure virtual

Implemented in BlockFilterIndex, CoinStatsIndex, and TxIndex.

Here is the caller graph for this function:

◆ GetName()

const std::string & BaseIndex::GetName ( ) const
inline

Get the name of the index for display in logs.

Definition at line 135 of file base.h.

Here is the caller graph for this function:

◆ GetSummary()

IndexSummary BaseIndex::GetSummary ( ) const

Get a summary of the index and its state.

Definition at line 421 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Init()

bool BaseIndex::Init ( )

Initializes the sync state and registers the instance to the validation interface so that it stays in sync with blockchain updates.

Definition at line 79 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Interrupt()

void BaseIndex::Interrupt ( )

Blocks the current thread until the index is caught up to the current state of the block chain.

This only blocks if the index has gotten in sync once and only needs to process blocks in the ValidationInterface queue. If the index is catching up from far behind, this method does not block and immediately returns false.

Definition at line 397 of file base.cpp.

Here is the caller graph for this function:

◆ Rewind()

bool BaseIndex::Rewind ( const CBlockIndex current_tip,
const CBlockIndex new_tip 
)
private

Loop over disconnected blocks and call CustomRewind.

Definition at line 247 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetBestBlockIndex()

void BaseIndex::SetBestBlockIndex ( const CBlockIndex block)
protected

Update the internal best block index as well as the prune lock.

Definition at line 436 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StartBackgroundSync()

bool BaseIndex::StartBackgroundSync ( )

Starts the initial sync process on a background thread.

Definition at line 402 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Stop()

void BaseIndex::Stop ( )

Stops the instance from staying in sync with blockchain updates.

Definition at line 410 of file base.cpp.

Here is the caller graph for this function:

◆ Sync()

void BaseIndex::Sync ( )

Sync the index with the block index starting from the current best block.

Intended to be run in its own thread, m_thread_sync, and can be interrupted with m_interrupt. Once the index gets in sync, the m_synced flag is set and the BlockConnected ValidationInterface callback takes over and the sync thread exits.

Definition at line 144 of file base.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_best_block_index

std::atomic<const CBlockIndex*> BaseIndex::m_best_block_index {nullptr}
private

The last block in the chain that the index is in sync with.

Definition at line 78 of file base.h.

◆ m_chain

std::unique_ptr<interfaces::Chain> BaseIndex::m_chain
protected

Definition at line 102 of file base.h.

◆ m_chainstate

Chainstate* BaseIndex::m_chainstate {nullptr}
protected

Definition at line 103 of file base.h.

◆ m_init

std::atomic<bool> BaseIndex::m_init {false}
private

Whether the index has been initialized or not.

Definition at line 67 of file base.h.

◆ m_interrupt

CThreadInterrupt BaseIndex::m_interrupt
private

Definition at line 81 of file base.h.

◆ m_name

const std::string BaseIndex::m_name
protected

Definition at line 104 of file base.h.

◆ m_synced

std::atomic<bool> BaseIndex::m_synced {false}
private

Whether the index is in sync with the main chain.

The flag is flipped from false to true once, after which point this starts processing ValidationInterface notifications to stay in sync.

Note that this will latch to true immediately upon startup if m_chainstate->m_chain is empty, which will be the case upon startup with an empty datadir if, e.g., -txindex=1 is specified.

Definition at line 75 of file base.h.

◆ m_thread_sync

std::thread BaseIndex::m_thread_sync
private

Definition at line 80 of file base.h.


The documentation for this class was generated from the following files: