12 #include <validation.h>
20 template <
typename... Args>
21 static void FatalError(
const char* fmt,
const Args&... args)
26 AbortError(
_(
"A fatal internal error occurred, see debug.log for details"));
30 BaseIndex::DB::DB(
const fs::path& path,
size_t n_cache_size,
bool f_memory,
bool f_wipe,
bool f_obfuscate) :
31 CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
57 if (!
GetDB().ReadBestBlock(locator)) {
93 int64_t last_log_time = 0;
94 int64_t last_locator_write_time = 0;
116 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
120 pindex = pindex_next;
123 int64_t current_time =
GetTime();
125 LogPrintf(
"Syncing %s with block chain from height %d\n",
127 last_log_time = current_time;
132 last_locator_write_time = current_time;
139 FatalError(
"%s: Failed to read block %s from disk",
144 FatalError(
"%s: Failed to write block %s to index database",
162 return error(
"%s: Failed to commit latest %s state", __func__,
GetName());
183 m_best_block_index = current_tip;
197 if (!best_block_index) {
199 FatalError(
"%s: First block connected is not the genesis block (height=%d)",
210 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor of "
211 "known best chain (tip=%s); not updating index\n",
216 if (best_block_index != pindex->
pprev && !
Rewind(best_block_index, pindex->
pprev)) {
217 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
226 FatalError(
"%s: Failed to write block %s to index",
238 const uint256& locator_tip_hash = locator.
vHave.front();
245 if (!locator_tip_index) {
246 FatalError(
"%s: First block (hash=%s) in locator was not found",
247 __func__, locator_tip_hash.
ToString());
258 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known best "
259 "chain (tip=%s); not writing index locator\n",
260 __func__, locator_tip_hash.
ToString(),
290 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
GetName());
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise pro...
bool Commit()
Write the current index state (eg.
virtual bool Init()
Initialize internal state from the database and block index.
CThreadInterrupt m_interrupt
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
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.
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
void SetMiscWarning(const bilingual_str &warning)
static void LogPrintf(const char *fmt, const Args &...args)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
constexpr int64_t SYNC_LOG_INTERVAL
const Consensus::Params & GetConsensus() const
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
void Stop()
Stops the instance from staying in sync with blockchain updates.
std::thread m_thread_sync
std::string ToString() const
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
bool error(const char *fmt, const Args &...args)
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
bilingual_str _(const char *psz)
Translation function.
AssertLockHeld(mempool.cs)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
constexpr auto AbortError
void Write(const K &key, const V &value)
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
std::vector< uint256 > vHave
void TraceThread(const char *name, Callable func)
static void FatalError(const char *fmt, const Args &...args)
bool BlockUntilSyncedToCurrentChain() const
Blocks the current thread until the index is caught up to the current state of the block chain...
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * LookupBlockIndex(const uint256 &hash)
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
IndexSummary GetSummary() const
Get a summary of the index and its state.
constexpr char DB_BEST_BLOCK
#define AssertLockNotHeld(cs)
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void ThreadSync()
Sync the index with the block index starting from the current best block.
virtual const char * GetName() const =0
Get the name of the index for display in logs.
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.
int64_t GetTime()
Return system time (or mocked time, if set)
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.
uint256 GetBlockHash() const