11 #include <validation.h> 19 template<
typename... Args>
20 static void FatalError(
const char* fmt,
const Args&... args)
24 LogPrintf(
"*** %s\n", strMessage);
26 "Error: A fatal internal error occurred, see debug.log for details",
31 BaseIndex::DB::DB(
const fs::path& path,
size_t n_cache_size,
bool f_memory,
bool f_wipe,
bool f_obfuscate) :
32 CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
58 if (!
GetDB().ReadBestBlock(locator)) {
90 int64_t last_log_time = 0;
91 int64_t last_locator_write_time = 0;
100 const CBlockIndex* pindex_next = NextSyncBlock(pindex);
107 pindex = pindex_next;
110 int64_t current_time =
GetTime();
112 LogPrintf(
"Syncing %s with block chain from height %d\n",
114 last_log_time = current_time;
119 last_locator_write_time = current_time;
124 FatalError(
"%s: Failed to read block %s from disk",
129 FatalError(
"%s: Failed to write block %s to index database",
137 LogPrintf(
"%s is enabled at height %d\n",
GetName(), pindex->
nHeight);
139 LogPrintf(
"%s is enabled\n",
GetName());
147 return error(
"%s: Failed to write locator to disk", __func__);
153 const std::vector<CTransactionRef>& txn_conflicted)
160 if (!best_block_index) {
162 FatalError(
"%s: First block connected is not the genesis block (height=%d)",
173 LogPrintf(
"%s: WARNING: Block %s does not connect to an ancestor of " 174 "known best chain (tip=%s); not updating index\n",
184 FatalError(
"%s: Failed to write block %s to index",
196 const uint256& locator_tip_hash = locator.
vHave.front();
203 if (!locator_tip_index) {
204 FatalError(
"%s: First block (hash=%s) in locator was not found",
205 __func__, locator_tip_hash.
ToString());
216 LogPrintf(
"%s: WARNING: Locator contains block (hash=%s) not on known best " 217 "chain (tip=%s); not writing index locator\n",
218 __func__, locator_tip_hash.
ToString(),
224 error(
"%s: Failed to write locator to disk", __func__);
247 LogPrintf(
"%s: %s is catching up on block notifications\n", __func__,
GetName());
263 FatalError(
"%s: %s failed to initialize", __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<void> pro...
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...
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
constexpr int64_t SYNC_LOG_INTERVAL
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
void Stop()
Stops the instance from staying in sync with blockchain updates.
std::thread m_thread_sync
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
uint256 GetBlockHash() const
#define AssertLockHeld(cs)
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
bool BlockUntilSyncedToCurrentChain()
Blocks the current thread until the index is caught up to the current state of the block chain...
void TraceThread(const char *name, Callable func)
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...
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
std::string ToString() const
std::vector< uint256 > vHave
bool WriteBestBlock(const CBlockIndex *block_index)
Write the current chain block locator to the DB.
void RegisterValidationInterface(CValidationInterface *pwalletIn)
Register a wallet to receive updates from core.
bool WriteBestBlock(const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
#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.
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
constexpr char DB_BEST_BLOCK
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetMiscWarning(const std::string &strWarning)
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip). ...
#define AssertLockNotHeld(cs)
bool error(const char *fmt, const Args &... args)
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex, const std::vector< CTransactionRef > &txn_conflicted) override
Notifies listeners of a block being connected.
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.
int64_t GetTime()
GetTimeMicros() and GetTimeMillis() both return the system time, but in different units...
CClientUIInterface uiInterface
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)
const Consensus::Params & GetConsensus() const
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
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.
CBlockIndex * LookupBlockIndex(const uint256 &hash)