Bitcoin Core 31.99.0
P2P Digital Currency
base.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <index/base.h>
6
7#include <chain.h>
8#include <common/args.h>
9#include <dbwrapper.h>
10#include <interfaces/chain.h>
11#include <interfaces/types.h>
12#include <kernel/types.h>
13#include <node/abort.h>
14#include <node/blockstorage.h>
15#include <node/context.h>
16#include <node/database_args.h>
17#include <node/interface_ui.h>
18#include <primitives/block.h>
19#include <sync.h>
20#include <tinyformat.h>
21#include <uint256.h>
22#include <undo.h>
23#include <util/check.h>
24#include <util/fs.h>
25#include <util/log.h>
26#include <util/string.h>
27#include <util/thread.h>
28#include <util/threadinterrupt.h>
29#include <util/time.h>
30#include <util/translation.h>
31#include <validation.h>
32#include <validationinterface.h>
33
34#include <compare>
35#include <cstdint>
36#include <functional>
37#include <memory>
38#include <optional>
39#include <stdexcept>
40#include <string>
41#include <thread>
42#include <utility>
43#include <vector>
44
46
47constexpr uint8_t DB_BEST_BLOCK{'B'};
48
49constexpr auto SYNC_LOG_INTERVAL{30s};
51
52template <typename... Args>
53void BaseIndex::FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
54{
55 auto message = tfm::format(fmt, args...);
56 node::AbortNode(m_chain->context()->shutdown_request, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
57}
58
60{
61 CBlockLocator locator;
62 bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator));
63 assert(found);
64 assert(!locator.IsNull());
65 return locator;
66}
67
68BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate, bool f_bloom) :
70 .path = path,
71 .cache_bytes = n_cache_size,
72 .memory_only = f_memory,
73 .wipe_data = f_wipe,
74 .obfuscate = f_obfuscate,
75 .bloom_filter = f_bloom,
76 .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
77{}
78
80{
81 CBlockLocator locator;
82
83 bool success = Read(DB_BEST_BLOCK, locator);
84 if (!success) {
85 locator.SetNull();
86 }
87
88 return locator;
89}
90
92{
93 batch.Write(DB_BEST_BLOCK, locator);
94}
95
96BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name, std::string thread_name)
97 : m_chain{std::move(chain)}, m_name{std::move(name)}, m_thread_name{std::move(thread_name)} {}
98
100{
101 Interrupt();
102 Stop();
103}
104
106{
108
109 // May need reset if index is being restarted.
111
112 // m_chainstate member gives indexing code access to node internals. It is
113 // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
115 return &m_chain->context()->chainman->ValidatedChainstate());
116 // Register to validation interface before setting the 'm_synced' flag, so that
117 // callbacks are not missed once m_synced is true.
118 m_chain->context()->validation_signals->RegisterValidationInterface(this);
119
120 const auto locator{GetDB().ReadBestBlock()};
121
122 LOCK(cs_main);
123 CChain& index_chain = m_chainstate->m_chain;
124
125 if (locator.IsNull()) {
126 SetBestBlockIndex(nullptr);
127 } else {
128 // Setting the best block to the locator's top block. If it is not part of the
129 // best chain, we will rewind to the fork point during index sync
130 const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
131 if (!locator_index) {
132 return InitError(Untranslated(strprintf("best block of %s not found. Please rebuild the index.", GetName())));
133 }
134 SetBestBlockIndex(locator_index);
135 }
136
137 // Child init
138 const CBlockIndex* start_block = m_best_block_index.load();
139 if (!CustomInit(start_block ? std::make_optional(interfaces::BlockRef{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
140 return false;
141 }
142
143 // Note: this will latch to true immediately if the user starts up with an empty
144 // datadir and an index enabled. If this is the case, indexation will happen solely
145 // via `BlockConnected` signals until, possibly, the next restart.
146 m_synced = start_block == index_chain.Tip();
147 m_init = true;
148 return true;
149}
150
151static const CBlockIndex* NextSyncBlock(const CBlockIndex* const pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
152{
154
155 if (!pindex_prev) {
156 return chain.Genesis();
157 }
158
159 if (const auto* pindex{chain.Next(*pindex_prev)}) {
160 return pindex;
161 }
162
163 // If there is no next block, we might be synced
164 if (pindex_prev == chain.Tip()) {
165 return nullptr;
166 }
167
168 // Since block is not in the chain, return the next block in the chain AFTER the last common ancestor.
169 // Caller will be responsible for rewinding back to the common ancestor.
170 const auto* fork{chain.FindFork(*pindex_prev)};
171 // Common ancestor must exist (genesis).
172 return chain.Next(*Assert(fork));
173}
174
175bool BaseIndex::ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data)
176{
177 interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block_data);
178
179 CBlock block;
180 if (!block_data) { // disk lookup if block data wasn't provided
181 if (!m_chainstate->m_blockman.ReadBlock(block, *pindex)) {
182 FatalErrorf("Failed to read block %s from disk",
183 pindex->GetBlockHash().ToString());
184 return false;
185 }
186 block_info.data = &block;
187 }
188
189 CBlockUndo block_undo;
190 if (CustomOptions().connect_undo_data) {
191 if (pindex->nHeight > 0 && !m_chainstate->m_blockman.ReadBlockUndo(block_undo, *pindex)) {
192 FatalErrorf("Failed to read undo block data %s from disk",
193 pindex->GetBlockHash().ToString());
194 return false;
195 }
196 block_info.undo_data = &block_undo;
197 }
198
199 if (!CustomAppend(block_info)) {
200 FatalErrorf("Failed to write block %s to index database",
201 pindex->GetBlockHash().ToString());
202 return false;
203 }
204
205 return true;
206}
207
209{
210 const CBlockIndex* pindex = m_best_block_index.load();
211 if (!m_synced) {
212 auto last_log_time{NodeClock::now()};
213 auto last_locator_write_time{last_log_time};
214 while (true) {
215 if (m_interrupt) {
216 LogInfo("%s: m_interrupt set; exiting ThreadSync", GetName());
217
218 SetBestBlockIndex(pindex);
219 // No need to handle errors in Commit. If it fails, the error will be already be
220 // logged. The best way to recover is to continue, as index cannot be corrupted by
221 // a missed commit to disk for an advanced index state.
222 Commit();
223 return;
224 }
225
226 const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
227 // If pindex_next is null, it means pindex is the chain tip, so
228 // commit data indexed so far.
229 if (!pindex_next) {
230 SetBestBlockIndex(pindex);
231 // No need to handle errors in Commit. See rationale above.
232 Commit();
233
234 // If pindex is still the chain tip after committing, exit the
235 // sync loop. It is important for cs_main to be locked while
236 // setting m_synced = true, otherwise a new block could be
237 // attached while m_synced is still false, and it would not be
238 // indexed.
240 pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
241 if (!pindex_next) {
242 m_synced = true;
243 break;
244 }
245 }
246 if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
247 FatalErrorf("Failed to rewind %s to a previous chain tip", GetName());
248 return;
249 }
250 pindex = pindex_next;
251
252
253 if (!ProcessBlock(pindex)) return; // error logged internally
254
255 auto current_time{NodeClock::now()};
256 if (current_time - last_log_time >= SYNC_LOG_INTERVAL) {
257 LogInfo("Syncing %s with block chain from height %d", GetName(), pindex->nHeight);
258 last_log_time = current_time;
259 }
260
261 if (current_time - last_locator_write_time >= SYNC_LOCATOR_WRITE_INTERVAL) {
262 SetBestBlockIndex(pindex);
263 last_locator_write_time = current_time;
264 // No need to handle errors in Commit. See rationale above.
265 Commit();
266 }
267 }
268 }
269
270 if (pindex) {
271 LogInfo("%s is enabled at height %d", GetName(), pindex->nHeight);
272 } else {
273 LogInfo("%s is enabled", GetName());
274 }
275}
276
278{
279 // Don't commit anything if we haven't indexed any block yet
280 // (this could happen if init is interrupted).
281 bool ok = m_best_block_index != nullptr;
282 if (ok) {
283 // Don't commit if the index best block is not an ancestor of the chainstate's last flushed
284 // block. Otherwise, after an unclean shutdown, the index could be
285 // persisted ahead of a chainstate it can no longer roll back to, which
286 // would corrupt indexes with state (e.g. coinstatsindex).
287 const CBlockIndex* index_tip = m_best_block_index.load();
288 const CBlockIndex* last_flushed = WITH_LOCK(::cs_main, return m_chainstate->GetLastFlushedBlock());
289 if (!last_flushed || last_flushed->GetAncestor(index_tip->nHeight) != index_tip) {
290 LogDebug(BCLog::COINDB, "Skipping commit, index is ahead of flushed chainstate (index height %d, last flush at height %d)",
291 index_tip->nHeight, last_flushed ? last_flushed->nHeight : -1);
292 return;
293 }
294 CDBBatch batch(GetDB());
295 ok = CustomCommit(batch);
296 if (ok) {
297 GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
298 GetDB().WriteBatch(batch);
299 }
300 }
301 if (!ok) {
302 LogError("Failed to commit latest %s state", GetName());
303 }
304}
305
306bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
307{
308 assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
309
310 CBlock block;
311 CBlockUndo block_undo;
312
313 for (const CBlockIndex* iter_tip = current_tip; iter_tip != new_tip; iter_tip = iter_tip->pprev) {
314 interfaces::BlockInfo block_info = kernel::MakeBlockInfo(iter_tip);
315 if (CustomOptions().disconnect_data) {
316 if (!m_chainstate->m_blockman.ReadBlock(block, *iter_tip)) {
317 LogError("Failed to read block %s from disk",
318 iter_tip->GetBlockHash().ToString());
319 return false;
320 }
321 block_info.data = &block;
322 }
323 if (CustomOptions().disconnect_undo_data && iter_tip->nHeight > 0) {
324 if (!m_chainstate->m_blockman.ReadBlockUndo(block_undo, *iter_tip)) {
325 return false;
326 }
327 block_info.undo_data = &block_undo;
328 }
329 if (!CustomRemove(block_info)) {
330 return false;
331 }
332 }
333
334 // Don't commit here - the committed index state must never be ahead of the
335 // flushed chainstate, otherwise unclean restarts would lead to index corruption.
336 // Pruning has a minimum of 288 blocks-to-keep and getting the index
337 // out of sync may be possible but a users fault.
338 // In case we reorg beyond the pruned depth, ReadBlock would
339 // throw and lead to a graceful shutdown
340 SetBestBlockIndex(new_tip);
341 return true;
342}
343
344void BaseIndex::BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
345{
346 // Ignore events from not fully validated chains to avoid out-of-order indexing.
347 //
348 // TODO at some point we could parameterize whether a particular index can be
349 // built out of order, but for now just do the conservative simple thing.
350 if (!role.validated) {
351 return;
352 }
353
354 // Ignore BlockConnected signals until we have fully indexed the chain.
355 if (!m_synced) {
356 return;
357 }
358
359 const CBlockIndex* best_block_index = m_best_block_index.load();
360 if (!best_block_index) {
361 if (pindex->nHeight != 0) {
362 FatalErrorf("First block connected is not the genesis block (height=%d)",
363 pindex->nHeight);
364 return;
365 }
366 } else {
367 // Ensure block connects to an ancestor of the current best block. This should be the case
368 // most of the time, but may not be immediately after the sync thread catches up and sets
369 // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
370 // in the ValidationInterface queue backlog even after the sync thread has caught up to the
371 // new chain tip. In this unlikely event, log a warning and let the queue clear.
372 if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
373 LogWarning("Block %s does not connect to an ancestor of "
374 "known best chain (tip=%s); not updating index",
375 pindex->GetBlockHash().ToString(),
376 best_block_index->GetBlockHash().ToString());
377 return;
378 }
379 if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
380 FatalErrorf("Failed to rewind %s to a previous chain tip",
381 GetName());
382 return;
383 }
384 }
385
386 // Dispatch block to child class; errors are logged internally and abort the node.
387 if (ProcessBlock(pindex, block.get())) {
388 // Setting the best block index is intentionally the last step of this
389 // function, so BlockUntilSyncedToCurrentChain callers waiting for the
390 // best block index to be updated can rely on the block being fully
391 // processed, and the index object being safe to delete.
392 SetBestBlockIndex(pindex);
393 }
394}
395
397{
398 // Ignore events from not fully validated chains to avoid out-of-order indexing.
399 if (!role.validated) {
400 return;
401 }
402
403 if (!m_synced) {
404 return;
405 }
406
407 const uint256& locator_tip_hash = locator.vHave.front();
408 const CBlockIndex* locator_tip_index;
409 {
410 LOCK(cs_main);
411 locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
412 }
413
414 if (!locator_tip_index) {
415 FatalErrorf("First block (hash=%s) in locator was not found",
416 locator_tip_hash.ToString());
417 return;
418 }
419
420 // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
421 // immediately after the sync thread catches up and sets m_synced. Consider the case where
422 // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
423 // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
424 // event, log a warning and let the queue clear.
425 const CBlockIndex* best_block_index = m_best_block_index.load();
426 if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
427 LogWarning("Locator contains block (hash=%s) not on known best "
428 "chain (tip=%s); not writing index locator",
429 locator_tip_hash.ToString(),
430 best_block_index->GetBlockHash().ToString());
431 return;
432 }
433
434 // No need to handle errors in Commit. If it fails, the error will be already be logged. The
435 // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
436 // for an advanced index state.
437 Commit();
438}
439
440bool BaseIndex::BlockUntilSyncedToCurrentChain() const
441{
443
444 if (!m_synced) {
445 return false;
446 }
447
448 {
449 // Skip the queue-draining stuff if we know we're caught up with
450 // m_chain.Tip().
451 LOCK(cs_main);
452 const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
453 const CBlockIndex* best_block_index = m_best_block_index.load();
454 if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
455 return true;
456 }
457 }
458
459 LogInfo("%s is catching up on block notifications", GetName());
460 m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
461 return true;
462}
463
465{
466 m_interrupt();
467}
468
470{
471 if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index");
472
474 return true;
475}
476
478{
479 if (m_chain->context()->validation_signals) {
480 m_chain->context()->validation_signals->UnregisterValidationInterface(this);
481 }
482
483 if (m_thread_sync.joinable()) {
484 m_thread_sync.join();
485 }
486}
487
489{
490 IndexSummary summary{};
491 summary.name = GetName();
492 summary.synced = m_synced;
493 if (const auto& pindex = m_best_block_index.load()) {
494 summary.best_block_height = pindex->nHeight;
495 summary.best_block_hash = pindex->GetBlockHash();
496 } else {
497 summary.best_block_height = 0;
498 summary.best_block_hash = m_chain->getBlockHash(0);
499 }
500 return summary;
501}
502
504{
506
507 if (AllowPrune() && block) {
508 node::PruneLockInfo prune_lock;
509 prune_lock.height_first = block->nHeight;
510 WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock));
511 }
512
513 // Intentionally set m_best_block_index as the last step in this function,
514 // after updating prune locks above, and after making any other references
515 // to *this, so the BlockUntilSyncedToCurrentChain function (which checks
516 // m_best_block_index as an optimization) can be used to wait for the last
517 // BlockConnected notification and safely assume that prune locks are
518 // updated and that the index object is safe to delete.
519 m_best_block_index = block;
520}
ArgsManager gArgs
Definition: args.cpp:40
constexpr uint8_t DB_BEST_BLOCK
Definition: base.cpp:47
constexpr auto SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:50
constexpr auto SYNC_LOG_INTERVAL
Definition: base.cpp:49
CBlockLocator GetLocator(interfaces::Chain &chain, const uint256 &block_hash)
Definition: base.cpp:59
static const CBlockIndex * NextSyncBlock(const CBlockIndex *const pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:151
ArgsManager & args
Definition: bitcoind.cpp:280
#define Assert(val)
Identity function.
Definition: check.h:116
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false, bool f_bloom=true)
Definition: base.cpp:68
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:91
CBlockLocator ReadBestBlock() const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:79
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:477
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
Definition: base.h:123
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:503
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:105
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:99
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:130
void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:344
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:146
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.
Definition: base.cpp:464
virtual bool AllowPrune() const =0
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:88
void Commit()
Write the current index state (eg.
Definition: base.cpp:277
CThreadInterrupt m_interrupt
Definition: base.h:94
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:488
const std::string m_name
Definition: base.h:115
virtual DB & GetDB() const =0
void Sync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:208
std::thread m_thread_sync
Definition: base.h:93
virtual interfaces::Chain::NotifyOptions CustomOptions()
Return custom notification options for index.
Definition: base.h:149
bool ProcessBlock(const CBlockIndex *pindex, const CBlock *block_data=nullptr)
Definition: base.cpp:175
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name, std::string thread_name)
Definition: base.cpp:96
void FatalErrorf(util::ConstevalFormatString< sizeof...(Args)> fmt, const Args &... args)
Definition: base.cpp:53
Chainstate * m_chainstate
Definition: base.h:114
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRemove.
Definition: base.cpp:306
virtual bool CustomRemove(const interfaces::BlockInfo &block)
Rewind index by one block during a chain reorg.
Definition: base.h:133
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
Definition: base.cpp:469
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:113
std::atomic< bool > m_init
Whether the index has been initialized or not.
Definition: base.h:80
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:91
const std::string m_thread_name
Definition: base.h:116
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:126
void ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:396
Definition: block.h:74
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:100
uint256 GetBlockHash() const
Definition: chain.h:198
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:109
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:106
Undo information for a CBlock.
Definition: undo.h:64
An in-memory indexed chain of blocks.
Definition: chain.h:380
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:396
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:88
void Write(const K &key, const V &value)
Definition: dbwrapper.h:112
void WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:288
virtual void reset()
Reset to an non-interrupted state.
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:628
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:581
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * GetLastFlushedBlock() const EXCLUSIVE_LOCKS_REQUIRED(
Dictates whether we need to flush the cache to disk or not.
Definition: validation.h:844
std::string ToString() const
Definition: uint256.cpp:21
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:118
virtual bool findBlock(const uint256 &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:53
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
CBlockFileInfo *GetBlockFileInfo(size_t n) EXCLUSIVE_LOCKS_REQUIRED(bool WriteBlockUndo(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos WriteBlock(const CBlock &block, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(bool IsPruneMode() const
Get block file info entry for one block file.
Definition: blockstorage.h:407
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
256-bit opaque blob.
Definition: uint256.h:196
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
bool InitError(const bilingual_str &str)
Show error message.
is a home for simple enum and struct type definitions that can be used internally by functions in the...
std::thread thread
Thread variable should be after other struct members so the thread does not start until the other mem...
#define LogWarning(...)
Definition: log.h:126
#define LogInfo(...)
Definition: log.h:125
#define LogError(...)
Definition: log.h:127
#define LogDebug(category,...)
Definition: log.h:143
@ COINDB
Definition: categories.h:33
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex *index, const CBlock *data)
Return data from block index.
Definition: chain.cpp:18
void AbortNode(const std::function< bool()> &shutdown_request, std::atomic< int > &exit_status, const bilingual_str &message, node::Warnings *warnings)
Definition: abort.cpp:19
void ReadDatabaseArgs(const ArgsManager &args, DBOptions &options)
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Definition: thread.cpp:15
const char * name
Definition: rest.cpp:50
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:117
std::vector< uint256 > vHave
Definition: block.h:127
bool IsNull() const
Definition: block.h:145
void SetNull()
Definition: block.h:140
User-controlled performance and debug options.
Definition: dbwrapper.h:35
Application-specific storage settings.
Definition: dbwrapper.h:41
std::string name
Definition: base.h:31
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:38
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:19
const CBlock * data
Definition: chain.h:25
const CBlockUndo * undo_data
Definition: chain.h:26
Hash/height pair to help track and identify blocks.
Definition: types.h:13
Information about chainstate that notifications are sent from.
Definition: types.h:18
bool validated
Whether this is a notification from a chainstate that's been fully validated starting from the genesi...
Definition: types.h:22
int height_first
Height of earliest block that should be kept and not pruned.
Definition: blockstorage.h:152
A wrapper for a compile-time partially validated format string.
Definition: string.h:96
#define AssertLockNotHeld(cs)
Definition: sync.h:149
#define LOCK(cs)
Definition: sync.h:268
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:299
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())