/ src / index / base.cpp
base.cpp
  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 <common/args.h>
  7  #include <index/base.h>
  8  #include <interfaces/chain.h>
  9  #include <kernel/chain.h>
 10  #include <logging.h>
 11  #include <node/abort.h>
 12  #include <node/blockstorage.h>
 13  #include <node/context.h>
 14  #include <node/database_args.h>
 15  #include <node/interface_ui.h>
 16  #include <tinyformat.h>
 17  #include <util/thread.h>
 18  #include <util/translation.h>
 19  #include <validation.h> // For g_chainman
 20  #include <warnings.h>
 21  
 22  #include <string>
 23  #include <utility>
 24  
 25  constexpr uint8_t DB_BEST_BLOCK{'B'};
 26  
 27  constexpr auto SYNC_LOG_INTERVAL{30s};
 28  constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s};
 29  
 30  template <typename... Args>
 31  void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
 32  {
 33      auto message = tfm::format(fmt, args...);
 34      node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message));
 35  }
 36  
 37  CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
 38  {
 39      CBlockLocator locator;
 40      bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator));
 41      assert(found);
 42      assert(!locator.IsNull());
 43      return locator;
 44  }
 45  
 46  BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
 47      CDBWrapper{DBParams{
 48          .path = path,
 49          .cache_bytes = n_cache_size,
 50          .memory_only = f_memory,
 51          .wipe_data = f_wipe,
 52          .obfuscate = f_obfuscate,
 53          .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
 54  {}
 55  
 56  bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
 57  {
 58      bool success = Read(DB_BEST_BLOCK, locator);
 59      if (!success) {
 60          locator.SetNull();
 61      }
 62      return success;
 63  }
 64  
 65  void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
 66  {
 67      batch.Write(DB_BEST_BLOCK, locator);
 68  }
 69  
 70  BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
 71      : m_chain{std::move(chain)}, m_name{std::move(name)} {}
 72  
 73  BaseIndex::~BaseIndex()
 74  {
 75      Interrupt();
 76      Stop();
 77  }
 78  
 79  bool BaseIndex::Init()
 80  {
 81      AssertLockNotHeld(cs_main);
 82  
 83      // May need reset if index is being restarted.
 84      m_interrupt.reset();
 85  
 86      // m_chainstate member gives indexing code access to node internals. It is
 87      // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
 88      m_chainstate = WITH_LOCK(::cs_main,
 89          return &m_chain->context()->chainman->GetChainstateForIndexing());
 90      // Register to validation interface before setting the 'm_synced' flag, so that
 91      // callbacks are not missed once m_synced is true.
 92      m_chain->context()->validation_signals->RegisterValidationInterface(this);
 93  
 94      CBlockLocator locator;
 95      if (!GetDB().ReadBestBlock(locator)) {
 96          locator.SetNull();
 97      }
 98  
 99      LOCK(cs_main);
100      CChain& index_chain = m_chainstate->m_chain;
101  
102      if (locator.IsNull()) {
103          SetBestBlockIndex(nullptr);
104      } else {
105          // Setting the best block to the locator's top block. If it is not part of the
106          // best chain, we will rewind to the fork point during index sync
107          const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
108          if (!locator_index) {
109              return InitError(strprintf(Untranslated("%s: best block of the index not found. Please rebuild the index."), GetName()));
110          }
111          SetBestBlockIndex(locator_index);
112      }
113  
114      // Child init
115      const CBlockIndex* start_block = m_best_block_index.load();
116      if (!CustomInit(start_block ? std::make_optional(interfaces::BlockKey{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
117          return false;
118      }
119  
120      // Note: this will latch to true immediately if the user starts up with an empty
121      // datadir and an index enabled. If this is the case, indexation will happen solely
122      // via `BlockConnected` signals until, possibly, the next restart.
123      m_synced = start_block == index_chain.Tip();
124      m_init = true;
125      return true;
126  }
127  
128  static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
129  {
130      AssertLockHeld(cs_main);
131  
132      if (!pindex_prev) {
133          return chain.Genesis();
134      }
135  
136      const CBlockIndex* pindex = chain.Next(pindex_prev);
137      if (pindex) {
138          return pindex;
139      }
140  
141      return chain.Next(chain.FindFork(pindex_prev));
142  }
143  
144  void BaseIndex::Sync()
145  {
146      const CBlockIndex* pindex = m_best_block_index.load();
147      if (!m_synced) {
148          std::chrono::steady_clock::time_point last_log_time{0s};
149          std::chrono::steady_clock::time_point last_locator_write_time{0s};
150          while (true) {
151              if (m_interrupt) {
152                  LogPrintf("%s: m_interrupt set; exiting ThreadSync\n", GetName());
153  
154                  SetBestBlockIndex(pindex);
155                  // No need to handle errors in Commit. If it fails, the error will be already be
156                  // logged. The best way to recover is to continue, as index cannot be corrupted by
157                  // a missed commit to disk for an advanced index state.
158                  Commit();
159                  return;
160              }
161  
162              const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
163              if (!pindex_next) {
164                  SetBestBlockIndex(pindex);
165                  m_synced = true;
166                  // No need to handle errors in Commit. See rationale above.
167                  Commit();
168                  break;
169              }
170              if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
171                  FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName());
172                  return;
173              }
174              pindex = pindex_next;
175  
176  
177              CBlock block;
178              interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
179              if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *pindex)) {
180                  FatalErrorf("%s: Failed to read block %s from disk",
181                             __func__, pindex->GetBlockHash().ToString());
182                  return;
183              } else {
184                  block_info.data = &block;
185              }
186              if (!CustomAppend(block_info)) {
187                  FatalErrorf("%s: Failed to write block %s to index database",
188                             __func__, pindex->GetBlockHash().ToString());
189                  return;
190              }
191  
192              auto current_time{std::chrono::steady_clock::now()};
193              if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
194                  LogPrintf("Syncing %s with block chain from height %d\n",
195                            GetName(), pindex->nHeight);
196                  last_log_time = current_time;
197              }
198  
199              if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
200                  SetBestBlockIndex(pindex);
201                  last_locator_write_time = current_time;
202                  // No need to handle errors in Commit. See rationale above.
203                  Commit();
204              }
205          }
206      }
207  
208      if (pindex) {
209          LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
210      } else {
211          LogPrintf("%s is enabled\n", GetName());
212      }
213  }
214  
215  bool BaseIndex::Commit()
216  {
217      // Don't commit anything if we haven't indexed any block yet
218      // (this could happen if init is interrupted).
219      bool ok = m_best_block_index != nullptr;
220      if (ok) {
221          CDBBatch batch(GetDB());
222          ok = CustomCommit(batch);
223          if (ok) {
224              GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
225              ok = GetDB().WriteBatch(batch);
226          }
227      }
228      if (!ok) {
229          LogError("%s: Failed to commit latest %s state\n", __func__, GetName());
230          return false;
231      }
232      return true;
233  }
234  
235  bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
236  {
237      assert(current_tip == m_best_block_index);
238      assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
239  
240      if (!CustomRewind({current_tip->GetBlockHash(), current_tip->nHeight}, {new_tip->GetBlockHash(), new_tip->nHeight})) {
241          return false;
242      }
243  
244      // In the case of a reorg, ensure persisted block locator is not stale.
245      // Pruning has a minimum of 288 blocks-to-keep and getting the index
246      // out of sync may be possible but a users fault.
247      // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
248      // throw and lead to a graceful shutdown
249      SetBestBlockIndex(new_tip);
250      if (!Commit()) {
251          // If commit fails, revert the best block index to avoid corruption.
252          SetBestBlockIndex(current_tip);
253          return false;
254      }
255  
256      return true;
257  }
258  
259  void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
260  {
261      // Ignore events from the assumed-valid chain; we will process its blocks
262      // (sequentially) after it is fully verified by the background chainstate. This
263      // is to avoid any out-of-order indexing.
264      //
265      // TODO at some point we could parameterize whether a particular index can be
266      // built out of order, but for now just do the conservative simple thing.
267      if (role == ChainstateRole::ASSUMEDVALID) {
268          return;
269      }
270  
271      // Ignore BlockConnected signals until we have fully indexed the chain.
272      if (!m_synced) {
273          return;
274      }
275  
276      const CBlockIndex* best_block_index = m_best_block_index.load();
277      if (!best_block_index) {
278          if (pindex->nHeight != 0) {
279              FatalErrorf("%s: First block connected is not the genesis block (height=%d)",
280                         __func__, pindex->nHeight);
281              return;
282          }
283      } else {
284          // Ensure block connects to an ancestor of the current best block. This should be the case
285          // most of the time, but may not be immediately after the sync thread catches up and sets
286          // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
287          // in the ValidationInterface queue backlog even after the sync thread has caught up to the
288          // new chain tip. In this unlikely event, log a warning and let the queue clear.
289          if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
290              LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of "
291                        "known best chain (tip=%s); not updating index\n",
292                        __func__, pindex->GetBlockHash().ToString(),
293                        best_block_index->GetBlockHash().ToString());
294              return;
295          }
296          if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
297              FatalErrorf("%s: Failed to rewind index %s to a previous chain tip",
298                         __func__, GetName());
299              return;
300          }
301      }
302      interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get());
303      if (CustomAppend(block_info)) {
304          // Setting the best block index is intentionally the last step of this
305          // function, so BlockUntilSyncedToCurrentChain callers waiting for the
306          // best block index to be updated can rely on the block being fully
307          // processed, and the index object being safe to delete.
308          SetBestBlockIndex(pindex);
309      } else {
310          FatalErrorf("%s: Failed to write block %s to index",
311                     __func__, pindex->GetBlockHash().ToString());
312          return;
313      }
314  }
315  
316  void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator)
317  {
318      // Ignore events from the assumed-valid chain; we will process its blocks
319      // (sequentially) after it is fully verified by the background chainstate.
320      if (role == ChainstateRole::ASSUMEDVALID) {
321          return;
322      }
323  
324      if (!m_synced) {
325          return;
326      }
327  
328      const uint256& locator_tip_hash = locator.vHave.front();
329      const CBlockIndex* locator_tip_index;
330      {
331          LOCK(cs_main);
332          locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
333      }
334  
335      if (!locator_tip_index) {
336          FatalErrorf("%s: First block (hash=%s) in locator was not found",
337                     __func__, locator_tip_hash.ToString());
338          return;
339      }
340  
341      // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
342      // immediately after the sync thread catches up and sets m_synced. Consider the case where
343      // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
344      // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
345      // event, log a warning and let the queue clear.
346      const CBlockIndex* best_block_index = m_best_block_index.load();
347      if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
348          LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best "
349                    "chain (tip=%s); not writing index locator\n",
350                    __func__, locator_tip_hash.ToString(),
351                    best_block_index->GetBlockHash().ToString());
352          return;
353      }
354  
355      // No need to handle errors in Commit. If it fails, the error will be already be logged. The
356      // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
357      // for an advanced index state.
358      Commit();
359  }
360  
361  bool BaseIndex::BlockUntilSyncedToCurrentChain() const
362  {
363      AssertLockNotHeld(cs_main);
364  
365      if (!m_synced) {
366          return false;
367      }
368  
369      {
370          // Skip the queue-draining stuff if we know we're caught up with
371          // m_chain.Tip().
372          LOCK(cs_main);
373          const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
374          const CBlockIndex* best_block_index = m_best_block_index.load();
375          if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
376              return true;
377          }
378      }
379  
380      LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
381      m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
382      return true;
383  }
384  
385  void BaseIndex::Interrupt()
386  {
387      m_interrupt();
388  }
389  
390  bool BaseIndex::StartBackgroundSync()
391  {
392      if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index");
393  
394      m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { Sync(); });
395      return true;
396  }
397  
398  void BaseIndex::Stop()
399  {
400      if (m_chain->context()->validation_signals) {
401          m_chain->context()->validation_signals->UnregisterValidationInterface(this);
402      }
403  
404      if (m_thread_sync.joinable()) {
405          m_thread_sync.join();
406      }
407  }
408  
409  IndexSummary BaseIndex::GetSummary() const
410  {
411      IndexSummary summary{};
412      summary.name = GetName();
413      summary.synced = m_synced;
414      if (const auto& pindex = m_best_block_index.load()) {
415          summary.best_block_height = pindex->nHeight;
416          summary.best_block_hash = pindex->GetBlockHash();
417      } else {
418          summary.best_block_height = 0;
419          summary.best_block_hash = m_chain->getBlockHash(0);
420      }
421      return summary;
422  }
423  
424  void BaseIndex::SetBestBlockIndex(const CBlockIndex* block)
425  {
426      assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune());
427  
428      if (AllowPrune() && block) {
429          node::PruneLockInfo prune_lock;
430          prune_lock.height_first = block->nHeight;
431          WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock));
432      }
433  
434      // Intentionally set m_best_block_index as the last step in this function,
435      // after updating prune locks above, and after making any other references
436      // to *this, so the BlockUntilSyncedToCurrentChain function (which checks
437      // m_best_block_index as an optimization) can be used to wait for the last
438      // BlockConnected notification and safely assume that prune locks are
439      // updated and that the index object is safe to delete.
440      m_best_block_index = block;
441  }