15 #include <validation.h>
27 template <
typename... Args>
28 static void FatalError(
const char *fmt,
const Args &...args) {
32 AbortError(
_(
"A fatal internal error occurred, see debug.log for details"));
37 bool f_wipe,
bool f_obfuscate)
38 :
CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate) {}
60 if (!
GetDB().ReadBestBlock(locator)) {
73 bool prune_violation =
false;
84 if (!active_chain.
Contains(block_to_test)) {
87 block_to_test = active_chain.
FindFork(block_to_test);
90 prune_violation =
true;
93 while (block_to_test && block && block->nStatus.hasData()) {
94 if (block_to_test == block) {
95 prune_violation =
false;
102 block = block->
pprev;
105 if (prune_violation) {
107 FatalError(
"%s: %s best block of the index goes beyond pruned "
108 "data. Please disable the index or reindex (which will "
109 "download the whole blockchain again)",
123 return chain.Genesis();
126 const CBlockIndex *pindex = chain.Next(pindex_prev);
131 return chain.Next(chain.FindFork(pindex_prev));
139 int64_t last_log_time = 0;
140 int64_t last_locator_write_time = 0;
163 if (pindex_next->
pprev != pindex &&
166 "%s: Failed to rewind index %s to a previous chain tip",
170 pindex = pindex_next;
173 int64_t current_time =
GetTime();
175 LogPrintf(
"Syncing %s with block chain from height %d\n",
177 last_log_time = current_time;
183 last_locator_write_time = current_time;
190 FatalError(
"%s: Failed to read block %s from disk", __func__,
195 FatalError(
"%s: Failed to write block %s to index database",
212 return error(
"%s: Failed to commit latest %s state", __func__,
257 if (!best_block_index) {
259 FatalError(
"%s: First block connected is not the genesis block "
274 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor "
275 "of known best chain (tip=%s); not updating index\n",
280 if (best_block_index != pindex->
pprev &&
282 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
291 FatalError(
"%s: Failed to write block %s to index", __func__,
310 if (!locator_tip_index) {
311 FatalError(
"%s: First block (hash=%s) in locator was not found",
312 __func__, locator_tip_hash.
ToString());
325 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known "
326 "best chain (tip=%s); not writing index locator\n",
327 __func__, locator_tip_hash.
ToString(),
339 bool BaseIndex::BlockUntilSyncedToCurrentChain()
const {
357 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
393 summary.best_block_height =
constexpr int64_t SYNC_LOG_INTERVAL
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
static void FatalError(const char *fmt, const Args &...args)
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
constexpr char DB_BEST_BLOCK
RecursiveMutex cs_main
Global state.
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex 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 txindex is in sync with.
void Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
void Stop()
Stops the instance from staying in sync with blockchain updates.
virtual bool Init()
Initialize internal state from the database and block index.
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
CChainState * m_chainstate
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...
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
CThreadInterrupt m_interrupt
IndexSummary GetSummary() const
Get a summary of the index and its state.
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
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.
void ThreadSync()
Sync the index with the block index starting from the current best block.
virtual DB & GetDB() const =0
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
virtual const char * GetName() const =0
Get the name of the index for display in logs.
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 * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
const Consensus::Params & GetConsensus() const
CChainState stores and provides an API to update our local knowledge of the current best chain.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all CChainState instances.
const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
CChain m_chain
The current chain of blockheaders we consult and build on.
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
virtual const CChainParams & GetChainParams() const =0
std::string ToString() const
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
const Config & GetConfig()
void Interrupt(NodeContext &node)
Interrupt threads.
const CBlockIndex * GetFirstStoredBlock(const CBlockIndex *start_block)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params ¶ms)
Functions for disk access for blocks.
void TraceThread(const char *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
#define AssertLockNotHeld(cs)
bool error(const char *fmt, const Args &...args)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
T GetTime()
Return system time (or mocked time, if set)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
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)