Bitcoin Core  21.99.0
P2P Digital Currency
base.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 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/system.h>
12 #include <util/translation.h>
13 #include <validation.h> // For g_chainman
14 #include <warnings.h>
15 
16 constexpr char DB_BEST_BLOCK = 'B';
17 
18 constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
19 constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
20 
21 template <typename... Args>
22 static void FatalError(const char* fmt, const Args&... args)
23 {
24  std::string strMessage = tfm::format(fmt, args...);
25  SetMiscWarning(Untranslated(strMessage));
26  LogPrintf("*** %s\n", strMessage);
27  AbortError(_("A fatal internal error occurred, see debug.log for details"));
28  StartShutdown();
29 }
30 
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)
33 {}
34 
36 {
37  bool success = Read(DB_BEST_BLOCK, locator);
38  if (!success) {
39  locator.SetNull();
40  }
41  return success;
42 }
43 
45 {
46  batch.Write(DB_BEST_BLOCK, locator);
47 }
48 
50 {
51  Interrupt();
52  Stop();
53 }
54 
56 {
57  CBlockLocator locator;
58  if (!GetDB().ReadBestBlock(locator)) {
59  locator.SetNull();
60  }
61 
62  LOCK(cs_main);
63  if (locator.IsNull()) {
64  m_best_block_index = nullptr;
65  } else {
66  m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
67  }
69  if (!m_synced) {
70  bool prune_violation = false;
71  if (!m_best_block_index) {
72  // index is not built yet
73  // make sure we have all block data back to the genesis
74  const CBlockIndex* block = ::ChainActive().Tip();
75  while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
76  block = block->pprev;
77  }
78  prune_violation = block != ::ChainActive().Genesis();
79  }
80  // in case the index has a best block set and is not fully synced
81  // check if we have the required blocks to continue building the index
82  else {
83  const CBlockIndex* block_to_test = m_best_block_index.load();
84  if (!ChainActive().Contains(block_to_test)) {
85  // if the bestblock is not part of the mainchain, find the fork
86  // and make sure we have all data down to the fork
87  block_to_test = ::ChainActive().FindFork(block_to_test);
88  }
89  const CBlockIndex* block = ::ChainActive().Tip();
90  prune_violation = true;
91  // check backwards from the tip if we have all block data until we reach the indexes bestblock
92  while (block_to_test && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
93  if (block_to_test == block) {
94  prune_violation = false;
95  break;
96  }
97  block = block->pprev;
98  }
99  }
100  if (prune_violation) {
101  // throw error and graceful shutdown if we can't build the index
102  FatalError("%s: %s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", __func__, GetName());
103  return false;
104  }
105  }
106  return true;
107 }
108 
110 {
112 
113  if (!pindex_prev) {
115  }
116 
117  const CBlockIndex* pindex = ::ChainActive().Next(pindex_prev);
118  if (pindex) {
119  return pindex;
120  }
121 
122  return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev));
123 }
124 
126 {
127  const CBlockIndex* pindex = m_best_block_index.load();
128  if (!m_synced) {
129  auto& consensus_params = Params().GetConsensus();
130 
131  int64_t last_log_time = 0;
132  int64_t last_locator_write_time = 0;
133  while (true) {
134  if (m_interrupt) {
135  m_best_block_index = pindex;
136  // No need to handle errors in Commit. If it fails, the error will be already be
137  // logged. The best way to recover is to continue, as index cannot be corrupted by
138  // a missed commit to disk for an advanced index state.
139  Commit();
140  return;
141  }
142 
143  {
144  LOCK(cs_main);
145  const CBlockIndex* pindex_next = NextSyncBlock(pindex);
146  if (!pindex_next) {
147  m_best_block_index = pindex;
148  m_synced = true;
149  // No need to handle errors in Commit. See rationale above.
150  Commit();
151  break;
152  }
153  if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
154  FatalError("%s: Failed to rewind index %s to a previous chain tip",
155  __func__, GetName());
156  return;
157  }
158  pindex = pindex_next;
159  }
160 
161  int64_t current_time = GetTime();
162  if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
163  LogPrintf("Syncing %s with block chain from height %d\n",
164  GetName(), pindex->nHeight);
165  last_log_time = current_time;
166  }
167 
168  if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
169  m_best_block_index = pindex;
170  last_locator_write_time = current_time;
171  // No need to handle errors in Commit. See rationale above.
172  Commit();
173  }
174 
175  CBlock block;
176  if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
177  FatalError("%s: Failed to read block %s from disk",
178  __func__, pindex->GetBlockHash().ToString());
179  return;
180  }
181  if (!WriteBlock(block, pindex)) {
182  FatalError("%s: Failed to write block %s to index database",
183  __func__, pindex->GetBlockHash().ToString());
184  return;
185  }
186  }
187  }
188 
189  if (pindex) {
190  LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
191  } else {
192  LogPrintf("%s is enabled\n", GetName());
193  }
194 }
195 
197 {
198  CDBBatch batch(GetDB());
199  if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
200  return error("%s: Failed to commit latest %s state", __func__, GetName());
201  }
202  return true;
203 }
204 
206 {
207  LOCK(cs_main);
208  GetDB().WriteBestBlock(batch, ::ChainActive().GetLocator(m_best_block_index));
209  return true;
210 }
211 
212 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
213 {
214  assert(current_tip == m_best_block_index);
215  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
216 
217  // In the case of a reorg, ensure persisted block locator is not stale.
218  // Pruning has a minimum of 288 blocks-to-keep and getting the index
219  // out of sync may be possible but a users fault.
220  // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
221  // throw and lead to a graceful shutdown
222  m_best_block_index = new_tip;
223  if (!Commit()) {
224  // If commit fails, revert the best block index to avoid corruption.
225  m_best_block_index = current_tip;
226  return false;
227  }
228 
229  return true;
230 }
231 
232 void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
233 {
234  if (!m_synced) {
235  return;
236  }
237 
238  const CBlockIndex* best_block_index = m_best_block_index.load();
239  if (!best_block_index) {
240  if (pindex->nHeight != 0) {
241  FatalError("%s: First block connected is not the genesis block (height=%d)",
242  __func__, pindex->nHeight);
243  return;
244  }
245  } else {
246  // Ensure block connects to an ancestor of the current best block. This should be the case
247  // most of the time, but may not be immediately after the sync thread catches up and sets
248  // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
249  // in the ValidationInterface queue backlog even after the sync thread has caught up to the
250  // new chain tip. In this unlikely event, log a warning and let the queue clear.
251  if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
252  LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
253  "known best chain (tip=%s); not updating index\n",
254  __func__, pindex->GetBlockHash().ToString(),
255  best_block_index->GetBlockHash().ToString());
256  return;
257  }
258  if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
259  FatalError("%s: Failed to rewind index %s to a previous chain tip",
260  __func__, GetName());
261  return;
262  }
263  }
264 
265  if (WriteBlock(*block, pindex)) {
266  m_best_block_index = pindex;
267  } else {
268  FatalError("%s: Failed to write block %s to index",
269  __func__, pindex->GetBlockHash().ToString());
270  return;
271  }
272 }
273 
275 {
276  if (!m_synced) {
277  return;
278  }
279 
280  const uint256& locator_tip_hash = locator.vHave.front();
281  const CBlockIndex* locator_tip_index;
282  {
283  LOCK(cs_main);
284  locator_tip_index = g_chainman.m_blockman.LookupBlockIndex(locator_tip_hash);
285  }
286 
287  if (!locator_tip_index) {
288  FatalError("%s: First block (hash=%s) in locator was not found",
289  __func__, locator_tip_hash.ToString());
290  return;
291  }
292 
293  // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
294  // immediately after the sync thread catches up and sets m_synced. Consider the case where
295  // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
296  // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
297  // event, log a warning and let the queue clear.
298  const CBlockIndex* best_block_index = m_best_block_index.load();
299  if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
300  LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
301  "chain (tip=%s); not writing index locator\n",
302  __func__, locator_tip_hash.ToString(),
303  best_block_index->GetBlockHash().ToString());
304  return;
305  }
306 
307  // No need to handle errors in Commit. If it fails, the error will be already be logged. The
308  // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
309  // for an advanced index state.
310  Commit();
311 }
312 
313 bool BaseIndex::BlockUntilSyncedToCurrentChain() const
314 {
316 
317  if (!m_synced) {
318  return false;
319  }
320 
321  {
322  // Skip the queue-draining stuff if we know we're caught up with
323  // ::ChainActive().Tip().
324  LOCK(cs_main);
325  const CBlockIndex* chain_tip = ::ChainActive().Tip();
326  const CBlockIndex* best_block_index = m_best_block_index.load();
327  if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
328  return true;
329  }
330  }
331 
332  LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
334  return true;
335 }
336 
338 {
339  m_interrupt();
340 }
341 
343 {
344  // Need to register this ValidationInterface before running Init(), so that
345  // callbacks are not missed if Init sets m_synced to true.
347  if (!Init()) {
348  FatalError("%s: %s failed to initialize", __func__, GetName());
349  return;
350  }
351 
352  m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
353  std::bind(&BaseIndex::ThreadSync, this));
354 }
355 
357 {
359 
360  if (m_thread_sync.joinable()) {
361  m_thread_sync.join();
362  }
363 }
364 
366 {
367  IndexSummary summary{};
368  summary.name = GetName();
369  summary.synced = m_synced;
370  summary.best_block_height = m_best_block_index ? m_best_block_index.load()->nHeight : 0;
371  return summary;
372 }
BaseIndex::ChainStateFlushed
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:274
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:57
CBlockIndex::GetAncestor
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:111
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
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
ChainActive
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
Definition: validation.cpp:115
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:55
BaseIndex::GetSummary
IndexSummary GetSummary() const
Get a summary of the index and its state.
Definition: base.cpp:365
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:144
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
SYNC_LOCATOR_WRITE_INTERVAL
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:19
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 GetSystemTimeInSeconds (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:88
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:77
CBlockLocator::SetNull
void SetNull()
Definition: block.h:130
chainparams.h
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:403
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:131
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:44
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:51
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:49
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
BaseIndex::Stop
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:356
TraceThread
void TraceThread(const char *name, Callable func)
Definition: system.h:464
BaseIndex::DB::ReadBestBlock
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:35
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
BaseIndex::Start
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:342
AssertLockNotHeld
#define AssertLockNotHeld(cs)
Definition: sync.h:82
DB_BEST_BLOCK
constexpr char DB_BEST_BLOCK
Definition: base.cpp:16
CBlockIndex::nStatus
uint32_t nStatus
Verification status of this block.
Definition: chain.h:187
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:212
SetMiscWarning
void SetMiscWarning(const bilingual_str &warning)
Definition: warnings.cpp:19
g_chainman
ChainstateManager g_chainman
Definition: validation.cpp:106
LogPrintf
#define LogPrintf(...)
Definition: logging.h:183
Interrupt
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:156
ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:44
AssertLockHeld
#define AssertLockHeld(cs)
Definition: sync.h:81
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:31
base_blob::ToString
std::string ToString() const
Definition: uint256.cpp:64
uint256
256-bit opaque blob.
Definition: uint256.h:124
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:246
UnregisterValidationInterface
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:140
ui_interface.h
CDBWrapper
Definition: dbwrapper.h:176
system.h
CBlock
Definition: block.h:62
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:205
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:232
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:538
NextSyncBlock
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:109
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:232
IndexSummary
Definition: base.h:16
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:50
BaseIndex::GetDB
virtual DB & GetDB() const =0
BLOCK_HAVE_DATA
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition: chain.h:121
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:125
AbortError
constexpr auto AbortError
Definition: ui_interface.h:117
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:137
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
warnings.h
FatalError
static void FatalError(const char *fmt, const Args &... args)
Definition: base.cpp:22
BaseIndex::m_interrupt
CThreadInterrupt m_interrupt
Definition: base.h:60
SYNC_LOG_INTERVAL
constexpr int64_t SYNC_LOG_INTERVAL
Definition: base.cpp:18
StartShutdown
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:56
CChain::Next
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...
Definition: chain.h:420
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:398
BaseIndex::Commit
bool Commit()
Write the current index state (eg.
Definition: base.cpp:196