6#include <chainparams.h>
20#include <validation.h>
32template <
typename... Args>
33static void FatalError(
const char *fmt,
const Args &...args) {
37 InitError(
_(
"A fatal internal error occurred, see debug.log for details"));
52 bool f_wipe,
bool f_obfuscate)
54 .cache_bytes = n_cache_size,
55 .memory_only = f_memory,
57 .obfuscate = f_obfuscate,
96 if (!
GetDB().ReadBestBlock(locator)) {
113 if (!locator_index) {
116 "Please rebuild the index."),
146 return chain.Genesis();
149 const CBlockIndex *pindex = chain.Next(pindex_prev);
154 return chain.Next(chain.FindFork(pindex_prev));
160 int64_t last_log_time = 0;
161 int64_t last_locator_write_time = 0;
164 LogPrintf(
"%s: m_interrupt set; exiting ThreadSync\n",
187 if (pindex_next->
pprev != pindex &&
190 "%s: Failed to rewind index %s to a previous chain tip",
194 pindex = pindex_next;
199 FatalError(
"%s: Failed to read block %s from disk", __func__,
204 FatalError(
"%s: Failed to write block %s to index database",
209 int64_t current_time =
GetTime();
211 LogPrintf(
"Syncing %s with block chain from height %d\n",
213 last_log_time = current_time;
219 last_locator_write_time = current_time;
248 return error(
"%s: Failed to commit latest %s state", __func__,
275 const std::shared_ptr<const CBlock> &block,
293 if (!best_block_index) {
295 FatalError(
"%s: First block connected is not the genesis block "
310 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor "
311 "of known best chain (tip=%s); not updating index\n",
316 if (best_block_index != pindex->
pprev &&
318 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
331 FatalError(
"%s: Failed to write block %s to index", __func__,
357 if (!locator_tip_index) {
358 FatalError(
"%s: First block (hash=%s) in locator was not found",
359 __func__, locator_tip_hash.
ToString());
372 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known "
373 "best chain (tip=%s); not writing index locator\n",
374 __func__, locator_tip_hash.
ToString(),
386bool BaseIndex::BlockUntilSyncedToCurrentChain()
const {
404 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
416 throw std::logic_error(
"Error: Cannot start a non-initialized index");
437 summary.best_block_height = pindex->nHeight;
438 summary.best_block_hash = pindex->GetBlockHash();
440 summary.best_block_height = 0;
441 summary.best_block_hash =
m_chain->getBlockHash(0);
constexpr int64_t SYNC_LOG_INTERVAL
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
constexpr uint8_t DB_BEST_BLOCK
CBlockLocator GetLocator(interfaces::Chain &chain, const BlockHash &block_hash)
static void FatalError(const char *fmt, const Args &...args)
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
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.
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.
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
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
std::thread m_thread_sync
bool Commit()
Write the current index state (eg.
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
virtual bool CustomInit(const std::optional< interfaces::BlockKey > &block)
Initialize internal state from the database and block index.
void ThreadSync()
Sync the index with the block index starting from the current best block.
Chainstate * m_chainstate
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
bool StartBackgroundSync()
Starts the initial sync process.
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.
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
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
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)
leveldb::Options options
database options used
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
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
virtual bool findBlock(const BlockHash &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 ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
bool error(const char *fmt, const Args &...args)
void ReadDatabaseArgs(const ArgsManager &args, DBOptions &options)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
void StartShutdown()
Request shutdown of the application.
A BlockHash is a unqiue identifier for a block.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< BlockHash > vHave
User-controlled performance and debug options.
Application-specific storage settings.
Hash/height pair to help track and identify blocks.
int height_first
Height of earliest block that should be kept and not pruned.
#define AssertLockNotHeld(cs)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool InitError(const bilingual_str &str)
Show error message.
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void SetMiscWarning(const bilingual_str &warning)