Bitcoin Core  22.99.0
P2P Digital Currency
base.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 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 <node/blockstorage.h>
8 #include <node/ui_interface.h>
9 #include <shutdown.h>
10 #include <tinyformat.h>
11 #include <util/syscall_sandbox.h>
12 #include <util/thread.h>
13 #include <util/translation.h>
14 #include <validation.h> // For g_chainman
15 #include <warnings.h>
16 
18 
19 constexpr uint8_t DB_BEST_BLOCK{'B'};
20 
21 constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
22 constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
23 
24 template <typename... Args>
25 static void FatalError(const char* fmt, const Args&... args)
26 {
27  std::string strMessage = tfm::format(fmt, args...);
28  SetMiscWarning(Untranslated(strMessage));
29  LogPrintf("*** %s\n", strMessage);
30  AbortError(_("A fatal internal error occurred, see debug.log for details"));
31  StartShutdown();
32 }
33 
34 BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
35  CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
36 {}
37 
39 {
40  bool success = Read(DB_BEST_BLOCK, locator);
41  if (!success) {
42  locator.SetNull();
43  }
44  return success;
45 }
46 
48 {
49  batch.Write(DB_BEST_BLOCK, locator);
50 }
51 
53 {
54  Interrupt();
55  Stop();
56 }
57 
59 {
60  CBlockLocator locator;
61  if (!GetDB().ReadBestBlock(locator)) {
62  locator.SetNull();
63  }
64 
65  LOCK(cs_main);
66  CChain& active_chain = m_chainstate->m_chain;
67  if (locator.IsNull()) {
68  m_best_block_index = nullptr;
69  } else {
71  }
72  m_synced = m_best_block_index.load() == active_chain.Tip();
73  if (!m_synced) {
74  bool prune_violation = false;
75  if (!m_best_block_index) {
76  // index is not built yet
77  // make sure we have all block data back to the genesis
78  const CBlockIndex* block = active_chain.Tip();
79  while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
80  block = block->pprev;
81  }
82  prune_violation = block != active_chain.Genesis();
83  }
84  // in case the index has a best block set and is not fully synced
85  // check if we have the required blocks to continue building the index
86  else {
87  const CBlockIndex* block_to_test = m_best_block_index.load();
88  if (!active_chain.Contains(block_to_test)) {
89  // if the bestblock is not part of the mainchain, find the fork
90  // and make sure we have all data down to the fork
91  block_to_test = active_chain.FindFork(block_to_test);
92  }
93  const CBlockIndex* block = active_chain.Tip();
94  prune_violation = true;
95  // check backwards from the tip if we have all block data until we reach the indexes bestblock
96  while (block_to_test && block && (block->nStatus & BLOCK_HAVE_DATA)) {
97  if (block_to_test == block) {
98  prune_violation = false;
99  break;
100  }
101  // block->pprev must exist at this point, since block_to_test is part of the chain
102  // and thus must be encountered when going backwards from the tip
103  assert(block->pprev);
104  block = block->pprev;
105  }
106  }
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()));
109  }
110  }
111  return true;
112 }
113 
115 {
117 
118  if (!pindex_prev) {
119  return chain.Genesis();
120  }
121 
122  const CBlockIndex* pindex = chain.Next(pindex_prev);
123  if (pindex) {
124  return pindex;
125  }
126 
127  return chain.Next(chain.FindFork(pindex_prev));
128 }
129 
131 {
133  const CBlockIndex* pindex = m_best_block_index.load();
134  if (!m_synced) {
135  auto& consensus_params = Params().GetConsensus();
136 
137  int64_t last_log_time = 0;
138  int64_t last_locator_write_time = 0;
139  while (true) {
140  if (m_interrupt) {
141  m_best_block_index = pindex;
142  // No need to handle errors in Commit. If it fails, the error will be already be
143  // logged. The best way to recover is to continue, as index cannot be corrupted by
144  // a missed commit to disk for an advanced index state.
145  Commit();
146  return;
147  }
148 
149  {
150  LOCK(cs_main);
151  const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
152  if (!pindex_next) {
153  m_best_block_index = pindex;
154  m_synced = true;
155  // No need to handle errors in Commit. See rationale above.
156  Commit();
157  break;
158  }
159  if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
160  FatalError("%s: Failed to rewind index %s to a previous chain tip",
161  __func__, GetName());
162  return;
163  }
164  pindex = pindex_next;
165  }
166 
167  int64_t current_time = GetTime();
168  if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
169  LogPrintf("Syncing %s with block chain from height %d\n",
170  GetName(), pindex->nHeight);
171  last_log_time = current_time;
172  }
173 
174  if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
175  m_best_block_index = pindex;
176  last_locator_write_time = current_time;
177  // No need to handle errors in Commit. See rationale above.
178  Commit();
179  }
180 
181  CBlock block;
182  if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
183  FatalError("%s: Failed to read block %s from disk",
184  __func__, pindex->GetBlockHash().ToString());
185  return;
186  }
187  if (!WriteBlock(block, pindex)) {
188  FatalError("%s: Failed to write block %s to index database",
189  __func__, pindex->GetBlockHash().ToString());
190  return;
191  }
192  }
193  }
194 
195  if (pindex) {
196  LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
197  } else {
198  LogPrintf("%s is enabled\n", GetName());
199  }
200 }
201 
203 {
204  CDBBatch batch(GetDB());
205  if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
206  return error("%s: Failed to commit latest %s state", __func__, GetName());
207  }
208  return true;
209 }
210 
212 {
213  LOCK(cs_main);
214  // Don't commit anything if we haven't indexed any block yet
215  // (this could happen if init is interrupted).
216  if (m_best_block_index == nullptr) {
217  return false;
218  }
220  return true;
221 }
222 
223 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
224 {
225  assert(current_tip == m_best_block_index);
226  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
227 
228  // In the case of a reorg, ensure persisted block locator is not stale.
229  // Pruning has a minimum of 288 blocks-to-keep and getting the index
230  // out of sync may be possible but a users fault.
231  // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
232  // throw and lead to a graceful shutdown
233  m_best_block_index = new_tip;
234  if (!Commit()) {
235  // If commit fails, revert the best block index to avoid corruption.
236  m_best_block_index = current_tip;
237  return false;
238  }
239 
240  return true;
241 }
242 
243 void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
244 {
245  if (!m_synced) {
246  return;
247  }
248 
249  const CBlockIndex* best_block_index = m_best_block_index.load();
250  if (!best_block_index) {
251  if (pindex->nHeight != 0) {
252  FatalError("%s: First block connected is not the genesis block (height=%d)",
253  __func__, pindex->nHeight);
254  return;
255  }
256  } else {
257  // Ensure block connects to an ancestor of the current best block. This should be the case
258  // most of the time, but may not be immediately after the sync thread catches up and sets
259  // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
260  // in the ValidationInterface queue backlog even after the sync thread has caught up to the
261  // new chain tip. In this unlikely event, log a warning and let the queue clear.
262  if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
263  LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
264  "known best chain (tip=%s); not updating index\n",
265  __func__, pindex->GetBlockHash().ToString(),
266  best_block_index->GetBlockHash().ToString());
267  return;
268  }
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",
271  __func__, GetName());
272  return;
273  }
274  }
275 
276  if (WriteBlock(*block, pindex)) {
277  m_best_block_index = pindex;
278  } else {
279  FatalError("%s: Failed to write block %s to index",
280  __func__, pindex->GetBlockHash().ToString());
281  return;
282  }
283 }
284 
286 {
287  if (!m_synced) {
288  return;
289  }
290 
291  const uint256& locator_tip_hash = locator.vHave.front();
292  const CBlockIndex* locator_tip_index;
293  {
294  LOCK(cs_main);
295  locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
296  }
297 
298  if (!locator_tip_index) {
299  FatalError("%s: First block (hash=%s) in locator was not found",
300  __func__, locator_tip_hash.ToString());
301  return;
302  }
303 
304  // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
305  // immediately after the sync thread catches up and sets m_synced. Consider the case where
306  // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
307  // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
308  // event, log a warning and let the queue clear.
309  const CBlockIndex* best_block_index = m_best_block_index.load();
310  if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
311  LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
312  "chain (tip=%s); not writing index locator\n",
313  __func__, locator_tip_hash.ToString(),
314  best_block_index->GetBlockHash().ToString());
315  return;
316  }
317 
318  // No need to handle errors in Commit. If it fails, the error will be already be logged. The
319  // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
320  // for an advanced index state.
321  Commit();
322 }
323 
324 bool BaseIndex::BlockUntilSyncedToCurrentChain() const
325 {
327 
328  if (!m_synced) {
329  return false;
330  }
331 
332  {
333  // Skip the queue-draining stuff if we know we're caught up with
334  // m_chain.Tip().
335  LOCK(cs_main);
336  const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
337  const CBlockIndex* best_block_index = m_best_block_index.load();
338  if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
339  return true;
340  }
341  }
342 
343  LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
345  return true;
346 }
347 
349 {
350  m_interrupt();
351 }
352 
353 bool BaseIndex::Start(CChainState& active_chainstate)
354 {
355  m_chainstate = &active_chainstate;
356  // Need to register this ValidationInterface before running Init(), so that
357  // callbacks are not missed if Init sets m_synced to true.
359  if (!Init()) {
360  return false;
361  }
362 
363  m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
364  return true;
365 }
366 
368 {
370 
371  if (m_thread_sync.joinable()) {
372  m_thread_sync.join();
373  }
374 }
375 
377 {
378  IndexSummary summary{};
379  summary.name = GetName();
380  summary.synced = m_synced;
381  summary.best_block_height = m_best_block_index ? m_best_block_index.load()->nHeight : 0;
382  return summary;
383 }
CChainState::FindForkInGlobalIndex
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:155
BaseIndex::ChainStateFlushed
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:285
BaseIndex::m_synced
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:54
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
CBlockIndex::GetAncestor
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:114
assert
assert(!tx.IsCoinBase())
tinyformat::format
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:1062
node::BlockManager::LookupBlockIndex
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: blockstorage.cpp:35
BaseIndex::m_best_block_index
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:57
util::TraceThread
void TraceThread(const char *thread_name, std::function< void()> thread_func)
A wrapper for do-something-once thread functions.
Definition: thread.cpp:13
IndexSummary::name
std::string name
Definition: base.h:17
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:47
BaseIndex::Init
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:58
BaseIndex::GetSummary
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:376
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:158
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
SYNC_LOCATOR_WRITE_INTERVAL
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:22
BaseIndex::m_chainstate
CChainState * m_chainstate
Definition: base.h:79
CBlockLocator::IsNull
bool IsNull() const
Definition: block.h:135
BaseIndex::m_thread_sync
std::thread m_thread_sync
Definition: base.h:59
GetTime
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
BaseIndex::WriteBlock
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:91
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
CBlockLocator::SetNull
void SetNull()
Definition: block.h:130
chainparams.h
node::ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:745
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:447
base.h
BaseIndex::DB::WriteBestBlock
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:47
CChain::FindFork
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:54
AssertLockHeld
AssertLockHeld(pool.cs)
tinyformat.h
shutdown.h
CDBBatch::Write
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
BaseIndex::~BaseIndex
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:52
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:160
syscall_sandbox.h
BaseIndex::Stop
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:367
CChainState::m_blockman
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all CChainState instances.
Definition: validation.h:498
BaseIndex::DB::ReadBestBlock
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:38
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
AssertLockNotHeld
#define AssertLockNotHeld(cs)
Definition: sync.h:84
BaseIndex::Rewind
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
Definition: base.cpp:223
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
InitError
bool InitError(const bilingual_str &str)
Show error message.
Definition: ui_interface.cpp:59
CChain::GetLocator
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
Definition: chain.cpp:26
SetMiscWarning
void SetMiscWarning(const bilingual_str &warning)
Definition: warnings.cpp:19
LogPrintf
#define LogPrintf(...)
Definition: logging.h:188
Interrupt
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:181
SetSyscallSandboxPolicy
void SetSyscallSandboxPolicy(SyscallSandboxPolicy syscall_policy)
Force the current thread (and threads created from the current thread) into a restricted-service oper...
Definition: syscall_sandbox.cpp:830
NextSyncBlock
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev, CChain &chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:114
BaseIndex::DB::DB
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:34
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:126
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:459
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:264
UnregisterValidationInterface
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:140
ui_interface.h
CDBWrapper
Definition: dbwrapper.h:176
CBlock
Definition: block.h:62
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:430
DB_BEST_BLOCK
constexpr uint8_t DB_BEST_BLOCK
Definition: base.cpp:19
translation.h
BaseIndex::CommitInternal
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:211
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CBlockLocator::vHave
std::vector< uint256 > vHave
Definition: block.h:116
LOCK
#define LOCK(cs)
Definition: sync.h:226
RegisterValidationInterface
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
Definition: validationinterface.cpp:128
blockstorage.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
BaseIndex::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:243
CChain::Contains
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:461
IndexSummary
Definition: base.h:16
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
BaseIndex::GetDB
virtual DB & GetDB() const =0
BLOCK_HAVE_DATA
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition: chain.h:127
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:114
BaseIndex::ThreadSync
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:130
AbortError
constexpr auto AbortError
Definition: ui_interface.h:119
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
BaseIndex::GetName
virtual const char * GetName() const =0
Get the name of the index for display in logs.
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
SyscallSandboxPolicy::TX_INDEX
@ TX_INDEX
thread.h
BaseIndex::Start
bool Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:353
warnings.h
FatalError
static void FatalError(const char *fmt, const Args &... args)
Definition: base.cpp:25
BaseIndex::m_interrupt
CThreadInterrupt m_interrupt
Definition: base.h:60
SYNC_LOG_INTERVAL
constexpr int64_t SYNC_LOG_INTERVAL
Definition: base.cpp:21
StartShutdown
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:56
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:441
CChainState::m_chain
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:540
args
ArgsManager args
Definition: notifications.cpp:36
BaseIndex::Commit
bool Commit()
Write the current index state (eg.
Definition: base.cpp:202