Bitcoin Core  0.18.99
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 <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 <validation.h>
12 #include <warnings.h>
13 
14 constexpr char DB_BEST_BLOCK = 'B';
15 
16 constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
17 constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds
18 
19 template<typename... Args>
20 static void FatalError(const char* fmt, const Args&... args)
21 {
22  std::string strMessage = tfm::format(fmt, args...);
23  SetMiscWarning(strMessage);
24  LogPrintf("*** %s\n", strMessage);
25  uiInterface.ThreadSafeMessageBox(
26  "Error: 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 {
67  }
69  return true;
70 }
71 
72 static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
73 {
75 
76  if (!pindex_prev) {
78  }
79 
80  const CBlockIndex* pindex = ::ChainActive().Next(pindex_prev);
81  if (pindex) {
82  return pindex;
83  }
84 
85  return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev));
86 }
87 
89 {
90  const CBlockIndex* pindex = m_best_block_index.load();
91  if (!m_synced) {
92  auto& consensus_params = Params().GetConsensus();
93 
94  int64_t last_log_time = 0;
95  int64_t last_locator_write_time = 0;
96  while (true) {
97  if (m_interrupt) {
98  m_best_block_index = pindex;
99  // No need to handle errors in Commit. If it fails, the error will be already be
100  // logged. The best way to recover is to continue, as index cannot be corrupted by
101  // a missed commit to disk for an advanced index state.
102  Commit();
103  return;
104  }
105 
106  {
107  LOCK(cs_main);
108  const CBlockIndex* pindex_next = NextSyncBlock(pindex);
109  if (!pindex_next) {
110  m_best_block_index = pindex;
111  m_synced = true;
112  // No need to handle errors in Commit. See rationale above.
113  Commit();
114  break;
115  }
116  if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
117  FatalError("%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 < current_time) {
132  m_best_block_index = pindex;
133  last_locator_write_time = current_time;
134  // No need to handle errors in Commit. See rationale above.
135  Commit();
136  }
137 
138  CBlock block;
139  if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
140  FatalError("%s: Failed to read block %s from disk",
141  __func__, pindex->GetBlockHash().ToString());
142  return;
143  }
144  if (!WriteBlock(block, pindex)) {
145  FatalError("%s: Failed to write block %s to index database",
146  __func__, pindex->GetBlockHash().ToString());
147  return;
148  }
149  }
150  }
151 
152  if (pindex) {
153  LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
154  } else {
155  LogPrintf("%s is enabled\n", GetName());
156  }
157 }
158 
160 {
161  CDBBatch batch(GetDB());
162  if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
163  return error("%s: Failed to commit latest %s state", __func__, GetName());
164  }
165  return true;
166 }
167 
169 {
170  LOCK(cs_main);
171  GetDB().WriteBestBlock(batch, ::ChainActive().GetLocator(m_best_block_index));
172  return true;
173 }
174 
175 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
176 {
177  assert(current_tip == m_best_block_index);
178  assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
179 
180  // In the case of a reorg, ensure persisted block locator is not stale.
181  m_best_block_index = new_tip;
182  if (!Commit()) {
183  // If commit fails, revert the best block index to avoid corruption.
184  m_best_block_index = current_tip;
185  return false;
186  }
187 
188  return true;
189 }
190 
191 void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
192  const std::vector<CTransactionRef>& txn_conflicted)
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:17
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:159
virtual bool Init()
Initialize internal state from the database and block index.
Definition: base.cpp:55
CThreadInterrupt m_interrupt
Definition: base.h:48
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:126
std::atomic< bool > m_synced
Whether the index is in sync with the main chain.
Definition: base.h:42
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:146
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:47
Definition: block.h:72
CChain & ChainActive()
Definition: validation.cpp:84
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: validation.cpp:921
constexpr int64_t SYNC_LOG_INTERVAL
Definition: base.cpp:16
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:419
bool IsNull() const
Definition: block.h:149
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:175
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:316
std::thread m_thread_sync
Definition: base.h:47
virtual bool WriteBlock(const CBlock &block, const CBlockIndex *pindex)
Write update index entries for a newly connected block.
Definition: base.h:77
uint256 GetBlockHash() const
Definition: chain.h:261
virtual ~BaseIndex()
Destructor interrupts sync thread if running and blocks until it exits.
Definition: base.cpp:49
void SetNull()
Definition: block.h:144
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:44
CCriticalSection cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:96
#define LOCK(cs)
Definition: sync.h:182
bool BlockUntilSyncedToCurrentChain()
Blocks the current thread until the index is caught up to the current state of the block chain...
Definition: base.cpp:273
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:967
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:447
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: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:168
std::string ToString() const
Definition: uint256.cpp:62
std::vector< uint256 > vHave
Definition: block.h:128
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:231
void TraceThread(const char *name, Callable func)
Definition: system.h:332
void RegisterValidationInterface(CValidationInterface *pwalletIn)
Register a wallet to receive updates from core.
256-bit opaque blob.
Definition: uint256.h:121
#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:139
const CChainParams & Params()
Return the currently selected parameters.
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
Definition: validation.cpp:151
constexpr char DB_BEST_BLOCK
Definition: base.cpp:14
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:424
void SetMiscWarning(const std::string &strWarning)
Definition: warnings.cpp:15
#define AssertLockNotHeld(cs)
Definition: sync.h:71
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex, const std::vector< CTransactionRef > &txn_conflicted) override
Notifies listeners of a block being connected.
Definition: base.cpp:191
void StartShutdown()
Definition: shutdown.cpp:12
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:408
void ThreadSync()
Sync the index with the block index starting from the current best block.
Definition: base.cpp:88
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:152
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
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:35
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:20
virtual DB & GetDB() const =0
bool error(const char *fmt, const Args &... args)
Definition: system.h:61
std::atomic< const CBlockIndex * > m_best_block_index
The last block in the chain that the index is in sync with.
Definition: base.h:45