6#include <chainparams.h>
20#include <validation.h>
32template <
typename... Args>
33static void FatalError(
const char *fmt,
const Args &...args) {
37 AbortError(
_(
"A fatal internal error occurred, see debug.log for details"));
42 bool f_wipe,
bool f_obfuscate)
44 .cache_bytes = n_cache_size,
45 .memory_only = f_memory,
47 .obfuscate = f_obfuscate,
84 if (!
GetDB().ReadBestBlock(locator)) {
100 "Please rebuild the index."),
130 return chain.Genesis();
133 const CBlockIndex *pindex = chain.Next(pindex_prev);
138 return chain.Next(chain.FindFork(pindex_prev));
144 int64_t last_log_time = 0;
145 int64_t last_locator_write_time = 0;
168 if (pindex_next->
pprev != pindex &&
171 "%s: Failed to rewind index %s to a previous chain tip",
175 pindex = pindex_next;
180 FatalError(
"%s: Failed to read block %s from disk", __func__,
185 FatalError(
"%s: Failed to write block %s to index database",
190 int64_t current_time =
GetTime();
192 LogPrintf(
"Syncing %s with block chain from height %d\n",
194 last_log_time = current_time;
200 last_locator_write_time = current_time;
217 return error(
"%s: Failed to commit latest %s state", __func__,
255 const std::shared_ptr<const CBlock> &block,
262 if (!best_block_index) {
264 FatalError(
"%s: First block connected is not the genesis block "
279 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor "
280 "of known best chain (tip=%s); not updating index\n",
285 if (best_block_index != pindex->
pprev &&
287 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
300 FatalError(
"%s: Failed to write block %s to index", __func__,
320 if (!locator_tip_index) {
321 FatalError(
"%s: First block (hash=%s) in locator was not found",
322 __func__, locator_tip_hash.
ToString());
335 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known "
336 "best chain (tip=%s); not writing index locator\n",
337 __func__, locator_tip_hash.
ToString(),
349bool BaseIndex::BlockUntilSyncedToCurrentChain()
const {
367 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
379 throw std::logic_error(
"Error: Cannot start a non-initialized index");
400 summary.best_block_height = pindex->nHeight;
401 summary.best_block_hash = pindex->GetBlockHash();
403 summary.best_block_height = 0;
404 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
static void FatalError(const char *fmt, const Args &...args)
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
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 CommitInternal(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)
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:...
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.
constexpr auto AbortError
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)