Bitcoin Core  24.99.0
P2P Digital Currency
base.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 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 <chainparams.h>
6 #include <index/base.h>
7 #include <interfaces/chain.h>
8 #include <kernel/chain.h>
9 #include <node/blockstorage.h>
10 #include <node/context.h>
11 #include <node/interface_ui.h>
12 #include <shutdown.h>
13 #include <tinyformat.h>
14 #include <util/syscall_sandbox.h>
15 #include <util/system.h>
16 #include <util/thread.h>
17 #include <util/translation.h>
18 #include <validation.h> // For g_chainman
19 #include <warnings.h>
20 
21 #include <string>
22 #include <utility>
23 
25 
26 constexpr uint8_t DB_BEST_BLOCK{'B'};
27 
28 constexpr auto SYNC_LOG_INTERVAL{30s};
29 constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s};
30 
31 template <typename... Args>
32 static void FatalError(const char* fmt, const Args&... args)
33 {
34  std::string strMessage = tfm::format(fmt, args...);
35  SetMiscWarning(Untranslated(strMessage));
36  LogPrintf("*** %s\n", strMessage);
37  AbortError(_("A fatal internal error occurred, see debug.log for details"));
38  StartShutdown();
39 }
40 
42 {
43  CBlockLocator locator;
44  bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator));
45  assert(found);
46  assert(!locator.IsNull());
47  return locator;
48 }
49 
50 BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
51  CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
52 {}
53 
55 {
56  bool success = Read(DB_BEST_BLOCK, locator);
57  if (!success) {
58  locator.SetNull();
59  }
60  return success;
61 }
62 
64 {
65  batch.Write(DB_BEST_BLOCK, locator);
66 }
67 
68 BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
69  : m_chain{std::move(chain)}, m_name{std::move(name)} {}
70 
72 {
73  Interrupt();
74  Stop();
75 }
76 
78 {
79  CBlockLocator locator;
80  if (!GetDB().ReadBestBlock(locator)) {
81  locator.SetNull();
82  }
83 
84  LOCK(cs_main);
85  CChain& active_chain = m_chainstate->m_chain;
86  if (locator.IsNull()) {
87  SetBestBlockIndex(nullptr);
88  } else {
90  }
91 
92  // Note: this will latch to true immediately if the user starts up with an empty
93  // datadir and an index enabled. If this is the case, indexation will happen solely
94  // via `BlockConnected` signals until, possibly, the next restart.
95  m_synced = m_best_block_index.load() == active_chain.Tip();
96  if (!m_synced) {
97  bool prune_violation = false;
98  if (!m_best_block_index) {
99  // index is not built yet
100  // make sure we have all block data back to the genesis
101  prune_violation = m_chainstate->m_blockman.GetFirstStoredBlock(*active_chain.Tip()) != active_chain.Genesis();
102  }
103  // in case the index has a best block set and is not fully synced
104  // check if we have the required blocks to continue building the index
105  else {
106  const CBlockIndex* block_to_test = m_best_block_index.load();
107  if (!active_chain.Contains(block_to_test)) {
108  // if the bestblock is not part of the mainchain, find the fork
109  // and make sure we have all data down to the fork
110  block_to_test = active_chain.FindFork(block_to_test);
111  }
112  const CBlockIndex* block = active_chain.Tip();
113  prune_violation = true;
114  // check backwards from the tip if we have all block data until we reach the indexes bestblock
115  while (block_to_test && block && (block->nStatus & BLOCK_HAVE_DATA)) {
116  if (block_to_test == block) {
117  prune_violation = false;
118  break;
119  }
120  // block->pprev must exist at this point, since block_to_test is part of the chain
121  // and thus must be encountered when going backwards from the tip
122  assert(block->pprev);
123  block = block->pprev;
124  }
125  }
126  if (prune_violation) {
127  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()));
128  }
129  }
130  return true;
131 }
132 
134 {
136 
137  if (!pindex_prev) {
138  return chain.Genesis();
139  }
140 
141  const CBlockIndex* pindex = chain.Next(pindex_prev);
142  if (pindex) {
143  return pindex;
144  }
145 
146  return chain.Next(chain.FindFork(pindex_prev));
147 }
148 
150 {
152  const CBlockIndex* pindex = m_best_block_index.load();
153  if (!m_synced) {
154  auto& consensus_params = Params().GetConsensus();
155 
156  std::chrono::steady_clock::time_point last_log_time{0s};
157  std::chrono::steady_clock::time_point last_locator_write_time{0s};
158  while (true) {
159  if (m_interrupt) {
160  SetBestBlockIndex(pindex);
161  // No need to handle errors in Commit. If it fails, the error will be already be
162  // logged. The best way to recover is to continue, as index cannot be corrupted by
163  // a missed commit to disk for an advanced index state.
164  Commit();
165  return;
166  }
167 
168  {
169  LOCK(cs_main);
170  const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
171  if (!pindex_next) {
172  SetBestBlockIndex(pindex);
173  m_synced = true;
174  // No need to handle errors in Commit. See rationale above.
175  Commit();
176  break;
177  }
178  if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
179  FatalError("%s: Failed to rewind index %s to a previous chain tip",
180  __func__, GetName());
181  return;
182  }
183  pindex = pindex_next;
184  }
185 
186  auto current_time{std::chrono::steady_clock::now()};
187  if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
188  LogPrintf("Syncing %s with block chain from height %d\n",
189  GetName(), pindex->nHeight);
190  last_log_time = current_time;
191  }
192 
193  if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
194  SetBestBlockIndex(pindex->pprev);
195  last_locator_write_time = current_time;
196  // No need to handle errors in Commit. See rationale above.
197  Commit();
198  }
199 
200  CBlock block;
201  interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
202  if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
203  FatalError("%s: Failed to read block %s from disk",
204  __func__, pindex->GetBlockHash().ToString());
205  return;
206  } else {
207  block_info.data = &block;
208  }
209  if (!CustomAppend(block_info)) {
210  FatalError("%s: Failed to write block %s to index database",
211  __func__, pindex->GetBlockHash().ToString());
212  return;
213  }
214  }
215  }
216 
217  if (pindex) {
218  LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
219  } else {
220  LogPrintf("%s is enabled\n", GetName());
221  }
222 }
223 
225 {
226  // Don't commit anything if we haven't indexed any block yet
227  // (this could happen if init is interrupted).
228  bool ok = m_best_block_index != nullptr;
229  if (ok) {
230  CDBBatch batch(GetDB());
231  ok = CustomCommit(batch);
232  if (ok) {
233  GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
234  ok = GetDB().WriteBatch(batch);
235  }
236  }
237  if (!ok) {
238  return error("%s: Failed to commit latest %s state", __func__, GetName());
239  }
240  return true;
241 }
242 
243 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
244 {
245  assert(current_tip == m_best_block_index);
246  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
247 
248  if (!CustomRewind({current_tip->GetBlockHash(), current_tip->nHeight}, {new_tip->GetBlockHash(), new_tip->nHeight})) {
249  return false;
250  }
251 
252  // In the case of a reorg, ensure persisted block locator is not stale.
253  // Pruning has a minimum of 288 blocks-to-keep and getting the index
254  // out of sync may be possible but a users fault.
255  // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
256  // throw and lead to a graceful shutdown
257  SetBestBlockIndex(new_tip);
258  if (!Commit()) {
259  // If commit fails, revert the best block index to avoid corruption.
260  SetBestBlockIndex(current_tip);
261  return false;
262  }
263 
264  return true;
265 }
266 
267 void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
268 {
269  if (!m_synced) {
270  return;
271  }
272 
273  const CBlockIndex* best_block_index = m_best_block_index.load();
274  if (!best_block_index) {
275  if (pindex->nHeight != 0) {
276  FatalError("%s: First block connected is not the genesis block (height=%d)",
277  __func__, pindex->nHeight);
278  return;
279  }
280  } else {
281  // Ensure block connects to an ancestor of the current best block. This should be the case
282  // most of the time, but may not be immediately after the sync thread catches up and sets
283  // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
284  // in the ValidationInterface queue backlog even after the sync thread has caught up to the
285  // new chain tip. In this unlikely event, log a warning and let the queue clear.
286  if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
287  LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
288  "known best chain (tip=%s); not updating index\n",
289  __func__, pindex->GetBlockHash().ToString(),
290  best_block_index->GetBlockHash().ToString());
291  return;
292  }
293  if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
294  FatalError("%s: Failed to rewind index %s to a previous chain tip",
295  __func__, GetName());
296  return;
297  }
298  }
299  interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get());
300  if (CustomAppend(block_info)) {
301  // Setting the best block index is intentionally the last step of this
302  // function, so BlockUntilSyncedToCurrentChain callers waiting for the
303  // best block index to be updated can rely on the block being fully
304  // processed, and the index object being safe to delete.
305  SetBestBlockIndex(pindex);
306  } else {
307  FatalError("%s: Failed to write block %s to index",
308  __func__, pindex->GetBlockHash().ToString());
309  return;
310  }
311 }
312 
314 {
315  if (!m_synced) {
316  return;
317  }
318 
319  const uint256& locator_tip_hash = locator.vHave.front();
320  const CBlockIndex* locator_tip_index;
321  {
322  LOCK(cs_main);
323  locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
324  }
325 
326  if (!locator_tip_index) {
327  FatalError("%s: First block (hash=%s) in locator was not found",
328  __func__, locator_tip_hash.ToString());
329  return;
330  }
331 
332  // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
333  // immediately after the sync thread catches up and sets m_synced. Consider the case where
334  // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
335  // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
336  // event, log a warning and let the queue clear.
337  const CBlockIndex* best_block_index = m_best_block_index.load();
338  if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
339  LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
340  "chain (tip=%s); not writing index locator\n",
341  __func__, locator_tip_hash.ToString(),
342  best_block_index->GetBlockHash().ToString());
343  return;
344  }
345 
346  // No need to handle errors in Commit. If it fails, the error will be already be logged. The
347  // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
348  // for an advanced index state.
349  Commit();
350 }
351 
352 bool BaseIndex::BlockUntilSyncedToCurrentChain() const
353 {
355 
356  if (!m_synced) {
357  return false;
358  }
359 
360  {
361  // Skip the queue-draining stuff if we know we're caught up with
362  // m_chain.Tip().
363  LOCK(cs_main);
364  const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
365  const CBlockIndex* best_block_index = m_best_block_index.load();
366  if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
367  return true;
368  }
369  }
370 
371  LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
373  return true;
374 }
375 
377 {
378  m_interrupt();
379 }
380 
382 {
383  // m_chainstate member gives indexing code access to node internals. It is
384  // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
385  m_chainstate = &m_chain->context()->chainman->ActiveChainstate();
386  // Need to register this ValidationInterface before running Init(), so that
387  // callbacks are not missed if Init sets m_synced to true.
389  if (!Init()) return false;
390 
391  const CBlockIndex* index = m_best_block_index.load();
392  if (!CustomInit(index ? std::make_optional(interfaces::BlockKey{index->GetBlockHash(), index->nHeight}) : std::nullopt)) {
393  return false;
394  }
395 
396  m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
397  return true;
398 }
399 
401 {
403 
404  if (m_thread_sync.joinable()) {
405  m_thread_sync.join();
406  }
407 }
408 
410 {
411  IndexSummary summary{};
412  summary.name = GetName();
413  summary.synced = m_synced;
414  summary.best_block_height = m_best_block_index ? m_best_block_index.load()->nHeight : 0;
415  return summary;
416 }
417 
419 {
421 
422  if (AllowPrune() && block) {
423  node::PruneLockInfo prune_lock;
424  prune_lock.height_first = block->nHeight;
425  WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock));
426  }
427 
428  // Intentionally set m_best_block_index as the last step in this function,
429  // after updating prune locks above, and after making any other references
430  // to *this, so the BlockUntilSyncedToCurrentChain function (which checks
431  // m_best_block_index as an optimization) can be used to wait for the last
432  // BlockConnected notification and safely assume that prune locks are
433  // updated and that the index object is safe to delete.
434  m_best_block_index = block;
435 }
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:133
static void FatalError(const char *fmt, const Args &... args)
Definition: base.cpp:32
constexpr uint8_t DB_BEST_BLOCK
Definition: base.cpp:26
constexpr auto SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:29
constexpr auto SYNC_LOG_INTERVAL
Definition: base.cpp:28
CBlockLocator GetLocator(interfaces::Chain &chain, const uint256 &block_hash)
Definition: base.cpp:41
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition: chain.h:126
const CChainParams & Params()
Return the currently selected parameters.
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the index is in sync with.
Definition: base.cpp:63
DB(const fs::path &path, size_t n_cache_size, bool f_memory=false, bool f_wipe=false, bool f_obfuscate=false)
Definition: base.cpp:50
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the index is in sync with.
Definition: base.cpp:54
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:400
void SetBestBlockIndex(const CBlockIndex *block)
Update the internal best block index as well as the prune lock.
Definition: base.cpp:418
bool Init()
Read best block locator and check that data needed to sync has not been pruned.
Definition: base.cpp:77
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:267
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:71
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:114
virtual bool AllowPrune() const =0
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
Definition: base.h:123
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:64
CThreadInterrupt m_interrupt
Definition: base.h:70
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
Definition: base.cpp:68
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:409
const std::string m_name
Definition: base.h:100
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:313
std::thread m_thread_sync
Definition: base.h:69
bool Commit()
Write the current index state (eg.
Definition: base.cpp:224
bool Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:381
virtual bool CustomInit(const std::optional< interfaces::BlockKey > &block)
Initialize internal state from the database and block index.
Definition: base.h:107
virtual bool CustomRewind(const interfaces::BlockKey &current_tip, const interfaces::BlockKey &new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.h:118
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:149
virtual DB & GetDB() const =0
Chainstate * m_chainstate
Definition: base.h:99
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Loop over disconnected blocks and call CustomRewind.
Definition: base.cpp:243
std::unique_ptr< interfaces::Chain > m_chain
Definition: base.h:98
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:67
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:110
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:157
uint256 GetBlockHash() const
Definition: chain.h:259
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:120
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:163
An in-memory indexed chain of blocks.
Definition: chain.h:436
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:446
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:452
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:60
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:466
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:64
void Write(const K &key, const V &value)
Definition: dbwrapper.h:89
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:200
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:518
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:479
const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
Definition: validation.cpp:112
std::string ToString() const
Definition: uint256.cpp:64
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:119
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:52
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:177
256-bit opaque blob.
Definition: uint256.h:119
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:207
bool InitError(const bilingual_str &str)
Show error message.
constexpr auto AbortError
Definition: interface_ui.h:119
#define LogPrintf(...)
Definition: logging.h:236
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex *index, const CBlock *data)
Return data from block index.
Definition: chain.cpp:13
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060
void TraceThread(std::string_view thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Definition: thread.cpp:16
ArgsManager args
const char * name
Definition: rest.cpp:46
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:58
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:121
std::vector< uint256 > vHave
Definition: block.h:122
bool IsNull() const
Definition: block.h:141
void SetNull()
Definition: block.h:136
std::string name
Definition: base.h:23
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:82
const CBlock * data
Definition: chain.h:88
Hash/height pair to help track and identify blocks.
Definition: chain.h:43
#define AssertLockNotHeld(cs)
Definition: sync.h:148
#define LOCK(cs)
Definition: sync.h:258
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:302
void SetSyscallSandboxPolicy(SyscallSandboxPolicy syscall_policy)
Force the current thread (and threads created from the current thread) into a restricted-service oper...
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
#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:1162
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:65
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:48
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
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)
Definition: warnings.cpp:19