Bitcoin Core  0.20.99
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 <shutdown.h>
8 #include <tinyformat.h>
9 #include <ui_interface.h>
10 #include <util/system.h>
11 #include <util/translation.h>
12 #include <validation.h>
13 #include <warnings.h>
14 
15 constexpr char DB_BEST_BLOCK = 'B';
16 
17 constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
18 constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
19 
20 template<typename... Args>
21 static void FatalError(const char* fmt, const Args&... args)
22 {
23  std::string strMessage = tfm::format(fmt, args...);
24  SetMiscWarning(strMessage);
25  LogPrintf("*** %s\n", strMessage);
26  uiInterface.ThreadSafeMessageBox(
27  Untranslated("Error: 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, bool f_wipe, bool f_obfuscate) :
33  CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
34 {}
35 
37 {
38  bool success = Read(DB_BEST_BLOCK, locator);
39  if (!success) {
40  locator.SetNull();
41  }
42  return success;
43 }
44 
46 {
47  batch.Write(DB_BEST_BLOCK, locator);
48 }
49 
51 {
52  Interrupt();
53  Stop();
54 }
55 
57 {
58  CBlockLocator locator;
59  if (!GetDB().ReadBestBlock(locator)) {
60  locator.SetNull();
61  }
62 
63  LOCK(cs_main);
64  if (locator.IsNull()) {
65  m_best_block_index = nullptr;
66  } else {
68  }
70  return true;
71 }
72 
74 {
76 
77  if (!pindex_prev) {
79  }
80 
81  const CBlockIndex* pindex = ::ChainActive().Next(pindex_prev);
82  if (pindex) {
83  return pindex;
84  }
85 
86  return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev));
87 }
88 
90 {
91  const CBlockIndex* pindex = m_best_block_index.load();
92  if (!m_synced) {
93  auto& consensus_params = Params().GetConsensus();
94 
95  int64_t last_log_time = 0;
96  int64_t last_locator_write_time = 0;
97  while (true) {
98  if (m_interrupt) {
99  m_best_block_index = pindex;
100  // No need to handle errors in Commit. If it fails, the error will be already be
101  // logged. The best way to recover is to continue, as index cannot be corrupted by
102  // a missed commit to disk for an advanced index state.
103  Commit();
104  return;
105  }
106 
107  {
108  LOCK(cs_main);
109  const CBlockIndex* pindex_next = NextSyncBlock(pindex);
110  if (!pindex_next) {
111  m_best_block_index = pindex;
112  m_synced = true;
113  // No need to handle errors in Commit. See rationale above.
114  Commit();
115  break;
116  }
117  if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
118  FatalError("%s: Failed to rewind index %s to a previous chain tip",
119  __func__, GetName());
120  return;
121  }
122  pindex = pindex_next;
123  }
124 
125  int64_t current_time = GetTime();
126  if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
127  LogPrintf("Syncing %s with block chain from height %d\n",
128  GetName(), pindex->nHeight);
129  last_log_time = current_time;
130  }
131 
132  if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < 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",
142  __func__, 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 {
162  CDBBatch batch(GetDB());
163  if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
164  return error("%s: Failed to commit latest %s state", __func__, GetName());
165  }
166  return true;
167 }
168 
170 {
171  LOCK(cs_main);
172  GetDB().WriteBestBlock(batch, ::ChainActive().GetLocator(m_best_block_index));
173  return true;
174 }
175 
176 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
177 {
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, const CBlockIndex* pindex)
193 {
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 (height=%d)",
202  __func__, pindex->nHeight);
203  return;
204  }
205  } else {
206  // Ensure block connects to an ancestor of the current best block. This should be the case
207  // most of the time, but may not be immediately after the sync thread catches up and sets
208  // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
209  // in the ValidationInterface queue backlog even after the sync thread has caught up to the
210  // new chain tip. In this unlikely event, log a warning and let the queue clear.
211  if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
212  LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
213  "known best chain (tip=%s); not updating index\n",
214  __func__, pindex->GetBlockHash().ToString(),
215  best_block_index->GetBlockHash().ToString());
216  return;
217  }
218  if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
219  FatalError("%s: Failed to rewind index %s to a previous chain tip",
220  __func__, GetName());
221  return;
222  }
223  }
224 
225  if (WriteBlock(*block, pindex)) {
226  m_best_block_index = pindex;
227  } else {
228  FatalError("%s: Failed to write block %s to index",
229  __func__, pindex->GetBlockHash().ToString());
230  return;
231  }
232 }
233 
235 {
236  if (!m_synced) {
237  return;
238  }
239 
240  const uint256& locator_tip_hash = locator.vHave.front();
241  const CBlockIndex* locator_tip_index;
242  {
243  LOCK(cs_main);
244  locator_tip_index = LookupBlockIndex(locator_tip_hash);
245  }
246 
247  if (!locator_tip_index) {
248  FatalError("%s: First block (hash=%s) in locator was not found",
249  __func__, locator_tip_hash.ToString());
250  return;
251  }
252 
253  // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
254  // immediately after the sync thread catches up and sets m_synced. Consider the case where
255  // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
256  // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
257  // event, log a warning and let the queue clear.
258  const CBlockIndex* best_block_index = m_best_block_index.load();
259  if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
260  LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
261  "chain (tip=%s); not writing index locator\n",
262  __func__, locator_tip_hash.ToString(),
263  best_block_index->GetBlockHash().ToString());
264  return;
265  }
266 
267  // No need to handle errors in Commit. If it fails, the error will be already be logged. The
268  // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
269  // for an advanced index state.
270  Commit();
271 }
272 
274 {
276 
277  if (!m_synced) {
278  return false;
279  }
280 
281  {
282  // Skip the queue-draining stuff if we know we're caught up with
283  // ::ChainActive().Tip().
284  LOCK(cs_main);
285  const CBlockIndex* chain_tip = ::ChainActive().Tip();
286  const CBlockIndex* best_block_index = m_best_block_index.load();
287  if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
288  return true;
289  }
290  }
291 
292  LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName());
294  return true;
295 }
296 
298 {
299  m_interrupt();
300 }
301 
303 {
304  // Need to register this ValidationInterface before running Init(), so that
305  // callbacks are not missed if Init sets m_synced to true.
307  if (!Init()) {
308  FatalError("%s: %s failed to initialize", __func__, GetName());
309  return;
310  }
311 
312  m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
313  std::bind(&BaseIndex::ThreadSync, this));
314 }
315 
317 {
319 
320  if (m_thread_sync.joinable()) {
321  m_thread_sync.join();
322  }
323 }
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL
Definition: base.cpp:18
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
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:56
CThreadInterrupt m_interrupt
Definition: base.h:47
void ChainStateFlushed(const CBlockLocator &locator) override
Notifies listeners of the new active block chain on-disk.
Definition: base.cpp:234
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:114
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:41
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:144
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:46
Definition: block.h:62
CChain & ChainActive()
Definition: validation.cpp:108
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:35
constexpr int64_t SYNC_LOG_INTERVAL
Definition: base.cpp:17
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:164
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:385
bool IsNull() const
Definition: block.h:135
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
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:316
std::thread m_thread_sync
Definition: base.h:46
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:75
uint256 GetBlockHash() const
Definition: chain.h:233
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:50
void SetNull()
Definition: block.h:130
void Interrupt()
Definition: base.cpp:297
void WriteBestBlock(CDBBatch &batch, const CBlockLocator &locator)
Write block locator of the chain that the txindex is in sync with.
Definition: base.cpp:45
static const CBlockIndex * NextSyncBlock(const CBlockIndex *pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: base.cpp:73
#define LOCK(cs)
Definition: sync.h:218
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
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:413
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:124
void Write(const K &key, const V &value)
Definition: dbwrapper.h:72
void Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:302
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.cpp:60
std::vector< uint256 > vHave
Definition: block.h:116
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:230
void TraceThread(const char *name, Callable func)
Definition: system.h:386
CBlockIndex * LookupBlockIndex(const uint256 &hash)
Definition: validation.cpp:170
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
256-bit opaque blob.
Definition: uint256.h:120
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
const CChainParams & Params()
Return the currently selected parameters.
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:21
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
Definition: validation.cpp:177
constexpr char DB_BEST_BLOCK
Definition: base.cpp:15
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:390
void SetMiscWarning(const std::string &strWarning)
Definition: warnings.cpp:17
#define AssertLockNotHeld(cs)
Definition: sync.h:74
void StartShutdown()
Definition: shutdown.cpp:12
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:89
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:183
CClientUIInterface uiInterface
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
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:60
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: chain.cpp:111
AssertLockHeld(g_cs_orphans)
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:23
virtual DB & GetDB() const =0
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:44
bool BlockUntilSyncedToCurrentChain() const
Blocks the current thread until the index is caught up to the current state of the block chain...
Definition: base.cpp:273