 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
14 #include <validation.h>
24 template <
typename... Args>
30 AbortError(
_(
"A fatal internal error occurred, see debug.log for details"));
35 CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
61 if (!
GetDB().ReadBestBlock(locator)) {
74 bool prune_violation =
false;
82 prune_violation = block != active_chain.
Genesis();
88 if (!active_chain.
Contains(block_to_test)) {
91 block_to_test = active_chain.
FindFork(block_to_test);
94 prune_violation =
true;
97 if (block_to_test == block) {
98 prune_violation =
false;
104 block = block->
pprev;
107 if (prune_violation) {
108 return InitError(
strprintf(
Untranslated(
"%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"),
GetName()));
119 return chain.Genesis();
122 const CBlockIndex* pindex = chain.Next(pindex_prev);
127 return chain.Next(chain.FindFork(pindex_prev));
137 int64_t last_log_time = 0;
138 int64_t last_locator_write_time = 0;
160 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
164 pindex = pindex_next;
167 int64_t current_time =
GetTime();
169 LogPrintf(
"Syncing %s with block chain from height %d\n",
171 last_log_time = current_time;
176 last_locator_write_time = current_time;
183 FatalError(
"%s: Failed to read block %s from disk",
188 FatalError(
"%s: Failed to write block %s to index database",
206 return error(
"%s: Failed to commit latest %s state", __func__,
GetName());
250 if (!best_block_index) {
252 FatalError(
"%s: First block connected is not the genesis block (height=%d)",
263 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor of "
264 "known best chain (tip=%s); not updating index\n",
269 if (best_block_index != pindex->
pprev && !
Rewind(best_block_index, pindex->
pprev)) {
270 FatalError(
"%s: Failed to rewind index %s to a previous chain tip",
279 FatalError(
"%s: Failed to write block %s to index",
291 const uint256& locator_tip_hash = locator.
vHave.front();
298 if (!locator_tip_index) {
299 FatalError(
"%s: First block (hash=%s) in locator was not found",
300 __func__, locator_tip_hash.
ToString());
311 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known best "
312 "chain (tip=%s); not writing index locator\n",
313 __func__, locator_tip_hash.
ToString(),
324 bool BaseIndex::BlockUntilSyncedToCurrentChain()
const
343 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
GetName());
CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
bilingual_str _(const char *psz)
Translation function.
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
void TraceThread(const char *thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Batch of changes queued to be written to a CDBWrapper.
virtual bool Init()
Initialize internal state from the database and block index.
IndexSummary GetSummary() const
Get a summary of the index and its state.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int nHeight
height of the entry in the chain. The genesis block has height 0
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
CChainState * m_chainstate
std::thread m_thread_sync
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
const Consensus::Params & GetConsensus() const
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
void Write(const K &key, const V &value)
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void Stop()
Stops the instance from staying in sync with blockchain updates.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all CChainState instances.
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
#define AssertLockNotHeld(cs)
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool InitError(const bilingual_str &str)
Show error message.
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
void SetMiscWarning(const bilingual_str &warning)
void Interrupt(NodeContext &node)
Interrupt threads.
void SetSyscallSandboxPolicy(SyscallSandboxPolicy syscall_policy)
Force the current thread (and threads created from the current thread) into a restricted-service oper...
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false)
std::string ToString() const
CChainState stores and provides an API to update our local knowledge of the current best chain.
uint256 GetBlockHash() const
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
An in-memory indexed chain of blocks.
constexpr uint8_t DB_BEST_BLOCK
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::vector< uint256 > vHave
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
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.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
bool error(const char *fmt, const Args &... args)
virtual DB & GetDB() const =0
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
void ThreadSync()
Sync the index with the block index starting from the current best block.
constexpr auto AbortError
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
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,...
bool Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
static void FatalError(const char *fmt, const Args &... args)
CThreadInterrupt m_interrupt
constexpr int64_t SYNC_LOG_INTERVAL
void StartShutdown()
Request shutdown of the application.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CChain m_chain
The current chain of blockheaders we consult and build on.
bool Commit()
Write the current index state (eg.