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