Bitcoin ABC  0.22.12
P2P Digital Currency
base.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 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 <blockdb.h>
6 #include <chain.h>
7 #include <chainparams.h>
8 #include <config.h>
9 #include <index/base.h>
10 #include <node/ui_interface.h>
11 #include <shutdown.h>
12 #include <tinyformat.h>
13 #include <util/system.h>
14 #include <util/translation.h>
15 #include <validation.h>
16 #include <warnings.h>
17 
18 constexpr char DB_BEST_BLOCK = 'B';
19 
20 constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
21 constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
22 
23 template <typename... Args>
24 static void FatalError(const char *fmt, const Args &... args) {
25  std::string strMessage = tfm::format(fmt, args...);
26  SetMiscWarning(strMessage);
27  LogPrintf("*** %s\n", strMessage);
28  AbortError(_("A fatal internal error occurred, see debug.log for details"));
29  StartShutdown();
30 }
31 
32 BaseIndex::DB::DB(const fs::path &path, size_t n_cache_size, bool f_memory,
33  bool f_wipe, bool f_obfuscate)
34  : CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate) {}
35 
37  bool success = Read(DB_BEST_BLOCK, locator);
38  if (!success) {
39  locator.SetNull();
40  }
41  return success;
42 }
43 
45  const CBlockLocator &locator) {
46  batch.Write(DB_BEST_BLOCK, locator);
47 }
48 
50  Interrupt();
51  Stop();
52 }
53 
55  CBlockLocator locator;
56  if (!GetDB().ReadBestBlock(locator)) {
57  locator.SetNull();
58  }
59 
60  LOCK(cs_main);
61  if (locator.IsNull()) {
62  m_best_block_index = nullptr;
63  } else {
65  }
67  return true;
68 }
69 
70 static const CBlockIndex *NextSyncBlock(const CBlockIndex *pindex_prev)
73 
74  if (!pindex_prev) {
76  }
77 
78  const CBlockIndex *pindex = ::ChainActive().Next(pindex_prev);
79  if (pindex) {
80  return pindex;
81  }
82 
83  return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev));
84 }
85 
87  const CBlockIndex *pindex = m_best_block_index.load();
88  if (!m_synced) {
89  auto &consensus_params = GetConfig().GetChainParams().GetConsensus();
90 
91  int64_t last_log_time = 0;
92  int64_t last_locator_write_time = 0;
93  while (true) {
94  if (m_interrupt) {
95  m_best_block_index = pindex;
96  // No need to handle errors in Commit. If it fails, the error
97  // will be already be logged. The best way to recover is to
98  // continue, as index cannot be corrupted by a missed commit to
99  // disk for an advanced index state.
100  Commit();
101  return;
102  }
103 
104  {
105  LOCK(cs_main);
106  const CBlockIndex *pindex_next = NextSyncBlock(pindex);
107  if (!pindex_next) {
108  m_best_block_index = pindex;
109  m_synced = true;
110  // No need to handle errors in Commit. See rationale above.
111  Commit();
112  break;
113  }
114  if (pindex_next->pprev != pindex &&
115  !Rewind(pindex, pindex_next->pprev)) {
116  FatalError(
117  "%s: Failed to rewind index %s to a previous chain tip",
118  __func__, GetName());
119  return;
120  }
121  pindex = pindex_next;
122  }
123 
124  int64_t current_time = GetTime();
125  if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
126  LogPrintf("Syncing %s with block chain from height %d\n",
127  GetName(), pindex->nHeight);
128  last_log_time = current_time;
129  }
130 
131  if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL <
132  current_time) {
133  m_best_block_index = pindex;
134  last_locator_write_time = current_time;
135  // No need to handle errors in Commit. See rationale above.
136  Commit();
137  }
138 
139  CBlock block;
140  if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
141  FatalError("%s: Failed to read block %s from disk", __func__,
142  pindex->GetBlockHash().ToString());
143  return;
144  }
145  if (!WriteBlock(block, pindex)) {
146  FatalError("%s: Failed to write block %s to index database",
147  __func__, pindex->GetBlockHash().ToString());
148  return;
149  }
150  }
151  }
152 
153  if (pindex) {
154  LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
155  } else {
156  LogPrintf("%s is enabled\n", GetName());
157  }
158 }
159 
161  CDBBatch batch(GetDB());
162  if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
163  return error("%s: Failed to commit latest %s state", __func__,
164  GetName());
165  }
166  return true;
167 }
168 
170  LOCK(cs_main);
171  GetDB().WriteBestBlock(batch,
172  ::ChainActive().GetLocator(m_best_block_index));
173  return true;
174 }
175 
176 bool BaseIndex::Rewind(const CBlockIndex *current_tip,
177  const CBlockIndex *new_tip) {
178  assert(current_tip == m_best_block_index);
179  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
180 
181  // In the case of a reorg, ensure persisted block locator is not stale.
182  m_best_block_index = new_tip;
183  if (!Commit()) {
184  // If commit fails, revert the best block index to avoid corruption.
185  m_best_block_index = current_tip;
186  return false;
187  }
188 
189  return true;
190 }
191 
192 void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock> &block,
193  const CBlockIndex *pindex) {
194  if (!m_synced) {
195  return;
196  }
197 
198  const CBlockIndex *best_block_index = m_best_block_index.load();
199  if (!best_block_index) {
200  if (pindex->nHeight != 0) {
201  FatalError("%s: First block connected is not the genesis block "
202  "(height=%d)",
203  __func__, pindex->nHeight);
204  return;
205  }
206  } else {
207  // Ensure block connects to an ancestor of the current best block. This
208  // should be the case most of the time, but may not be immediately after
209  // the the sync thread catches up and sets m_synced. Consider the case
210  // where there is a reorg and the blocks on the stale branch are in the
211  // ValidationInterface queue backlog even after the sync thread has
212  // caught up to the new chain tip. In this unlikely event, log a warning
213  // and let the queue clear.
214  if (best_block_index->GetAncestor(pindex->nHeight - 1) !=
215  pindex->pprev) {
216  LogPrintf("%s: WARNING: Block %s does not connect to an ancestor "
217  "of known best chain (tip=%s); not updating index\n",
218  __func__, pindex->GetBlockHash().ToString(),
219  best_block_index->GetBlockHash().ToString());
220  return;
221  }
222  if (best_block_index != pindex->pprev &&
223  !Rewind(best_block_index, pindex->pprev)) {
224  FatalError("%s: Failed to rewind index %s to a previous chain tip",
225  __func__, GetName());
226  return;
227  }
228  }
229 
230  if (WriteBlock(*block, pindex)) {
231  m_best_block_index = pindex;
232  } else {
233  FatalError("%s: Failed to write block %s to index", __func__,
234  pindex->GetBlockHash().ToString());
235  return;
236  }
237 }
238 
240  if (!m_synced) {
241  return;
242  }
243 
244  const BlockHash &locator_tip_hash = locator.vHave.front();
245  const CBlockIndex *locator_tip_index;
246  {
247  LOCK(cs_main);
248  locator_tip_index = LookupBlockIndex(locator_tip_hash);
249  }
250 
251  if (!locator_tip_index) {
252  FatalError("%s: First block (hash=%s) in locator was not found",
253  __func__, locator_tip_hash.ToString());
254  return;
255  }
256 
257  // This checks that ChainStateFlushed callbacks are received after
258  // BlockConnected. The check may fail immediately after the the sync thread
259  // catches up and sets m_synced. Consider the case where there is a reorg
260  // and the blocks on the stale branch are in the ValidationInterface queue
261  // backlog even after the sync thread has caught up to the new chain tip. In
262  // this unlikely event, log a warning and let the queue clear.
263  const CBlockIndex *best_block_index = m_best_block_index.load();
264  if (best_block_index->GetAncestor(locator_tip_index->nHeight) !=
265  locator_tip_index) {
266  LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known "
267  "best chain (tip=%s); not writing index locator\n",
268  __func__, locator_tip_hash.ToString(),
269  best_block_index->GetBlockHash().ToString());
270  return;
271  }
272 
273  // No need to handle errors in Commit. If it fails, the error will be
274  // already be logged. The best way to recover is to continue, as index
275  // cannot be corrupted by a missed commit to disk for an advanced index
276  // state.
277  Commit();
278 }
279 
282 
283  if (!m_synced) {
284  return false;
285  }
286 
287  {
288  // Skip the queue-draining stuff if we know we're caught up with
289  // ::ChainActive().Tip().
290  LOCK(cs_main);
291  const CBlockIndex *chain_tip = ::ChainActive().Tip();
292  const CBlockIndex *best_block_index = m_best_block_index.load();
293  if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
294  return true;
295  }
296  }
297 
298  LogPrintf("%s: %s is catching up on block notifications\n", __func__,
299  GetName());
301  return true;
302 }
303 
305  m_interrupt();
306 }
307 
309  // Need to register this ValidationInterface before running Init(), so that
310  // callbacks are not missed if Init sets m_synced to true.
312  if (!Init()) {
313  FatalError("%s: %s failed to initialize", __func__, GetName());
314  return;
315  }
316 
317  m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
318  std::bind(&BaseIndex::ThreadSync, this));
319 }
320 
323 
324  if (m_thread_sync.joinable()) {
325  m_thread_sync.join();
326  }
327 }
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:21
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
bool Commit()
Write the current index state (eg.
Definition: base.cpp:160
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &params)
Functions for disk access for blocks.
Definition: blockdb.cpp:33
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:54
CThreadInterrupt m_interrupt
Definition: base.h:45
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:239
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:110
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:39
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:48
Definition: block.h:62
CChain & ChainActive()
Definition: validation.cpp:73
constexpr int64_t SYNC_LOG_INTERVAL
Definition: base.cpp:20
std::vector< BlockHash > vHave
Definition: block.h:111
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:171
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:161
bool IsNull() const
Definition: block.h:131
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
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:176
virtual const CChainParams & GetChainParams() const =0
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:321
std::thread m_thread_sync
Definition: base.h:44
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:76
BlockHash GetBlockHash() const
Definition: blockindex.h:133
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:49
void SetNull()
Definition: block.h:129
void Interrupt()
Definition: base.cpp:304
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:44
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:70
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:42
#define LOCK(cs)
Definition: sync.h:230
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
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:1111
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:198
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:90
constexpr auto AbortError
Definition: ui_interface.h:141
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:308
const Config & GetConfig()
Definition: config.cpp:34
virtual bool CommitInternal(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.cpp:169
std::string ToString() const
Definition: uint256.h:74
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:230
void TraceThread(const char *name, Callable func)
Definition: system.h:445
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: base.cpp:192
static void FatalError(const char *fmt, const Args &... args)
Definition: base.cpp:24
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
Definition: validation.cpp:146
constexpr char DB_BEST_BLOCK
Definition: base.cpp:18
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
CBlockIndex * LookupBlockIndex(const BlockHash &hash)
Definition: validation.cpp:140
void SetMiscWarning(const std::string &strWarning)
Definition: warnings.cpp:18
#define AssertLockNotHeld(cs)
Definition: sync.h:88
void StartShutdown()
Definition: shutdown.cpp:12
void RegisterValidationInterface(CValidationInterface *callbacks)
Register a wallet to receive updates from core.
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:86
virtual const char * GetName() const =0
Get the name of the index for display in logs.
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:186
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
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:32
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
bool ReadBestBlock(CBlockLocator &locator) const
Read block locator of the chain that the txindex is in sync with.
Definition: base.cpp:36
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:71
AssertLockHeld(g_cs_orphans)
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:27
virtual DB & GetDB() const =0
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
bool BlockUntilSyncedToCurrentChain() const
Blocks the current thread until the index is caught up to the current state of the block chain...
Definition: base.cpp:280